Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: chrome/browser/privacy_blacklist/blacklist_unittest.cc

Issue 149737: Blacklist API change for allowing multiple rule matches... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug. 53 // NOTE: Silly bitwise-or with zero to workaround a Mac compiler bug.
54 EXPECT_EQ(Blacklist::kBlockAll|0, (*i)->attributes()); 54 EXPECT_EQ(Blacklist::kBlockAll|0, (*i)->attributes());
55 EXPECT_FALSE((*i)->MatchType("application/x-shockwave-flash")); 55 EXPECT_FALSE((*i)->MatchType("application/x-shockwave-flash"));
56 EXPECT_FALSE((*i)->MatchType("image/jpeg")); 56 EXPECT_FALSE((*i)->MatchType("image/jpeg"));
57 EXPECT_EQ("www.site.com/bad/url", (*i++)->pattern()); 57 EXPECT_EQ("www.site.com/bad/url", (*i++)->pattern());
58 58
59 EXPECT_EQ(1U, blacklist.providers_.size()); 59 EXPECT_EQ(1U, blacklist.providers_.size());
60 EXPECT_EQ("Sample", blacklist.providers_.front()->name()); 60 EXPECT_EQ("Sample", blacklist.providers_.front()->name());
61 EXPECT_EQ("http://www.google.com", blacklist.providers_.front()->url()); 61 EXPECT_EQ("http://www.google.com", blacklist.providers_.front()->url());
62 62
63 // Empty blacklist should not match any URL. 63 // No match for chrome, about or empty URLs.
64 EXPECT_FALSE(blacklist.findMatch(GURL())); 64 EXPECT_FALSE(blacklist.findMatch(GURL()));
65 EXPECT_FALSE(blacklist.findMatch(GURL("http://www.google.com"))); 65 EXPECT_FALSE(blacklist.findMatch(GURL("chrome://new-tab")));
66 EXPECT_FALSE(blacklist.findMatch(GURL("about:blank")));
67
68 // Expected rule matches.
69 Blacklist::Match* match;
70 match = blacklist.findMatch(GURL("http://www.google.com"));
71 EXPECT_TRUE(match);
72 if (match) {
73 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies,
74 match->attributes());
75 EXPECT_EQ(1U, match->entries().size());
76 }
77
78 match = blacklist.findMatch(GURL("http://www.site.com/bad/url"));
79 EXPECT_TRUE(match);
80 if (match) {
81 EXPECT_EQ(Blacklist::kBlockAll|
82 Blacklist::kBlockByType|Blacklist::kDontPersistCookies,
83 match->attributes());
84 EXPECT_EQ(2U, match->entries().size());
85 }
86
87 match = blacklist.findMatch(GURL("http://www.site.com/anonymous"));
88 EXPECT_TRUE(match);
89 if (match) {
90 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies,
91 match->attributes());
92 EXPECT_EQ(1U, match->entries().size());
93 }
94
95 match = blacklist.findMatch(GURL("http://www.site.com/anonymous/folder"));
96 EXPECT_TRUE(match);
97 if (match) {
98 EXPECT_EQ(Blacklist::kBlockByType|Blacklist::kDontPersistCookies,
99 match->attributes());
100 EXPECT_EQ(1U, match->entries().size());
101 }
102
103 match = blacklist.findMatch(
104 GURL("http://www.site.com/anonymous/folder/subfolder"));
105 EXPECT_TRUE(match);
106 if (match) {
107 EXPECT_EQ(Blacklist::kDontSendUserAgent|Blacklist::kDontSendReferrer|
108 Blacklist::kBlockByType|Blacklist::kDontPersistCookies,
109 match->attributes());
110 EXPECT_EQ(2U, match->entries().size());
111 }
66 112
67 // StripCookieExpiry Tests 113 // StripCookieExpiry Tests
68 std::string cookie1( 114 std::string cookie1(
69 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;" 115 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;"
70 " expires=Thu, 16-Jun-2011 20:13:52 GMT; path=/; domain=.google.com"); 116 " expires=Thu, 16-Jun-2011 20:13:52 GMT; path=/; domain=.google.com");
71 std::string cookie2( 117 std::string cookie2(
72 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;" 118 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;"
73 " path=/; domain=.google.com"); 119 " path=/; domain=.google.com");
74 std::string cookie3( 120 std::string cookie3(
75 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;" 121 "PREF=ID=14a549990453e42a:TM=1245183232:LM=1245183232:S=Occ7khRVIEE36Ao5;"
76 " expires=Thu, 17-Jun-2011 02:13:52 GMT; path=/; domain=.google.com"); 122 " expires=Thu, 17-Jun-2011 02:13:52 GMT; path=/; domain=.google.com");
123 std::string cookie4("E=MC^2; path=relative; expires=never;");
124 std::string cookie5("E=MC^2; path=relative;");
77 125
78 // No expiry, should be equal to itself after stripping. 126 // No expiry, should be equal to itself after stripping.
79 EXPECT_TRUE(cookie2 == Blacklist::StripCookieExpiry(cookie2)); 127 EXPECT_EQ(cookie2, Blacklist::StripCookieExpiry(cookie2));
128 EXPECT_EQ(cookie5, Blacklist::StripCookieExpiry(cookie5));
80 129
81 // Expiry, should be equal to non-expiry version after stripping. 130 // Expiry, should be equal to non-expiry version after stripping.
82 EXPECT_TRUE(cookie2 == Blacklist::StripCookieExpiry(cookie1)); 131 EXPECT_EQ(cookie2, Blacklist::StripCookieExpiry(cookie1));
132 EXPECT_EQ(cookie5, Blacklist::StripCookieExpiry(cookie4));
133
134 // Same cookie other than expiry should be same after stripping.
135 EXPECT_EQ(Blacklist::StripCookieExpiry(cookie2),
136 Blacklist::StripCookieExpiry(cookie3));
83 137
84 // Edge cases. 138 // Edge cases.
85 EXPECT_TRUE(std::string() == Blacklist::StripCookieExpiry(std::string())); 139 std::string invalid("#$%^&*()_+");
86 EXPECT_TRUE(Blacklist::StripCookieExpiry(cookie2) == 140 EXPECT_EQ(invalid, Blacklist::StripCookieExpiry(invalid));
87 Blacklist::StripCookieExpiry(cookie3)); 141 EXPECT_EQ(std::string(), Blacklist::StripCookieExpiry(std::string()));
88 142
89 // StripCookies Test. Note that "\r\n" line terminators are used 143 // StripCookies Test. Note that "\r\n" line terminators are used
90 // because the underlying net util uniformizes those when stripping 144 // because the underlying net util uniformizes those when stripping
91 // headers. 145 // headers.
92 std::string header1("Host: www.example.com\r\n"); 146 std::string header1("Host: www.example.com\r\n");
93 std::string header2("Upgrade: TLS/1.0, HTTP/1.1\r\n" 147 std::string header2("Upgrade: TLS/1.0, HTTP/1.1\r\n"
94 "Connection: Upgrade\r\n"); 148 "Connection: Upgrade\r\n");
95 std::string header3("Date: Mon, 12 Mar 2001 19:20:33 GMT\r\n" 149 std::string header3("Date: Mon, 12 Mar 2001 19:20:33 GMT\r\n"
96 "Expires: Mon, 12 Mar 2001 19:20:33 GMT\r\n" 150 "Expires: Mon, 12 Mar 2001 19:20:33 GMT\r\n"
97 "Content-Type: text/html\r\n" 151 "Content-Type: text/html\r\n"
98 "Set-Cookie: B=460soc0taq8c1&b=2; " 152 "Set-Cookie: B=460soc0taq8c1&b=2; "
99 "expires=Thu, 15 Apr 2010 20:00:00 GMT; path=/;\r\n"); 153 "expires=Thu, 15 Apr 2010 20:00:00 GMT; path=/;\r\n");
100 std::string header4("Date: Mon, 12 Mar 2001 19:20:33 GMT\r\n" 154 std::string header4("Date: Mon, 12 Mar 2001 19:20:33 GMT\r\n"
101 "Expires: Mon, 12 Mar 2001 19:20:33 GMT\r\n" 155 "Expires: Mon, 12 Mar 2001 19:20:33 GMT\r\n"
102 "Content-Type: text/html\r\n"); 156 "Content-Type: text/html\r\n");
103 157
104 EXPECT_TRUE(header1 == Blacklist::StripCookies(header1)); 158 EXPECT_TRUE(header1 == Blacklist::StripCookies(header1));
105 EXPECT_TRUE(header2 == Blacklist::StripCookies(header2)); 159 EXPECT_TRUE(header2 == Blacklist::StripCookies(header2));
106 EXPECT_TRUE(header4 == Blacklist::StripCookies(header3)); 160 EXPECT_TRUE(header4 == Blacklist::StripCookies(header3));
107 } 161 }
162
163 TEST(BlacklistTest, PatternMatch) {
164 // @ matches all but empty strings.
165 EXPECT_TRUE(Blacklist::Matches("@", "foo.com"));
166 EXPECT_TRUE(Blacklist::Matches("@", "path"));
167 EXPECT_TRUE(Blacklist::Matches("@", "foo.com/path"));
168 EXPECT_TRUE(Blacklist::Matches("@", "x"));
169 EXPECT_FALSE(Blacklist::Matches("@", ""));
170 EXPECT_FALSE(Blacklist::Matches("@", std::string()));
171
172 // Prefix match.
173 EXPECT_TRUE(Blacklist::Matches("prefix@", "prefix.com"));
174 EXPECT_TRUE(Blacklist::Matches("prefix@", "prefix.com/path"));
175 EXPECT_TRUE(Blacklist::Matches("prefix@", "prefix/path"));
176 EXPECT_TRUE(Blacklist::Matches("prefix@", "prefix/prefix"));
177 EXPECT_FALSE(Blacklist::Matches("prefix@", "prefix"));
178 EXPECT_FALSE(Blacklist::Matches("prefix@", "Xprefix"));
179 EXPECT_FALSE(Blacklist::Matches("prefix@", "Y.Xprefix"));
180 EXPECT_FALSE(Blacklist::Matches("prefix@", "Y/Xprefix"));
181
182 // Postfix match.
183 EXPECT_TRUE(Blacklist::Matches("@postfix", "something.postfix"));
184 EXPECT_TRUE(Blacklist::Matches("@postfix", "something/postfix"));
185 EXPECT_TRUE(Blacklist::Matches("@postfix", "foo.com/something/postfix"));
186 EXPECT_FALSE(Blacklist::Matches("@postfix", "postfix"));
187 EXPECT_FALSE(Blacklist::Matches("@postfix", "postfixZ"));
188 EXPECT_FALSE(Blacklist::Matches("@postfix", "postfixZ.Y"));
189
190 // Infix matches.
191 EXPECT_TRUE(Blacklist::Matches("@evil@", "www.evil.com"));
192 EXPECT_TRUE(Blacklist::Matches("@evil@", "www.evil.com/whatever"));
193 EXPECT_TRUE(Blacklist::Matches("@evil@", "www.whatever.com/evilpath"));
194 EXPECT_TRUE(Blacklist::Matches("@evil@", "www.evil.whatever.com"));
195 EXPECT_FALSE(Blacklist::Matches("@evil@", "evil"));
196 EXPECT_FALSE(Blacklist::Matches("@evil@", "evil/"));
197 EXPECT_FALSE(Blacklist::Matches("@evil@", "/evil"));
198
199 // Outfix matches.
200 EXPECT_TRUE(Blacklist::Matches("really@bad", "really/bad"));
201 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/bad"));
202 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.com/path/bad"));
203 EXPECT_TRUE(Blacklist::Matches("really@bad", "really.evil.com/path/bad"));
204 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad.com"));
205 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad"));
206 EXPECT_FALSE(Blacklist::Matches("really@bad", ".reallybad"));
207 EXPECT_FALSE(Blacklist::Matches("really@bad", "reallybad."));
208 EXPECT_FALSE(Blacklist::Matches("really@bad", "really.bad."));
209 EXPECT_FALSE(Blacklist::Matches("really@bad", ".really.bad"));
210 }
OLDNEW
« no previous file with comments | « chrome/browser/privacy_blacklist/blacklist.cc ('k') | chrome/browser/renderer_host/resource_dispatcher_host.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698