diff options
-rw-r--r-- | chrome/test/live_sync/live_sync_test.cc | 35 | ||||
-rw-r--r-- | chrome/test/live_sync/live_sync_test.h | 28 | ||||
-rw-r--r-- | net/test/test_server.cc | 3 | ||||
-rw-r--r-- | net/test/test_server.h | 1 | ||||
-rw-r--r-- | net/tools/testserver/run_testserver.cc | 18 | ||||
-rwxr-xr-x | net/tools/testserver/testserver.py | 212 |
6 files changed, 175 insertions, 122 deletions
diff --git a/chrome/test/live_sync/live_sync_test.cc b/chrome/test/live_sync/live_sync_test.cc index 2ae08cc..888527a 100644 --- a/chrome/test/live_sync/live_sync_test.cc +++ b/chrome/test/live_sync/live_sync_test.cc @@ -27,6 +27,7 @@ #include "googleurl/src/gurl.h" #include "net/base/escape.h" #include "net/base/network_change_notifier.h" +#include "net/test/test_server.h" #include "net/proxy/proxy_config.h" #include "net/proxy/proxy_config_service_fixed.h" #include "net/proxy/proxy_service.h" @@ -86,6 +87,32 @@ class SetProxyConfigTask : public Task { net::ProxyConfig proxy_config_; }; +LiveSyncTest::LiveSyncTest(TestType test_type) + : sync_server_(net::TestServer::TYPE_SYNC, FilePath()), + test_type_(test_type), + num_clients_(-1), + test_server_handle_(base::kNullProcessHandle) { + InProcessBrowserTest::set_show_window(true); + switch (test_type_) { + case SINGLE_CLIENT: { + num_clients_ = 1; + break; + } + case TWO_CLIENT: { + num_clients_ = 2; + break; + } + case MULTIPLE_CLIENT: { + num_clients_ = 3; + break; + } + case MANY_CLIENT: { + num_clients_ = 10; + break; + } + } +} + void LiveSyncTest::SetUp() { // At this point, the browser hasn't been launched, and no services are // available. But we can verify our command line parameters and fail @@ -292,13 +319,11 @@ void LiveSyncTest::SetUpTestServerIfRequired() { } bool LiveSyncTest::SetUpLocalPythonTestServer() { - EXPECT_TRUE(test_server()->Start()) + EXPECT_TRUE(sync_server_.Start()) << "Could not launch local python test server."; CommandLine* cl = CommandLine::ForCurrentProcess(); - std::string sync_service_url = StringPrintf("http://%s:%d/chromiumsync", - test_server()->host_port_pair().host().c_str(), - test_server()->host_port_pair().port()); + std::string sync_service_url = sync_server_.GetURL("chromiumsync").spec(); cl->AppendSwitchASCII(switches::kSyncServiceURL, sync_service_url); VLOG(1) << "Started local python test server at " << sync_service_url; @@ -337,7 +362,7 @@ bool LiveSyncTest::SetUpLocalTestServer() { } bool LiveSyncTest::TearDownLocalPythonTestServer() { - if (!test_server()->Stop()) { + if (!sync_server_.Stop()) { LOG(ERROR) << "Could not stop local python test server."; return false; } diff --git a/chrome/test/live_sync/live_sync_test.h b/chrome/test/live_sync/live_sync_test.h index 1e93b64..1759d93 100644 --- a/chrome/test/live_sync/live_sync_test.h +++ b/chrome/test/live_sync/live_sync_test.h @@ -55,30 +55,7 @@ class LiveSyncTest : public InProcessBrowserTest { }; // A LiveSyncTest must be associated with a particular test type. - explicit LiveSyncTest(TestType test_type) - : test_type_(test_type), - num_clients_(-1), - test_server_handle_(base::kNullProcessHandle) { - InProcessBrowserTest::set_show_window(true); - switch (test_type_) { - case SINGLE_CLIENT: { - num_clients_ = 1; - break; - } - case TWO_CLIENT: { - num_clients_ = 2; - break; - } - case MULTIPLE_CLIENT: { - num_clients_ = 3; - break; - } - case MANY_CLIENT: { - num_clients_ = 10; - break; - } - } - } + explicit LiveSyncTest(TestType test_type); virtual ~LiveSyncTest() {} @@ -194,6 +171,9 @@ class LiveSyncTest : public InProcessBrowserTest { void SetProxyConfig(URLRequestContextGetter* context, const net::ProxyConfig& proxy_config); + // Test server of type sync, started on demand. + net::TestServer sync_server_; + // Used to differentiate between single-client, two-client, multi-client and // many-client tests. TestType test_type_; diff --git a/net/test/test_server.cc b/net/test/test_server.cc index 9a89cc9..0e8c461 100644 --- a/net/test/test_server.cc +++ b/net/test/test_server.cc @@ -191,6 +191,7 @@ std::string TestServer::GetScheme() const { case TYPE_FTP: return "ftp"; case TYPE_HTTP: + case TYPE_SYNC: return "http"; case TYPE_HTTPS: return "https"; @@ -330,6 +331,8 @@ bool TestServer::AddCommandLineArguments(CommandLine* command_line) const { if (type_ == TYPE_FTP) { command_line->AppendArg("-f"); + } else if (type_ == TYPE_SYNC) { + command_line->AppendArg("--sync"); } else if (type_ == TYPE_HTTPS) { FilePath certificate_path(certificates_dir_); certificate_path = certificate_path.Append( diff --git a/net/test/test_server.h b/net/test/test_server.h index b639094..f819365 100644 --- a/net/test/test_server.h +++ b/net/test/test_server.h @@ -42,6 +42,7 @@ class TestServer { TYPE_FTP, TYPE_HTTP, TYPE_HTTPS, + TYPE_SYNC, }; // Container for various options to control how the HTTPS server is diff --git a/net/tools/testserver/run_testserver.cc b/net/tools/testserver/run_testserver.cc index 157ce3e..e92dfb2 100644 --- a/net/tools/testserver/run_testserver.cc +++ b/net/tools/testserver/run_testserver.cc @@ -11,7 +11,7 @@ #include "net/test/test_server.h" static void PrintUsage() { - printf("run_testserver --doc-root=relpath [--http|--https|--ftp]\n"); + printf("run_testserver --doc-root=relpath [--http|--https|--ftp|--sync]\n"); printf("(NOTE: relpath should be relative to the 'src' directory)\n"); } @@ -29,18 +29,20 @@ int main(int argc, const char* argv[]) { return -1; } - FilePath doc_root = command_line->GetSwitchValuePath("doc-root"); - if (doc_root.empty()) { - printf("Error: --doc-root must be specified\n"); - PrintUsage(); - return -1; - } - net::TestServer::Type server_type(net::TestServer::TYPE_HTTP); if (command_line->HasSwitch("https")) { server_type = net::TestServer::TYPE_HTTPS; } else if (command_line->HasSwitch("ftp")) { server_type = net::TestServer::TYPE_FTP; + } else if (command_line->HasSwitch("sync")) { + server_type = net::TestServer::TYPE_SYNC; + } + + FilePath doc_root = command_line->GetSwitchValuePath("doc-root"); + if ((server_type != net::TestServer::TYPE_SYNC) && doc_root.empty()) { + printf("Error: --doc-root must be specified\n"); + PrintUsage(); + return -1; } net::TestServer test_server(server_type, doc_root); diff --git a/net/tools/testserver/testserver.py b/net/tools/testserver/testserver.py index 408e1e57..2a13c2b 100755 --- a/net/tools/testserver/testserver.py +++ b/net/tools/testserver/testserver.py @@ -46,6 +46,7 @@ if sys.platform == 'win32': SERVER_HTTP = 0 SERVER_FTP = 1 +SERVER_SYNC = 2 debug_output = sys.stderr def debug(str): @@ -106,14 +107,75 @@ class HTTPSServer(tlslite.api.TLSSocketServerMixIn, StoppableHTTPServer): print "Handshake failure:", str(error) return False -class TestPageHandler(BaseHTTPServer.BaseHTTPRequestHandler): + +class SyncHTTPServer(StoppableHTTPServer): + """An HTTP server that handles sync commands.""" + + def __init__(self, server_address, request_handler_class): + # We import here to avoid pulling in chromiumsync's dependencies + # unless strictly necessary. + import chromiumsync + self._sync_handler = chromiumsync.TestServer() + StoppableHTTPServer.__init__(self, server_address, request_handler_class) + + def HandleCommand(self, query, raw_request): + return self._sync_handler.HandleCommand(query, raw_request) + + +class BasePageHandler(BaseHTTPServer.BaseHTTPRequestHandler): + + def __init__(self, request, client_address, socket_server, + connect_handlers, get_handlers, post_handlers, put_handlers): + self._connect_handlers = connect_handlers + self._get_handlers = get_handlers + self._post_handlers = post_handlers + self._put_handlers = put_handlers + BaseHTTPServer.BaseHTTPRequestHandler.__init__( + self, request, client_address, socket_server) + + def log_request(self, *args, **kwargs): + # Disable request logging to declutter test log output. + pass + + def _ShouldHandleRequest(self, handler_name): + """Determines if the path can be handled by the handler. + + We consider a handler valid if the path begins with the + handler name. It can optionally be followed by "?*", "/*". + """ + + pattern = re.compile('%s($|\?|/).*' % handler_name) + return pattern.match(self.path) + + def do_CONNECT(self): + for handler in self._connect_handlers: + if handler(): + return + + def do_GET(self): + for handler in self._get_handlers: + if handler(): + return + + def do_POST(self): + for handler in self._post_handlers: + if handler(): + return + + def do_PUT(self): + for handler in self._put_handlers: + if handler(): + return + + +class TestPageHandler(BasePageHandler): def __init__(self, request, client_address, socket_server): - self._connect_handlers = [ + connect_handlers = [ self.RedirectConnectHandler, self.ServerAuthConnectHandler, self.DefaultConnectResponseHandler] - self._get_handlers = [ + get_handlers = [ self.NoCacheMaxAgeTimeHandler, self.NoCacheTimeHandler, self.CacheTimeHandler, @@ -142,19 +204,17 @@ class TestPageHandler(BaseHTTPServer.BaseHTTPRequestHandler): self.ContentTypeHandler, self.ServerRedirectHandler, self.ClientRedirectHandler, - self.ChromiumSyncTimeHandler, self.MultipartHandler, self.DefaultResponseHandler] - self._post_handlers = [ + post_handlers = [ self.EchoTitleHandler, self.EchoAllHandler, - self.ChromiumSyncCommandHandler, self.EchoHandler, - self.DeviceManagementHandler] + self._get_handlers - self._put_handlers = [ + self.DeviceManagementHandler] + get_handlers + put_handlers = [ self.EchoTitleHandler, self.EchoAllHandler, - self.EchoHandler] + self._get_handlers + self.EchoHandler] + get_handlers self._mime_types = { 'crx' : 'application/x-chrome-extension', @@ -165,23 +225,9 @@ class TestPageHandler(BaseHTTPServer.BaseHTTPRequestHandler): } self._default_mime_type = 'text/html' - BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, - client_address, - socket_server) - - def log_request(self, *args, **kwargs): - # Disable request logging to declutter test log output. - pass - - def _ShouldHandleRequest(self, handler_name): - """Determines if the path can be handled by the handler. - - We consider a handler valid if the path begins with the - handler name. It can optionally be followed by "?*", "/*". - """ - - pattern = re.compile('%s($|\?|/).*' % handler_name) - return pattern.match(self.path) + BasePageHandler.__init__(self, request, client_address, socket_server, + connect_handlers, get_handlers, post_handlers, + put_handlers) def GetMIMETypeFromName(self, file_name): """Returns the mime type for the specified file_name. So far it only looks @@ -996,43 +1042,6 @@ class TestPageHandler(BaseHTTPServer.BaseHTTPRequestHandler): return True - def ChromiumSyncTimeHandler(self): - """Handle Chromium sync .../time requests. - - The syncer sometimes checks server reachability by examining /time. - """ - test_name = "/chromiumsync/time" - if not self._ShouldHandleRequest(test_name): - return False - - self.send_response(200) - self.send_header('Content-type', 'text/html') - self.end_headers() - return True - - def ChromiumSyncCommandHandler(self): - """Handle a chromiumsync command arriving via http. - - This covers all sync protocol commands: authentication, getupdates, and - commit. - """ - test_name = "/chromiumsync/command" - if not self._ShouldHandleRequest(test_name): - return False - - length = int(self.headers.getheader('content-length')) - raw_request = self.rfile.read(length) - - if not self.server._sync_handler: - import chromiumsync - self.server._sync_handler = chromiumsync.TestServer() - http_response, raw_reply = self.server._sync_handler.HandleCommand( - self.path, raw_request) - self.send_response(http_response) - self.end_headers() - self.wfile.write(raw_reply) - return True - def MultipartHandler(self): """Send a multipart response (10 text/html pages).""" test_name = "/multipart" @@ -1137,26 +1146,6 @@ class TestPageHandler(BaseHTTPServer.BaseHTTPRequestHandler): self.wfile.write(raw_reply) return True - def do_CONNECT(self): - for handler in self._connect_handlers: - if handler(): - return - - def do_GET(self): - for handler in self._get_handlers: - if handler(): - return - - def do_POST(self): - for handler in self._post_handlers: - if handler(): - return - - def do_PUT(self): - for handler in self._put_handlers: - if handler(): - return - # called by the redirect handling function when there is no parameter def sendRedirectHelp(self, redirect_name): self.send_response(200) @@ -1166,6 +1155,52 @@ class TestPageHandler(BaseHTTPServer.BaseHTTPRequestHandler): self.wfile.write('Use <pre>%s?http://dest...</pre>' % redirect_name) self.wfile.write('</body></html>') + +class SyncPageHandler(BasePageHandler): + """Handler for the main HTTP sync server.""" + + def __init__(self, request, client_address, sync_http_server): + get_handlers = [self.ChromiumSyncTimeHandler] + post_handlers = [self.ChromiumSyncCommandHandler] + BasePageHandler.__init__(self, request, client_address, + sync_http_server, [], get_handlers, + post_handlers, []) + + def ChromiumSyncTimeHandler(self): + """Handle Chromium sync .../time requests. + + The syncer sometimes checks server reachability by examining /time. + """ + test_name = "/chromiumsync/time" + if not self._ShouldHandleRequest(test_name): + return False + + self.send_response(200) + self.send_header('Content-type', 'text/html') + self.end_headers() + return True + + def ChromiumSyncCommandHandler(self): + """Handle a chromiumsync command arriving via http. + + This covers all sync protocol commands: authentication, getupdates, and + commit. + """ + test_name = "/chromiumsync/command" + if not self._ShouldHandleRequest(test_name): + return False + + length = int(self.headers.getheader('content-length')) + raw_request = self.rfile.read(length) + + http_response, raw_reply = self.server.HandleCommand( + self.path, raw_request) + self.send_response(http_response) + self.end_headers() + self.wfile.write(raw_reply) + return True + + def MakeDataDir(): if options.data_dir: if not os.path.isdir(options.data_dir): @@ -1231,9 +1266,12 @@ def main(options, args): server.data_dir = MakeDataDir() server.file_root_url = options.file_root_url - server._sync_handler = None listen_port = server.server_port server._device_management_handler = None + elif options.server_type == SERVER_SYNC: + server = SyncHTTPServer(('127.0.0.1', port), SyncPageHandler) + print 'Sync HTTP server started on port %d...' % server.server_port + listen_port = server.server_port # means FTP Server else: my_data_dir = MakeDataDir() @@ -1286,7 +1324,11 @@ if __name__ == '__main__': option_parser.add_option("-f", '--ftp', action='store_const', const=SERVER_FTP, default=SERVER_HTTP, dest='server_type', - help='FTP or HTTP server: default is HTTP.') + help='start up an FTP server.') + option_parser.add_option('', '--sync', action='store_const', + const=SERVER_SYNC, default=SERVER_HTTP, + dest='server_type', + help='start up a sync server.') option_parser.add_option('', '--port', default='0', type='int', help='Port used by the server. If unspecified, the ' 'server will listen on an ephemeral port.') |