This source file includes following definitions.
- SetUp
- CreateProtocolManager
- ValidateUpdateFetcherRequest
- ValidateUpdateFetcherRequest
- ValidateRedirectFetcherRequest
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- InvokeGetChunksCallback
- HandleAddChunks
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/strings/stringprintf.h"
#include "base/test/test_simple_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "chrome/browser/safe_browsing/protocol_manager.h"
#include "google_apis/google_api_keys.h"
#include "net/base/escape.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gmock_mutant.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
using base::TimeDelta;
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";
class SafeBrowsingProtocolManagerTest : public testing::Test {
protected:
std::string key_param_;
virtual void SetUp() {
std::string key = google_apis::GetAPIKey();
if (!key.empty()) {
key_param_ = base::StringPrintf(
"&key=%s",
net::EscapeQueryParamValue(key, true).c_str());
}
}
scoped_ptr<SafeBrowsingProtocolManager> CreateProtocolManager(
SafeBrowsingProtocolManagerDelegate* delegate) {
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>(
SafeBrowsingProtocolManager::Create(delegate, NULL, config));
}
void ValidateUpdateFetcherRequest(
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 ValidateUpdateFetcherRequest(const net::TestURLFetcher* url_fetcher) {
ValidateUpdateFetcherRequest(url_fetcher, kUrlPrefix);
}
void ValidateRedirectFetcherRequest(const net::TestURLFetcher* url_fetcher,
const std::string& expected_url) {
ASSERT_TRUE(url_fetcher);
EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags());
EXPECT_EQ("", url_fetcher->upload_data());
EXPECT_EQ(GURL(expected_url), url_fetcher->GetOriginalURL());
}
};
TEST_F(SafeBrowsingProtocolManagerTest, TestBackOffTimes) {
scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
pm->next_update_interval_ = TimeDelta::FromSeconds(1800);
ASSERT_TRUE(pm->back_off_fuzz_ >= 0.0 && pm->back_off_fuzz_ <= 1.0);
TimeDelta next;
next = pm->GetNextUpdateInterval(false);
EXPECT_EQ(next, TimeDelta::FromSeconds(1800));
next = pm->GetNextUpdateInterval(true);
EXPECT_EQ(next, TimeDelta::FromSeconds(60));
next = pm->GetNextUpdateInterval(true);
EXPECT_TRUE(next >= TimeDelta::FromMinutes(30) &&
next <= TimeDelta::FromMinutes(60));
next = pm->GetNextUpdateInterval(true);
EXPECT_TRUE(next >= TimeDelta::FromMinutes(60) &&
next <= TimeDelta::FromMinutes(120));
next = pm->GetNextUpdateInterval(true);
EXPECT_TRUE(next >= TimeDelta::FromMinutes(120) &&
next <= TimeDelta::FromMinutes(240));
next = pm->GetNextUpdateInterval(true);
EXPECT_TRUE(next >= TimeDelta::FromMinutes(240) &&
next <= TimeDelta::FromMinutes(480));
next = pm->GetNextUpdateInterval(true);
EXPECT_EQ(next, TimeDelta::FromMinutes(480));
next = pm->GetNextUpdateInterval(true);
EXPECT_EQ(next, TimeDelta::FromMinutes(480));
next = pm->GetNextUpdateInterval(false);
EXPECT_EQ(next, TimeDelta::FromSeconds(1800));
}
TEST_F(SafeBrowsingProtocolManagerTest, TestChunkStrings) {
scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
SBListChunkRanges phish("goog-phish-shavar");
phish.adds = "1,4,6,8-20,99";
phish.subs = "16,32,64-96";
EXPECT_EQ(pm->FormatList(phish),
"goog-phish-shavar;a:1,4,6,8-20,99:s:16,32,64-96\n");
phish.subs = "";
EXPECT_EQ(pm->FormatList(phish), "goog-phish-shavar;a:1,4,6,8-20,99\n");
phish.adds = "";
phish.subs = "16,32,64-96";
EXPECT_EQ(pm->FormatList(phish), "goog-phish-shavar;s:16,32,64-96\n");
phish.adds = "";
phish.subs = "";
EXPECT_EQ(pm->FormatList(phish), "goog-phish-shavar;\n");
}
TEST_F(SafeBrowsingProtocolManagerTest, TestGetHashBackOffTimes) {
scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
EXPECT_EQ(pm->gethash_error_count_, 0);
EXPECT_TRUE(pm->next_gethash_time_.is_null());
Time now = Time::Now();
pm->HandleGetHashError(now);
EXPECT_EQ(pm->gethash_error_count_, 1);
TimeDelta margin = TimeDelta::FromSeconds(5);
Time future = now + TimeDelta::FromMinutes(1);
EXPECT_TRUE(pm->next_gethash_time_ >= future - margin &&
pm->next_gethash_time_ <= future + margin);
pm->HandleGetHashError(now);
EXPECT_EQ(pm->gethash_error_count_, 2);
EXPECT_TRUE(pm->next_gethash_time_ >= now + TimeDelta::FromMinutes(30));
EXPECT_TRUE(pm->next_gethash_time_ <= now + TimeDelta::FromMinutes(60));
pm->HandleGetHashError(now);
EXPECT_EQ(pm->gethash_error_count_, 3);
EXPECT_TRUE(pm->next_gethash_time_ >= now + TimeDelta::FromMinutes(60));
EXPECT_TRUE(pm->next_gethash_time_ <= now + TimeDelta::FromMinutes(120));
pm->HandleGetHashError(now);
EXPECT_EQ(pm->gethash_error_count_, 4);
EXPECT_TRUE(pm->next_gethash_time_ >= now + TimeDelta::FromMinutes(120));
EXPECT_TRUE(pm->next_gethash_time_ <= now + TimeDelta::FromMinutes(240));
pm->HandleGetHashError(now);
EXPECT_EQ(pm->gethash_error_count_, 5);
EXPECT_TRUE(pm->next_gethash_time_ >= now + TimeDelta::FromMinutes(240));
EXPECT_TRUE(pm->next_gethash_time_ <= now + TimeDelta::FromMinutes(480));
pm->HandleGetHashError(now);
EXPECT_EQ(pm->gethash_error_count_, 6);
EXPECT_TRUE(pm->next_gethash_time_ == now + TimeDelta::FromMinutes(480));
pm->HandleGetHashError(now);
EXPECT_EQ(pm->gethash_error_count_, 7);
EXPECT_TRUE(pm->next_gethash_time_== now + TimeDelta::FromMinutes(480));
}
TEST_F(SafeBrowsingProtocolManagerTest, TestGetHashUrl) {
scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
EXPECT_EQ("https://prefix.com/foo/gethash?client=unittest&appver=1.0&"
"pver=2.2" + key_param_, pm->GetHashUrl().spec());
pm->set_additional_query(kAdditionalQuery);
EXPECT_EQ("https://prefix.com/foo/gethash?client=unittest&appver=1.0&"
"pver=2.2" + key_param_ + "&additional_query",
pm->GetHashUrl().spec());
}
TEST_F(SafeBrowsingProtocolManagerTest, TestUpdateUrl) {
scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
EXPECT_EQ("https://prefix.com/foo/downloads?client=unittest&appver=1.0&"
"pver=2.2" + key_param_, pm->UpdateUrl().spec());
pm->set_additional_query(kAdditionalQuery);
EXPECT_EQ("https://prefix.com/foo/downloads?client=unittest&appver=1.0&"
"pver=2.2" + key_param_ + "&additional_query",
pm->UpdateUrl().spec());
}
TEST_F(SafeBrowsingProtocolManagerTest, TestNextChunkUrl) {
scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
std::string url_partial = "localhost:1234/foo/bar?foo";
std::string url_http_full = "http://localhost:1234/foo/bar?foo";
std::string url_https_full = "https://localhost:1234/foo/bar?foo";
std::string url_https_no_query = "https://localhost:1234/foo/bar";
EXPECT_EQ("https://localhost:1234/foo/bar?foo",
pm->NextChunkUrl(url_partial).spec());
EXPECT_EQ("http://localhost:1234/foo/bar?foo",
pm->NextChunkUrl(url_http_full).spec());
EXPECT_EQ("https://localhost:1234/foo/bar?foo",
pm->NextChunkUrl(url_https_full).spec());
EXPECT_EQ("https://localhost:1234/foo/bar",
pm->NextChunkUrl(url_https_no_query).spec());
pm->set_additional_query(kAdditionalQuery);
EXPECT_EQ("https://localhost:1234/foo/bar?foo&additional_query",
pm->NextChunkUrl(url_partial).spec());
EXPECT_EQ("http://localhost:1234/foo/bar?foo&additional_query",
pm->NextChunkUrl(url_http_full).spec());
EXPECT_EQ("https://localhost:1234/foo/bar?foo&additional_query",
pm->NextChunkUrl(url_https_full).spec());
EXPECT_EQ("https://localhost:1234/foo/bar?additional_query",
pm->NextChunkUrl(url_https_no_query).spec());
}
namespace {
class MockProtocolDelegate : public SafeBrowsingProtocolManagerDelegate {
public:
MockProtocolDelegate() {}
virtual ~MockProtocolDelegate() {}
MOCK_METHOD0(UpdateStarted, void());
MOCK_METHOD1(UpdateFinished, void(bool));
MOCK_METHOD0(ResetDatabase, void());
MOCK_METHOD1(GetChunks, void(GetChunksCallback));
MOCK_METHOD3(AddChunks, void(const std::string&, SBChunkList*,
AddChunksCallback));
MOCK_METHOD1(DeleteChunks, void(std::vector<SBChunkDelete>*));
};
void InvokeGetChunksCallback(
const std::vector<SBListChunkRanges>& ranges,
bool database_error,
SafeBrowsingProtocolManagerDelegate::GetChunksCallback callback) {
callback.Run(ranges, database_error);
}
void HandleAddChunks(
const std::string& unused_list,
SBChunkList* chunks,
SafeBrowsingProtocolManagerDelegate::AddChunksCallback callback) {
delete chunks;
scoped_refptr<base::SingleThreadTaskRunner> task_runner(
base::ThreadTaskRunnerHandle::Get());
if (!task_runner.get())
return;
task_runner->PostTask(FROM_HERE, callback);
}
}
TEST_F(SafeBrowsingProtocolManagerTest, ProblemAccessingDatabase) {
scoped_refptr<base::TestSimpleTaskRunner> runner(
new base::TestSimpleTaskRunner());
base::ThreadTaskRunnerHandle runner_handler(runner);
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>(),
true)));
EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1);
scoped_ptr<SafeBrowsingProtocolManager> pm(
CreateProtocolManager(&test_delegate));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
EXPECT_TRUE(pm->IsUpdateScheduled());
}
TEST_F(SafeBrowsingProtocolManagerTest, ExistingDatabase) {
scoped_refptr<base::TestSimpleTaskRunner> runner(
new base::TestSimpleTaskRunner());
base::ThreadTaskRunnerHandle runner_handler(runner);
net::TestURLFetcherFactory url_fetcher_factory;
std::vector<SBListChunkRanges> ranges;
SBListChunkRanges range_phish(safe_browsing_util::kPhishingList);
range_phish.adds = "adds_phish";
range_phish.subs = "subs_phish";
ranges.push_back(range_phish);
SBListChunkRanges range_unknown("unknown_list");
range_unknown.adds = "adds_unknown";
range_unknown.subs = "subs_unknown";
ranges.push_back(range_unknown);
testing::StrictMock<MockProtocolDelegate> test_delegate;
EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
ranges,
false)));
EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
scoped_ptr<SafeBrowsingProtocolManager> pm(
CreateProtocolManager(&test_delegate));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ASSERT_TRUE(url_fetcher);
EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags());
EXPECT_EQ("goog-phish-shavar;a:adds_phish:s:subs_phish\n"
"unknown_list;a:adds_unknown:s:subs_unknown\n"
"goog-malware-shavar;\n",
url_fetcher->upload_data());
EXPECT_EQ(GURL("https://prefix.com/foo/downloads?client=unittest&appver=1.0"
"&pver=2.2" + key_param_),
url_fetcher->GetOriginalURL());
url_fetcher->set_status(net::URLRequestStatus());
url_fetcher->set_response_code(200);
url_fetcher->SetResponseString(std::string());
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseBadBodyBackupSuccess) {
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));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(net::URLRequestStatus());
url_fetcher->set_response_code(200);
url_fetcher->SetResponseString("THIS_IS_A_BAD_RESPONSE");
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* backup_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateUpdateFetcherRequest(backup_url_fetcher,
kBackupHttpUrlPrefix);
backup_url_fetcher->set_status(net::URLRequestStatus());
backup_url_fetcher->set_response_code(200);
backup_url_fetcher->SetResponseString(std::string());
backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
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));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(net::URLRequestStatus());
url_fetcher->set_response_code(404);
url_fetcher->SetResponseString(std::string());
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* backup_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateUpdateFetcherRequest(backup_url_fetcher, kBackupHttpUrlPrefix);
backup_url_fetcher->set_status(net::URLRequestStatus());
backup_url_fetcher->set_response_code(404);
backup_url_fetcher->SetResponseString(std::string());
backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
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));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(net::URLRequestStatus());
url_fetcher->set_response_code(404);
url_fetcher->SetResponseString(std::string());
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* backup_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateUpdateFetcherRequest(backup_url_fetcher,
kBackupHttpUrlPrefix);
backup_url_fetcher->set_status(net::URLRequestStatus());
backup_url_fetcher->set_response_code(200);
backup_url_fetcher->SetResponseString(std::string());
backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpErrorBackupTimeout) {
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));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(net::URLRequestStatus());
url_fetcher->set_response_code(404);
url_fetcher->SetResponseString(std::string());
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* backup_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateUpdateFetcherRequest(backup_url_fetcher, kBackupHttpUrlPrefix);
runner->RunPendingTasks();
if (!pm->IsUpdateScheduled())
runner->RunPendingTasks();
EXPECT_TRUE(pm->IsUpdateScheduled());
}
TEST_F(SafeBrowsingProtocolManagerTest,
UpdateResponseConnectionErrorBackupError) {
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));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
net::ERR_CONNECTION_RESET));
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* backup_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateUpdateFetcherRequest(backup_url_fetcher,
kBackupConnectUrlPrefix);
backup_url_fetcher->set_status(net::URLRequestStatus());
backup_url_fetcher->set_response_code(404);
backup_url_fetcher->SetResponseString(std::string());
backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
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));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
net::ERR_CONNECTION_RESET));
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* backup_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateUpdateFetcherRequest(backup_url_fetcher,
kBackupConnectUrlPrefix);
backup_url_fetcher->set_status(net::URLRequestStatus());
backup_url_fetcher->set_response_code(200);
backup_url_fetcher->SetResponseString(std::string());
backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
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));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(
net::URLRequestStatus(net::URLRequestStatus::FAILED,
net::ERR_INTERNET_DISCONNECTED));
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* backup_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateUpdateFetcherRequest(backup_url_fetcher,
kBackupNetworkUrlPrefix);
backup_url_fetcher->set_status(net::URLRequestStatus());
backup_url_fetcher->set_response_code(404);
backup_url_fetcher->SetResponseString(std::string());
backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
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));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(
net::URLRequestStatus(net::URLRequestStatus::FAILED,
net::ERR_INTERNET_DISCONNECTED));
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* backup_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateUpdateFetcherRequest(backup_url_fetcher,
kBackupNetworkUrlPrefix);
backup_url_fetcher->set_status(net::URLRequestStatus());
backup_url_fetcher->set_response_code(200);
backup_url_fetcher->SetResponseString(std::string());
backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseTimeoutBackupSuccess) {
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));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
runner->RunPendingTasks();
net::TestURLFetcher* backup_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateUpdateFetcherRequest(backup_url_fetcher,
kBackupConnectUrlPrefix);
backup_url_fetcher->set_status(net::URLRequestStatus());
backup_url_fetcher->set_response_code(200);
backup_url_fetcher->SetResponseString(std::string());
backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseReset) {
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, ResetDatabase()).Times(1);
EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
scoped_ptr<SafeBrowsingProtocolManager> pm(
CreateProtocolManager(&test_delegate));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(net::URLRequestStatus());
url_fetcher->set_response_code(200);
url_fetcher->SetResponseString("r:pleasereset\n");
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
TEST_F(SafeBrowsingProtocolManagerTest, EmptyRedirectResponse) {
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));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(net::URLRequestStatus());
url_fetcher->set_response_code(200);
url_fetcher->SetResponseString(
"i:goog-phish-shavar\n"
"u:redirect-server.example.com/path\n");
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* chunk_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateRedirectFetcherRequest(
chunk_url_fetcher, "https://redirect-server.example.com/path");
chunk_url_fetcher->set_status(net::URLRequestStatus());
chunk_url_fetcher->set_response_code(200);
chunk_url_fetcher->SetResponseString(std::string());
chunk_url_fetcher->delegate()->OnURLFetchComplete(chunk_url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
TEST_F(SafeBrowsingProtocolManagerTest, InvalidRedirectResponse) {
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));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(net::URLRequestStatus());
url_fetcher->set_response_code(200);
url_fetcher->SetResponseString(
"i:goog-phish-shavar\n"
"u:redirect-server.example.com/path\n");
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* chunk_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateRedirectFetcherRequest(
chunk_url_fetcher, "https://redirect-server.example.com/path");
chunk_url_fetcher->set_status(net::URLRequestStatus());
chunk_url_fetcher->set_response_code(200);
chunk_url_fetcher->SetResponseString("THIS IS AN INVALID RESPONSE");
chunk_url_fetcher->delegate()->OnURLFetchComplete(chunk_url_fetcher);
EXPECT_TRUE(pm->IsUpdateScheduled());
}
TEST_F(SafeBrowsingProtocolManagerTest, SingleRedirectResponseWithChunks) {
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, AddChunks("goog-phish-shavar", _, _)).WillOnce(
Invoke(HandleAddChunks));
EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
scoped_ptr<SafeBrowsingProtocolManager> pm(
CreateProtocolManager(&test_delegate));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(net::URLRequestStatus());
url_fetcher->set_response_code(200);
url_fetcher->SetResponseString(
"i:goog-phish-shavar\n"
"u:redirect-server.example.com/path\n");
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* chunk_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateRedirectFetcherRequest(
chunk_url_fetcher, "https://redirect-server.example.com/path");
chunk_url_fetcher->set_status(net::URLRequestStatus());
chunk_url_fetcher->set_response_code(200);
chunk_url_fetcher->SetResponseString(
"a:4:4:9\n"
"host\1fdaf");
chunk_url_fetcher->delegate()->OnURLFetchComplete(chunk_url_fetcher);
EXPECT_FALSE(pm->IsUpdateScheduled());
runner->RunPendingTasks();
EXPECT_TRUE(pm->IsUpdateScheduled());
}
TEST_F(SafeBrowsingProtocolManagerTest, MultipleRedirectResponsesWithChunks) {
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, AddChunks("goog-phish-shavar", _, _)).
WillRepeatedly(Invoke(HandleAddChunks));
EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
scoped_ptr<SafeBrowsingProtocolManager> pm(
CreateProtocolManager(&test_delegate));
pm->ForceScheduleNextUpdate(TimeDelta());
runner->RunPendingTasks();
net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
ValidateUpdateFetcherRequest(url_fetcher);
url_fetcher->set_status(net::URLRequestStatus());
url_fetcher->set_response_code(200);
url_fetcher->SetResponseString(
"i:goog-phish-shavar\n"
"u:redirect-server.example.com/one\n"
"u:redirect-server.example.com/two\n");
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
net::TestURLFetcher* first_chunk_url_fetcher =
url_fetcher_factory.GetFetcherByID(1);
ValidateRedirectFetcherRequest(
first_chunk_url_fetcher, "https://redirect-server.example.com/one");
first_chunk_url_fetcher->set_status(net::URLRequestStatus());
first_chunk_url_fetcher->set_response_code(200);
first_chunk_url_fetcher->SetResponseString(
"a:4:4:9\n"
"host\1aaaa");
first_chunk_url_fetcher->delegate()->OnURLFetchComplete(
first_chunk_url_fetcher);
runner->RunPendingTasks();
EXPECT_FALSE(pm->IsUpdateScheduled());
net::TestURLFetcher* second_chunk_url_fetcher =
url_fetcher_factory.GetFetcherByID(2);
ValidateRedirectFetcherRequest(
second_chunk_url_fetcher, "https://redirect-server.example.com/two");
second_chunk_url_fetcher->set_status(net::URLRequestStatus());
second_chunk_url_fetcher->set_response_code(200);
second_chunk_url_fetcher->SetResponseString(
"a:5:4:9\n"
"host\1bbbb");
second_chunk_url_fetcher->delegate()->OnURLFetchComplete(
second_chunk_url_fetcher);
EXPECT_FALSE(pm->IsUpdateScheduled());
runner->RunPendingTasks();
EXPECT_TRUE(pm->IsUpdateScheduled());
}