| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/privacy_blacklist/blacklist_io.h" | 5 #include "chrome/browser/privacy_blacklist/blacklist_io.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "chrome/browser/privacy_blacklist/blacklist.h" |
| 11 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 TEST(BlacklistIOTest, Generic) { | 15 TEST(BlacklistIOTest, Generic) { |
| 15 // Testing data path. | 16 // Testing data path. |
| 16 FilePath data_dir; | 17 FilePath data_dir; |
| 17 PathService::Get(chrome::DIR_TEST_DATA, &data_dir); | 18 PathService::Get(chrome::DIR_TEST_DATA, &data_dir); |
| 18 | 19 |
| 19 FilePath input = data_dir.AppendASCII("blacklist_small.pbl"); | 20 FilePath input = data_dir.AppendASCII("blacklist_small.pbl"); |
| 20 | 21 |
| 21 FilePath expected = data_dir.AppendASCII("blacklist_small.pbr"); | 22 FilePath expected = data_dir.AppendASCII("blacklist_small.pbr"); |
| 22 | 23 |
| 23 BlacklistIO io; | 24 Blacklist blacklist; |
| 24 EXPECT_TRUE(io.Read(input)); | 25 std::string error_string; |
| 25 const std::list<Blacklist::Entry*>& blacklist = io.blacklist(); | 26 ASSERT_TRUE(BlacklistIO::ReadText(&blacklist, input, &error_string)); |
| 26 EXPECT_EQ(5U, blacklist.size()); | 27 EXPECT_TRUE(error_string.empty()); |
| 28 |
| 29 const Blacklist::EntryList entries(blacklist.entries_begin(), |
| 30 blacklist.entries_end()); |
| 31 ASSERT_EQ(5U, entries.size()); |
| 27 | 32 |
| 28 std::list<Blacklist::Entry*>::const_iterator i = blacklist.begin(); | 33 EXPECT_EQ("@", entries[0]->pattern()); |
| 29 EXPECT_EQ("@", (*i++)->pattern()); | 34 EXPECT_EQ("@poor-security-site.com", entries[1]->pattern()); |
| 30 EXPECT_EQ("@poor-security-site.com", (*i++)->pattern()); | 35 EXPECT_EQ("@.ad-serving-place.com", entries[2]->pattern()); |
| 31 EXPECT_EQ("@.ad-serving-place.com", (*i++)->pattern()); | 36 EXPECT_EQ("www.site.com/anonymous/folder/@", entries[3]->pattern()); |
| 32 EXPECT_EQ("www.site.com/anonymous/folder/@", (*i++)->pattern()); | 37 EXPECT_EQ("www.site.com/bad/url", entries[4]->pattern()); |
| 33 EXPECT_EQ("www.site.com/bad/url", (*i++)->pattern()); | 38 |
| 39 const Blacklist::ProviderList providers(blacklist.providers_begin(), |
| 40 blacklist.providers_end()); |
| 34 | 41 |
| 35 EXPECT_EQ(1U, io.providers().size()); | 42 ASSERT_EQ(1U, providers.size()); |
| 36 EXPECT_EQ("Sample", io.providers().front()->name()); | 43 EXPECT_EQ("Sample", providers[0]->name()); |
| 37 EXPECT_EQ("http://www.google.com", io.providers().front()->url()); | 44 EXPECT_EQ("http://www.google.com", providers[0]->url()); |
| 38 | 45 |
| 39 FilePath output; | 46 FilePath output; |
| 40 PathService::Get(base::DIR_TEMP, &output); | 47 PathService::Get(base::DIR_TEMP, &output); |
| 41 output = output.AppendASCII("blacklist_small.pbr"); | 48 output = output.AppendASCII("blacklist_small.pbr"); |
| 42 CHECK(io.Write(output)); | 49 ASSERT_TRUE(BlacklistIO::WriteBinary(&blacklist, output)); |
| 43 EXPECT_TRUE(file_util::ContentsEqual(output, expected)); | 50 EXPECT_TRUE(file_util::ContentsEqual(output, expected)); |
| 44 EXPECT_TRUE(file_util::Delete(output, false)); | 51 EXPECT_TRUE(file_util::Delete(output, false)); |
| 45 } | 52 } |
| 46 | 53 |
| 47 TEST(BlacklistIOTest, Combine) { | |
| 48 // Testing data path. | |
| 49 FilePath data_dir; | |
| 50 PathService::Get(chrome::DIR_TEST_DATA, &data_dir); | |
| 51 data_dir = data_dir.AppendASCII("blacklist_samples"); | |
| 52 | 54 |
| 53 FilePath input1 = data_dir.AppendASCII("annoying_ads.pbl"); | |
| 54 | |
| 55 FilePath input2 = data_dir.AppendASCII("block_flash.pbl"); | |
| 56 | |
| 57 FilePath input3 = data_dir.AppendASCII("session_cookies.pbl"); | |
| 58 | |
| 59 BlacklistIO io; | |
| 60 EXPECT_TRUE(io.Read(input1)); | |
| 61 EXPECT_TRUE(io.Read(input2)); | |
| 62 EXPECT_TRUE(io.Read(input3)); | |
| 63 | |
| 64 const std::list<Blacklist::Entry*>& blacklist = io.blacklist(); | |
| 65 EXPECT_EQ(5U, blacklist.size()); | |
| 66 | |
| 67 std::list<Blacklist::Entry*>::const_iterator i = blacklist.begin(); | |
| 68 EXPECT_EQ(Blacklist::kBlockAll, (*i)->attributes()); | |
| 69 EXPECT_EQ("annoying.ads.tv/@", (*i++)->pattern()); | |
| 70 EXPECT_EQ(Blacklist::kBlockAll, (*i)->attributes()); | |
| 71 EXPECT_EQ("@/annoying/120x600.jpg", (*i++)->pattern()); | |
| 72 EXPECT_EQ(Blacklist::kBlockAll, (*i)->attributes()); | |
| 73 EXPECT_EQ("@/annoying_ads/@", (*i++)->pattern()); | |
| 74 EXPECT_EQ(Blacklist::kBlockByType, (*i)->attributes()); | |
| 75 EXPECT_EQ("@", (*i++)->pattern()); | |
| 76 EXPECT_EQ(Blacklist::kDontPersistCookies, (*i)->attributes()); | |
| 77 EXPECT_EQ("@", (*i++)->pattern()); | |
| 78 | |
| 79 const std::list<Blacklist::Provider*>& providers = io.providers(); | |
| 80 EXPECT_EQ(3U, providers.size()); | |
| 81 | |
| 82 std::list<Blacklist::Provider*>::const_iterator j = providers.begin(); | |
| 83 EXPECT_EQ("AnnoyingAds", (*j)->name()); | |
| 84 EXPECT_EQ("http://www.ads.tv", (*j++)->url()); | |
| 85 EXPECT_EQ("BlockFlash", (*j)->name()); | |
| 86 EXPECT_EQ("http://www.google.com", (*j++)->url()); | |
| 87 EXPECT_EQ("SessionCookies", (*j)->name()); | |
| 88 EXPECT_EQ("http://www.google.com", (*j++)->url()); | |
| 89 | |
| 90 FilePath output; | |
| 91 PathService::Get(base::DIR_TEMP, &output); | |
| 92 output = output.AppendASCII("combine3.pbr"); | |
| 93 | |
| 94 FilePath expected = data_dir.AppendASCII("combine3.pbr"); | |
| 95 | |
| 96 CHECK(io.Write(output)); | |
| 97 EXPECT_TRUE(file_util::ContentsEqual(output, expected)); | |
| 98 EXPECT_TRUE(file_util::Delete(output, false)); | |
| 99 } | |
| OLD | NEW |