From 653aa54bd4a87008873f5d3154eb6d82f222f029 Mon Sep 17 00:00:00 2001 From: "idanan@chromium.org" Date: Tue, 28 Jul 2009 15:18:24 +0000 Subject: Load Multiple Blacklists Add the ability to load multiple text blacklists into the binary blacklist representation. NOTE: Included Binary file means this change expects to fail on the trybots in blacklist_io_test.cc:103. All other checks must pass. BUG=16932 TEST=BlacklistIO* Review URL: http://codereview.chromium.org/159199 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21841 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/privacy_blacklist/blacklist.cc | 17 +++++ chrome/browser/privacy_blacklist/blacklist.h | 29 ++++----- chrome/browser/privacy_blacklist/blacklist_io.h | 1 + .../privacy_blacklist/blacklist_io_unittest.cc | 76 +++++++++++++++++++--- 4 files changed, 98 insertions(+), 25 deletions(-) (limited to 'chrome/browser/privacy_blacklist') diff --git a/chrome/browser/privacy_blacklist/blacklist.cc b/chrome/browser/privacy_blacklist/blacklist.cc index a234bf2..0be3931 100644 --- a/chrome/browser/privacy_blacklist/blacklist.cc +++ b/chrome/browser/privacy_blacklist/blacklist.cc @@ -22,6 +22,23 @@ const char* const cookie_headers[2] = { "cookie", "set-cookie" }; } // namespace +const unsigned int Blacklist::kBlockAll = 1; +const unsigned int Blacklist::kDontSendCookies = 1 << 1; +const unsigned int Blacklist::kDontStoreCookies = 1 << 2; +const unsigned int Blacklist::kDontPersistCookies = 1 << 3; +const unsigned int Blacklist::kDontSendReferrer = 1 << 4; +const unsigned int Blacklist::kDontSendUserAgent = 1 << 5; +const unsigned int Blacklist::kBlockByType = 1 << 6; +const unsigned int Blacklist::kBlockUnsecure = 1 << 7; +const unsigned int Blacklist::kBlockRequest = kBlockAll | kBlockUnsecure; +const unsigned int Blacklist::kBlockResponse = kBlockByType; +const unsigned int Blacklist::kModifySentHeaders = + kDontSendCookies | kDontSendUserAgent | kDontSendReferrer; +const unsigned int Blacklist::kModifyReceivedHeaders = + kDontPersistCookies | kDontStoreCookies; +const unsigned int Blacklist::kFilterByHeaders = + kModifyReceivedHeaders | kBlockByType; + // Value is not important, here just that the object has an address. const void* const Blacklist::kRequestDataKey = 0; diff --git a/chrome/browser/privacy_blacklist/blacklist.h b/chrome/browser/privacy_blacklist/blacklist.h index 4c36d08..a4b6b9ba 100644 --- a/chrome/browser/privacy_blacklist/blacklist.h +++ b/chrome/browser/privacy_blacklist/blacklist.h @@ -32,24 +32,21 @@ class FilePath; class Blacklist { public: // Filter attributes (more to come): - static const unsigned int kBlockAll = 1; - static const unsigned int kDontSendCookies = 1 << 1; - static const unsigned int kDontStoreCookies = 1 << 2; - static const unsigned int kDontPersistCookies = 1 << 3; - static const unsigned int kDontSendReferrer = 1 << 4; - static const unsigned int kDontSendUserAgent = 1 << 5; - static const unsigned int kBlockByType = 1 << 6; - static const unsigned int kBlockUnsecure = 1 << 7; + static const unsigned int kBlockAll; + static const unsigned int kDontSendCookies; + static const unsigned int kDontStoreCookies; + static const unsigned int kDontPersistCookies; + static const unsigned int kDontSendReferrer; + static const unsigned int kDontSendUserAgent; + static const unsigned int kBlockByType; + static const unsigned int kBlockUnsecure; // Aggregate filter types: - static const unsigned int kBlockRequest = kBlockAll | kBlockUnsecure; - static const unsigned int kBlockResponse = kBlockByType; - static const unsigned int kModifySentHeaders = - kDontSendCookies | kDontSendUserAgent | kDontSendReferrer; - static const unsigned int kModifyReceivedHeaders = - kDontPersistCookies | kDontStoreCookies; - static const unsigned int kFilterByHeaders = kModifyReceivedHeaders | - kBlockByType; + static const unsigned int kBlockRequest; + static const unsigned int kBlockResponse; + static const unsigned int kModifySentHeaders; + static const unsigned int kModifyReceivedHeaders; + static const unsigned int kFilterByHeaders; // Key used to access data attached to URLRequest objects. static const void* const kRequestDataKey; diff --git a/chrome/browser/privacy_blacklist/blacklist_io.h b/chrome/browser/privacy_blacklist/blacklist_io.h index fd19ae6..4971d35 100644 --- a/chrome/browser/privacy_blacklist/blacklist_io.h +++ b/chrome/browser/privacy_blacklist/blacklist_io.h @@ -37,6 +37,7 @@ class BlacklistIO { std::list providers_; FRIEND_TEST(BlacklistIOTest, Generic); + FRIEND_TEST(BlacklistIOTest, Combine); DISALLOW_COPY_AND_ASSIGN(BlacklistIO); }; diff --git a/chrome/browser/privacy_blacklist/blacklist_io_unittest.cc b/chrome/browser/privacy_blacklist/blacklist_io_unittest.cc index 6899321..cd43449 100644 --- a/chrome/browser/privacy_blacklist/blacklist_io_unittest.cc +++ b/chrome/browser/privacy_blacklist/blacklist_io_unittest.cc @@ -13,17 +13,17 @@ TEST(BlacklistIOTest, Generic) { // Testing data path. - std::wstring data_dir; + FilePath data_dir; PathService::Get(chrome::DIR_TEST_DATA, &data_dir); - std::wstring input(data_dir); - file_util::AppendToPath(&input, L"blacklist_small.pbl"); + FilePath input = + data_dir.Append(FilePath::FromWStringHack(L"blacklist_small.pbl")); - std::wstring expected(data_dir); - file_util::AppendToPath(&expected, L"blacklist_small.pbr"); + FilePath expected = + data_dir.Append(FilePath::FromWStringHack(L"blacklist_small.pbr")); BlacklistIO io; - EXPECT_TRUE(io.Read(FilePath::FromWStringHack(input))); + EXPECT_TRUE(io.Read(input)); const std::list& blacklist = io.blacklist(); EXPECT_EQ(5U, blacklist.size()); @@ -38,10 +38,68 @@ TEST(BlacklistIOTest, Generic) { EXPECT_EQ("Sample", io.providers().front()->name()); EXPECT_EQ("http://www.google.com", io.providers().front()->url()); - std::wstring output; + FilePath output; PathService::Get(base::DIR_TEMP, &output); - file_util::AppendToPath(&output, L"blacklist_small.pbr"); - CHECK(io.Write(FilePath::FromWStringHack(output))); + output = output.Append(FilePath::FromWStringHack(L"blacklist_small.pbr")); + CHECK(io.Write(output)); + EXPECT_TRUE(file_util::ContentsEqual(output, expected)); + EXPECT_TRUE(file_util::Delete(output, false)); +} + +TEST(BlacklistIOTest, Combine) { + // Testing data path. + FilePath data_dir; + PathService::Get(chrome::DIR_TEST_DATA, &data_dir); + data_dir = data_dir.Append(FilePath::FromWStringHack(L"blacklist_samples")); + + FilePath input1 = + data_dir.Append(FilePath::FromWStringHack(L"annoying_ads.pbl")); + + FilePath input2 = + data_dir.Append(FilePath::FromWStringHack(L"block_flash.pbl")); + + FilePath input3 = + data_dir.Append(FilePath::FromWStringHack(L"session_cookies.pbl")); + + BlacklistIO io; + EXPECT_TRUE(io.Read(input1)); + EXPECT_TRUE(io.Read(input2)); + EXPECT_TRUE(io.Read(input3)); + + const std::list& blacklist = io.blacklist(); + EXPECT_EQ(5U, blacklist.size()); + + std::list::const_iterator i = blacklist.begin(); + EXPECT_EQ(Blacklist::kBlockAll, (*i)->attributes()); + EXPECT_EQ("annoying.ads.tv/@", (*i++)->pattern()); + EXPECT_EQ(Blacklist::kBlockAll, (*i)->attributes()); + EXPECT_EQ("@/annoying/120x600.jpg", (*i++)->pattern()); + EXPECT_EQ(Blacklist::kBlockAll, (*i)->attributes()); + EXPECT_EQ("@/annoying_ads/@", (*i++)->pattern()); + EXPECT_EQ(Blacklist::kBlockByType, (*i)->attributes()); + EXPECT_EQ("@", (*i++)->pattern()); + EXPECT_EQ(Blacklist::kDontPersistCookies, (*i)->attributes()); + EXPECT_EQ("@", (*i++)->pattern()); + + const std::list& providers = io.providers(); + EXPECT_EQ(3U, providers.size()); + + std::list::const_iterator j = providers.begin(); + EXPECT_EQ("AnnoyingAds", (*j)->name()); + EXPECT_EQ("http://www.ads.tv", (*j++)->url()); + EXPECT_EQ("BlockFlash", (*j)->name()); + EXPECT_EQ("http://www.google.com", (*j++)->url()); + EXPECT_EQ("SessionCookies", (*j)->name()); + EXPECT_EQ("http://www.google.com", (*j++)->url()); + + FilePath output; + PathService::Get(base::DIR_TEMP, &output); + output = output.Append(FilePath::FromWStringHack(L"combine3.pbr")); + + FilePath expected = + data_dir.Append(FilePath::FromWStringHack(L"combine3.pbr")); + + CHECK(io.Write(output)); EXPECT_TRUE(file_util::ContentsEqual(output, expected)); EXPECT_TRUE(file_util::Delete(output, false)); } -- cgit v1.1