summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/safe_browsing/local_safebrowsing_test_server.cc61
-rw-r--r--chrome/browser/safe_browsing/local_safebrowsing_test_server.h38
-rw-r--r--chrome/browser/safe_browsing/safe_browsing_test.cc203
-rwxr-xr-xchrome/browser/safe_browsing/safe_browsing_testserver.py47
-rw-r--r--chrome/chrome_tests.gypi1
5 files changed, 193 insertions, 157 deletions
diff --git a/chrome/browser/safe_browsing/local_safebrowsing_test_server.cc b/chrome/browser/safe_browsing/local_safebrowsing_test_server.cc
new file mode 100644
index 0000000..c25e4d7
--- /dev/null
+++ b/chrome/browser/safe_browsing/local_safebrowsing_test_server.cc
@@ -0,0 +1,61 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/safe_browsing/local_safebrowsing_test_server.h"
+
+#include "base/command_line.h"
+#include "base/path_service.h"
+#include "base/string_number_conversions.h"
+#include "base/values.h"
+#include "net/test/python_utils.h"
+#include "net/test/test_server.h"
+
+LocalSafeBrowsingTestServer::LocalSafeBrowsingTestServer(
+ const FilePath& data_file)
+ : net::LocalTestServer(net::TestServer::TYPE_HTTP,
+ net::TestServer::kLocalhost,
+ FilePath()),
+ data_file_(data_file) {
+}
+
+LocalSafeBrowsingTestServer::~LocalSafeBrowsingTestServer() {}
+
+bool LocalSafeBrowsingTestServer::GetTestServerPath(
+ FilePath* testserver_path) const {
+ FilePath testserver_dir;
+ if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_dir)) {
+ LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT";
+ return false;
+ }
+
+ testserver_dir = testserver_dir
+ .Append(FILE_PATH_LITERAL("chrome"))
+ .Append(FILE_PATH_LITERAL("browser"))
+ .Append(FILE_PATH_LITERAL("safe_browsing"));
+
+ *testserver_path = testserver_dir.Append(FILE_PATH_LITERAL(
+ "safe_browsing_testserver.py"));
+ return true;
+}
+
+bool LocalSafeBrowsingTestServer::SetPythonPath() const {
+ if (!net::LocalTestServer::SetPythonPath())
+ return false;
+
+ // Locate the Python code generated by the protocol buffers compiler.
+ FilePath pyproto_dir;
+ if (!GetPyProtoPath(&pyproto_dir)) {
+ LOG(ERROR) << "Cannot find pyproto dir for generated code.";
+ return false;
+ }
+
+ AppendToPythonPath(pyproto_dir.AppendASCII("google"));
+ return true;
+}
+
+bool LocalSafeBrowsingTestServer::GenerateAdditionalArguments(
+ base::DictionaryValue* arguments) const {
+ arguments->SetString("data-file", data_file_.value());
+ return true;
+}
diff --git a/chrome/browser/safe_browsing/local_safebrowsing_test_server.h b/chrome/browser/safe_browsing/local_safebrowsing_test_server.h
new file mode 100644
index 0000000..13d8baf
--- /dev/null
+++ b/chrome/browser/safe_browsing/local_safebrowsing_test_server.h
@@ -0,0 +1,38 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_SAFE_BROWSING_LOCAL_SAFEBROWSING_TEST_SERVER_H_
+#define CHROME_BROWSER_SAFE_BROWSING_LOCAL_SAFEBROWSING_TEST_SERVER_H_
+
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "base/file_path.h"
+#include "net/test/local_test_server.h"
+
+// Runs a Python-based safebrowsing test server on the same machine in which the
+// LocalSafeBrowsingTestServer runs.
+class LocalSafeBrowsingTestServer : public net::LocalTestServer {
+ public:
+ // Initialize a safebrowsing server using the given |data_file|.
+ explicit LocalSafeBrowsingTestServer(const FilePath& data_file);
+
+ virtual ~LocalSafeBrowsingTestServer();
+
+ virtual bool SetPythonPath() const OVERRIDE;
+
+ // Returns the path to safe_browsing_testserver.py.
+ virtual bool GetTestServerPath(FilePath* testserver_path) const OVERRIDE;
+
+ protected:
+ // Adds the --data-file switch. Returns true on success.
+ virtual bool GenerateAdditionalArguments(
+ base::DictionaryValue* arguments) const OVERRIDE;
+
+ private:
+ FilePath data_file_;
+
+ DISALLOW_COPY_AND_ASSIGN(LocalSafeBrowsingTestServer);
+};
+
+#endif // CHROME_BROWSER_SAFE_BROWSING_LOCAL_SAFEBROWSING_TEST_SERVER_H_
diff --git a/chrome/browser/safe_browsing/safe_browsing_test.cc b/chrome/browser/safe_browsing/safe_browsing_test.cc
index 38b5ce5..2ab9c41 100644
--- a/chrome/browser/safe_browsing/safe_browsing_test.cc
+++ b/chrome/browser/safe_browsing/safe_browsing_test.cc
@@ -32,6 +32,7 @@
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/safe_browsing/local_safebrowsing_test_server.h"
#include "chrome/browser/safe_browsing/protocol_manager.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/browser/ui/browser.h"
@@ -57,10 +58,9 @@ namespace {
const FilePath::CharType kDataFile[] =
FILE_PATH_LITERAL("testing_input_nomac.dat");
-const char kUrlVerifyPath[] = "/safebrowsing/verify_urls";
-const char kDBVerifyPath[] = "/safebrowsing/verify_database";
-const char kDBResetPath[] = "/reset";
-const char kTestCompletePath[] = "/test_complete";
+const char kUrlVerifyPath[] = "safebrowsing/verify_urls";
+const char kDBVerifyPath[] = "safebrowsing/verify_database";
+const char kTestCompletePath[] = "test_complete";
struct PhishingUrl {
std::string url;
@@ -110,100 +110,6 @@ bool ParsePhishingUrls(const std::string& data,
} // namespace
-class SafeBrowsingTestServer {
- public:
- explicit SafeBrowsingTestServer(const FilePath& datafile)
- : datafile_(datafile),
- server_handle_(base::kNullProcessHandle) {
- }
-
- ~SafeBrowsingTestServer() {
- EXPECT_EQ(base::kNullProcessHandle, server_handle_);
- }
-
- // Start the python server test suite.
- bool Start() {
- // Get path to python server script
- FilePath testserver_path;
- if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)) {
- LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT";
- return false;
- }
- testserver_path = testserver_path
- .Append(FILE_PATH_LITERAL("third_party"))
- .Append(FILE_PATH_LITERAL("safe_browsing"))
- .Append(FILE_PATH_LITERAL("testing"));
- AppendToPythonPath(testserver_path);
- FilePath testserver = testserver_path.Append(
- FILE_PATH_LITERAL("safebrowsing_test_server.py"));
-
- FilePath pyproto_code_dir;
- if (!GetPyProtoPath(&pyproto_code_dir)) {
- LOG(ERROR) << "Failed to get generated python protobuf dir";
- return false;
- }
- AppendToPythonPath(pyproto_code_dir);
- pyproto_code_dir = pyproto_code_dir.Append(FILE_PATH_LITERAL("google"));
- AppendToPythonPath(pyproto_code_dir);
-
- CommandLine cmd_line(CommandLine::NO_PROGRAM);
- EXPECT_TRUE(GetPythonCommand(&cmd_line));
-
- FilePath datafile = testserver_path.Append(datafile_);
- cmd_line.AppendArgPath(testserver);
- cmd_line.AppendArg(base::StringPrintf("--port=%d", kPort_));
- cmd_line.AppendArgNative(FILE_PATH_LITERAL("--datafile=") +
- datafile.value());
-
- base::LaunchOptions options;
-#if defined(OS_WIN)
- options.start_hidden = true;
-#endif
- if (!base::LaunchProcess(cmd_line, options, &server_handle_)) {
- LOG(ERROR) << "Failed to launch server: "
- << cmd_line.GetCommandLineString();
- return false;
- }
- return true;
- }
-
- // Stop the python server test suite.
- bool Stop() {
- if (server_handle_ == base::kNullProcessHandle)
- return true;
-
- // First check if the process has already terminated.
- if (!base::WaitForSingleProcess(server_handle_, base::TimeDelta()) &&
- !base::KillProcess(server_handle_, 1, true)) {
- VLOG(1) << "Kill failed?";
- return false;
- }
-
- base::CloseProcessHandle(server_handle_);
- server_handle_ = base::kNullProcessHandle;
- VLOG(1) << "Stopped.";
- return true;
- }
-
- static const char* Host() {
- return kHost_;
- }
-
- static int Port() {
- return kPort_;
- }
-
- private:
- static const char kHost_[];
- static const int kPort_;
- FilePath datafile_;
- base::ProcessHandle server_handle_;
- DISALLOW_COPY_AND_ASSIGN(SafeBrowsingTestServer);
-};
-
-const char SafeBrowsingTestServer::kHost_[] = "localhost";
-const int SafeBrowsingTestServer::kPort_ = 40102;
-
// This starts the browser and keeps status of states related to SafeBrowsing.
class SafeBrowsingServiceTest : public InProcessBrowserTest {
public:
@@ -297,13 +203,28 @@ class SafeBrowsingServiceTest : public InProcessBrowserTest {
return safe_browsing_service_->safe_browsing_thread_->message_loop();
}
+ const net::TestServer& test_server() const {
+ return *test_server_;
+ }
+
protected:
bool InitSafeBrowsingService() {
safe_browsing_service_ = g_browser_process->safe_browsing_service();
return safe_browsing_service_ != NULL;
}
- virtual void SetUpCommandLine(CommandLine* command_line) {
+ virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
+ FilePath datafile_path;
+ ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &datafile_path));
+
+ datafile_path = datafile_path.Append(FILE_PATH_LITERAL("third_party"))
+ .Append(FILE_PATH_LITERAL("safe_browsing"))
+ .Append(FILE_PATH_LITERAL("testing"))
+ .Append(kDataFile);
+ test_server_.reset(new LocalSafeBrowsingTestServer(datafile_path));
+ ASSERT_TRUE(test_server_->Start());
+ LOG(INFO) << "server is " << test_server_->host_port_pair().ToString();
+
// Makes sure the auto update is not triggered. This test will force the
// update when needed.
command_line->AppendSwitch(switches::kSbDisableAutoUpdate);
@@ -323,10 +244,7 @@ class SafeBrowsingServiceTest : public InProcessBrowserTest {
switches::kDisableClientSidePhishingDetection);
// Point to the testing server for all SafeBrowsing requests.
- std::string url_prefix =
- base::StringPrintf("http://%s:%d/safebrowsing",
- SafeBrowsingTestServer::Host(),
- SafeBrowsingTestServer::Port());
+ std::string url_prefix = test_server_->GetURL("safebrowsing").spec();
command_line->AppendSwitchASCII(switches::kSbURLPrefix, url_prefix);
}
@@ -338,6 +256,8 @@ class SafeBrowsingServiceTest : public InProcessBrowserTest {
private:
SafeBrowsingService* safe_browsing_service_;
+ scoped_ptr<net::TestServer> test_server_;
+
// Protects all variables below since they are read on UI thread
// but updated on IO thread or safebrowsing thread.
base::Lock update_status_mutex_;
@@ -443,52 +363,36 @@ class SafeBrowsingServiceTestHelper
content::RunMessageLoop();
}
- void WaitTillServerReady(const char* host, int port) {
- response_status_ = net::URLRequestStatus::FAILED;
- GURL url(base::StringPrintf("http://%s:%d%s?test_step=0",
- host, port, kDBResetPath));
- // TODO(lzheng): We should have a way to reliably tell when a server is
- // ready so we could get rid of the Sleep and retry loop.
- while (true) {
- if (FetchUrl(url) == net::URLRequestStatus::SUCCESS)
- break;
- // Wait and try again if last fetch was failed. The loop will hit the
- // timeout in OutOfProcTestRunner if the fetch can not get success
- // response.
- base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
- }
- }
-
// Calls test server to fetch database for verification.
- net::URLRequestStatus::Status FetchDBToVerify(const char* host, int port,
- int test_step) {
+ net::URLRequestStatus::Status FetchDBToVerify(
+ const net::TestServer& test_server,
+ int test_step) {
// TODO(lzheng): Remove chunk_type=add once it is not needed by the server.
- GURL url(base::StringPrintf(
- "http://%s:%d%s?"
- "client=chromium&appver=1.0&pver=2.2&test_step=%d&"
- "chunk_type=add",
- host, port, kDBVerifyPath, test_step));
- return FetchUrl(url);
+ std::string path = base::StringPrintf(
+ "%s?client=chromium&appver=1.0&pver=2.2&test_step=%d&chunk_type=add",
+ kDBVerifyPath, test_step);
+ return FetchUrl(test_server.GetURL(path));
}
// Calls test server to fetch URLs for verification.
- net::URLRequestStatus::Status FetchUrlsToVerify(const char* host, int port,
- int test_step) {
- GURL url(base::StringPrintf(
- "http://%s:%d%s?"
- "client=chromium&appver=1.0&pver=2.2&test_step=%d",
- host, port, kUrlVerifyPath, test_step));
- return FetchUrl(url);
+ net::URLRequestStatus::Status FetchUrlsToVerify(
+ const net::TestServer& test_server,
+ int test_step) {
+ std::string path = base::StringPrintf(
+ "%s?client=chromium&appver=1.0&pver=2.2&test_step=%d",
+ kUrlVerifyPath, test_step);
+ return FetchUrl(test_server.GetURL(path));
}
// Calls test server to check if test data is done. E.g.: if there is a
// bad URL that server expects test to fetch full hash but the test didn't,
// this verification will fail.
- net::URLRequestStatus::Status VerifyTestComplete(const char* host, int port,
- int test_step) {
- GURL url(StringPrintf("http://%s:%d%s?test_step=%d",
- host, port, kTestCompletePath, test_step));
- return FetchUrl(url);
+ net::URLRequestStatus::Status VerifyTestComplete(
+ const net::TestServer& test_server,
+ int test_step) {
+ std::string path = base::StringPrintf(
+ "%s?test_step=%d", kTestCompletePath, test_step);
+ return FetchUrl(test_server.GetURL(path));
}
// Callback for URLFetcher.
@@ -537,8 +441,6 @@ class SafeBrowsingServiceTestHelper
IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest,
DISABLED_SafeBrowsingSystemTest) {
LOG(INFO) << "Start test";
- const char* server_host = SafeBrowsingTestServer::Host();
- int server_port = SafeBrowsingTestServer::Port();
ASSERT_TRUE(InitSafeBrowsingService());
net::URLRequestContextGetter* request_context =
@@ -546,12 +448,6 @@ IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest,
scoped_refptr<SafeBrowsingServiceTestHelper> safe_browsing_helper(
new SafeBrowsingServiceTestHelper(this, request_context));
int last_step = 0;
- FilePath datafile_path = FilePath(kDataFile);
- SafeBrowsingTestServer test_server(datafile_path);
- ASSERT_TRUE(test_server.Start());
-
- // Make sure the server is running.
- safe_browsing_helper->WaitTillServerReady(server_host, server_port);
// Waits and makes sure safebrowsing update is not happening.
// The wait will stop once OnWaitForStatusUpdateDone in
@@ -588,9 +484,7 @@ IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest,
// Fetches URLs to verify and waits till server responses with data.
EXPECT_EQ(net::URLRequestStatus::SUCCESS,
- safe_browsing_helper->FetchUrlsToVerify(server_host,
- server_port,
- step));
+ safe_browsing_helper->FetchUrlsToVerify(test_server(), step));
std::vector<PhishingUrl> phishing_urls;
EXPECT_TRUE(ParsePhishingUrls(safe_browsing_helper->response_data(),
@@ -619,18 +513,13 @@ IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest,
// TODO(lzheng): We should verify the fetched database with local
// database to make sure they match.
EXPECT_EQ(net::URLRequestStatus::SUCCESS,
- safe_browsing_helper->FetchDBToVerify(server_host,
- server_port,
- step));
+ safe_browsing_helper->FetchDBToVerify(test_server(), step));
EXPECT_GT(safe_browsing_helper->response_data().size(), 0U);
last_step = step;
}
// Verifies with server if test is done and waits till server responses.
EXPECT_EQ(net::URLRequestStatus::SUCCESS,
- safe_browsing_helper->VerifyTestComplete(server_host,
- server_port,
- last_step));
+ safe_browsing_helper->VerifyTestComplete(test_server(), last_step));
EXPECT_EQ("yes", safe_browsing_helper->response_data());
- test_server.Stop();
}
diff --git a/chrome/browser/safe_browsing/safe_browsing_testserver.py b/chrome/browser/safe_browsing/safe_browsing_testserver.py
new file mode 100755
index 0000000..674bf5a
--- /dev/null
+++ b/chrome/browser/safe_browsing/safe_browsing_testserver.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Wraps the upstream safebrowsing_test_server.py to run in Chrome tests."""
+
+import os
+import sys
+
+BASE_DIR = os.path.dirname(os.path.abspath(__file__))
+
+sys.path.append(os.path.join(BASE_DIR, '..', '..', '..', 'net',
+ 'tools', 'testserver'))
+import testserver_base
+
+
+class ServerRunner(testserver_base.TestServerRunner):
+ """TestServerRunner for safebrowsing_test_server.py."""
+
+ def create_server(self, server_data):
+ sys.path.append(os.path.join(BASE_DIR, '..', '..', '..', 'third_party',
+ 'safe_browsing', 'testing'))
+ import safebrowsing_test_server
+ server = safebrowsing_test_server.SetupServer(
+ self.options.data_file, self.options.host, self.options.port,
+ opt_enforce_caching=False, opt_validate_database=True)
+ print 'Safebrowsing HTTP server started on port %d...' % server.server_port
+ server_data['port'] = server.server_port
+
+ return server
+
+ def add_options(self):
+ testserver_base.TestServerRunner.add_options(self)
+ self.option_parser.add_option('--data-file', dest='data_file',
+ help='File containing safebrowsing test '
+ 'data and expectations')
+ # TODO(mattm): we define an unnecessary --data-dir option because
+ # BaseTestServer unconditionally sets --data-dir. This should be removed
+ # when BaseTestServer is refactored to not contain all the net/
+ # test_server.py specific stuff.
+ self.option_parser.add_option('--data-dir', dest='data_dir',
+ help='unused')
+
+
+if __name__ == '__main__':
+ sys.exit(ServerRunner().main())
diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi
index b44e8ab..2227e0c 100644
--- a/chrome/chrome_tests.gypi
+++ b/chrome/chrome_tests.gypi
@@ -3785,6 +3785,7 @@
],
'sources': [
'app/chrome_dll.rc',
+ 'browser/safe_browsing/local_safebrowsing_test_server.cc',
'browser/safe_browsing/safe_browsing_test.cc',
'test/base/chrome_test_launcher.cc',
],