| 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/browser/privacy_blacklist/blacklist_io.h" |
| 12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 TEST(BlacklistTest, Generic) { | 15 TEST(BlacklistTest, Generic) { |
| 16 // Get path relative to test data dir. | 16 // Get path relative to test data dir. |
| 17 FilePath input; | 17 FilePath input; |
| 18 PathService::Get(chrome::DIR_TEST_DATA, &input); | 18 PathService::Get(chrome::DIR_TEST_DATA, &input); |
| 19 input = input.AppendASCII("blacklist_small.pbr"); | 19 input = input.AppendASCII("blacklist_small.pbr"); |
| 20 | 20 |
| 21 Blacklist blacklist; | 21 Blacklist blacklist; |
| 22 ASSERT_TRUE(BlacklistIO::ReadBinary(&blacklist, input)); | 22 ASSERT_TRUE(BlacklistIO::ReadBinary(&blacklist, input)); |
| 23 | 23 |
| 24 Blacklist::EntryList entries(blacklist.entries_begin(), | 24 Blacklist::EntryList entries(blacklist.entries_begin(), |
| 25 blacklist.entries_end()); | 25 blacklist.entries_end()); |
| 26 | 26 |
| 27 ASSERT_EQ(5U, entries.size()); | 27 ASSERT_EQ(7U, entries.size()); |
| 28 | 28 |
| 29 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, | 29 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, |
| 30 entries[0]->attributes()); | 30 entries[0]->attributes()); |
| 31 EXPECT_TRUE(entries[0]->MatchesType("application/x-shockwave-flash")); | 31 EXPECT_TRUE(entries[0]->MatchesType("application/x-shockwave-flash")); |
| 32 EXPECT_FALSE(entries[0]->MatchesType("image/jpeg")); | 32 EXPECT_FALSE(entries[0]->MatchesType("image/jpeg")); |
| 33 EXPECT_EQ("@", entries[0]->pattern()); | 33 EXPECT_EQ("@", entries[0]->pattern()); |
| 34 | 34 |
| 35 // All entries include global attributes. | 35 // All entries include global attributes. |
| 36 // 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. |
| 37 EXPECT_EQ(Blacklist::kBlockUnsecure|0, entries[1]->attributes()); | 37 EXPECT_EQ(Blacklist::kBlockUnsecure|0, entries[1]->attributes()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 50 EXPECT_FALSE(entries[3]->MatchesType("application/x-shockwave-flash")); | 50 EXPECT_FALSE(entries[3]->MatchesType("application/x-shockwave-flash")); |
| 51 EXPECT_FALSE(entries[3]->MatchesType("image/jpeg")); | 51 EXPECT_FALSE(entries[3]->MatchesType("image/jpeg")); |
| 52 EXPECT_EQ("www.site.com/anonymous/folder/@", entries[3]->pattern()); | 52 EXPECT_EQ("www.site.com/anonymous/folder/@", entries[3]->pattern()); |
| 53 | 53 |
| 54 // 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. |
| 55 EXPECT_EQ(Blacklist::kBlockAll|0, entries[4]->attributes()); | 55 EXPECT_EQ(Blacklist::kBlockAll|0, entries[4]->attributes()); |
| 56 EXPECT_FALSE(entries[4]->MatchesType("application/x-shockwave-flash")); | 56 EXPECT_FALSE(entries[4]->MatchesType("application/x-shockwave-flash")); |
| 57 EXPECT_FALSE(entries[4]->MatchesType("image/jpeg")); | 57 EXPECT_FALSE(entries[4]->MatchesType("image/jpeg")); |
| 58 EXPECT_EQ("www.site.com/bad/url", entries[4]->pattern()); | 58 EXPECT_EQ("www.site.com/bad/url", entries[4]->pattern()); |
| 59 | 59 |
| 60 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. |
| 61 EXPECT_EQ(Blacklist::kBlockAll|0, entries[5]->attributes()); |
| 62 EXPECT_FALSE(entries[5]->MatchesType("application/x-shockwave-flash")); |
| 63 EXPECT_FALSE(entries[5]->MatchesType("image/jpeg")); |
| 64 EXPECT_EQ("@/script?@", entries[5]->pattern()); |
| 65 |
| 66 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. |
| 67 EXPECT_EQ(Blacklist::kBlockAll|0, entries[6]->attributes()); |
| 68 EXPECT_FALSE(entries[6]->MatchesType("application/x-shockwave-flash")); |
| 69 EXPECT_FALSE(entries[6]->MatchesType("image/jpeg")); |
| 70 EXPECT_EQ("@?badparam@", entries[6]->pattern()); |
| 71 |
| 60 Blacklist::ProviderList providers(blacklist.providers_begin(), | 72 Blacklist::ProviderList providers(blacklist.providers_begin(), |
| 61 blacklist.providers_end()); | 73 blacklist.providers_end()); |
| 62 | 74 |
| 63 ASSERT_EQ(1U, providers.size()); | 75 ASSERT_EQ(1U, providers.size()); |
| 64 EXPECT_EQ("Sample", providers[0]->name()); | 76 EXPECT_EQ("Sample", providers[0]->name()); |
| 65 EXPECT_EQ("http://www.google.com", providers[0]->url()); | 77 EXPECT_EQ("http://www.google.com", providers[0]->url()); |
| 66 | 78 |
| 67 // No match for chrome, about or empty URLs. | 79 // No match for chrome, about or empty URLs. |
| 68 EXPECT_FALSE(blacklist.findMatch(GURL())); | 80 EXPECT_FALSE(blacklist.FindMatch(GURL())); |
| 69 EXPECT_FALSE(blacklist.findMatch(GURL("chrome://new-tab"))); | 81 EXPECT_FALSE(blacklist.FindMatch(GURL("chrome://new-tab"))); |
| 70 EXPECT_FALSE(blacklist.findMatch(GURL("about:blank"))); | 82 EXPECT_FALSE(blacklist.FindMatch(GURL("about:blank"))); |
| 71 | 83 |
| 72 // Expected rule matches. | 84 // Expected rule matches. |
| 73 Blacklist::Match* match; | 85 Blacklist::Match* match; |
| 74 match = blacklist.findMatch(GURL("http://www.google.com")); | 86 match = blacklist.FindMatch(GURL("http://www.google.com")); |
| 75 EXPECT_TRUE(match); | 87 EXPECT_TRUE(match); |
| 76 if (match) { | 88 if (match) { |
| 77 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, | 89 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, |
| 78 match->attributes()); | 90 match->attributes()); |
| 79 EXPECT_EQ(1U, match->entries().size()); | 91 EXPECT_EQ(1U, match->entries().size()); |
| 80 delete match; | 92 delete match; |
| 81 } | 93 } |
| 82 | 94 |
| 83 match = blacklist.findMatch(GURL("http://www.site.com/bad/url")); | 95 match = blacklist.FindMatch(GURL("http://www.site.com/bad/url")); |
| 84 EXPECT_TRUE(match); | 96 EXPECT_TRUE(match); |
| 85 if (match) { | 97 if (match) { |
| 86 EXPECT_EQ(Blacklist::kBlockAll| | 98 EXPECT_EQ(Blacklist::kBlockAll| |
| 87 Blacklist::kBlockByType|Blacklist::kDontPersistCookies, | 99 Blacklist::kBlockByType|Blacklist::kDontPersistCookies, |
| 88 match->attributes()); | 100 match->attributes()); |
| 89 EXPECT_EQ(2U, match->entries().size()); | 101 EXPECT_EQ(2U, match->entries().size()); |
| 90 delete match; | 102 delete match; |
| 91 } | 103 } |
| 92 | 104 |
| 93 match = blacklist.findMatch(GURL("http://www.site.com/anonymous")); | 105 match = blacklist.FindMatch(GURL("http://www.site.com/anonymous")); |
| 94 EXPECT_TRUE(match); | 106 EXPECT_TRUE(match); |
| 95 if (match) { | 107 if (match) { |
| 96 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, | 108 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, |
| 97 match->attributes()); | 109 match->attributes()); |
| 98 EXPECT_EQ(1U, match->entries().size()); | 110 EXPECT_EQ(1U, match->entries().size()); |
| 99 delete match; | 111 delete match; |
| 100 } | 112 } |
| 101 | 113 |
| 102 match = blacklist.findMatch(GURL("http://www.site.com/anonymous/folder")); | 114 match = blacklist.FindMatch(GURL("http://www.site.com/anonymous/folder")); |
| 103 EXPECT_TRUE(match); | 115 EXPECT_TRUE(match); |
| 104 if (match) { | 116 if (match) { |
| 105 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, | 117 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, |
| 106 match->attributes()); | 118 match->attributes()); |
| 107 EXPECT_EQ(1U, match->entries().size()); | 119 EXPECT_EQ(1U, match->entries().size()); |
| 108 delete match; | 120 delete match; |
| 109 } | 121 } |
| 110 | 122 |
| 111 match = blacklist.findMatch( | 123 match = blacklist.FindMatch( |
| 112 GURL("http://www.site.com/anonymous/folder/subfolder")); | 124 GURL("http://www.site.com/anonymous/folder/subfolder")); |
| 113 EXPECT_TRUE(match); | 125 EXPECT_TRUE(match); |
| 114 if (match) { | 126 if (match) { |
| 115 EXPECT_EQ(Blacklist::kDontSendUserAgent|Blacklist::kDontSendReferrer| | 127 EXPECT_EQ(Blacklist::kDontSendUserAgent|Blacklist::kDontSendReferrer| |
| 116 Blacklist::kBlockByType|Blacklist::kDontPersistCookies, | 128 Blacklist::kBlockByType|Blacklist::kDontPersistCookies, |
| 117 match->attributes()); | 129 match->attributes()); |
| 118 EXPECT_EQ(2U, match->entries().size()); | 130 EXPECT_EQ(2U, match->entries().size()); |
| 119 delete match; | 131 delete match; |
| 120 } | 132 } |
| 121 | 133 |
| 134 // No matches for URLs without query string |
| 135 match = blacklist.FindMatch(GURL("http://badparam.com/")); |
| 136 EXPECT_TRUE(match); |
| 137 if (match) { |
| 138 EXPECT_EQ(1U, match->entries().size()); |
| 139 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, |
| 140 match->attributes()); |
| 141 delete match; |
| 142 } |
| 143 |
| 144 match = blacklist.FindMatch(GURL("http://script.bad.org/")); |
| 145 EXPECT_TRUE(match); |
| 146 if (match) { |
| 147 EXPECT_EQ(1U, match->entries().size()); |
| 148 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies, |
| 149 match->attributes()); |
| 150 delete match; |
| 151 } |
| 152 |
| 153 // Expected rule matches. |
| 154 match = blacklist.FindMatch(GURL("http://host.com/script?q=x")); |
| 155 EXPECT_TRUE(match); |
| 156 if (match) { |
| 157 EXPECT_EQ(2U, match->entries().size()); |
| 158 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies| |
| 159 Blacklist::kBlockAll, |
| 160 match->attributes()); |
| 161 delete match; |
| 162 } |
| 163 |
| 164 match = blacklist.FindMatch(GURL("http://host.com/img?badparam=x")); |
| 165 EXPECT_TRUE(match); |
| 166 if (match) { |
| 167 EXPECT_EQ(2U, match->entries().size()); |
| 168 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies| |
| 169 Blacklist::kBlockAll, |
| 170 match->attributes()); |
| 171 delete match; |
| 172 } |
| 173 |
| 122 // StripCookieExpiry Tests | 174 // StripCookieExpiry Tests |
| 123 std::string cookie1( | 175 std::string cookie1( |
| 124 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;" | 176 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;" |
| 125 " expires=Thu, 16-Jun-2011 20:13:52 GMT; path=/; domain=.google.com"); | 177 " expires=Thu, 16-Jun-2011 20:13:52 GMT; path=/; domain=.google.com"); |
| 126 std::string cookie2( | 178 std::string cookie2( |
| 127 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;" | 179 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;" |
| 128 " path=/; domain=.google.com"); | 180 " path=/; domain=.google.com"); |
| 129 std::string cookie3( | 181 std::string cookie3( |
| 130 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;" | 182 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;" |
| 131 " expires=Thu, 17-Jun-2011 02:13:52 GMT; path=/; domain=.google.com"); | 183 " expires=Thu, 17-Jun-2011 02:13:52 GMT; path=/; domain=.google.com"); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 160 "Content-Type: text/html\r\n" | 212 "Content-Type: text/html\r\n" |
| 161 "Set-Cookie: B=460soc0taq8c1&b=2; " | 213 "Set-Cookie: B=460soc0taq8c1&b=2; " |
| 162 "expires=Thu, 15 Apr 2010 20:00:00 GMT; path=/;\r\n"); | 214 "expires=Thu, 15 Apr 2010 20:00:00 GMT; path=/;\r\n"); |
| 163 std::string header4("Date: Mon, 12 Mar 2001 19:20:33 GMT\r\n" | 215 std::string header4("Date: Mon, 12 Mar 2001 19:20:33 GMT\r\n" |
| 164 "Expires: Mon, 12 Mar 2001 19:20:33 GMT\r\n" | 216 "Expires: Mon, 12 Mar 2001 19:20:33 GMT\r\n" |
| 165 "Content-Type: text/html\r\n"); | 217 "Content-Type: text/html\r\n"); |
| 166 | 218 |
| 167 EXPECT_TRUE(header1 == Blacklist::StripCookies(header1)); | 219 EXPECT_TRUE(header1 == Blacklist::StripCookies(header1)); |
| 168 EXPECT_TRUE(header2 == Blacklist::StripCookies(header2)); | 220 EXPECT_TRUE(header2 == Blacklist::StripCookies(header2)); |
| 169 EXPECT_TRUE(header4 == Blacklist::StripCookies(header3)); | 221 EXPECT_TRUE(header4 == Blacklist::StripCookies(header3)); |
| 222 |
| 223 // GetURLAsString Test. |
| 224 std::string url_spec1("example.com/some/path"); |
| 225 std::string url_spec2("example.com/script?param=1"); |
| 226 |
| 227 EXPECT_TRUE(url_spec1 == Blacklist::GetURLAsString( |
| 228 GURL("http://example.com/some/path"))); |
| 229 EXPECT_TRUE(url_spec1 == Blacklist::GetURLAsString( |
| 230 GURL("ftp://example.com/some/path"))); |
| 231 EXPECT_TRUE(url_spec1 == Blacklist::GetURLAsString( |
| 232 GURL("http://example.com:8080/some/path"))); |
| 233 EXPECT_TRUE(url_spec1 == Blacklist::GetURLAsString( |
| 234 GURL("http://user:login@example.com/some/path"))); |
| 235 EXPECT_TRUE(url_spec2 == Blacklist::GetURLAsString( |
| 236 GURL("http://example.com/script?param=1"))); |
| 170 } | 237 } |
| 171 | 238 |
| 172 TEST(BlacklistTest, PatternMatch) { | 239 TEST(BlacklistTest, PatternMatch) { |
| 173 // @ matches all but empty strings. | 240 // @ matches all but empty strings. |
| 174 EXPECT_TRUE(Blacklist::Matches("@", "foo.com")); | 241 EXPECT_TRUE(Blacklist::Matches("@", "foo.com")); |
| 175 EXPECT_TRUE(Blacklist::Matches("@", "path")); | 242 EXPECT_TRUE(Blacklist::Matches("@", "path")); |
| 176 EXPECT_TRUE(Blacklist::Matches("@", "foo.com/path")); | 243 EXPECT_TRUE(Blacklist::Matches("@", "foo.com/path")); |
| 177 EXPECT_TRUE(Blacklist::Matches("@", "x")); | 244 EXPECT_TRUE(Blacklist::Matches("@", "x")); |
| 178 EXPECT_FALSE(Blacklist::Matches("@", "")); | 245 EXPECT_FALSE(Blacklist::Matches("@", "")); |
| 179 EXPECT_FALSE(Blacklist::Matches("@", std::string())); | 246 EXPECT_FALSE(Blacklist::Matches("@", std::string())); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 210 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/bad")); | 277 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/bad")); |
| 211 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/path/bad")); | 278 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/path/bad")); |
| 212 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.evil.com/path/bad")); | 279 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.evil.com/path/bad")); |
| 213 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad.com")); | 280 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad.com")); |
| 214 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad")); | 281 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad")); |
| 215 EXPECT_FALSE(Blacklist::Matches("really@bad", ".reallybad")); | 282 EXPECT_FALSE(Blacklist::Matches("really@bad", ".reallybad")); |
| 216 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad.")); | 283 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad.")); |
| 217 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad.")); | 284 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad.")); |
| 218 EXPECT_FALSE(Blacklist::Matches("really@bad", ".really.bad")); | 285 EXPECT_FALSE(Blacklist::Matches("really@bad", ".really.bad")); |
| 219 } | 286 } |
| OLD | NEW |