| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/privacy_blacklist/blacklist.h" | |
| 6 | |
| 7 #include "base/file_path.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/path_service.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "chrome/browser/browser_prefs.h" | |
| 12 #include "chrome/browser/chrome_thread.h" | |
| 13 #include "chrome/browser/pref_service.h" | |
| 14 #include "chrome/browser/pref_value_store.h" | |
| 15 #include "chrome/browser/profile.h" | |
| 16 #include "chrome/common/chrome_paths.h" | |
| 17 #include "chrome/common/json_pref_store.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 class BlacklistTest : public testing::Test { | |
| 21 protected: | |
| 22 virtual void SetUp() { | |
| 23 FilePath source_path; | |
| 24 PathService::Get(chrome::DIR_TEST_DATA, &source_path); | |
| 25 source_path = source_path.AppendASCII("profiles") | |
| 26 .AppendASCII("blacklist_prefs").AppendASCII("Preferences"); | |
| 27 | |
| 28 // Create a preference service that only contains user defined | |
| 29 // preference values. | |
| 30 prefs_.reset(new PrefService(new PrefValueStore( | |
| 31 NULL, /* No managed preference values */ | |
| 32 new JsonPrefStore( /* user defined preference values */ | |
| 33 source_path, | |
| 34 ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE)), | |
| 35 NULL /* No suggested preference values */))); | |
| 36 | |
| 37 Profile::RegisterUserPrefs(prefs_.get()); | |
| 38 browser::RegisterAllPrefs(prefs_.get(), prefs_.get()); | |
| 39 } | |
| 40 | |
| 41 scoped_ptr<PrefService> prefs_; | |
| 42 }; | |
| 43 | |
| 44 TEST_F(BlacklistTest, Generic) { | |
| 45 scoped_refptr<Blacklist> blacklist = new Blacklist(prefs_.get()); | |
| 46 Blacklist::EntryList entries(blacklist->entries_begin(), | |
| 47 blacklist->entries_end()); | |
| 48 | |
| 49 ASSERT_EQ(7U, entries.size()); | |
| 50 | |
| 51 // All entries include global attributes. | |
| 52 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. | |
| 53 EXPECT_EQ(Blacklist::kBlockUnsecure|0, entries[0]->attributes()); | |
| 54 EXPECT_FALSE(entries[0]->is_exception()); | |
| 55 EXPECT_EQ("@poor-security-site.com", entries[0]->pattern()); | |
| 56 | |
| 57 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. | |
| 58 EXPECT_EQ(Blacklist::kBlockCookies|0, entries[1]->attributes()); | |
| 59 EXPECT_FALSE(entries[1]->is_exception()); | |
| 60 EXPECT_EQ("@.ad-serving-place.com", entries[1]->pattern()); | |
| 61 | |
| 62 EXPECT_EQ(Blacklist::kDontSendUserAgent|Blacklist::kDontSendReferrer, | |
| 63 entries[2]->attributes()); | |
| 64 EXPECT_FALSE(entries[2]->is_exception()); | |
| 65 EXPECT_EQ("www.site.com/anonymous/folder/@", entries[2]->pattern()); | |
| 66 | |
| 67 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. | |
| 68 EXPECT_EQ(Blacklist::kBlockAll|0, entries[3]->attributes()); | |
| 69 EXPECT_FALSE(entries[3]->is_exception()); | |
| 70 EXPECT_EQ("www.site.com/bad/url", entries[3]->pattern()); | |
| 71 | |
| 72 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. | |
| 73 EXPECT_EQ(Blacklist::kBlockAll|0, entries[4]->attributes()); | |
| 74 EXPECT_FALSE(entries[4]->is_exception()); | |
| 75 EXPECT_EQ("@/script?@", entries[4]->pattern()); | |
| 76 | |
| 77 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. | |
| 78 EXPECT_EQ(Blacklist::kBlockAll|0, entries[5]->attributes()); | |
| 79 EXPECT_FALSE(entries[5]->is_exception()); | |
| 80 EXPECT_EQ("@?badparam@", entries[5]->pattern()); | |
| 81 | |
| 82 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. | |
| 83 EXPECT_EQ(Blacklist::kBlockAll|0, entries[6]->attributes()); | |
| 84 EXPECT_TRUE(entries[6]->is_exception()); | |
| 85 EXPECT_EQ("www.site.com/bad/url/good", entries[6]->pattern()); | |
| 86 | |
| 87 Blacklist::ProviderList providers(blacklist->providers_begin(), | |
| 88 blacklist->providers_end()); | |
| 89 | |
| 90 ASSERT_EQ(1U, providers.size()); | |
| 91 EXPECT_EQ("Sample", providers[0]->name()); | |
| 92 EXPECT_EQ("http://www.example.com", providers[0]->url()); | |
| 93 | |
| 94 // No match for chrome, about or empty URLs. | |
| 95 EXPECT_FALSE(blacklist->FindMatch(GURL())); | |
| 96 EXPECT_FALSE(blacklist->FindMatch(GURL("chrome://new-tab"))); | |
| 97 EXPECT_FALSE(blacklist->FindMatch(GURL("about:blank"))); | |
| 98 | |
| 99 // Expected rule matches. | |
| 100 Blacklist::Match* match = | |
| 101 blacklist->FindMatch(GURL("http://www.site.com/bad/url")); | |
| 102 EXPECT_TRUE(match); | |
| 103 if (match) { | |
| 104 EXPECT_EQ(Blacklist::kBlockAll|0, match->attributes()); | |
| 105 EXPECT_EQ(1U, match->entries().size()); | |
| 106 delete match; | |
| 107 } | |
| 108 | |
| 109 match = blacklist->FindMatch(GURL("http://www.site.com/anonymous")); | |
| 110 EXPECT_FALSE(match); | |
| 111 if (match) | |
| 112 delete match; | |
| 113 | |
| 114 match = blacklist->FindMatch(GURL("http://www.site.com/anonymous/folder")); | |
| 115 EXPECT_FALSE(match); | |
| 116 if (match) | |
| 117 delete match; | |
| 118 | |
| 119 match = blacklist->FindMatch( | |
| 120 GURL("http://www.site.com/anonymous/folder/subfolder")); | |
| 121 EXPECT_TRUE(match); | |
| 122 if (match) { | |
| 123 EXPECT_EQ(Blacklist::kDontSendUserAgent|Blacklist::kDontSendReferrer, | |
| 124 match->attributes()); | |
| 125 EXPECT_EQ(1U, match->entries().size()); | |
| 126 delete match; | |
| 127 } | |
| 128 | |
| 129 // No matches for URLs without query string | |
| 130 match = blacklist->FindMatch(GURL("http://badparam.com/")); | |
| 131 EXPECT_FALSE(match); | |
| 132 if (match) | |
| 133 delete match; | |
| 134 | |
| 135 match = blacklist->FindMatch(GURL("http://script.bad.org/")); | |
| 136 EXPECT_FALSE(match); | |
| 137 if (match) | |
| 138 delete match; | |
| 139 | |
| 140 // Expected rule matches. | |
| 141 match = blacklist->FindMatch(GURL("http://host.com/script?q=x")); | |
| 142 EXPECT_TRUE(match); | |
| 143 if (match) { | |
| 144 EXPECT_EQ(Blacklist::kBlockAll, match->attributes()); | |
| 145 EXPECT_EQ(1U, match->entries().size()); | |
| 146 delete match; | |
| 147 } | |
| 148 | |
| 149 match = blacklist->FindMatch(GURL("http://host.com/img?badparam=x")); | |
| 150 EXPECT_TRUE(match); | |
| 151 if (match) { | |
| 152 EXPECT_EQ(Blacklist::kBlockAll, match->attributes()); | |
| 153 EXPECT_EQ(1U, match->entries().size()); | |
| 154 delete match; | |
| 155 } | |
| 156 | |
| 157 // Whitelisting tests. | |
| 158 match = blacklist->FindMatch(GURL("http://www.site.com/bad/url/good")); | |
| 159 EXPECT_TRUE(match); | |
| 160 if (match) { | |
| 161 EXPECT_EQ(0U, match->attributes()); | |
| 162 EXPECT_EQ(1U, match->entries().size()); | |
| 163 delete match; | |
| 164 } | |
| 165 | |
| 166 // StripCookies Test. Note that "\r\n" line terminators are used | |
| 167 // because the underlying net util uniformizes those when stripping | |
| 168 // headers. | |
| 169 std::string header1("Host: www.example.com\r\n"); | |
| 170 std::string header2("Upgrade: TLS/1.0, HTTP/1.1\r\n" | |
| 171 "Connection: Upgrade\r\n"); | |
| 172 std::string header3("Date: Mon, 12 Mar 2001 19:20:33 GMT\r\n" | |
| 173 "Expires: Mon, 12 Mar 2001 19:20:33 GMT\r\n" | |
| 174 "Content-Type: text/html\r\n" | |
| 175 "Set-Cookie: B=460soc0taq8c1&b=2; " | |
| 176 "expires=Thu, 15 Apr 2010 20:00:00 GMT; path=/;\r\n"); | |
| 177 std::string header4("Date: Mon, 12 Mar 2001 19:20:33 GMT\r\n" | |
| 178 "Expires: Mon, 12 Mar 2001 19:20:33 GMT\r\n" | |
| 179 "Content-Type: text/html\r\n"); | |
| 180 | |
| 181 EXPECT_TRUE(header1 == Blacklist::StripCookies(header1)); | |
| 182 EXPECT_TRUE(header2 == Blacklist::StripCookies(header2)); | |
| 183 EXPECT_TRUE(header4 == Blacklist::StripCookies(header3)); | |
| 184 | |
| 185 // GetURLAsLookupString Test. | |
| 186 std::string url_spec1("example.com/some/path"); | |
| 187 std::string url_spec2("example.com/script?param=1"); | |
| 188 | |
| 189 EXPECT_TRUE(url_spec1 == Blacklist::GetURLAsLookupString( | |
| 190 GURL("http://example.com/some/path"))); | |
| 191 EXPECT_TRUE(url_spec1 == Blacklist::GetURLAsLookupString( | |
| 192 GURL("ftp://example.com/some/path"))); | |
| 193 EXPECT_TRUE(url_spec1 == Blacklist::GetURLAsLookupString( | |
| 194 GURL("http://example.com:8080/some/path"))); | |
| 195 EXPECT_TRUE(url_spec1 == Blacklist::GetURLAsLookupString( | |
| 196 GURL("http://user:login@example.com/some/path"))); | |
| 197 EXPECT_TRUE(url_spec2 == Blacklist::GetURLAsLookupString( | |
| 198 GURL("http://example.com/script?param=1"))); | |
| 199 } | |
| 200 | |
| 201 TEST_F(BlacklistTest, PatternMatch) { | |
| 202 // @ matches all but empty strings. | |
| 203 EXPECT_TRUE(Blacklist::Matches("@", "foo.com")); | |
| 204 EXPECT_TRUE(Blacklist::Matches("@", "path")); | |
| 205 EXPECT_TRUE(Blacklist::Matches("@", "foo.com/path")); | |
| 206 EXPECT_TRUE(Blacklist::Matches("@", "x")); | |
| 207 EXPECT_FALSE(Blacklist::Matches("@", "")); | |
| 208 EXPECT_FALSE(Blacklist::Matches("@", std::string())); | |
| 209 | |
| 210 // Prefix match. | |
| 211 EXPECT_TRUE(Blacklist::Matches("prefix@", "prefix.com")); | |
| 212 EXPECT_TRUE(Blacklist::Matches("prefix@", "prefix.com/path")); | |
| 213 EXPECT_TRUE(Blacklist::Matches("prefix@", "prefix/path")); | |
| 214 EXPECT_TRUE(Blacklist::Matches("prefix@", "prefix/prefix")); | |
| 215 EXPECT_FALSE(Blacklist::Matches("prefix@", "prefix")); | |
| 216 EXPECT_FALSE(Blacklist::Matches("prefix@", "Xprefix")); | |
| 217 EXPECT_FALSE(Blacklist::Matches("prefix@", "Y.Xprefix")); | |
| 218 EXPECT_FALSE(Blacklist::Matches("prefix@", "Y/Xprefix")); | |
| 219 | |
| 220 // Postfix match. | |
| 221 EXPECT_TRUE(Blacklist::Matches("@postfix", "something.postfix")); | |
| 222 EXPECT_TRUE(Blacklist::Matches("@postfix", "something/postfix")); | |
| 223 EXPECT_TRUE(Blacklist::Matches("@postfix", "foo.com/something/postfix")); | |
| 224 EXPECT_FALSE(Blacklist::Matches("@postfix", "postfix")); | |
| 225 EXPECT_FALSE(Blacklist::Matches("@postfix", "postfixZ")); | |
| 226 EXPECT_FALSE(Blacklist::Matches("@postfix", "postfixZ.Y")); | |
| 227 | |
| 228 // Infix matches. | |
| 229 EXPECT_TRUE(Blacklist::Matches("@evil@", "www.evil.com")); | |
| 230 EXPECT_TRUE(Blacklist::Matches("@evil@", "www.evil.com/whatever")); | |
| 231 EXPECT_TRUE(Blacklist::Matches("@evil@", "www.whatever.com/evilpath")); | |
| 232 EXPECT_TRUE(Blacklist::Matches("@evil@", "www.evil.whatever.com")); | |
| 233 EXPECT_FALSE(Blacklist::Matches("@evil@", "evil")); | |
| 234 EXPECT_FALSE(Blacklist::Matches("@evil@", "evil/")); | |
| 235 EXPECT_FALSE(Blacklist::Matches("@evil@", "/evil")); | |
| 236 | |
| 237 // Outfix matches. | |
| 238 EXPECT_TRUE(Blacklist::Matches("really@bad", "really/bad")); | |
| 239 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/bad")); | |
| 240 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/path/bad")); | |
| 241 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.evil.com/path/bad")); | |
| 242 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad.com")); | |
| 243 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad")); | |
| 244 EXPECT_FALSE(Blacklist::Matches("really@bad", ".reallybad")); | |
| 245 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad.")); | |
| 246 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad.")); | |
| 247 EXPECT_FALSE(Blacklist::Matches("really@bad", ".really.bad")); | |
| 248 } | |
| OLD | NEW |