1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
17 # Ignore deprecation warnings, they make our output more cluttered.
18 warnings.filterwarnings("ignore", category=DeprecationWarning)
20 if sys.platform == 'win32':
23 # Using debug() seems to cause hangs on XP: see http://crbug.com/64515.
24 debug_output = sys.stderr
26 debug_output.write(string + "\n")
30 class Error(Exception):
31 """Error class for this module."""
34 class OptionError(Error):
35 """Error for bad command line options."""
38 class FileMultiplexer(object):
39 def __init__(self, fd1, fd2) :
44 if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr:
46 if self.__fd2 != sys.stdout and self.__fd2 != sys.stderr:
49 def write(self, text) :
50 self.__fd1.write(text)
51 self.__fd2.write(text)
58 class ClientRestrictingServerMixIn:
59 """Implements verify_request to limit connections to our configured IP
62 def verify_request(self, _request, client_address):
63 return client_address[0] == self.server_address[0]
66 class BrokenPipeHandlerMixIn:
67 """Allows the server to deal with "broken pipe" errors (which happen if the
68 browser quits with outstanding requests, like for the favicon). This mix-in
69 requires the class to derive from SocketServer.BaseServer and not override its
70 handle_error() method. """
72 def handle_error(self, request, client_address):
73 value = sys.exc_info()[1]
74 if isinstance(value, socket.error):
76 if sys.platform in ('win32', 'cygwin'):
77 # "An established connection was aborted by the software in your host."
80 pipe_err = errno.EPIPE
82 print "testserver.py: Broken pipe"
84 SocketServer.BaseServer.handle_error(self, request, client_address)
87 class StoppableHTTPServer(BaseHTTPServer.HTTPServer):
88 """This is a specialization of BaseHTTPServer to allow it
89 to be exited cleanly (by setting its "stop" member to True)."""
91 def serve_forever(self):
93 self.nonce_time = None
99 def MultiplexerHack(std_fd, log_fd):
100 """Creates a FileMultiplexer that will write to both specified files.
102 When running on Windows XP bots, stdout and stderr will be invalid file
103 handles, so log_fd will be returned directly. (This does not occur if you
104 run the test suite directly from a console, but only if the output of the
105 test executable is redirected.)
107 if std_fd.fileno() <= 0:
109 return FileMultiplexer(std_fd, log_fd)
112 class BasePageHandler(BaseHTTPServer.BaseHTTPRequestHandler):
114 def __init__(self, request, client_address, socket_server,
115 connect_handlers, get_handlers, head_handlers, post_handlers,
117 self._connect_handlers = connect_handlers
118 self._get_handlers = get_handlers
119 self._head_handlers = head_handlers
120 self._post_handlers = post_handlers
121 self._put_handlers = put_handlers
122 BaseHTTPServer.BaseHTTPRequestHandler.__init__(
123 self, request, client_address, socket_server)
125 def log_request(self, *args, **kwargs):
126 # Disable request logging to declutter test log output.
129 def _ShouldHandleRequest(self, handler_name):
130 """Determines if the path can be handled by the handler.
132 We consider a handler valid if the path begins with the
133 handler name. It can optionally be followed by "?*", "/*".
136 pattern = re.compile('%s($|\?|/).*' % handler_name)
137 return pattern.match(self.path)
139 def do_CONNECT(self):
140 for handler in self._connect_handlers:
145 for handler in self._get_handlers:
150 for handler in self._head_handlers:
155 for handler in self._post_handlers:
160 for handler in self._put_handlers:
165 class TestServerRunner(object):
166 """Runs a test server and communicates with the controlling C++ test code.
168 Subclasses should override the create_server method to create their server
169 object, and the add_options method to add their own options.
173 self.option_parser = optparse.OptionParser()
177 self.options, self.args = self.option_parser.parse_args()
179 logfile = open(self.options.log_file, 'w')
180 sys.stderr = MultiplexerHack(sys.stderr, logfile)
181 if self.options.log_to_console:
182 sys.stdout = MultiplexerHack(sys.stdout, logfile)
187 'host': self.options.host,
189 self.server = self.create_server(server_data)
190 self._notify_startup_complete(server_data)
193 def create_server(self, server_data):
194 """Creates a server object and returns it.
196 Must populate server_data['port'], and can set additional server_data
197 elements if desired."""
198 raise NotImplementedError()
200 def run_server(self):
202 self.server.serve_forever()
203 except KeyboardInterrupt:
204 print 'shutting down server'
205 self.server.stop = True
207 def add_options(self):
208 self.option_parser.add_option('--startup-pipe', type='int',
210 help='File handle of pipe to parent process')
211 self.option_parser.add_option('--log-to-console', action='store_const',
212 const=True, default=False,
213 dest='log_to_console',
214 help='Enables or disables sys.stdout logging '
216 self.option_parser.add_option('--log-file', default='testserver.log',
218 help='The name of the server log file.')
219 self.option_parser.add_option('--port', default=0, type='int',
220 help='Port used by the server. If '
221 'unspecified, the server will listen on an '
223 self.option_parser.add_option('--host', default='127.0.0.1',
225 help='Hostname or IP upon which the server '
226 'will listen. Client connections will also '
227 'only be allowed from this address.')
228 self.option_parser.add_option('--data-dir', dest='data_dir',
229 help='Directory from which to read the '
232 def _notify_startup_complete(self, server_data):
233 # Notify the parent that we've started. (BaseServer subclasses
234 # bind their sockets on construction.)
235 if self.options.startup_pipe is not None:
236 server_data_json = json.dumps(server_data)
237 server_data_len = len(server_data_json)
238 print 'sending server_data: %s (%d bytes)' % (
239 server_data_json, server_data_len)
240 if sys.platform == 'win32':
241 fd = msvcrt.open_osfhandle(self.options.startup_pipe, 0)
243 fd = self.options.startup_pipe
244 startup_pipe = os.fdopen(fd, "w")
245 # First write the data length as an unsigned 4-byte value. This
246 # is _not_ using network byte ordering since the other end of the
247 # pipe is on the same machine.
248 startup_pipe.write(struct.pack('=L', server_data_len))
249 startup_pipe.write(server_data_json)