| 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.h" | 5 #include "chrome/browser/privacy_blacklist/blacklist.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_io.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(BlacklistTest, Generic) { | 15 TEST(BlacklistTest, Generic) { |
| 15 // Get path relative to test data dir. | 16 // Get path relative to test data dir. |
| 16 FilePath input; | 17 FilePath input; |
| 17 PathService::Get(chrome::DIR_TEST_DATA, &input); | 18 PathService::Get(chrome::DIR_TEST_DATA, &input); |
| 18 input = input.AppendASCII("blacklist_small.pbr"); | 19 input = input.AppendASCII("blacklist_small.pbr"); |
| 19 | 20 |
| 20 Blacklist blacklist(input); | 21 Blacklist blacklist; |
| 22 ASSERT_TRUE(BlacklistIO::ReadBinary(&blacklist, input)); |
| 23 |
| 24 Blacklist::EntryList entries(blacklist.entries_begin(), |
| 25 blacklist.entries_end()); |
| 21 | 26 |
| 22 // This test is a friend, so inspect the internal structures. | 27 ASSERT_EQ(5U, entries.size()); |
| 23 EXPECT_EQ(5U, blacklist.blacklist_.size()); | |
| 24 std::vector<Blacklist::Entry*>::const_iterator i = | |
| 25 blacklist.blacklist_.begin(); | |
| 26 | 28 |
| 27 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, | 29 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, |
| 28 (*i)->attributes()); | 30 entries[0]->attributes()); |
| 29 EXPECT_TRUE((*i)->MatchType("application/x-shockwave-flash")); | 31 EXPECT_TRUE(entries[0]->MatchesType("application/x-shockwave-flash")); |
| 30 EXPECT_FALSE((*i)->MatchType("image/jpeg")); | 32 EXPECT_FALSE(entries[0]->MatchesType("image/jpeg")); |
| 31 EXPECT_EQ("@", (*i++)->pattern()); | 33 EXPECT_EQ("@", entries[0]->pattern()); |
| 32 | 34 |
| 33 // All entries include global attributes. | 35 // All entries include global attributes. |
| 34 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. | 36 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. |
| 35 EXPECT_EQ(Blacklist::kBlockUnsecure|0, (*i)->attributes()); | 37 EXPECT_EQ(Blacklist::kBlockUnsecure|0, entries[1]->attributes()); |
| 36 EXPECT_FALSE((*i)->MatchType("application/x-shockwave-flash")); | 38 EXPECT_FALSE(entries[1]->MatchesType("application/x-shockwave-flash")); |
| 37 EXPECT_FALSE((*i)->MatchType("image/jpeg")); | 39 EXPECT_FALSE(entries[1]->MatchesType("image/jpeg")); |
| 38 EXPECT_EQ("@poor-security-site.com", (*i++)->pattern()); | 40 EXPECT_EQ("@poor-security-site.com", entries[1]->pattern()); |
| 39 | 41 |
| 40 EXPECT_EQ(Blacklist::kDontSendCookies|Blacklist::kDontStoreCookies, | 42 EXPECT_EQ(Blacklist::kDontSendCookies|Blacklist::kDontStoreCookies, |
| 41 (*i)->attributes()); | 43 entries[2]->attributes()); |
| 42 EXPECT_FALSE((*i)->MatchType("application/x-shockwave-flash")); | 44 EXPECT_FALSE(entries[2]->MatchesType("application/x-shockwave-flash")); |
| 43 EXPECT_FALSE((*i)->MatchType("image/jpeg")); | 45 EXPECT_FALSE(entries[2]->MatchesType("image/jpeg")); |
| 44 EXPECT_EQ("@.ad-serving-place.com", (*i++)->pattern()); | 46 EXPECT_EQ("@.ad-serving-place.com", entries[2]->pattern()); |
| 45 | 47 |
| 46 EXPECT_EQ(Blacklist::kDontSendUserAgent|Blacklist::kDontSendReferrer, | 48 EXPECT_EQ(Blacklist::kDontSendUserAgent|Blacklist::kDontSendReferrer, |
| 47 (*i)->attributes()); | 49 entries[3]->attributes()); |
| 48 EXPECT_FALSE((*i)->MatchType("application/x-shockwave-flash")); | 50 EXPECT_FALSE(entries[3]->MatchesType("application/x-shockwave-flash")); |
| 49 EXPECT_FALSE((*i)->MatchType("image/jpeg")); | 51 EXPECT_FALSE(entries[3]->MatchesType("image/jpeg")); |
| 50 EXPECT_EQ("www.site.com/anonymous/folder/@", (*i++)->pattern()); | 52 EXPECT_EQ("www.site.com/anonymous/folder/@", entries[3]->pattern()); |
| 51 | 53 |
| 52 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. | 54 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. |
| 53 EXPECT_EQ(Blacklist::kBlockAll|0, (*i)->attributes()); | 55 EXPECT_EQ(Blacklist::kBlockAll|0, entries[4]->attributes()); |
| 54 EXPECT_FALSE((*i)->MatchType("application/x-shockwave-flash")); | 56 EXPECT_FALSE(entries[4]->MatchesType("application/x-shockwave-flash")); |
| 55 EXPECT_FALSE((*i)->MatchType("image/jpeg")); | 57 EXPECT_FALSE(entries[4]->MatchesType("image/jpeg")); |
| 56 EXPECT_EQ("www.site.com/bad/url", (*i++)->pattern()); | 58 EXPECT_EQ("www.site.com/bad/url", entries[4]->pattern()); |
| 57 | 59 |
| 58 EXPECT_EQ(1U, blacklist.providers_.size()); | 60 Blacklist::ProviderList providers(blacklist.providers_begin(), |
| 59 EXPECT_EQ("Sample", blacklist.providers_.front()->name()); | 61 blacklist.providers_end()); |
| 60 EXPECT_EQ("http://www.google.com", blacklist.providers_.front()->url()); | 62 |
| 63 ASSERT_EQ(1U, providers.size()); |
| 64 EXPECT_EQ("Sample", providers[0]->name()); |
| 65 EXPECT_EQ("http://www.google.com", providers[0]->url()); |
| 61 | 66 |
| 62 // No match for chrome, about or empty URLs. | 67 // No match for chrome, about or empty URLs. |
| 63 EXPECT_FALSE(blacklist.findMatch(GURL())); | 68 EXPECT_FALSE(blacklist.findMatch(GURL())); |
| 64 EXPECT_FALSE(blacklist.findMatch(GURL("chrome://new-tab"))); | 69 EXPECT_FALSE(blacklist.findMatch(GURL("chrome://new-tab"))); |
| 65 EXPECT_FALSE(blacklist.findMatch(GURL("about:blank"))); | 70 EXPECT_FALSE(blacklist.findMatch(GURL("about:blank"))); |
| 66 | 71 |
| 67 // Expected rule matches. | 72 // Expected rule matches. |
| 68 Blacklist::Match* match; | 73 Blacklist::Match* match; |
| 69 match = blacklist.findMatch(GURL("http://www.google.com")); | 74 match = blacklist.findMatch(GURL("http://www.google.com")); |
| 70 EXPECT_TRUE(match); | 75 EXPECT_TRUE(match); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/bad")); | 210 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/bad")); |
| 206 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/path/bad")); | 211 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/path/bad")); |
| 207 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.evil.com/path/bad")); | 212 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.evil.com/path/bad")); |
| 208 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad.com")); | 213 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad.com")); |
| 209 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad")); | 214 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad")); |
| 210 EXPECT_FALSE(Blacklist::Matches("really@bad", ".reallybad")); | 215 EXPECT_FALSE(Blacklist::Matches("really@bad", ".reallybad")); |
| 211 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad.")); | 216 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad.")); |
| 212 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad.")); | 217 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad.")); |
| 213 EXPECT_FALSE(Blacklist::Matches("really@bad", ".really.bad")); | 218 EXPECT_FALSE(Blacklist::Matches("really@bad", ".really.bad")); |
| 214 } | 219 } |
| OLD | NEW |