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

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

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/strings/string16.h" 7 #include "base/strings/string16.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 } 43 }
44 44
45 virtual int GenerateAuthTokenImpl(const AuthCredentials*, 45 virtual int GenerateAuthTokenImpl(const AuthCredentials*,
46 const HttpRequestInfo*, 46 const HttpRequestInfo*,
47 const CompletionCallback& callback, 47 const CompletionCallback& callback,
48 std::string* auth_token) OVERRIDE { 48 std::string* auth_token) OVERRIDE {
49 *auth_token = "mock-credentials"; 49 *auth_token = "mock-credentials";
50 return OK; 50 return OK;
51 } 51 }
52 52
53
54 private: 53 private:
55 virtual ~MockAuthHandler() {} 54 virtual ~MockAuthHandler() {}
56 }; 55 };
57 56
58 const char* kRealm1 = "Realm1"; 57 const char* kRealm1 = "Realm1";
59 const char* kRealm2 = "Realm2"; 58 const char* kRealm2 = "Realm2";
60 const char* kRealm3 = "Realm3"; 59 const char* kRealm3 = "Realm3";
61 const char* kRealm4 = "Realm4"; 60 const char* kRealm4 = "Realm4";
62 const char* kRealm5 = "Realm5"; 61 const char* kRealm5 = "Realm5";
63 const base::string16 k123(ASCIIToUTF16("123")); 62 const base::string16 k123(ASCIIToUTF16("123"));
(...skipping 15 matching lines...) Expand all
79 78
80 // Test adding and looking-up cache entries (both by realm and by path). 79 // Test adding and looking-up cache entries (both by realm and by path).
81 TEST(HttpAuthCacheTest, Basic) { 80 TEST(HttpAuthCacheTest, Basic) {
82 GURL origin("http://www.google.com"); 81 GURL origin("http://www.google.com");
83 HttpAuthCache cache; 82 HttpAuthCache cache;
84 HttpAuthCache::Entry* entry; 83 HttpAuthCache::Entry* entry;
85 84
86 // Add cache entries for 4 realms: "Realm1", "Realm2", "Realm3" and 85 // Add cache entries for 4 realms: "Realm1", "Realm2", "Realm3" and
87 // "Realm4" 86 // "Realm4"
88 87
89 scoped_ptr<HttpAuthHandler> realm1_handler( 88 scoped_ptr<HttpAuthHandler> realm1_handler(new MockAuthHandler(
90 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, 89 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER));
91 kRealm1, 90 cache.Add(origin,
92 HttpAuth::AUTH_SERVER)); 91 realm1_handler->realm(),
93 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(), 92 realm1_handler->auth_scheme(),
94 "Basic realm=Realm1", 93 "Basic realm=Realm1",
95 CreateASCIICredentials("realm1-user", "realm1-password"), 94 CreateASCIICredentials("realm1-user", "realm1-password"),
96 "/foo/bar/index.html"); 95 "/foo/bar/index.html");
97 96
98 scoped_ptr<HttpAuthHandler> realm2_handler( 97 scoped_ptr<HttpAuthHandler> realm2_handler(new MockAuthHandler(
99 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, 98 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_SERVER));
100 kRealm2, 99 cache.Add(origin,
101 HttpAuth::AUTH_SERVER)); 100 realm2_handler->realm(),
102 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(), 101 realm2_handler->auth_scheme(),
103 "Basic realm=Realm2", 102 "Basic realm=Realm2",
104 CreateASCIICredentials("realm2-user", "realm2-password"), 103 CreateASCIICredentials("realm2-user", "realm2-password"),
105 "/foo2/index.html"); 104 "/foo2/index.html");
106 105
107 scoped_ptr<HttpAuthHandler> realm3_basic_handler( 106 scoped_ptr<HttpAuthHandler> realm3_basic_handler(new MockAuthHandler(
108 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, 107 HttpAuth::AUTH_SCHEME_BASIC, kRealm3, HttpAuth::AUTH_PROXY));
109 kRealm3,
110 HttpAuth::AUTH_PROXY));
111 cache.Add( 108 cache.Add(
112 origin, 109 origin,
113 realm3_basic_handler->realm(), 110 realm3_basic_handler->realm(),
114 realm3_basic_handler->auth_scheme(), 111 realm3_basic_handler->auth_scheme(),
115 "Basic realm=Realm3", 112 "Basic realm=Realm3",
116 CreateASCIICredentials("realm3-basic-user", "realm3-basic-password"), 113 CreateASCIICredentials("realm3-basic-user", "realm3-basic-password"),
117 std::string()); 114 std::string());
118 115
119 scoped_ptr<HttpAuthHandler> realm3_digest_handler( 116 scoped_ptr<HttpAuthHandler> realm3_digest_handler(new MockAuthHandler(
120 new MockAuthHandler(HttpAuth::AUTH_SCHEME_DIGEST, 117 HttpAuth::AUTH_SCHEME_DIGEST, kRealm3, HttpAuth::AUTH_PROXY));
121 kRealm3, 118 cache.Add(
122 HttpAuth::AUTH_PROXY)); 119 origin,
123 cache.Add(origin, realm3_digest_handler->realm(), 120 realm3_digest_handler->realm(),
124 realm3_digest_handler->auth_scheme(), "Digest realm=Realm3", 121 realm3_digest_handler->auth_scheme(),
125 CreateASCIICredentials("realm3-digest-user", 122 "Digest realm=Realm3",
126 "realm3-digest-password"), 123 CreateASCIICredentials("realm3-digest-user", "realm3-digest-password"),
127 "/baz/index.html"); 124 "/baz/index.html");
128 125
129 scoped_ptr<HttpAuthHandler> realm4_basic_handler( 126 scoped_ptr<HttpAuthHandler> realm4_basic_handler(new MockAuthHandler(
130 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, 127 HttpAuth::AUTH_SCHEME_BASIC, kRealm4, HttpAuth::AUTH_SERVER));
131 kRealm4, 128 cache.Add(
132 HttpAuth::AUTH_SERVER)); 129 origin,
133 cache.Add(origin, realm4_basic_handler->realm(), 130 realm4_basic_handler->realm(),
134 realm4_basic_handler->auth_scheme(), "Basic realm=Realm4", 131 realm4_basic_handler->auth_scheme(),
135 CreateASCIICredentials("realm4-basic-user", 132 "Basic realm=Realm4",
136 "realm4-basic-password"), 133 CreateASCIICredentials("realm4-basic-user", "realm4-basic-password"),
137 "/"); 134 "/");
138 135
139 // There is no Realm5 136 // There is no Realm5
140 entry = cache.Lookup(origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC); 137 entry = cache.Lookup(origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC);
141 EXPECT_TRUE(NULL == entry); 138 EXPECT_TRUE(NULL == entry);
142 139
143 // While Realm3 does exist, the origin scheme is wrong. 140 // While Realm3 does exist, the origin scheme is wrong.
144 entry = cache.Lookup(GURL("https://www.google.com"), kRealm3, 141 entry = cache.Lookup(
145 HttpAuth::AUTH_SCHEME_BASIC); 142 GURL("https://www.google.com"), kRealm3, HttpAuth::AUTH_SCHEME_BASIC);
146 EXPECT_TRUE(NULL == entry); 143 EXPECT_TRUE(NULL == entry);
147 144
148 // Realm, origin scheme ok, authentication scheme wrong 145 // Realm, origin scheme ok, authentication scheme wrong
149 entry = cache.Lookup 146 entry = cache.Lookup(
150 (GURL("http://www.google.com"), kRealm1, HttpAuth::AUTH_SCHEME_DIGEST); 147 GURL("http://www.google.com"), kRealm1, HttpAuth::AUTH_SCHEME_DIGEST);
151 EXPECT_TRUE(NULL == entry); 148 EXPECT_TRUE(NULL == entry);
152 149
153 // Valid lookup by origin, realm, scheme. 150 // Valid lookup by origin, realm, scheme.
154 entry = cache.Lookup( 151 entry = cache.Lookup(
155 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_BASIC); 152 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_BASIC);
156 ASSERT_FALSE(NULL == entry); 153 ASSERT_FALSE(NULL == entry);
157 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme()); 154 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme());
158 EXPECT_EQ(kRealm3, entry->realm()); 155 EXPECT_EQ(kRealm3, entry->realm());
159 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge()); 156 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge());
160 EXPECT_EQ(ASCIIToUTF16("realm3-basic-user"), entry->credentials().username()); 157 EXPECT_EQ(ASCIIToUTF16("realm3-basic-user"), entry->credentials().username());
(...skipping 16 matching lines...) Expand all
177 // Valid lookup by realm. 174 // Valid lookup by realm.
178 entry = cache.Lookup(origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC); 175 entry = cache.Lookup(origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC);
179 ASSERT_FALSE(NULL == entry); 176 ASSERT_FALSE(NULL == entry);
180 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme()); 177 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme());
181 EXPECT_EQ(kRealm2, entry->realm()); 178 EXPECT_EQ(kRealm2, entry->realm());
182 EXPECT_EQ("Basic realm=Realm2", entry->auth_challenge()); 179 EXPECT_EQ("Basic realm=Realm2", entry->auth_challenge());
183 EXPECT_EQ(ASCIIToUTF16("realm2-user"), entry->credentials().username()); 180 EXPECT_EQ(ASCIIToUTF16("realm2-user"), entry->credentials().username());
184 EXPECT_EQ(ASCIIToUTF16("realm2-password"), entry->credentials().password()); 181 EXPECT_EQ(ASCIIToUTF16("realm2-password"), entry->credentials().password());
185 182
186 // Check that subpaths are recognized. 183 // Check that subpaths are recognized.
187 HttpAuthCache::Entry* realm2_entry = cache.Lookup( 184 HttpAuthCache::Entry* realm2_entry =
188 origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC); 185 cache.Lookup(origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC);
189 HttpAuthCache::Entry* realm4_entry = cache.Lookup( 186 HttpAuthCache::Entry* realm4_entry =
190 origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC); 187 cache.Lookup(origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC);
191 EXPECT_FALSE(NULL == realm2_entry); 188 EXPECT_FALSE(NULL == realm2_entry);
192 EXPECT_FALSE(NULL == realm4_entry); 189 EXPECT_FALSE(NULL == realm4_entry);
193 // Realm4 applies to '/' and Realm2 applies to '/foo2/'. 190 // Realm4 applies to '/' and Realm2 applies to '/foo2/'.
194 // LookupByPath() should return the closest enclosing path. 191 // LookupByPath() should return the closest enclosing path.
195 // Positive tests: 192 // Positive tests:
196 entry = cache.LookupByPath(origin, "/foo2/index.html"); 193 entry = cache.LookupByPath(origin, "/foo2/index.html");
197 EXPECT_TRUE(realm2_entry == entry); 194 EXPECT_TRUE(realm2_entry == entry);
198 entry = cache.LookupByPath(origin, "/foo2/foobar.html"); 195 entry = cache.LookupByPath(origin, "/foo2/foobar.html");
199 EXPECT_TRUE(realm2_entry == entry); 196 EXPECT_TRUE(realm2_entry == entry);
200 entry = cache.LookupByPath(origin, "/foo2/bar/index.html"); 197 entry = cache.LookupByPath(origin, "/foo2/bar/index.html");
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 EXPECT_EQ("/", entry.paths_.front()); 270 EXPECT_EQ("/", entry.paths_.front());
274 } 271 }
275 272
276 // Calling Add when the realm entry already exists, should append that 273 // Calling Add when the realm entry already exists, should append that
277 // path. 274 // path.
278 TEST(HttpAuthCacheTest, AddToExistingEntry) { 275 TEST(HttpAuthCacheTest, AddToExistingEntry) {
279 HttpAuthCache cache; 276 HttpAuthCache cache;
280 GURL origin("http://www.foobar.com:70"); 277 GURL origin("http://www.foobar.com:70");
281 const std::string auth_challenge = "Basic realm=MyRealm"; 278 const std::string auth_challenge = "Basic realm=MyRealm";
282 279
283 scoped_ptr<HttpAuthHandler> handler( 280 scoped_ptr<HttpAuthHandler> handler(new MockAuthHandler(
284 new MockAuthHandler( 281 HttpAuth::AUTH_SCHEME_BASIC, "MyRealm", HttpAuth::AUTH_SERVER));
285 HttpAuth::AUTH_SCHEME_BASIC, "MyRealm", HttpAuth::AUTH_SERVER)); 282 HttpAuthCache::Entry* orig_entry =
286 HttpAuthCache::Entry* orig_entry = cache.Add( 283 cache.Add(origin,
287 origin, handler->realm(), handler->auth_scheme(), auth_challenge, 284 handler->realm(),
288 CreateASCIICredentials("user1", "password1"), "/x/y/z/"); 285 handler->auth_scheme(),
289 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, 286 auth_challenge,
290 CreateASCIICredentials("user2", "password2"), "/z/y/x/"); 287 CreateASCIICredentials("user1", "password1"),
291 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, 288 "/x/y/z/");
292 CreateASCIICredentials("user3", "password3"), "/z/y"); 289 cache.Add(origin,
290 handler->realm(),
291 handler->auth_scheme(),
292 auth_challenge,
293 CreateASCIICredentials("user2", "password2"),
294 "/z/y/x/");
295 cache.Add(origin,
296 handler->realm(),
297 handler->auth_scheme(),
298 auth_challenge,
299 CreateASCIICredentials("user3", "password3"),
300 "/z/y");
293 301
294 HttpAuthCache::Entry* entry = cache.Lookup( 302 HttpAuthCache::Entry* entry =
295 origin, "MyRealm", HttpAuth::AUTH_SCHEME_BASIC); 303 cache.Lookup(origin, "MyRealm", HttpAuth::AUTH_SCHEME_BASIC);
296 304
297 EXPECT_TRUE(entry == orig_entry); 305 EXPECT_TRUE(entry == orig_entry);
298 EXPECT_EQ(ASCIIToUTF16("user3"), entry->credentials().username()); 306 EXPECT_EQ(ASCIIToUTF16("user3"), entry->credentials().username());
299 EXPECT_EQ(ASCIIToUTF16("password3"), entry->credentials().password()); 307 EXPECT_EQ(ASCIIToUTF16("password3"), entry->credentials().password());
300 308
301 EXPECT_EQ(2U, entry->paths_.size()); 309 EXPECT_EQ(2U, entry->paths_.size());
302 EXPECT_EQ("/z/", entry->paths_.front()); 310 EXPECT_EQ("/z/", entry->paths_.front());
303 EXPECT_EQ("/x/y/z/", entry->paths_.back()); 311 EXPECT_EQ("/x/y/z/", entry->paths_.back());
304 } 312 }
305 313
306 TEST(HttpAuthCacheTest, Remove) { 314 TEST(HttpAuthCacheTest, Remove) {
307 GURL origin("http://foobar2.com"); 315 GURL origin("http://foobar2.com");
308 316
309 scoped_ptr<HttpAuthHandler> realm1_handler( 317 scoped_ptr<HttpAuthHandler> realm1_handler(new MockAuthHandler(
310 new MockAuthHandler( 318 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER));
311 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER));
312 319
313 scoped_ptr<HttpAuthHandler> realm2_handler( 320 scoped_ptr<HttpAuthHandler> realm2_handler(new MockAuthHandler(
314 new MockAuthHandler( 321 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_SERVER));
315 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_SERVER));
316 322
317 scoped_ptr<HttpAuthHandler> realm3_basic_handler( 323 scoped_ptr<HttpAuthHandler> realm3_basic_handler(new MockAuthHandler(
318 new MockAuthHandler( 324 HttpAuth::AUTH_SCHEME_BASIC, kRealm3, HttpAuth::AUTH_SERVER));
319 HttpAuth::AUTH_SCHEME_BASIC, kRealm3, HttpAuth::AUTH_SERVER));
320 325
321 scoped_ptr<HttpAuthHandler> realm3_digest_handler( 326 scoped_ptr<HttpAuthHandler> realm3_digest_handler(new MockAuthHandler(
322 new MockAuthHandler( 327 HttpAuth::AUTH_SCHEME_DIGEST, kRealm3, HttpAuth::AUTH_SERVER));
323 HttpAuth::AUTH_SCHEME_DIGEST, kRealm3, HttpAuth::AUTH_SERVER));
324 328
325 HttpAuthCache cache; 329 HttpAuthCache cache;
326 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(), 330 cache.Add(origin,
327 "basic realm=Realm1", AuthCredentials(kAlice, k123), "/"); 331 realm1_handler->realm(),
328 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(), 332 realm1_handler->auth_scheme(),
329 "basic realm=Realm2", CreateASCIICredentials("bob", "princess"), 333 "basic realm=Realm1",
334 AuthCredentials(kAlice, k123),
330 "/"); 335 "/");
331 cache.Add(origin, realm3_basic_handler->realm(), 336 cache.Add(origin,
332 realm3_basic_handler->auth_scheme(), "basic realm=Realm3", 337 realm2_handler->realm(),
333 AuthCredentials(kAdmin, kPassword), "/"); 338 realm2_handler->auth_scheme(),
334 cache.Add(origin, realm3_digest_handler->realm(), 339 "basic realm=Realm2",
335 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", 340 CreateASCIICredentials("bob", "princess"),
336 AuthCredentials(kRoot, kWileCoyote), "/"); 341 "/");
342 cache.Add(origin,
343 realm3_basic_handler->realm(),
344 realm3_basic_handler->auth_scheme(),
345 "basic realm=Realm3",
346 AuthCredentials(kAdmin, kPassword),
347 "/");
348 cache.Add(origin,
349 realm3_digest_handler->realm(),
350 realm3_digest_handler->auth_scheme(),
351 "digest realm=Realm3",
352 AuthCredentials(kRoot, kWileCoyote),
353 "/");
337 354
338 // Fails, because there is no realm "Realm5". 355 // Fails, because there is no realm "Realm5".
339 EXPECT_FALSE(cache.Remove( 356 EXPECT_FALSE(cache.Remove(origin,
340 origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC, 357 kRealm5,
341 AuthCredentials(kAlice, k123))); 358 HttpAuth::AUTH_SCHEME_BASIC,
359 AuthCredentials(kAlice, k123)));
342 360
343 // Fails because the origin is wrong. 361 // Fails because the origin is wrong.
344 EXPECT_FALSE(cache.Remove(GURL("http://foobar2.com:100"), 362 EXPECT_FALSE(cache.Remove(GURL("http://foobar2.com:100"),
345 kRealm1, 363 kRealm1,
346 HttpAuth::AUTH_SCHEME_BASIC, 364 HttpAuth::AUTH_SCHEME_BASIC,
347 AuthCredentials(kAlice, k123))); 365 AuthCredentials(kAlice, k123)));
348 366
349 // Fails because the username is wrong. 367 // Fails because the username is wrong.
350 EXPECT_FALSE(cache.Remove( 368 EXPECT_FALSE(cache.Remove(origin,
351 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, 369 kRealm1,
352 AuthCredentials(kAlice2, k123))); 370 HttpAuth::AUTH_SCHEME_BASIC,
371 AuthCredentials(kAlice2, k123)));
353 372
354 // Fails because the password is wrong. 373 // Fails because the password is wrong.
355 EXPECT_FALSE(cache.Remove( 374 EXPECT_FALSE(cache.Remove(origin,
356 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, 375 kRealm1,
357 AuthCredentials(kAlice, k1234))); 376 HttpAuth::AUTH_SCHEME_BASIC,
377 AuthCredentials(kAlice, k1234)));
358 378
359 // Fails because the authentication type is wrong. 379 // Fails because the authentication type is wrong.
360 EXPECT_FALSE(cache.Remove( 380 EXPECT_FALSE(cache.Remove(origin,
361 origin, kRealm1, HttpAuth::AUTH_SCHEME_DIGEST, 381 kRealm1,
362 AuthCredentials(kAlice, k123))); 382 HttpAuth::AUTH_SCHEME_DIGEST,
383 AuthCredentials(kAlice, k123)));
363 384
364 // Succeeds. 385 // Succeeds.
365 EXPECT_TRUE(cache.Remove( 386 EXPECT_TRUE(cache.Remove(origin,
366 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, 387 kRealm1,
367 AuthCredentials(kAlice, k123))); 388 HttpAuth::AUTH_SCHEME_BASIC,
389 AuthCredentials(kAlice, k123)));
368 390
369 // Fails because we just deleted the entry! 391 // Fails because we just deleted the entry!
370 EXPECT_FALSE(cache.Remove( 392 EXPECT_FALSE(cache.Remove(origin,
371 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, 393 kRealm1,
372 AuthCredentials(kAlice, k123))); 394 HttpAuth::AUTH_SCHEME_BASIC,
395 AuthCredentials(kAlice, k123)));
373 396
374 // Succeed when there are two authentication types for the same origin,realm. 397 // Succeed when there are two authentication types for the same origin,realm.
375 EXPECT_TRUE(cache.Remove( 398 EXPECT_TRUE(cache.Remove(origin,
376 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST, 399 kRealm3,
377 AuthCredentials(kRoot, kWileCoyote))); 400 HttpAuth::AUTH_SCHEME_DIGEST,
401 AuthCredentials(kRoot, kWileCoyote)));
378 402
379 // Succeed as above, but when entries were added in opposite order 403 // Succeed as above, but when entries were added in opposite order
380 cache.Add(origin, realm3_digest_handler->realm(), 404 cache.Add(origin,
381 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", 405 realm3_digest_handler->realm(),
382 AuthCredentials(kRoot, kWileCoyote), "/"); 406 realm3_digest_handler->auth_scheme(),
383 EXPECT_TRUE(cache.Remove( 407 "digest realm=Realm3",
384 origin, kRealm3, HttpAuth::AUTH_SCHEME_BASIC, 408 AuthCredentials(kRoot, kWileCoyote),
385 AuthCredentials(kAdmin, kPassword))); 409 "/");
410 EXPECT_TRUE(cache.Remove(origin,
411 kRealm3,
412 HttpAuth::AUTH_SCHEME_BASIC,
413 AuthCredentials(kAdmin, kPassword)));
386 414
387 // Make sure that removing one entry still leaves the other available for 415 // Make sure that removing one entry still leaves the other available for
388 // lookup. 416 // lookup.
389 HttpAuthCache::Entry* entry = cache.Lookup( 417 HttpAuthCache::Entry* entry =
390 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); 418 cache.Lookup(origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST);
391 EXPECT_FALSE(NULL == entry); 419 EXPECT_FALSE(NULL == entry);
392 } 420 }
393 421
394 TEST(HttpAuthCacheTest, UpdateStaleChallenge) { 422 TEST(HttpAuthCacheTest, UpdateStaleChallenge) {
395 HttpAuthCache cache; 423 HttpAuthCache cache;
396 GURL origin("http://foobar2.com"); 424 GURL origin("http://foobar2.com");
397 scoped_ptr<HttpAuthHandler> digest_handler( 425 scoped_ptr<HttpAuthHandler> digest_handler(new MockAuthHandler(
398 new MockAuthHandler( 426 HttpAuth::AUTH_SCHEME_DIGEST, kRealm1, HttpAuth::AUTH_PROXY));
399 HttpAuth::AUTH_SCHEME_DIGEST, kRealm1, HttpAuth::AUTH_PROXY));
400 HttpAuthCache::Entry* entry_pre = cache.Add( 427 HttpAuthCache::Entry* entry_pre = cache.Add(
401 origin, 428 origin,
402 digest_handler->realm(), 429 digest_handler->realm(),
403 digest_handler->auth_scheme(), 430 digest_handler->auth_scheme(),
404 "Digest realm=Realm1," 431 "Digest realm=Realm1,"
405 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\"", 432 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\"",
406 CreateASCIICredentials("realm-digest-user", "realm-digest-password"), 433 CreateASCIICredentials("realm-digest-user", "realm-digest-password"),
407 "/baz/index.html"); 434 "/baz/index.html");
408 ASSERT_TRUE(entry_pre != NULL); 435 ASSERT_TRUE(entry_pre != NULL);
409 436
410 EXPECT_EQ(2, entry_pre->IncrementNonceCount()); 437 EXPECT_EQ(2, entry_pre->IncrementNonceCount());
411 EXPECT_EQ(3, entry_pre->IncrementNonceCount()); 438 EXPECT_EQ(3, entry_pre->IncrementNonceCount());
412 EXPECT_EQ(4, entry_pre->IncrementNonceCount()); 439 EXPECT_EQ(4, entry_pre->IncrementNonceCount());
413 440
414 bool update_success = cache.UpdateStaleChallenge( 441 bool update_success = cache.UpdateStaleChallenge(
415 origin, 442 origin,
416 digest_handler->realm(), 443 digest_handler->realm(),
417 digest_handler->auth_scheme(), 444 digest_handler->auth_scheme(),
418 "Digest realm=Realm1," 445 "Digest realm=Realm1,"
419 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\"," 446 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\","
420 "stale=\"true\""); 447 "stale=\"true\"");
421 EXPECT_TRUE(update_success); 448 EXPECT_TRUE(update_success);
422 449
423 // After the stale update, the entry should still exist in the cache and 450 // After the stale update, the entry should still exist in the cache and
424 // the nonce count should be reset to 0. 451 // the nonce count should be reset to 0.
425 HttpAuthCache::Entry* entry_post = cache.Lookup( 452 HttpAuthCache::Entry* entry_post = cache.Lookup(
426 origin, 453 origin, digest_handler->realm(), digest_handler->auth_scheme());
427 digest_handler->realm(),
428 digest_handler->auth_scheme());
429 ASSERT_TRUE(entry_post != NULL); 454 ASSERT_TRUE(entry_post != NULL);
430 EXPECT_EQ(2, entry_post->IncrementNonceCount()); 455 EXPECT_EQ(2, entry_post->IncrementNonceCount());
431 456
432 // UpdateStaleChallenge will fail if an entry doesn't exist in the cache. 457 // UpdateStaleChallenge will fail if an entry doesn't exist in the cache.
433 bool update_failure = cache.UpdateStaleChallenge( 458 bool update_failure = cache.UpdateStaleChallenge(
434 origin, 459 origin,
435 kRealm2, 460 kRealm2,
436 digest_handler->auth_scheme(), 461 digest_handler->auth_scheme(),
437 "Digest realm=Realm2," 462 "Digest realm=Realm2,"
438 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\"," 463 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\","
439 "stale=\"true\""); 464 "stale=\"true\"");
440 EXPECT_FALSE(update_failure); 465 EXPECT_FALSE(update_failure);
441 } 466 }
442 467
443 TEST(HttpAuthCacheTest, UpdateAllFrom) { 468 TEST(HttpAuthCacheTest, UpdateAllFrom) {
444 GURL origin("http://example.com"); 469 GURL origin("http://example.com");
445 std::string path("/some/path"); 470 std::string path("/some/path");
446 std::string another_path("/another/path"); 471 std::string another_path("/another/path");
447 472
448 scoped_ptr<HttpAuthHandler> realm1_handler( 473 scoped_ptr<HttpAuthHandler> realm1_handler(new MockAuthHandler(
449 new MockAuthHandler( 474 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER));
450 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER));
451 475
452 scoped_ptr<HttpAuthHandler> realm2_handler( 476 scoped_ptr<HttpAuthHandler> realm2_handler(new MockAuthHandler(
453 new MockAuthHandler( 477 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_PROXY));
454 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_PROXY));
455 478
456 scoped_ptr<HttpAuthHandler> realm3_digest_handler( 479 scoped_ptr<HttpAuthHandler> realm3_digest_handler(new MockAuthHandler(
457 new MockAuthHandler( 480 HttpAuth::AUTH_SCHEME_DIGEST, kRealm3, HttpAuth::AUTH_SERVER));
458 HttpAuth::AUTH_SCHEME_DIGEST, kRealm3, HttpAuth::AUTH_SERVER));
459 481
460 scoped_ptr<HttpAuthHandler> realm4_handler( 482 scoped_ptr<HttpAuthHandler> realm4_handler(new MockAuthHandler(
461 new MockAuthHandler( 483 HttpAuth::AUTH_SCHEME_BASIC, kRealm4, HttpAuth::AUTH_SERVER));
462 HttpAuth::AUTH_SCHEME_BASIC, kRealm4, HttpAuth::AUTH_SERVER));
463 484
464 HttpAuthCache first_cache; 485 HttpAuthCache first_cache;
465 HttpAuthCache::Entry* entry; 486 HttpAuthCache::Entry* entry;
466 487
467 first_cache.Add(origin, realm1_handler->realm(), 488 first_cache.Add(origin,
468 realm1_handler->auth_scheme(), "basic realm=Realm1", 489 realm1_handler->realm(),
469 AuthCredentials(kAlice, k123), path); 490 realm1_handler->auth_scheme(),
470 first_cache.Add(origin, realm2_handler->realm(), 491 "basic realm=Realm1",
471 realm2_handler->auth_scheme(), "basic realm=Realm2", 492 AuthCredentials(kAlice, k123),
472 AuthCredentials(kAlice2, k1234), path); 493 path);
473 first_cache.Add(origin, realm3_digest_handler->realm(), 494 first_cache.Add(origin,
474 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", 495 realm2_handler->realm(),
475 AuthCredentials(kRoot, kWileCoyote), path); 496 realm2_handler->auth_scheme(),
476 entry = first_cache.Add( 497 "basic realm=Realm2",
477 origin, realm3_digest_handler->realm(), 498 AuthCredentials(kAlice2, k1234),
478 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", 499 path);
479 AuthCredentials(kRoot, kWileCoyote), another_path); 500 first_cache.Add(origin,
501 realm3_digest_handler->realm(),
502 realm3_digest_handler->auth_scheme(),
503 "digest realm=Realm3",
504 AuthCredentials(kRoot, kWileCoyote),
505 path);
506 entry = first_cache.Add(origin,
507 realm3_digest_handler->realm(),
508 realm3_digest_handler->auth_scheme(),
509 "digest realm=Realm3",
510 AuthCredentials(kRoot, kWileCoyote),
511 another_path);
480 512
481 EXPECT_EQ(2, entry->IncrementNonceCount()); 513 EXPECT_EQ(2, entry->IncrementNonceCount());
482 514
483 HttpAuthCache second_cache; 515 HttpAuthCache second_cache;
484 // Will be overwritten by kRoot:kWileCoyote. 516 // Will be overwritten by kRoot:kWileCoyote.
485 second_cache.Add(origin, realm3_digest_handler->realm(), 517 second_cache.Add(origin,
486 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", 518 realm3_digest_handler->realm(),
487 AuthCredentials(kAlice2, k1234), path); 519 realm3_digest_handler->auth_scheme(),
520 "digest realm=Realm3",
521 AuthCredentials(kAlice2, k1234),
522 path);
488 // Should be left intact. 523 // Should be left intact.
489 second_cache.Add(origin, realm4_handler->realm(), 524 second_cache.Add(origin,
490 realm4_handler->auth_scheme(), "basic realm=Realm4", 525 realm4_handler->realm(),
491 AuthCredentials(kAdmin, kRoot), path); 526 realm4_handler->auth_scheme(),
527 "basic realm=Realm4",
528 AuthCredentials(kAdmin, kRoot),
529 path);
492 530
493 second_cache.UpdateAllFrom(first_cache); 531 second_cache.UpdateAllFrom(first_cache);
494 532
495 // Copied from first_cache. 533 // Copied from first_cache.
496 entry = second_cache.Lookup(origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC); 534 entry = second_cache.Lookup(origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC);
497 EXPECT_TRUE(NULL != entry); 535 EXPECT_TRUE(NULL != entry);
498 EXPECT_EQ(kAlice, entry->credentials().username()); 536 EXPECT_EQ(kAlice, entry->credentials().username());
499 EXPECT_EQ(k123, entry->credentials().password()); 537 EXPECT_EQ(k123, entry->credentials().password());
500 538
501 // Copied from first_cache. 539 // Copied from first_cache.
(...skipping 20 matching lines...) Expand all
522 entry = second_cache.Lookup(origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC); 560 entry = second_cache.Lookup(origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC);
523 EXPECT_TRUE(NULL != entry); 561 EXPECT_TRUE(NULL != entry);
524 EXPECT_EQ(kAdmin, entry->credentials().username()); 562 EXPECT_EQ(kAdmin, entry->credentials().username());
525 EXPECT_EQ(kRoot, entry->credentials().password()); 563 EXPECT_EQ(kRoot, entry->credentials().password());
526 } 564 }
527 565
528 // Test fixture class for eviction tests (contains helpers for bulk 566 // Test fixture class for eviction tests (contains helpers for bulk
529 // insertion and existence testing). 567 // insertion and existence testing).
530 class HttpAuthCacheEvictionTest : public testing::Test { 568 class HttpAuthCacheEvictionTest : public testing::Test {
531 protected: 569 protected:
532 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") { } 570 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") {}
533 571
534 std::string GenerateRealm(int realm_i) { 572 std::string GenerateRealm(int realm_i) {
535 return base::StringPrintf("Realm %d", realm_i); 573 return base::StringPrintf("Realm %d", realm_i);
536 } 574 }
537 575
538 std::string GeneratePath(int realm_i, int path_i) { 576 std::string GeneratePath(int realm_i, int path_i) {
539 return base::StringPrintf("/%d/%d/x/y", realm_i, path_i); 577 return base::StringPrintf("/%d/%d/x/y", realm_i, path_i);
540 } 578 }
541 579
542 void AddRealm(int realm_i) { 580 void AddRealm(int realm_i) { AddPathToRealm(realm_i, 0); }
543 AddPathToRealm(realm_i, 0);
544 }
545 581
546 void AddPathToRealm(int realm_i, int path_i) { 582 void AddPathToRealm(int realm_i, int path_i) {
547 cache_.Add(origin_, 583 cache_.Add(origin_,
548 GenerateRealm(realm_i), 584 GenerateRealm(realm_i),
549 HttpAuth::AUTH_SCHEME_BASIC, 585 HttpAuth::AUTH_SCHEME_BASIC,
550 std::string(), 586 std::string(),
551 AuthCredentials(kUsername, kPassword), 587 AuthCredentials(kUsername, kPassword),
552 GeneratePath(realm_i, path_i)); 588 GeneratePath(realm_i, path_i));
553 } 589 }
554 590
555 void CheckRealmExistence(int realm_i, bool exists) { 591 void CheckRealmExistence(int realm_i, bool exists) {
556 const HttpAuthCache::Entry* entry = 592 const HttpAuthCache::Entry* entry = cache_.Lookup(
557 cache_.Lookup( 593 origin_, GenerateRealm(realm_i), HttpAuth::AUTH_SCHEME_BASIC);
558 origin_, GenerateRealm(realm_i), HttpAuth::AUTH_SCHEME_BASIC);
559 if (exists) { 594 if (exists) {
560 EXPECT_FALSE(entry == NULL); 595 EXPECT_FALSE(entry == NULL);
561 EXPECT_EQ(GenerateRealm(realm_i), entry->realm()); 596 EXPECT_EQ(GenerateRealm(realm_i), entry->realm());
562 } else { 597 } else {
563 EXPECT_TRUE(entry == NULL); 598 EXPECT_TRUE(entry == NULL);
564 } 599 }
565 } 600 }
566 601
567 void CheckPathExistence(int realm_i, int path_i, bool exists) { 602 void CheckPathExistence(int realm_i, int path_i, bool exists) {
568 const HttpAuthCache::Entry* entry = 603 const HttpAuthCache::Entry* entry =
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 CheckPathExistence(0, i, false); 654 CheckPathExistence(0, i, false);
620 655
621 for (int i = 0; i < kMaxPaths; ++i) 656 for (int i = 0; i < kMaxPaths; ++i)
622 CheckPathExistence(0, i + 3, true); 657 CheckPathExistence(0, i + 3, true);
623 658
624 for (int i = 0; i < kMaxRealms; ++i) 659 for (int i = 0; i < kMaxRealms; ++i)
625 CheckRealmExistence(i, true); 660 CheckRealmExistence(i, true);
626 } 661 }
627 662
628 } // namespace net 663 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698