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

Side by Side Diff: net/http/http_auth_cache_unittest.cc

Issue 8340026: Use AuthCredentials throughout the network stack instead of username/password. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix comments Created 9 years, 1 month 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
« no previous file with comments | « net/http/http_auth_cache.cc ('k') | net/http/http_auth_controller.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 <string> 5 #include <string>
6 6
7 #include "base/string16.h" 7 #include "base/string16.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 22 matching lines...) Expand all
33 virtual HttpAuth::AuthorizationResult HandleAnotherChallenge( 33 virtual HttpAuth::AuthorizationResult HandleAnotherChallenge(
34 HttpAuth::ChallengeTokenizer* challenge) { 34 HttpAuth::ChallengeTokenizer* challenge) {
35 return HttpAuth::AUTHORIZATION_RESULT_REJECT; 35 return HttpAuth::AUTHORIZATION_RESULT_REJECT;
36 } 36 }
37 37
38 protected: 38 protected:
39 virtual bool Init(HttpAuth::ChallengeTokenizer* challenge) { 39 virtual bool Init(HttpAuth::ChallengeTokenizer* challenge) {
40 return false; // Unused. 40 return false; // Unused.
41 } 41 }
42 42
43 virtual int GenerateAuthTokenImpl(const string16*, 43 virtual int GenerateAuthTokenImpl(const AuthCredentials*,
44 const string16*,
45 const HttpRequestInfo*, 44 const HttpRequestInfo*,
46 OldCompletionCallback* callback, 45 OldCompletionCallback* callback,
47 std::string* auth_token) { 46 std::string* auth_token) {
48 *auth_token = "mock-credentials"; 47 *auth_token = "mock-credentials";
49 return OK; 48 return OK;
50 } 49 }
51 50
52 51
53 private: 52 private:
54 ~MockAuthHandler() {} 53 ~MockAuthHandler() {}
55 }; 54 };
56 55
57 const char* kRealm1 = "Realm1"; 56 const char* kRealm1 = "Realm1";
58 const char* kRealm2 = "Realm2"; 57 const char* kRealm2 = "Realm2";
59 const char* kRealm3 = "Realm3"; 58 const char* kRealm3 = "Realm3";
60 const char* kRealm4 = "Realm4"; 59 const char* kRealm4 = "Realm4";
61 const char* kRealm5 = "Realm5"; 60 const char* kRealm5 = "Realm5";
62 const string16 k123(ASCIIToUTF16("123")); 61 const string16 k123(ASCIIToUTF16("123"));
63 const string16 k1234(ASCIIToUTF16("1234")); 62 const string16 k1234(ASCIIToUTF16("1234"));
64 const string16 kAdmin(ASCIIToUTF16("admin")); 63 const string16 kAdmin(ASCIIToUTF16("admin"));
65 const string16 kAlice(ASCIIToUTF16("alice")); 64 const string16 kAlice(ASCIIToUTF16("alice"));
66 const string16 kAlice2(ASCIIToUTF16("alice2")); 65 const string16 kAlice2(ASCIIToUTF16("alice2"));
67 const string16 kPassword(ASCIIToUTF16("password")); 66 const string16 kPassword(ASCIIToUTF16("password"));
68 const string16 kRoot(ASCIIToUTF16("root")); 67 const string16 kRoot(ASCIIToUTF16("root"));
69 const string16 kUsername(ASCIIToUTF16("username")); 68 const string16 kUsername(ASCIIToUTF16("username"));
70 const string16 kWileCoyote(ASCIIToUTF16("wilecoyote")); 69 const string16 kWileCoyote(ASCIIToUTF16("wilecoyote"));
71 70
71 AuthCredentials CreateASCIICredentials(const char* username,
72 const char* password) {
73 return AuthCredentials(ASCIIToUTF16(username), ASCIIToUTF16(password));
74 }
75
72 } // namespace 76 } // namespace
73 77
74 // Test adding and looking-up cache entries (both by realm and by path). 78 // Test adding and looking-up cache entries (both by realm and by path).
75 TEST(HttpAuthCacheTest, Basic) { 79 TEST(HttpAuthCacheTest, Basic) {
76 GURL origin("http://www.google.com"); 80 GURL origin("http://www.google.com");
77 HttpAuthCache cache; 81 HttpAuthCache cache;
78 HttpAuthCache::Entry* entry; 82 HttpAuthCache::Entry* entry;
79 83
80 // Add cache entries for 4 realms: "Realm1", "Realm2", "Realm3" and 84 // Add cache entries for 4 realms: "Realm1", "Realm2", "Realm3" and
81 // "Realm4" 85 // "Realm4"
82 86
83 scoped_ptr<HttpAuthHandler> realm1_handler( 87 scoped_ptr<HttpAuthHandler> realm1_handler(
84 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, 88 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC,
85 kRealm1, 89 kRealm1,
86 HttpAuth::AUTH_SERVER)); 90 HttpAuth::AUTH_SERVER));
87 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(), 91 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(),
88 "Basic realm=Realm1", ASCIIToUTF16("realm1-user"), 92 "Basic realm=Realm1",
89 ASCIIToUTF16("realm1-password"), "/foo/bar/index.html"); 93 CreateASCIICredentials("realm1-user", "realm1-password"),
94 "/foo/bar/index.html");
90 95
91 scoped_ptr<HttpAuthHandler> realm2_handler( 96 scoped_ptr<HttpAuthHandler> realm2_handler(
92 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, 97 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC,
93 kRealm2, 98 kRealm2,
94 HttpAuth::AUTH_SERVER)); 99 HttpAuth::AUTH_SERVER));
95 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(), 100 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(),
96 "Basic realm=Realm2", ASCIIToUTF16("realm2-user"), 101 "Basic realm=Realm2",
97 ASCIIToUTF16("realm2-password"), "/foo2/index.html"); 102 CreateASCIICredentials("realm2-user", "realm2-password"),
103 "/foo2/index.html");
98 104
99 scoped_ptr<HttpAuthHandler> realm3_basic_handler( 105 scoped_ptr<HttpAuthHandler> realm3_basic_handler(
100 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, 106 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC,
101 kRealm3, 107 kRealm3,
102 HttpAuth::AUTH_PROXY)); 108 HttpAuth::AUTH_PROXY));
103 cache.Add(origin, realm3_basic_handler->realm(), 109 cache.Add(origin, realm3_basic_handler->realm(),
104 realm3_basic_handler->auth_scheme(), "Basic realm=Realm3", 110 realm3_basic_handler->auth_scheme(), "Basic realm=Realm3",
105 ASCIIToUTF16("realm3-basic-user"), 111 CreateASCIICredentials("realm3-basic-user",
106 ASCIIToUTF16("realm3-basic-password"), ""); 112 "realm3-basic-password"),
113 "");
107 114
108 scoped_ptr<HttpAuthHandler> realm3_digest_handler( 115 scoped_ptr<HttpAuthHandler> realm3_digest_handler(
109 new MockAuthHandler(HttpAuth::AUTH_SCHEME_DIGEST, 116 new MockAuthHandler(HttpAuth::AUTH_SCHEME_DIGEST,
110 kRealm3, 117 kRealm3,
111 HttpAuth::AUTH_PROXY)); 118 HttpAuth::AUTH_PROXY));
112 cache.Add(origin, realm3_digest_handler->realm(), 119 cache.Add(origin, realm3_digest_handler->realm(),
113 realm3_digest_handler->auth_scheme(), "Digest realm=Realm3", 120 realm3_digest_handler->auth_scheme(), "Digest realm=Realm3",
114 ASCIIToUTF16("realm3-digest-user"), 121 CreateASCIICredentials("realm3-digest-user",
115 ASCIIToUTF16("realm3-digest-password"), "/baz/index.html"); 122 "realm3-digest-password"),
123 "/baz/index.html");
116 124
117 scoped_ptr<HttpAuthHandler> realm4_basic_handler( 125 scoped_ptr<HttpAuthHandler> realm4_basic_handler(
118 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, 126 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC,
119 kRealm4, 127 kRealm4,
120 HttpAuth::AUTH_SERVER)); 128 HttpAuth::AUTH_SERVER));
121 cache.Add(origin, realm4_basic_handler->realm(), 129 cache.Add(origin, realm4_basic_handler->realm(),
122 realm4_basic_handler->auth_scheme(), "Basic realm=Realm4", 130 realm4_basic_handler->auth_scheme(), "Basic realm=Realm4",
123 ASCIIToUTF16("realm4-basic-user"), 131 CreateASCIICredentials("realm4-basic-user",
124 ASCIIToUTF16("realm4-basic-password"), "/"); 132 "realm4-basic-password"),
133 "/");
125 134
126 // There is no Realm5 135 // There is no Realm5
127 entry = cache.Lookup(origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC); 136 entry = cache.Lookup(origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC);
128 EXPECT_TRUE(NULL == entry); 137 EXPECT_TRUE(NULL == entry);
129 138
130 // While Realm3 does exist, the origin scheme is wrong. 139 // While Realm3 does exist, the origin scheme is wrong.
131 entry = cache.Lookup(GURL("https://www.google.com"), kRealm3, 140 entry = cache.Lookup(GURL("https://www.google.com"), kRealm3,
132 HttpAuth::AUTH_SCHEME_BASIC); 141 HttpAuth::AUTH_SCHEME_BASIC);
133 EXPECT_TRUE(NULL == entry); 142 EXPECT_TRUE(NULL == entry);
134 143
135 // Realm, origin scheme ok, authentication scheme wrong 144 // Realm, origin scheme ok, authentication scheme wrong
136 entry = cache.Lookup 145 entry = cache.Lookup
137 (GURL("http://www.google.com"), kRealm1, HttpAuth::AUTH_SCHEME_DIGEST); 146 (GURL("http://www.google.com"), kRealm1, HttpAuth::AUTH_SCHEME_DIGEST);
138 EXPECT_TRUE(NULL == entry); 147 EXPECT_TRUE(NULL == entry);
139 148
140 // Valid lookup by origin, realm, scheme. 149 // Valid lookup by origin, realm, scheme.
141 entry = cache.Lookup( 150 entry = cache.Lookup(
142 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_BASIC); 151 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_BASIC);
143 ASSERT_FALSE(NULL == entry); 152 ASSERT_FALSE(NULL == entry);
144 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme()); 153 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme());
145 EXPECT_EQ(kRealm3, entry->realm()); 154 EXPECT_EQ(kRealm3, entry->realm());
146 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge()); 155 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge());
147 EXPECT_EQ(ASCIIToUTF16("realm3-basic-user"), entry->username()); 156 EXPECT_EQ(ASCIIToUTF16("realm3-basic-user"), entry->credentials().username());
148 EXPECT_EQ(ASCIIToUTF16("realm3-basic-password"), entry->password()); 157 EXPECT_EQ(ASCIIToUTF16("realm3-basic-password"),
158 entry->credentials().password());
149 159
150 // Valid lookup by origin, realm, scheme when there's a duplicate 160 // Valid lookup by origin, realm, scheme when there's a duplicate
151 // origin, realm in the cache 161 // origin, realm in the cache
152 entry = cache.Lookup( 162 entry = cache.Lookup(
153 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); 163 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_DIGEST);
154 ASSERT_FALSE(NULL == entry); 164 ASSERT_FALSE(NULL == entry);
155 EXPECT_EQ(HttpAuth::AUTH_SCHEME_DIGEST, entry->scheme()); 165 EXPECT_EQ(HttpAuth::AUTH_SCHEME_DIGEST, entry->scheme());
156 EXPECT_EQ(kRealm3, entry->realm()); 166 EXPECT_EQ(kRealm3, entry->realm());
157 EXPECT_EQ("Digest realm=Realm3", entry->auth_challenge()); 167 EXPECT_EQ("Digest realm=Realm3", entry->auth_challenge());
158 EXPECT_EQ(ASCIIToUTF16("realm3-digest-user"), entry->username()); 168 EXPECT_EQ(ASCIIToUTF16("realm3-digest-user"),
159 EXPECT_EQ(ASCIIToUTF16("realm3-digest-password"), entry->password()); 169 entry->credentials().username());
170 EXPECT_EQ(ASCIIToUTF16("realm3-digest-password"),
171 entry->credentials().password());
160 172
161 // Valid lookup by realm. 173 // Valid lookup by realm.
162 entry = cache.Lookup(origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC); 174 entry = cache.Lookup(origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC);
163 ASSERT_FALSE(NULL == entry); 175 ASSERT_FALSE(NULL == entry);
164 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme()); 176 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme());
165 EXPECT_EQ(kRealm2, entry->realm()); 177 EXPECT_EQ(kRealm2, entry->realm());
166 EXPECT_EQ("Basic realm=Realm2", entry->auth_challenge()); 178 EXPECT_EQ("Basic realm=Realm2", entry->auth_challenge());
167 EXPECT_EQ(ASCIIToUTF16("realm2-user"), entry->username()); 179 EXPECT_EQ(ASCIIToUTF16("realm2-user"), entry->credentials().username());
168 EXPECT_EQ(ASCIIToUTF16("realm2-password"), entry->password()); 180 EXPECT_EQ(ASCIIToUTF16("realm2-password"), entry->credentials().password());
169 181
170 // Check that subpaths are recognized. 182 // Check that subpaths are recognized.
171 HttpAuthCache::Entry* realm2_entry = cache.Lookup( 183 HttpAuthCache::Entry* realm2_entry = cache.Lookup(
172 origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC); 184 origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC);
173 HttpAuthCache::Entry* realm4_entry = cache.Lookup( 185 HttpAuthCache::Entry* realm4_entry = cache.Lookup(
174 origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC); 186 origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC);
175 EXPECT_FALSE(NULL == realm2_entry); 187 EXPECT_FALSE(NULL == realm2_entry);
176 EXPECT_FALSE(NULL == realm4_entry); 188 EXPECT_FALSE(NULL == realm4_entry);
177 // Realm4 applies to '/' and Realm2 applies to '/foo2/'. 189 // Realm4 applies to '/' and Realm2 applies to '/foo2/'.
178 // LookupByPath() should return the closest enclosing path. 190 // LookupByPath() should return the closest enclosing path.
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 TEST(HttpAuthCacheTest, AddToExistingEntry) { 274 TEST(HttpAuthCacheTest, AddToExistingEntry) {
263 HttpAuthCache cache; 275 HttpAuthCache cache;
264 GURL origin("http://www.foobar.com:70"); 276 GURL origin("http://www.foobar.com:70");
265 const std::string auth_challenge = "Basic realm=MyRealm"; 277 const std::string auth_challenge = "Basic realm=MyRealm";
266 278
267 scoped_ptr<HttpAuthHandler> handler( 279 scoped_ptr<HttpAuthHandler> handler(
268 new MockAuthHandler( 280 new MockAuthHandler(
269 HttpAuth::AUTH_SCHEME_BASIC, "MyRealm", HttpAuth::AUTH_SERVER)); 281 HttpAuth::AUTH_SCHEME_BASIC, "MyRealm", HttpAuth::AUTH_SERVER));
270 HttpAuthCache::Entry* orig_entry = cache.Add( 282 HttpAuthCache::Entry* orig_entry = cache.Add(
271 origin, handler->realm(), handler->auth_scheme(), auth_challenge, 283 origin, handler->realm(), handler->auth_scheme(), auth_challenge,
272 ASCIIToUTF16("user1"), ASCIIToUTF16("password1"), "/x/y/z/"); 284 CreateASCIICredentials("user1", "password1"), "/x/y/z/");
273 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, 285 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge,
274 ASCIIToUTF16("user2"), ASCIIToUTF16("password2"), "/z/y/x/"); 286 CreateASCIICredentials("user2", "password2"), "/z/y/x/");
275 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, 287 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge,
276 ASCIIToUTF16("user3"), ASCIIToUTF16("password3"), "/z/y"); 288 CreateASCIICredentials("user3", "password3"), "/z/y");
277 289
278 HttpAuthCache::Entry* entry = cache.Lookup( 290 HttpAuthCache::Entry* entry = cache.Lookup(
279 origin, "MyRealm", HttpAuth::AUTH_SCHEME_BASIC); 291 origin, "MyRealm", HttpAuth::AUTH_SCHEME_BASIC);
280 292
281 EXPECT_TRUE(entry == orig_entry); 293 EXPECT_TRUE(entry == orig_entry);
282 EXPECT_EQ(ASCIIToUTF16("user3"), entry->username()); 294 EXPECT_EQ(ASCIIToUTF16("user3"), entry->credentials().username());
283 EXPECT_EQ(ASCIIToUTF16("password3"), entry->password()); 295 EXPECT_EQ(ASCIIToUTF16("password3"), entry->credentials().password());
284 296
285 EXPECT_EQ(2U, entry->paths_.size()); 297 EXPECT_EQ(2U, entry->paths_.size());
286 EXPECT_EQ("/z/", entry->paths_.front()); 298 EXPECT_EQ("/z/", entry->paths_.front());
287 EXPECT_EQ("/x/y/z/", entry->paths_.back()); 299 EXPECT_EQ("/x/y/z/", entry->paths_.back());
288 } 300 }
289 301
290 TEST(HttpAuthCacheTest, Remove) { 302 TEST(HttpAuthCacheTest, Remove) {
291 GURL origin("http://foobar2.com"); 303 GURL origin("http://foobar2.com");
292 304
293 scoped_ptr<HttpAuthHandler> realm1_handler( 305 scoped_ptr<HttpAuthHandler> realm1_handler(
294 new MockAuthHandler( 306 new MockAuthHandler(
295 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER)); 307 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER));
296 308
297 scoped_ptr<HttpAuthHandler> realm2_handler( 309 scoped_ptr<HttpAuthHandler> realm2_handler(
298 new MockAuthHandler( 310 new MockAuthHandler(
299 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_SERVER)); 311 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_SERVER));
300 312
301 scoped_ptr<HttpAuthHandler> realm3_basic_handler( 313 scoped_ptr<HttpAuthHandler> realm3_basic_handler(
302 new MockAuthHandler( 314 new MockAuthHandler(
303 HttpAuth::AUTH_SCHEME_BASIC, kRealm3, HttpAuth::AUTH_SERVER)); 315 HttpAuth::AUTH_SCHEME_BASIC, kRealm3, HttpAuth::AUTH_SERVER));
304 316
305 scoped_ptr<HttpAuthHandler> realm3_digest_handler( 317 scoped_ptr<HttpAuthHandler> realm3_digest_handler(
306 new MockAuthHandler( 318 new MockAuthHandler(
307 HttpAuth::AUTH_SCHEME_DIGEST, kRealm3, HttpAuth::AUTH_SERVER)); 319 HttpAuth::AUTH_SCHEME_DIGEST, kRealm3, HttpAuth::AUTH_SERVER));
308 320
309 HttpAuthCache cache; 321 HttpAuthCache cache;
310 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(), 322 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(),
311 "basic realm=Realm1", kAlice, k123, "/"); 323 "basic realm=Realm1", AuthCredentials(kAlice, k123), "/");
312 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(), 324 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(),
313 "basic realm=Realm2", ASCIIToUTF16("bob"), ASCIIToUTF16("princess"), 325 "basic realm=Realm2", CreateASCIICredentials("bob", "princess"),
314 "/"); 326 "/");
315 cache.Add(origin, realm3_basic_handler->realm(), 327 cache.Add(origin, realm3_basic_handler->realm(),
316 realm3_basic_handler->auth_scheme(), "basic realm=Realm3", 328 realm3_basic_handler->auth_scheme(), "basic realm=Realm3",
317 kAdmin, kPassword, "/"); 329 AuthCredentials(kAdmin, kPassword), "/");
318 cache.Add(origin, realm3_digest_handler->realm(), 330 cache.Add(origin, realm3_digest_handler->realm(),
319 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", 331 realm3_digest_handler->auth_scheme(), "digest realm=Realm3",
320 kRoot, kWileCoyote, "/"); 332 AuthCredentials(kRoot, kWileCoyote), "/");
321 333
322 // Fails, because there is no realm "Realm5". 334 // Fails, because there is no realm "Realm5".
323 EXPECT_FALSE(cache.Remove( 335 EXPECT_FALSE(cache.Remove(
324 origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC, kAlice, k123)); 336 origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC,
337 AuthCredentials(kAlice, k123)));
325 338
326 // Fails because the origin is wrong. 339 // Fails because the origin is wrong.
327 EXPECT_FALSE(cache.Remove(GURL("http://foobar2.com:100"), 340 EXPECT_FALSE(cache.Remove(GURL("http://foobar2.com:100"),
328 kRealm1, 341 kRealm1,
329 HttpAuth::AUTH_SCHEME_BASIC, 342 HttpAuth::AUTH_SCHEME_BASIC,
330 kAlice, 343 AuthCredentials(kAlice, k123)));
331 k123));
332 344
333 // Fails because the username is wrong. 345 // Fails because the username is wrong.
334 EXPECT_FALSE(cache.Remove( 346 EXPECT_FALSE(cache.Remove(
335 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, kAlice2, k123)); 347 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
348 AuthCredentials(kAlice2, k123)));
336 349
337 // Fails because the password is wrong. 350 // Fails because the password is wrong.
338 EXPECT_FALSE(cache.Remove( 351 EXPECT_FALSE(cache.Remove(
339 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, kAlice, k1234)); 352 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
353 AuthCredentials(kAlice, k1234)));
340 354
341 // Fails because the authentication type is wrong. 355 // Fails because the authentication type is wrong.
342 EXPECT_FALSE(cache.Remove( 356 EXPECT_FALSE(cache.Remove(
343 origin, kRealm1, HttpAuth::AUTH_SCHEME_DIGEST, kAlice, k123)); 357 origin, kRealm1, HttpAuth::AUTH_SCHEME_DIGEST,
358 AuthCredentials(kAlice, k123)));
344 359
345 // Succeeds. 360 // Succeeds.
346 EXPECT_TRUE(cache.Remove( 361 EXPECT_TRUE(cache.Remove(
347 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, kAlice, k123)); 362 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
363 AuthCredentials(kAlice, k123)));
348 364
349 // Fails because we just deleted the entry! 365 // Fails because we just deleted the entry!
350 EXPECT_FALSE(cache.Remove( 366 EXPECT_FALSE(cache.Remove(
351 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, kAlice, k123)); 367 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
368 AuthCredentials(kAlice, k123)));
352 369
353 // Succeed when there are two authentication types for the same origin,realm. 370 // Succeed when there are two authentication types for the same origin,realm.
354 EXPECT_TRUE(cache.Remove( 371 EXPECT_TRUE(cache.Remove(
355 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST, kRoot, kWileCoyote)); 372 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST,
373 AuthCredentials(kRoot, kWileCoyote)));
356 374
357 // Succeed as above, but when entries were added in opposite order 375 // Succeed as above, but when entries were added in opposite order
358 cache.Add(origin, realm3_digest_handler->realm(), 376 cache.Add(origin, realm3_digest_handler->realm(),
359 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", 377 realm3_digest_handler->auth_scheme(), "digest realm=Realm3",
360 kRoot, kWileCoyote, "/"); 378 AuthCredentials(kRoot, kWileCoyote), "/");
361 EXPECT_TRUE(cache.Remove( 379 EXPECT_TRUE(cache.Remove(
362 origin, kRealm3, HttpAuth::AUTH_SCHEME_BASIC, kAdmin, kPassword)); 380 origin, kRealm3, HttpAuth::AUTH_SCHEME_BASIC,
381 AuthCredentials(kAdmin, kPassword)));
363 382
364 // Make sure that removing one entry still leaves the other available for 383 // Make sure that removing one entry still leaves the other available for
365 // lookup. 384 // lookup.
366 HttpAuthCache::Entry* entry = cache.Lookup( 385 HttpAuthCache::Entry* entry = cache.Lookup(
367 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); 386 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST);
368 EXPECT_FALSE(NULL == entry); 387 EXPECT_FALSE(NULL == entry);
369 } 388 }
370 389
371 TEST(HttpAuthCacheTest, UpdateStaleChallenge) { 390 TEST(HttpAuthCacheTest, UpdateStaleChallenge) {
372 HttpAuthCache cache; 391 HttpAuthCache cache;
373 GURL origin("http://foobar2.com"); 392 GURL origin("http://foobar2.com");
374 scoped_ptr<HttpAuthHandler> digest_handler( 393 scoped_ptr<HttpAuthHandler> digest_handler(
375 new MockAuthHandler( 394 new MockAuthHandler(
376 HttpAuth::AUTH_SCHEME_DIGEST, kRealm1, HttpAuth::AUTH_PROXY)); 395 HttpAuth::AUTH_SCHEME_DIGEST, kRealm1, HttpAuth::AUTH_PROXY));
377 HttpAuthCache::Entry* entry_pre = cache.Add( 396 HttpAuthCache::Entry* entry_pre = cache.Add(
378 origin, 397 origin,
379 digest_handler->realm(), 398 digest_handler->realm(),
380 digest_handler->auth_scheme(), 399 digest_handler->auth_scheme(),
381 "Digest realm=Realm1," 400 "Digest realm=Realm1,"
382 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\"", 401 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\"",
383 ASCIIToUTF16("realm-digest-user"), 402 CreateASCIICredentials("realm-digest-user", "realm-digest-password"),
384 ASCIIToUTF16("realm-digest-password"),
385 "/baz/index.html"); 403 "/baz/index.html");
386 ASSERT_TRUE(entry_pre != NULL); 404 ASSERT_TRUE(entry_pre != NULL);
387 405
388 EXPECT_EQ(2, entry_pre->IncrementNonceCount()); 406 EXPECT_EQ(2, entry_pre->IncrementNonceCount());
389 EXPECT_EQ(3, entry_pre->IncrementNonceCount()); 407 EXPECT_EQ(3, entry_pre->IncrementNonceCount());
390 EXPECT_EQ(4, entry_pre->IncrementNonceCount()); 408 EXPECT_EQ(4, entry_pre->IncrementNonceCount());
391 409
392 bool update_success = cache.UpdateStaleChallenge( 410 bool update_success = cache.UpdateStaleChallenge(
393 origin, 411 origin,
394 digest_handler->realm(), 412 digest_handler->realm(),
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 455
438 scoped_ptr<HttpAuthHandler> realm4_handler( 456 scoped_ptr<HttpAuthHandler> realm4_handler(
439 new MockAuthHandler( 457 new MockAuthHandler(
440 HttpAuth::AUTH_SCHEME_BASIC, kRealm4, HttpAuth::AUTH_SERVER)); 458 HttpAuth::AUTH_SCHEME_BASIC, kRealm4, HttpAuth::AUTH_SERVER));
441 459
442 HttpAuthCache first_cache; 460 HttpAuthCache first_cache;
443 HttpAuthCache::Entry* entry; 461 HttpAuthCache::Entry* entry;
444 462
445 first_cache.Add(origin, realm1_handler->realm(), 463 first_cache.Add(origin, realm1_handler->realm(),
446 realm1_handler->auth_scheme(), "basic realm=Realm1", 464 realm1_handler->auth_scheme(), "basic realm=Realm1",
447 kAlice, k123, path); 465 AuthCredentials(kAlice, k123), path);
448 first_cache.Add(origin, realm2_handler->realm(), 466 first_cache.Add(origin, realm2_handler->realm(),
449 realm2_handler->auth_scheme(), "basic realm=Realm2", 467 realm2_handler->auth_scheme(), "basic realm=Realm2",
450 kAlice2, k1234, path); 468 AuthCredentials(kAlice2, k1234), path);
451 first_cache.Add(origin, realm3_digest_handler->realm(), 469 first_cache.Add(origin, realm3_digest_handler->realm(),
452 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", 470 realm3_digest_handler->auth_scheme(), "digest realm=Realm3",
453 kRoot, kWileCoyote, path); 471 AuthCredentials(kRoot, kWileCoyote), path);
454 entry = first_cache.Add( 472 entry = first_cache.Add(
455 origin, realm3_digest_handler->realm(), 473 origin, realm3_digest_handler->realm(),
456 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", 474 realm3_digest_handler->auth_scheme(), "digest realm=Realm3",
457 kRoot, kWileCoyote, another_path); 475 AuthCredentials(kRoot, kWileCoyote), another_path);
458 476
459 EXPECT_EQ(2, entry->IncrementNonceCount()); 477 EXPECT_EQ(2, entry->IncrementNonceCount());
460 478
461 HttpAuthCache second_cache; 479 HttpAuthCache second_cache;
462 // Will be overwritten by kRoot:kWileCoyote. 480 // Will be overwritten by kRoot:kWileCoyote.
463 second_cache.Add(origin, realm3_digest_handler->realm(), 481 second_cache.Add(origin, realm3_digest_handler->realm(),
464 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", 482 realm3_digest_handler->auth_scheme(), "digest realm=Realm3",
465 kAlice2, k1234, path); 483 AuthCredentials(kAlice2, k1234), path);
466 // Should be left intact. 484 // Should be left intact.
467 second_cache.Add(origin, realm4_handler->realm(), 485 second_cache.Add(origin, realm4_handler->realm(),
468 realm4_handler->auth_scheme(), "basic realm=Realm4", 486 realm4_handler->auth_scheme(), "basic realm=Realm4",
469 kAdmin, kRoot, path); 487 AuthCredentials(kAdmin, kRoot), path);
470 488
471 second_cache.UpdateAllFrom(first_cache); 489 second_cache.UpdateAllFrom(first_cache);
472 490
473 // Copied from first_cache. 491 // Copied from first_cache.
474 entry = second_cache.Lookup(origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC); 492 entry = second_cache.Lookup(origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC);
475 EXPECT_TRUE(NULL != entry); 493 EXPECT_TRUE(NULL != entry);
476 EXPECT_EQ(kAlice, entry->username()); 494 EXPECT_EQ(kAlice, entry->credentials().username());
477 EXPECT_EQ(k123, entry->password()); 495 EXPECT_EQ(k123, entry->credentials().password());
478 496
479 // Copied from first_cache. 497 // Copied from first_cache.
480 entry = second_cache.Lookup(origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC); 498 entry = second_cache.Lookup(origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC);
481 EXPECT_TRUE(NULL != entry); 499 EXPECT_TRUE(NULL != entry);
482 EXPECT_EQ(kAlice2, entry->username()); 500 EXPECT_EQ(kAlice2, entry->credentials().username());
483 EXPECT_EQ(k1234, entry->password()); 501 EXPECT_EQ(k1234, entry->credentials().password());
484 502
485 // Overwritten from first_cache. 503 // Overwritten from first_cache.
486 entry = second_cache.Lookup(origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); 504 entry = second_cache.Lookup(origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST);
487 EXPECT_TRUE(NULL != entry); 505 EXPECT_TRUE(NULL != entry);
488 EXPECT_EQ(kRoot, entry->username()); 506 EXPECT_EQ(kRoot, entry->credentials().username());
489 EXPECT_EQ(kWileCoyote, entry->password()); 507 EXPECT_EQ(kWileCoyote, entry->credentials().password());
490 // Nonce count should get copied. 508 // Nonce count should get copied.
491 EXPECT_EQ(3, entry->IncrementNonceCount()); 509 EXPECT_EQ(3, entry->IncrementNonceCount());
492 510
493 // All paths should get copied. 511 // All paths should get copied.
494 entry = second_cache.LookupByPath(origin, another_path); 512 entry = second_cache.LookupByPath(origin, another_path);
495 EXPECT_TRUE(NULL != entry); 513 EXPECT_TRUE(NULL != entry);
496 EXPECT_EQ(kRoot, entry->username()); 514 EXPECT_EQ(kRoot, entry->credentials().username());
497 EXPECT_EQ(kWileCoyote, entry->password()); 515 EXPECT_EQ(kWileCoyote, entry->credentials().password());
498 516
499 // Left intact in second_cache. 517 // Left intact in second_cache.
500 entry = second_cache.Lookup(origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC); 518 entry = second_cache.Lookup(origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC);
501 EXPECT_TRUE(NULL != entry); 519 EXPECT_TRUE(NULL != entry);
502 EXPECT_EQ(kAdmin, entry->username()); 520 EXPECT_EQ(kAdmin, entry->credentials().username());
503 EXPECT_EQ(kRoot, entry->password()); 521 EXPECT_EQ(kRoot, entry->credentials().password());
504 } 522 }
505 523
506 // Test fixture class for eviction tests (contains helpers for bulk 524 // Test fixture class for eviction tests (contains helpers for bulk
507 // insertion and existence testing). 525 // insertion and existence testing).
508 class HttpAuthCacheEvictionTest : public testing::Test { 526 class HttpAuthCacheEvictionTest : public testing::Test {
509 protected: 527 protected:
510 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") { } 528 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") { }
511 529
512 std::string GenerateRealm(int realm_i) { 530 std::string GenerateRealm(int realm_i) {
513 return base::StringPrintf("Realm %d", realm_i); 531 return base::StringPrintf("Realm %d", realm_i);
514 } 532 }
515 533
516 std::string GeneratePath(int realm_i, int path_i) { 534 std::string GeneratePath(int realm_i, int path_i) {
517 return base::StringPrintf("/%d/%d/x/y", realm_i, path_i); 535 return base::StringPrintf("/%d/%d/x/y", realm_i, path_i);
518 } 536 }
519 537
520 void AddRealm(int realm_i) { 538 void AddRealm(int realm_i) {
521 AddPathToRealm(realm_i, 0); 539 AddPathToRealm(realm_i, 0);
522 } 540 }
523 541
524 void AddPathToRealm(int realm_i, int path_i) { 542 void AddPathToRealm(int realm_i, int path_i) {
525 cache_.Add(origin_, GenerateRealm(realm_i), HttpAuth::AUTH_SCHEME_BASIC, "", 543 cache_.Add(origin_, GenerateRealm(realm_i), HttpAuth::AUTH_SCHEME_BASIC, "",
526 kUsername, kPassword, GeneratePath(realm_i, path_i)); 544 AuthCredentials(kUsername, kPassword),
545 GeneratePath(realm_i, path_i));
527 } 546 }
528 547
529 void CheckRealmExistence(int realm_i, bool exists) { 548 void CheckRealmExistence(int realm_i, bool exists) {
530 const HttpAuthCache::Entry* entry = 549 const HttpAuthCache::Entry* entry =
531 cache_.Lookup( 550 cache_.Lookup(
532 origin_, GenerateRealm(realm_i), HttpAuth::AUTH_SCHEME_BASIC); 551 origin_, GenerateRealm(realm_i), HttpAuth::AUTH_SCHEME_BASIC);
533 if (exists) { 552 if (exists) {
534 EXPECT_FALSE(entry == NULL); 553 EXPECT_FALSE(entry == NULL);
535 EXPECT_EQ(GenerateRealm(realm_i), entry->realm()); 554 EXPECT_EQ(GenerateRealm(realm_i), entry->realm());
536 } else { 555 } else {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 CheckPathExistence(0, i, false); 612 CheckPathExistence(0, i, false);
594 613
595 for (int i = 0; i < kMaxPaths; ++i) 614 for (int i = 0; i < kMaxPaths; ++i)
596 CheckPathExistence(0, i + 3, true); 615 CheckPathExistence(0, i + 3, true);
597 616
598 for (int i = 0; i < kMaxRealms; ++i) 617 for (int i = 0; i < kMaxRealms; ++i)
599 CheckRealmExistence(i, true); 618 CheckRealmExistence(i, true);
600 } 619 }
601 620
602 } // namespace net 621 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_auth_cache.cc ('k') | net/http/http_auth_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698