Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7339)

Unified Diff: chrome/browser/safe_browsing/protocol_manager_unittest.cc

Issue 11784038: Add backup URL support for SafeBrowsing data requests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix bad response case Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/safe_browsing/protocol_manager_unittest.cc
diff --git a/chrome/browser/safe_browsing/protocol_manager_unittest.cc b/chrome/browser/safe_browsing/protocol_manager_unittest.cc
index 3f56aac244e4ed8ea1437df00e04d082ad4ce621..777fe148a293ba5a2f779820a69f40de4951a708 100644
--- a/chrome/browser/safe_browsing/protocol_manager_unittest.cc
+++ b/chrome/browser/safe_browsing/protocol_manager_unittest.cc
@@ -23,6 +23,9 @@ using testing::_;
using testing::Invoke;
static const char kUrlPrefix[] = "https://prefix.com/foo";
+static const char kBackupConnectUrlPrefix[] = "https://alt1-prefix.com/foo";
+static const char kBackupHttpUrlPrefix[] = "https://alt2-prefix.com/foo";
+static const char kBackupNetworkUrlPrefix[] = "https://alt3-prefix.com/foo";
static const char kClient[] = "unittest";
static const char kAppVer[] = "1.0";
static const char kAdditionalQuery[] = "additional_query";
@@ -45,6 +48,9 @@ class SafeBrowsingProtocolManagerTest : public testing::Test {
SafeBrowsingProtocolConfig config;
config.client_name = kClient;
config.url_prefix = kUrlPrefix;
+ config.backup_connect_error_url_prefix = kBackupConnectUrlPrefix;
+ config.backup_http_error_url_prefix = kBackupHttpUrlPrefix;
+ config.backup_network_error_url_prefix = kBackupNetworkUrlPrefix;
config.version = kAppVer;
return scoped_ptr<SafeBrowsingProtocolManager>(
@@ -61,6 +67,18 @@ class SafeBrowsingProtocolManagerTest : public testing::Test {
url_fetcher->GetOriginalURL());
}
+ void ValidateBackupUpdateFetcherRequest(
mattm 2013/01/12 02:24:49 maybe ValidateUpdateFetcherRequest should just cal
cbentzel 2013/01/14 11:33:35 Good idea. Done.
+ const net::TestURLFetcher* url_fetcher,
+ const std::string& expected_prefix) {
+ ASSERT_TRUE(url_fetcher);
+ EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags());
+ EXPECT_EQ("goog-phish-shavar;\ngoog-malware-shavar;\n",
+ url_fetcher->upload_data());
+ EXPECT_EQ(GURL(expected_prefix + "/downloads?client=unittest&appver=1.0"
+ "&pver=2.2" + key_param_),
+ url_fetcher->GetOriginalURL());
+ }
+
void ValidateRedirectFetcherRequest(const net::TestURLFetcher* url_fetcher,
const std::string& expected_url) {
ASSERT_TRUE(url_fetcher);
@@ -406,8 +424,104 @@ TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseBadBody) {
EXPECT_TRUE(pm->IsUpdateScheduled());
}
-// Tests what happens when there is an error in the update response.
-TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpError) {
+// Tests what happens when there is an HTTP error response to the update
+// request, as well as an error response to the backup update request.
+TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpErrorBackupError) {
+ scoped_refptr<base::TestSimpleTaskRunner> runner(
+ new base::TestSimpleTaskRunner());
+ base::ThreadTaskRunnerHandle runner_handler(runner);
+ net::TestURLFetcherFactory url_fetcher_factory;
+
+ testing::StrictMock<MockProtocolDelegate> test_delegate;
+ EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
+ EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
+ Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
+ std::vector<SBListChunkRanges>(),
+ false)));
+ EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1);
+
+ scoped_ptr<SafeBrowsingProtocolManager> pm(
+ CreateProtocolManager(&test_delegate));
+
+ // Kick off initialization. This returns chunks from the DB synchronously.
+ pm->ForceScheduleNextUpdate(TimeDelta());
+ runner->RunPendingTasks();
+
+ // We should have an URLFetcher at this point in time.
+ net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
+ ValidateUpdateFetcherRequest(url_fetcher);
+
+ // Go ahead and respond to it.
+ url_fetcher->set_status(net::URLRequestStatus());
+ url_fetcher->set_response_code(404);
+ url_fetcher->SetResponseString("");
+ url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
+
+ // There should now be a backup request.
+ net::TestURLFetcher* backup_url_fetcher =
+ url_fetcher_factory.GetFetcherByID(1);
+ ValidateBackupUpdateFetcherRequest(backup_url_fetcher, kBackupHttpUrlPrefix);
+
+ // Respond to the backup unsuccessfully.
+ backup_url_fetcher->set_status(net::URLRequestStatus());
+ backup_url_fetcher->set_response_code(404);
+ backup_url_fetcher->SetResponseString("");
+ backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
+
+ EXPECT_TRUE(pm->IsUpdateScheduled());
+}
+
+// Tests what happens when there is an HTTP error response to the update
+// request, followed by a successful response to the backup update request.
+TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpErrorBackupSuccess) {
+ scoped_refptr<base::TestSimpleTaskRunner> runner(
+ new base::TestSimpleTaskRunner());
+ base::ThreadTaskRunnerHandle runner_handler(runner);
+ net::TestURLFetcherFactory url_fetcher_factory;
+
+ testing::StrictMock<MockProtocolDelegate> test_delegate;
+ EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
+ EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
+ Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
+ std::vector<SBListChunkRanges>(),
+ false)));
+ EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
+
+ scoped_ptr<SafeBrowsingProtocolManager> pm(
+ CreateProtocolManager(&test_delegate));
+
+ // Kick off initialization. This returns chunks from the DB synchronously.
+ pm->ForceScheduleNextUpdate(TimeDelta());
+ runner->RunPendingTasks();
+
+ // We should have an URLFetcher at this point in time.
+ net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
+ ValidateUpdateFetcherRequest(url_fetcher);
+
+ // Go ahead and respond to it.
+ url_fetcher->set_status(net::URLRequestStatus());
+ url_fetcher->set_response_code(404);
+ url_fetcher->SetResponseString("");
+ url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
+
+ // There should now be a backup request.
+ net::TestURLFetcher* backup_url_fetcher =
+ url_fetcher_factory.GetFetcherByID(1);
+ ValidateBackupUpdateFetcherRequest(backup_url_fetcher,
+ kBackupHttpUrlPrefix);
+
+ // Respond to the backup successfully.
+ backup_url_fetcher->set_status(net::URLRequestStatus());
+ backup_url_fetcher->set_response_code(200);
+ backup_url_fetcher->SetResponseString("");
+ backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
+
+ EXPECT_TRUE(pm->IsUpdateScheduled());
+}
+
+// Tests what happens when there is an HTTP error response to the update
+// request, and a timeout on the backup update request.
+TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpErrorBackupTimeout) {
scoped_refptr<base::TestSimpleTaskRunner> runner(
new base::TestSimpleTaskRunner());
base::ThreadTaskRunnerHandle runner_handler(runner);
@@ -438,11 +552,27 @@ TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpError) {
url_fetcher->SetResponseString("");
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
+ // There should now be a backup request.
+ net::TestURLFetcher* backup_url_fetcher =
+ url_fetcher_factory.GetFetcherByID(1);
+ ValidateBackupUpdateFetcherRequest(backup_url_fetcher, kBackupHttpUrlPrefix);
+
+ // This call of RunPendingTasks will invoke the timeout.
+ runner->RunPendingTasks();
+
+ // Respond to the backup unsuccessfully.
+ backup_url_fetcher->set_status(net::URLRequestStatus());
+ backup_url_fetcher->set_response_code(404);
+ backup_url_fetcher->SetResponseString("");
+ backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
+
EXPECT_TRUE(pm->IsUpdateScheduled());
}
-// Tests what happens when there is an error with the connection.
-TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseConnectionError) {
+// Tests what happens when there is a connection error when issuing the update
+// request, and an error with the backup update request.
+TEST_F(SafeBrowsingProtocolManagerTest,
+ UpdateResponseConnectionErrorBackupError) {
scoped_refptr<base::TestSimpleTaskRunner> runner(
new base::TestSimpleTaskRunner());
base::ThreadTaskRunnerHandle runner_handler(runner);
@@ -472,6 +602,163 @@ TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseConnectionError) {
net::ERR_CONNECTION_RESET));
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
+ // There should be a backup URLFetcher now.
+ net::TestURLFetcher* backup_url_fetcher =
+ url_fetcher_factory.GetFetcherByID(1);
+ ValidateBackupUpdateFetcherRequest(backup_url_fetcher,
+ kBackupConnectUrlPrefix);
+
+ // Respond to the backup unsuccessfully.
+ backup_url_fetcher->set_status(net::URLRequestStatus());
+ backup_url_fetcher->set_response_code(404);
+ backup_url_fetcher->SetResponseString("");
+ backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
+
+ EXPECT_TRUE(pm->IsUpdateScheduled());
+}
+
+// Tests what happens when there is a connection error when issuing the update
+// request, and a successful response to the backup update request.
+TEST_F(SafeBrowsingProtocolManagerTest,
+ UpdateResponseConnectionErrorBackupSuccess) {
+ scoped_refptr<base::TestSimpleTaskRunner> runner(
+ new base::TestSimpleTaskRunner());
+ base::ThreadTaskRunnerHandle runner_handler(runner);
+ net::TestURLFetcherFactory url_fetcher_factory;
+
+ testing::StrictMock<MockProtocolDelegate> test_delegate;
+ EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
+ EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
+ Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
+ std::vector<SBListChunkRanges>(),
+ false)));
+ EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
+
+ scoped_ptr<SafeBrowsingProtocolManager> pm(
+ CreateProtocolManager(&test_delegate));
+
+ // Kick off initialization. This returns chunks from the DB synchronously.
+ pm->ForceScheduleNextUpdate(TimeDelta());
+ runner->RunPendingTasks();
+
+ // We should have an URLFetcher at this point in time.
+ net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
+ ValidateUpdateFetcherRequest(url_fetcher);
+
+ // Go ahead and respond to it.
+ url_fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
+ net::ERR_CONNECTION_RESET));
+ url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
+
+ // There should be a backup URLFetcher now.
+ net::TestURLFetcher* backup_url_fetcher =
+ url_fetcher_factory.GetFetcherByID(1);
+ ValidateBackupUpdateFetcherRequest(backup_url_fetcher,
+ kBackupConnectUrlPrefix);
+
+ // Respond to the backup unsuccessfully.
+ backup_url_fetcher->set_status(net::URLRequestStatus());
+ backup_url_fetcher->set_response_code(200);
+ backup_url_fetcher->SetResponseString("");
+ backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
+
+ EXPECT_TRUE(pm->IsUpdateScheduled());
+}
+// Tests what happens when there is a network state error when issuing the
+// update request, and an error with the backup update request.
+TEST_F(SafeBrowsingProtocolManagerTest,
+ UpdateResponseNetworkErrorBackupError) {
+ scoped_refptr<base::TestSimpleTaskRunner> runner(
+ new base::TestSimpleTaskRunner());
+ base::ThreadTaskRunnerHandle runner_handler(runner);
+ net::TestURLFetcherFactory url_fetcher_factory;
+
+ testing::StrictMock<MockProtocolDelegate> test_delegate;
+ EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
+ EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
+ Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
+ std::vector<SBListChunkRanges>(),
+ false)));
+ EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1);
+
+ scoped_ptr<SafeBrowsingProtocolManager> pm(
+ CreateProtocolManager(&test_delegate));
+
+ // Kick off initialization. This returns chunks from the DB synchronously.
+ pm->ForceScheduleNextUpdate(TimeDelta());
+ runner->RunPendingTasks();
+
+ // We should have an URLFetcher at this point in time.
+ net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
+ ValidateUpdateFetcherRequest(url_fetcher);
+
+ // Go ahead and respond to it.
+ url_fetcher->set_status(
+ net::URLRequestStatus(net::URLRequestStatus::FAILED,
+ net::ERR_INTERNET_DISCONNECTED));
+ url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
+
+ // There should be a backup URLFetcher now.
+ net::TestURLFetcher* backup_url_fetcher =
+ url_fetcher_factory.GetFetcherByID(1);
+ ValidateBackupUpdateFetcherRequest(backup_url_fetcher,
+ kBackupNetworkUrlPrefix);
+
+ // Respond to the backup unsuccessfully.
+ backup_url_fetcher->set_status(net::URLRequestStatus());
+ backup_url_fetcher->set_response_code(404);
+ backup_url_fetcher->SetResponseString("");
+ backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
+
+ EXPECT_TRUE(pm->IsUpdateScheduled());
+}
+
+// Tests what happens when there is a network state error when issuing the
+// update request, and a successful response to the backup update request.
+TEST_F(SafeBrowsingProtocolManagerTest,
+ UpdateResponseNetworkErrorBackupSuccess) {
+ scoped_refptr<base::TestSimpleTaskRunner> runner(
+ new base::TestSimpleTaskRunner());
+ base::ThreadTaskRunnerHandle runner_handler(runner);
+ net::TestURLFetcherFactory url_fetcher_factory;
+
+ testing::StrictMock<MockProtocolDelegate> test_delegate;
+ EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
+ EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
+ Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
+ std::vector<SBListChunkRanges>(),
+ false)));
+ EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
+
+ scoped_ptr<SafeBrowsingProtocolManager> pm(
+ CreateProtocolManager(&test_delegate));
+
+ // Kick off initialization. This returns chunks from the DB synchronously.
+ pm->ForceScheduleNextUpdate(TimeDelta());
+ runner->RunPendingTasks();
+
+ // We should have an URLFetcher at this point in time.
+ net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
+ ValidateUpdateFetcherRequest(url_fetcher);
+
+ // Go ahead and respond to it.
+ url_fetcher->set_status(
+ net::URLRequestStatus(net::URLRequestStatus::FAILED,
+ net::ERR_INTERNET_DISCONNECTED));
+ url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
+
+ // There should be a backup URLFetcher now.
+ net::TestURLFetcher* backup_url_fetcher =
+ url_fetcher_factory.GetFetcherByID(1);
+ ValidateBackupUpdateFetcherRequest(backup_url_fetcher,
+ kBackupNetworkUrlPrefix);
+
+ // Respond to the backup unsuccessfully.
+ backup_url_fetcher->set_status(net::URLRequestStatus());
+ backup_url_fetcher->set_response_code(200);
+ backup_url_fetcher->SetResponseString("");
+ backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
+
EXPECT_TRUE(pm->IsUpdateScheduled());
}

Powered by Google App Engine
This is Rietveld 408576698