| OLD | NEW |
| 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" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/http/http_auth_cache.h" | 12 #include "net/http/http_auth_cache.h" |
| 13 #include "net/http/http_auth_handler.h" | 13 #include "net/http/http_auth_handler.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class MockAuthHandler : public HttpAuthHandler { | 20 class MockAuthHandler : public HttpAuthHandler { |
| 21 public: | 21 public: |
| 22 MockAuthHandler(const char* scheme, const std::string& realm, | 22 MockAuthHandler(HttpAuth::Scheme scheme, |
| 23 const std::string& realm, |
| 23 HttpAuth::Target target) { | 24 HttpAuth::Target target) { |
| 24 // Can't use initializer list since these are members of the base class. | 25 // Can't use initializer list since these are members of the base class. |
| 25 scheme_ = scheme; | 26 auth_scheme_ = scheme; |
| 26 realm_ = realm; | 27 realm_ = realm; |
| 27 score_ = 1; | 28 score_ = 1; |
| 28 target_ = target; | 29 target_ = target; |
| 29 properties_ = 0; | 30 properties_ = 0; |
| 30 } | 31 } |
| 31 | 32 |
| 32 virtual HttpAuth::AuthorizationResult HandleAnotherChallenge( | 33 virtual HttpAuth::AuthorizationResult HandleAnotherChallenge( |
| 33 HttpAuth::ChallengeTokenizer* challenge) { | 34 HttpAuth::ChallengeTokenizer* challenge) { |
| 34 return HttpAuth::AUTHORIZATION_RESULT_REJECT; | 35 return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
| 35 } | 36 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 46 std::string* auth_token) { | 47 std::string* auth_token) { |
| 47 *auth_token = "mock-credentials"; | 48 *auth_token = "mock-credentials"; |
| 48 return OK; | 49 return OK; |
| 49 } | 50 } |
| 50 | 51 |
| 51 | 52 |
| 52 private: | 53 private: |
| 53 ~MockAuthHandler() {} | 54 ~MockAuthHandler() {} |
| 54 }; | 55 }; |
| 55 | 56 |
| 56 const char* kBasic = "basic"; | |
| 57 const char* kDigest = "digest"; | |
| 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 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 |
| 72 } // namespace | 71 } // namespace |
| 73 | 72 |
| 74 // Test adding and looking-up cache entries (both by realm and by path). | 73 // Test adding and looking-up cache entries (both by realm and by path). |
| 75 TEST(HttpAuthCacheTest, Basic) { | 74 TEST(HttpAuthCacheTest, Basic) { |
| 76 GURL origin("http://www.google.com"); | 75 GURL origin("http://www.google.com"); |
| 77 HttpAuthCache cache; | 76 HttpAuthCache cache; |
| 78 HttpAuthCache::Entry* entry; | 77 HttpAuthCache::Entry* entry; |
| 79 | 78 |
| 80 // Add cache entries for 3 realms: "Realm1", "Realm2", "Realm3" | 79 // Add cache entries for 3 realms: "Realm1", "Realm2", "Realm3" |
| 81 | 80 |
| 82 scoped_ptr<HttpAuthHandler> realm1_handler( | 81 scoped_ptr<HttpAuthHandler> realm1_handler( |
| 83 new MockAuthHandler(kBasic, kRealm1, HttpAuth::AUTH_SERVER)); | 82 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, |
| 84 cache.Add(origin, realm1_handler->realm(), realm1_handler->scheme(), | 83 kRealm1, |
| 84 HttpAuth::AUTH_SERVER)); |
| 85 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(), |
| 85 "Basic realm=Realm1", ASCIIToUTF16("realm1-user"), | 86 "Basic realm=Realm1", ASCIIToUTF16("realm1-user"), |
| 86 ASCIIToUTF16("realm1-password"), "/foo/bar/index.html"); | 87 ASCIIToUTF16("realm1-password"), "/foo/bar/index.html"); |
| 87 | 88 |
| 88 scoped_ptr<HttpAuthHandler> realm2_handler( | 89 scoped_ptr<HttpAuthHandler> realm2_handler( |
| 89 new MockAuthHandler(kBasic, kRealm2, HttpAuth::AUTH_SERVER)); | 90 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, |
| 90 cache.Add(origin, realm2_handler->realm(), realm2_handler->scheme(), | 91 kRealm2, |
| 92 HttpAuth::AUTH_SERVER)); |
| 93 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(), |
| 91 "Basic realm=Realm2", ASCIIToUTF16("realm2-user"), | 94 "Basic realm=Realm2", ASCIIToUTF16("realm2-user"), |
| 92 ASCIIToUTF16("realm2-password"), "/foo2/index.html"); | 95 ASCIIToUTF16("realm2-password"), "/foo2/index.html"); |
| 93 | 96 |
| 94 scoped_ptr<HttpAuthHandler> realm3_basic_handler( | 97 scoped_ptr<HttpAuthHandler> realm3_basic_handler( |
| 95 new MockAuthHandler(kBasic, kRealm3, HttpAuth::AUTH_PROXY)); | 98 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, |
| 99 kRealm3, |
| 100 HttpAuth::AUTH_PROXY)); |
| 96 cache.Add(origin, realm3_basic_handler->realm(), | 101 cache.Add(origin, realm3_basic_handler->realm(), |
| 97 realm3_basic_handler->scheme(), "Basic realm=Realm3", | 102 realm3_basic_handler->auth_scheme(), "Basic realm=Realm3", |
| 98 ASCIIToUTF16("realm3-basic-user"), | 103 ASCIIToUTF16("realm3-basic-user"), |
| 99 ASCIIToUTF16("realm3-basic-password"), ""); | 104 ASCIIToUTF16("realm3-basic-password"), ""); |
| 100 | 105 |
| 101 scoped_ptr<HttpAuthHandler> realm3_digest_handler( | 106 scoped_ptr<HttpAuthHandler> realm3_digest_handler( |
| 102 new MockAuthHandler(kDigest, kRealm3, HttpAuth::AUTH_PROXY)); | 107 new MockAuthHandler(HttpAuth::AUTH_SCHEME_DIGEST, |
| 108 kRealm3, |
| 109 HttpAuth::AUTH_PROXY)); |
| 103 cache.Add(origin, realm3_digest_handler->realm(), | 110 cache.Add(origin, realm3_digest_handler->realm(), |
| 104 realm3_digest_handler->scheme(), "Digest realm=Realm3", | 111 realm3_digest_handler->auth_scheme(), "Digest realm=Realm3", |
| 105 ASCIIToUTF16("realm3-digest-user"), | 112 ASCIIToUTF16("realm3-digest-user"), |
| 106 ASCIIToUTF16("realm3-digest-password"), "/baz/index.html"); | 113 ASCIIToUTF16("realm3-digest-password"), "/baz/index.html"); |
| 107 | 114 |
| 108 // There is no Realm4 | 115 // There is no Realm4 |
| 109 entry = cache.Lookup(origin, kRealm4, kBasic); | 116 entry = cache.Lookup(origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC); |
| 110 EXPECT_TRUE(NULL == entry); | 117 EXPECT_TRUE(NULL == entry); |
| 111 | 118 |
| 112 // While Realm3 does exist, the origin scheme is wrong. | 119 // While Realm3 does exist, the origin scheme is wrong. |
| 113 entry = cache.Lookup(GURL("https://www.google.com"), kRealm3, | 120 entry = cache.Lookup(GURL("https://www.google.com"), kRealm3, |
| 114 kBasic); | 121 HttpAuth::AUTH_SCHEME_BASIC); |
| 115 EXPECT_TRUE(NULL == entry); | 122 EXPECT_TRUE(NULL == entry); |
| 116 | 123 |
| 117 // Realm, origin scheme ok, authentication scheme wrong | 124 // Realm, origin scheme ok, authentication scheme wrong |
| 118 entry = cache.Lookup(GURL("http://www.google.com"), kRealm1, kDigest); | 125 entry = cache.Lookup |
| 126 (GURL("http://www.google.com"), kRealm1, HttpAuth::AUTH_SCHEME_DIGEST); |
| 119 EXPECT_TRUE(NULL == entry); | 127 EXPECT_TRUE(NULL == entry); |
| 120 | 128 |
| 121 // Valid lookup by origin, realm, scheme. | 129 // Valid lookup by origin, realm, scheme. |
| 122 entry = cache.Lookup(GURL("http://www.google.com:80"), kRealm3, kBasic); | 130 entry = cache.Lookup( |
| 131 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_BASIC); |
| 123 ASSERT_FALSE(NULL == entry); | 132 ASSERT_FALSE(NULL == entry); |
| 124 EXPECT_EQ(kBasic, entry->scheme()); | 133 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme()); |
| 125 EXPECT_EQ(kRealm3, entry->realm()); | 134 EXPECT_EQ(kRealm3, entry->realm()); |
| 126 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge()); | 135 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge()); |
| 127 EXPECT_EQ(ASCIIToUTF16("realm3-basic-user"), entry->username()); | 136 EXPECT_EQ(ASCIIToUTF16("realm3-basic-user"), entry->username()); |
| 128 EXPECT_EQ(ASCIIToUTF16("realm3-basic-password"), entry->password()); | 137 EXPECT_EQ(ASCIIToUTF16("realm3-basic-password"), entry->password()); |
| 129 | 138 |
| 130 // Valid lookup by origin, realm, scheme when there's a duplicate | 139 // Valid lookup by origin, realm, scheme when there's a duplicate |
| 131 // origin, realm in the cache | 140 // origin, realm in the cache |
| 132 entry = cache.Lookup(GURL("http://www.google.com:80"), kRealm3, kDigest); | 141 entry = cache.Lookup( |
| 142 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); |
| 133 ASSERT_FALSE(NULL == entry); | 143 ASSERT_FALSE(NULL == entry); |
| 134 EXPECT_EQ(kDigest, entry->scheme()); | 144 EXPECT_EQ(HttpAuth::AUTH_SCHEME_DIGEST, entry->scheme()); |
| 135 EXPECT_EQ(kRealm3, entry->realm()); | 145 EXPECT_EQ(kRealm3, entry->realm()); |
| 136 EXPECT_EQ("Digest realm=Realm3", entry->auth_challenge()); | 146 EXPECT_EQ("Digest realm=Realm3", entry->auth_challenge()); |
| 137 EXPECT_EQ(ASCIIToUTF16("realm3-digest-user"), entry->username()); | 147 EXPECT_EQ(ASCIIToUTF16("realm3-digest-user"), entry->username()); |
| 138 EXPECT_EQ(ASCIIToUTF16("realm3-digest-password"), entry->password()); | 148 EXPECT_EQ(ASCIIToUTF16("realm3-digest-password"), entry->password()); |
| 139 | 149 |
| 140 // Valid lookup by realm. | 150 // Valid lookup by realm. |
| 141 entry = cache.Lookup(origin, kRealm2, kBasic); | 151 entry = cache.Lookup(origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC); |
| 142 ASSERT_FALSE(NULL == entry); | 152 ASSERT_FALSE(NULL == entry); |
| 143 EXPECT_EQ(kBasic, entry->scheme()); | 153 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme()); |
| 144 EXPECT_EQ(kRealm2, entry->realm()); | 154 EXPECT_EQ(kRealm2, entry->realm()); |
| 145 EXPECT_EQ("Basic realm=Realm2", entry->auth_challenge()); | 155 EXPECT_EQ("Basic realm=Realm2", entry->auth_challenge()); |
| 146 EXPECT_EQ(ASCIIToUTF16("realm2-user"), entry->username()); | 156 EXPECT_EQ(ASCIIToUTF16("realm2-user"), entry->username()); |
| 147 EXPECT_EQ(ASCIIToUTF16("realm2-password"), entry->password()); | 157 EXPECT_EQ(ASCIIToUTF16("realm2-password"), entry->password()); |
| 148 | 158 |
| 149 // Check that subpaths are recognized. | 159 // Check that subpaths are recognized. |
| 150 HttpAuthCache::Entry* realm2_entry = cache.Lookup(origin, kRealm2, kBasic); | 160 HttpAuthCache::Entry* realm2_entry = cache.Lookup( |
| 161 origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC); |
| 151 EXPECT_FALSE(NULL == realm2_entry); | 162 EXPECT_FALSE(NULL == realm2_entry); |
| 152 // Positive tests: | 163 // Positive tests: |
| 153 entry = cache.LookupByPath(origin, "/foo2/index.html"); | 164 entry = cache.LookupByPath(origin, "/foo2/index.html"); |
| 154 EXPECT_TRUE(realm2_entry == entry); | 165 EXPECT_TRUE(realm2_entry == entry); |
| 155 entry = cache.LookupByPath(origin, "/foo2/foobar.html"); | 166 entry = cache.LookupByPath(origin, "/foo2/foobar.html"); |
| 156 EXPECT_TRUE(realm2_entry == entry); | 167 EXPECT_TRUE(realm2_entry == entry); |
| 157 entry = cache.LookupByPath(origin, "/foo2/bar/index.html"); | 168 entry = cache.LookupByPath(origin, "/foo2/bar/index.html"); |
| 158 EXPECT_TRUE(realm2_entry == entry); | 169 EXPECT_TRUE(realm2_entry == entry); |
| 159 entry = cache.LookupByPath(origin, "/foo2/"); | 170 entry = cache.LookupByPath(origin, "/foo2/"); |
| 160 EXPECT_TRUE(realm2_entry == entry); | 171 EXPECT_TRUE(realm2_entry == entry); |
| 161 | 172 |
| 162 // Negative tests: | 173 // Negative tests: |
| 163 entry = cache.LookupByPath(origin, "/foo2"); | 174 entry = cache.LookupByPath(origin, "/foo2"); |
| 164 EXPECT_FALSE(realm2_entry == entry); | 175 EXPECT_FALSE(realm2_entry == entry); |
| 165 entry = cache.LookupByPath(origin, "/foo3/index.html"); | 176 entry = cache.LookupByPath(origin, "/foo3/index.html"); |
| 166 EXPECT_FALSE(realm2_entry == entry); | 177 EXPECT_FALSE(realm2_entry == entry); |
| 167 entry = cache.LookupByPath(origin, ""); | 178 entry = cache.LookupByPath(origin, ""); |
| 168 EXPECT_FALSE(realm2_entry == entry); | 179 EXPECT_FALSE(realm2_entry == entry); |
| 169 entry = cache.LookupByPath(origin, "/"); | 180 entry = cache.LookupByPath(origin, "/"); |
| 170 EXPECT_FALSE(realm2_entry == entry); | 181 EXPECT_FALSE(realm2_entry == entry); |
| 171 | 182 |
| 172 // Confirm we find the same realm, different auth scheme by path lookup | 183 // Confirm we find the same realm, different auth scheme by path lookup |
| 173 HttpAuthCache::Entry* realm3_digest_entry = | 184 HttpAuthCache::Entry* realm3_digest_entry = |
| 174 cache.Lookup(origin, kRealm3, kDigest); | 185 cache.Lookup(origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); |
| 175 EXPECT_FALSE(NULL == realm3_digest_entry); | 186 EXPECT_FALSE(NULL == realm3_digest_entry); |
| 176 entry = cache.LookupByPath(origin, "/baz/index.html"); | 187 entry = cache.LookupByPath(origin, "/baz/index.html"); |
| 177 EXPECT_TRUE(realm3_digest_entry == entry); | 188 EXPECT_TRUE(realm3_digest_entry == entry); |
| 178 entry = cache.LookupByPath(origin, "/baz/"); | 189 entry = cache.LookupByPath(origin, "/baz/"); |
| 179 EXPECT_TRUE(realm3_digest_entry == entry); | 190 EXPECT_TRUE(realm3_digest_entry == entry); |
| 180 entry = cache.LookupByPath(origin, "/baz"); | 191 entry = cache.LookupByPath(origin, "/baz"); |
| 181 EXPECT_FALSE(realm3_digest_entry == entry); | 192 EXPECT_FALSE(realm3_digest_entry == entry); |
| 182 | 193 |
| 183 // Confirm we find the same realm, different auth scheme by path lookup | 194 // Confirm we find the same realm, different auth scheme by path lookup |
| 184 HttpAuthCache::Entry* realm3DigestEntry = | 195 HttpAuthCache::Entry* realm3DigestEntry = |
| 185 cache.Lookup(origin, kRealm3, kDigest); | 196 cache.Lookup(origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); |
| 186 EXPECT_FALSE(NULL == realm3DigestEntry); | 197 EXPECT_FALSE(NULL == realm3DigestEntry); |
| 187 entry = cache.LookupByPath(origin, "/baz/index.html"); | 198 entry = cache.LookupByPath(origin, "/baz/index.html"); |
| 188 EXPECT_TRUE(realm3DigestEntry == entry); | 199 EXPECT_TRUE(realm3DigestEntry == entry); |
| 189 entry = cache.LookupByPath(origin, "/baz/"); | 200 entry = cache.LookupByPath(origin, "/baz/"); |
| 190 EXPECT_TRUE(realm3DigestEntry == entry); | 201 EXPECT_TRUE(realm3DigestEntry == entry); |
| 191 entry = cache.LookupByPath(origin, "/baz"); | 202 entry = cache.LookupByPath(origin, "/baz"); |
| 192 EXPECT_FALSE(realm3DigestEntry == entry); | 203 EXPECT_FALSE(realm3DigestEntry == entry); |
| 193 | 204 |
| 194 // Lookup using empty path (may be used for proxy). | 205 // Lookup using empty path (may be used for proxy). |
| 195 entry = cache.LookupByPath(origin, ""); | 206 entry = cache.LookupByPath(origin, ""); |
| 196 EXPECT_FALSE(NULL == entry); | 207 EXPECT_FALSE(NULL == entry); |
| 197 EXPECT_EQ(kBasic, entry->scheme()); | 208 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme()); |
| 198 EXPECT_EQ(kRealm3, entry->realm()); | 209 EXPECT_EQ(kRealm3, entry->realm()); |
| 199 } | 210 } |
| 200 | 211 |
| 201 TEST(HttpAuthCacheTest, AddPath) { | 212 TEST(HttpAuthCacheTest, AddPath) { |
| 202 HttpAuthCache::Entry entry; | 213 HttpAuthCache::Entry entry; |
| 203 | 214 |
| 204 // All of these paths have a common root /1/2/2/4/5/ | 215 // All of these paths have a common root /1/2/2/4/5/ |
| 205 entry.AddPath("/1/2/3/4/5/x.txt"); | 216 entry.AddPath("/1/2/3/4/5/x.txt"); |
| 206 entry.AddPath("/1/2/3/4/5/y.txt"); | 217 entry.AddPath("/1/2/3/4/5/y.txt"); |
| 207 entry.AddPath("/1/2/3/4/5/z.txt"); | 218 entry.AddPath("/1/2/3/4/5/z.txt"); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 231 } | 242 } |
| 232 | 243 |
| 233 // Calling Add when the realm entry already exists, should append that | 244 // Calling Add when the realm entry already exists, should append that |
| 234 // path. | 245 // path. |
| 235 TEST(HttpAuthCacheTest, AddToExistingEntry) { | 246 TEST(HttpAuthCacheTest, AddToExistingEntry) { |
| 236 HttpAuthCache cache; | 247 HttpAuthCache cache; |
| 237 GURL origin("http://www.foobar.com:70"); | 248 GURL origin("http://www.foobar.com:70"); |
| 238 const std::string auth_challenge = "Basic realm=MyRealm"; | 249 const std::string auth_challenge = "Basic realm=MyRealm"; |
| 239 | 250 |
| 240 scoped_ptr<HttpAuthHandler> handler( | 251 scoped_ptr<HttpAuthHandler> handler( |
| 241 new MockAuthHandler(kBasic, "MyRealm", HttpAuth::AUTH_SERVER)); | 252 new MockAuthHandler( |
| 242 | 253 HttpAuth::AUTH_SCHEME_BASIC, "MyRealm", HttpAuth::AUTH_SERVER)); |
| 243 HttpAuthCache::Entry* orig_entry = cache.Add( | 254 HttpAuthCache::Entry* orig_entry = cache.Add( |
| 244 origin, handler->realm(), handler->scheme(), auth_challenge, | 255 origin, handler->realm(), handler->auth_scheme(), auth_challenge, |
| 245 ASCIIToUTF16("user1"), ASCIIToUTF16("password1"), "/x/y/z/"); | 256 ASCIIToUTF16("user1"), ASCIIToUTF16("password1"), "/x/y/z/"); |
| 246 cache.Add(origin, handler->realm(), handler->scheme(), auth_challenge, | 257 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, |
| 247 ASCIIToUTF16("user2"), ASCIIToUTF16("password2"), "/z/y/x/"); | 258 ASCIIToUTF16("user2"), ASCIIToUTF16("password2"), "/z/y/x/"); |
| 248 cache.Add(origin, handler->realm(), handler->scheme(), auth_challenge, | 259 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, |
| 249 ASCIIToUTF16("user3"), ASCIIToUTF16("password3"), "/z/y"); | 260 ASCIIToUTF16("user3"), ASCIIToUTF16("password3"), "/z/y"); |
| 250 | 261 |
| 251 HttpAuthCache::Entry* entry = cache.Lookup(origin, "MyRealm", kBasic); | 262 HttpAuthCache::Entry* entry = cache.Lookup( |
| 263 origin, "MyRealm", HttpAuth::AUTH_SCHEME_BASIC); |
| 252 | 264 |
| 253 EXPECT_TRUE(entry == orig_entry); | 265 EXPECT_TRUE(entry == orig_entry); |
| 254 EXPECT_EQ(ASCIIToUTF16("user3"), entry->username()); | 266 EXPECT_EQ(ASCIIToUTF16("user3"), entry->username()); |
| 255 EXPECT_EQ(ASCIIToUTF16("password3"), entry->password()); | 267 EXPECT_EQ(ASCIIToUTF16("password3"), entry->password()); |
| 256 | 268 |
| 257 EXPECT_EQ(2U, entry->paths_.size()); | 269 EXPECT_EQ(2U, entry->paths_.size()); |
| 258 EXPECT_EQ("/z/", entry->paths_.front()); | 270 EXPECT_EQ("/z/", entry->paths_.front()); |
| 259 EXPECT_EQ("/x/y/z/", entry->paths_.back()); | 271 EXPECT_EQ("/x/y/z/", entry->paths_.back()); |
| 260 } | 272 } |
| 261 | 273 |
| 262 TEST(HttpAuthCacheTest, Remove) { | 274 TEST(HttpAuthCacheTest, Remove) { |
| 263 GURL origin("http://foobar2.com"); | 275 GURL origin("http://foobar2.com"); |
| 264 | 276 |
| 265 scoped_ptr<HttpAuthHandler> realm1_handler( | 277 scoped_ptr<HttpAuthHandler> realm1_handler( |
| 266 new MockAuthHandler(kBasic, kRealm1, HttpAuth::AUTH_SERVER)); | 278 new MockAuthHandler( |
| 279 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER)); |
| 267 | 280 |
| 268 scoped_ptr<HttpAuthHandler> realm2_handler( | 281 scoped_ptr<HttpAuthHandler> realm2_handler( |
| 269 new MockAuthHandler(kBasic, kRealm2, HttpAuth::AUTH_SERVER)); | 282 new MockAuthHandler( |
| 283 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_SERVER)); |
| 270 | 284 |
| 271 scoped_ptr<HttpAuthHandler> realm3_basic_handler( | 285 scoped_ptr<HttpAuthHandler> realm3_basic_handler( |
| 272 new MockAuthHandler(kBasic, kRealm3, HttpAuth::AUTH_SERVER)); | 286 new MockAuthHandler( |
| 287 HttpAuth::AUTH_SCHEME_BASIC, kRealm3, HttpAuth::AUTH_SERVER)); |
| 273 | 288 |
| 274 scoped_ptr<HttpAuthHandler> realm3_digest_handler( | 289 scoped_ptr<HttpAuthHandler> realm3_digest_handler( |
| 275 new MockAuthHandler(kDigest, kRealm3, HttpAuth::AUTH_SERVER)); | 290 new MockAuthHandler( |
| 291 HttpAuth::AUTH_SCHEME_DIGEST, kRealm3, HttpAuth::AUTH_SERVER)); |
| 276 | 292 |
| 277 HttpAuthCache cache; | 293 HttpAuthCache cache; |
| 278 cache.Add(origin, realm1_handler->realm(), realm1_handler->scheme(), | 294 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(), |
| 279 "basic realm=Realm1", kAlice, k123, "/"); | 295 "basic realm=Realm1", kAlice, k123, "/"); |
| 280 cache.Add(origin, realm2_handler->realm(), realm2_handler->scheme(), | 296 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(), |
| 281 "basic realm=Realm2", ASCIIToUTF16("bob"), ASCIIToUTF16("princess"), | 297 "basic realm=Realm2", ASCIIToUTF16("bob"), ASCIIToUTF16("princess"), |
| 282 "/"); | 298 "/"); |
| 283 cache.Add(origin, realm3_basic_handler->realm(), | 299 cache.Add(origin, realm3_basic_handler->realm(), |
| 284 realm3_basic_handler->scheme(), "basic realm=Realm3", | 300 realm3_basic_handler->auth_scheme(), "basic realm=Realm3", |
| 285 kAdmin, kPassword, "/"); | 301 kAdmin, kPassword, "/"); |
| 286 cache.Add(origin, realm3_digest_handler->realm(), | 302 cache.Add(origin, realm3_digest_handler->realm(), |
| 287 realm3_digest_handler->scheme(), "digest realm=Realm3", | 303 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", |
| 288 kRoot, kWileCoyote, "/"); | 304 kRoot, kWileCoyote, "/"); |
| 289 | 305 |
| 290 // Fails, because there is no realm "Realm4". | 306 // Fails, because there is no realm "Realm4". |
| 291 EXPECT_FALSE(cache.Remove(origin, kRealm4, kBasic, kAlice, k123)); | 307 EXPECT_FALSE(cache.Remove( |
| 308 origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC, kAlice, k123)); |
| 292 | 309 |
| 293 // Fails because the origin is wrong. | 310 // Fails because the origin is wrong. |
| 294 EXPECT_FALSE(cache.Remove( | 311 EXPECT_FALSE(cache.Remove(GURL("http://foobar2.com:100"), |
| 295 GURL("http://foobar2.com:100"), kRealm1, kBasic, kAlice, k123)); | 312 kRealm1, |
| 313 HttpAuth::AUTH_SCHEME_BASIC, |
| 314 kAlice, |
| 315 k123)); |
| 296 | 316 |
| 297 // Fails because the username is wrong. | 317 // Fails because the username is wrong. |
| 298 EXPECT_FALSE(cache.Remove(origin, kRealm1, kBasic, kAlice2, k123)); | 318 EXPECT_FALSE(cache.Remove( |
| 319 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, kAlice2, k123)); |
| 299 | 320 |
| 300 // Fails because the password is wrong. | 321 // Fails because the password is wrong. |
| 301 EXPECT_FALSE(cache.Remove(origin, kRealm1, kBasic, kAlice, k1234)); | 322 EXPECT_FALSE(cache.Remove( |
| 323 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, kAlice, k1234)); |
| 302 | 324 |
| 303 // Fails because the authentication type is wrong. | 325 // Fails because the authentication type is wrong. |
| 304 EXPECT_FALSE(cache.Remove(origin, kRealm1, kDigest, kAlice, k123)); | 326 EXPECT_FALSE(cache.Remove( |
| 327 origin, kRealm1, HttpAuth::AUTH_SCHEME_DIGEST, kAlice, k123)); |
| 305 | 328 |
| 306 // Succeeds. | 329 // Succeeds. |
| 307 EXPECT_TRUE(cache.Remove(origin, kRealm1, kBasic, kAlice, k123)); | 330 EXPECT_TRUE(cache.Remove( |
| 331 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, kAlice, k123)); |
| 308 | 332 |
| 309 // Fails because we just deleted the entry! | 333 // Fails because we just deleted the entry! |
| 310 EXPECT_FALSE(cache.Remove(origin, kRealm1, kBasic, kAlice, k123)); | 334 EXPECT_FALSE(cache.Remove( |
| 335 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, kAlice, k123)); |
| 311 | 336 |
| 312 // Succeed when there are two authentication types for the same origin,realm. | 337 // Succeed when there are two authentication types for the same origin,realm. |
| 313 EXPECT_TRUE(cache.Remove(origin, kRealm3, kDigest, kRoot, kWileCoyote)); | 338 EXPECT_TRUE(cache.Remove( |
| 339 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST, kRoot, kWileCoyote)); |
| 314 | 340 |
| 315 // Succeed as above, but when entries were added in opposite order | 341 // Succeed as above, but when entries were added in opposite order |
| 316 cache.Add(origin, realm3_digest_handler->realm(), | 342 cache.Add(origin, realm3_digest_handler->realm(), |
| 317 realm3_digest_handler->scheme(), "digest realm=Realm3", | 343 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", |
| 318 kRoot, kWileCoyote, "/"); | 344 kRoot, kWileCoyote, "/"); |
| 319 EXPECT_TRUE(cache.Remove(origin, kRealm3, kBasic, kAdmin, kPassword)); | 345 EXPECT_TRUE(cache.Remove( |
| 346 origin, kRealm3, HttpAuth::AUTH_SCHEME_BASIC, kAdmin, kPassword)); |
| 320 | 347 |
| 321 // Make sure that removing one entry still leaves the other available for | 348 // Make sure that removing one entry still leaves the other available for |
| 322 // lookup. | 349 // lookup. |
| 323 HttpAuthCache::Entry* entry = cache.Lookup(origin, kRealm3, kDigest); | 350 HttpAuthCache::Entry* entry = cache.Lookup( |
| 351 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); |
| 324 EXPECT_FALSE(NULL == entry); | 352 EXPECT_FALSE(NULL == entry); |
| 325 } | 353 } |
| 326 | 354 |
| 327 TEST(HttpAuthCacheTest, UpdateStaleChallenge) { | 355 TEST(HttpAuthCacheTest, UpdateStaleChallenge) { |
| 328 HttpAuthCache cache; | 356 HttpAuthCache cache; |
| 329 GURL origin("http://foobar2.com"); | 357 GURL origin("http://foobar2.com"); |
| 330 scoped_ptr<HttpAuthHandler> digest_handler( | 358 scoped_ptr<HttpAuthHandler> digest_handler( |
| 331 new MockAuthHandler(kDigest, kRealm1, HttpAuth::AUTH_PROXY)); | 359 new MockAuthHandler( |
| 360 HttpAuth::AUTH_SCHEME_DIGEST, kRealm1, HttpAuth::AUTH_PROXY)); |
| 332 HttpAuthCache::Entry* entry_pre = cache.Add( | 361 HttpAuthCache::Entry* entry_pre = cache.Add( |
| 333 origin, | 362 origin, |
| 334 digest_handler->realm(), | 363 digest_handler->realm(), |
| 335 digest_handler->scheme(), | 364 digest_handler->auth_scheme(), |
| 336 "Digest realm=Realm1," | 365 "Digest realm=Realm1," |
| 337 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\"", | 366 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\"", |
| 338 ASCIIToUTF16("realm-digest-user"), | 367 ASCIIToUTF16("realm-digest-user"), |
| 339 ASCIIToUTF16("realm-digest-password"), | 368 ASCIIToUTF16("realm-digest-password"), |
| 340 "/baz/index.html"); | 369 "/baz/index.html"); |
| 341 ASSERT_TRUE(entry_pre != NULL); | 370 ASSERT_TRUE(entry_pre != NULL); |
| 342 | 371 |
| 343 EXPECT_EQ(2, entry_pre->IncrementNonceCount()); | 372 EXPECT_EQ(2, entry_pre->IncrementNonceCount()); |
| 344 EXPECT_EQ(3, entry_pre->IncrementNonceCount()); | 373 EXPECT_EQ(3, entry_pre->IncrementNonceCount()); |
| 345 EXPECT_EQ(4, entry_pre->IncrementNonceCount()); | 374 EXPECT_EQ(4, entry_pre->IncrementNonceCount()); |
| 346 | 375 |
| 347 bool update_success = cache.UpdateStaleChallenge( | 376 bool update_success = cache.UpdateStaleChallenge( |
| 348 origin, | 377 origin, |
| 349 digest_handler->realm(), | 378 digest_handler->realm(), |
| 350 digest_handler->scheme(), | 379 digest_handler->auth_scheme(), |
| 351 "Digest realm=Realm1," | 380 "Digest realm=Realm1," |
| 352 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\"," | 381 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\"," |
| 353 "stale=\"true\""); | 382 "stale=\"true\""); |
| 354 EXPECT_TRUE(update_success); | 383 EXPECT_TRUE(update_success); |
| 355 | 384 |
| 356 // After the stale update, the entry should still exist in the cache and | 385 // After the stale update, the entry should still exist in the cache and |
| 357 // the nonce count should be reset to 0. | 386 // the nonce count should be reset to 0. |
| 358 HttpAuthCache::Entry* entry_post = cache.Lookup( | 387 HttpAuthCache::Entry* entry_post = cache.Lookup( |
| 359 origin, | 388 origin, |
| 360 digest_handler->realm(), | 389 digest_handler->realm(), |
| 361 digest_handler->scheme()); | 390 digest_handler->auth_scheme()); |
| 362 ASSERT_TRUE(entry_post != NULL); | 391 ASSERT_TRUE(entry_post != NULL); |
| 363 EXPECT_EQ(2, entry_post->IncrementNonceCount()); | 392 EXPECT_EQ(2, entry_post->IncrementNonceCount()); |
| 364 | 393 |
| 365 // UpdateStaleChallenge will fail if an entry doesn't exist in the cache. | 394 // UpdateStaleChallenge will fail if an entry doesn't exist in the cache. |
| 366 bool update_failure = cache.UpdateStaleChallenge( | 395 bool update_failure = cache.UpdateStaleChallenge( |
| 367 origin, | 396 origin, |
| 368 kRealm2, | 397 kRealm2, |
| 369 digest_handler->scheme(), | 398 digest_handler->auth_scheme(), |
| 370 "Digest realm=Realm2," | 399 "Digest realm=Realm2," |
| 371 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\"," | 400 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\"," |
| 372 "stale=\"true\""); | 401 "stale=\"true\""); |
| 373 EXPECT_FALSE(update_failure); | 402 EXPECT_FALSE(update_failure); |
| 374 } | 403 } |
| 375 | 404 |
| 376 // Test fixture class for eviction tests (contains helpers for bulk | 405 // Test fixture class for eviction tests (contains helpers for bulk |
| 377 // insertion and existence testing). | 406 // insertion and existence testing). |
| 378 class HttpAuthCacheEvictionTest : public testing::Test { | 407 class HttpAuthCacheEvictionTest : public testing::Test { |
| 379 protected: | 408 protected: |
| 380 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") { } | 409 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") { } |
| 381 | 410 |
| 382 std::string GenerateRealm(int realm_i) { | 411 std::string GenerateRealm(int realm_i) { |
| 383 return base::StringPrintf("Realm %d", realm_i); | 412 return base::StringPrintf("Realm %d", realm_i); |
| 384 } | 413 } |
| 385 | 414 |
| 386 std::string GeneratePath(int realm_i, int path_i) { | 415 std::string GeneratePath(int realm_i, int path_i) { |
| 387 return base::StringPrintf("/%d/%d/x/y", realm_i, path_i); | 416 return base::StringPrintf("/%d/%d/x/y", realm_i, path_i); |
| 388 } | 417 } |
| 389 | 418 |
| 390 void AddRealm(int realm_i) { | 419 void AddRealm(int realm_i) { |
| 391 AddPathToRealm(realm_i, 0); | 420 AddPathToRealm(realm_i, 0); |
| 392 } | 421 } |
| 393 | 422 |
| 394 void AddPathToRealm(int realm_i, int path_i) { | 423 void AddPathToRealm(int realm_i, int path_i) { |
| 395 cache_.Add(origin_, GenerateRealm(realm_i), kBasic, "", | 424 cache_.Add(origin_, GenerateRealm(realm_i), HttpAuth::AUTH_SCHEME_BASIC, "", |
| 396 kUsername, kPassword, GeneratePath(realm_i, path_i)); | 425 kUsername, kPassword, GeneratePath(realm_i, path_i)); |
| 397 } | 426 } |
| 398 | 427 |
| 399 void CheckRealmExistence(int realm_i, bool exists) { | 428 void CheckRealmExistence(int realm_i, bool exists) { |
| 400 const HttpAuthCache::Entry* entry = | 429 const HttpAuthCache::Entry* entry = |
| 401 cache_.Lookup(origin_, GenerateRealm(realm_i), kBasic); | 430 cache_.Lookup( |
| 431 origin_, GenerateRealm(realm_i), HttpAuth::AUTH_SCHEME_BASIC); |
| 402 if (exists) { | 432 if (exists) { |
| 403 EXPECT_FALSE(entry == NULL); | 433 EXPECT_FALSE(entry == NULL); |
| 404 EXPECT_EQ(GenerateRealm(realm_i), entry->realm()); | 434 EXPECT_EQ(GenerateRealm(realm_i), entry->realm()); |
| 405 } else { | 435 } else { |
| 406 EXPECT_TRUE(entry == NULL); | 436 EXPECT_TRUE(entry == NULL); |
| 407 } | 437 } |
| 408 } | 438 } |
| 409 | 439 |
| 410 void CheckPathExistence(int realm_i, int path_i, bool exists) { | 440 void CheckPathExistence(int realm_i, int path_i, bool exists) { |
| 411 const HttpAuthCache::Entry* entry = | 441 const HttpAuthCache::Entry* entry = |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 CheckPathExistence(0, i, false); | 492 CheckPathExistence(0, i, false); |
| 463 | 493 |
| 464 for (int i = 0; i < kMaxPaths; ++i) | 494 for (int i = 0; i < kMaxPaths; ++i) |
| 465 CheckPathExistence(0, i + 3, true); | 495 CheckPathExistence(0, i + 3, true); |
| 466 | 496 |
| 467 for (int i = 0; i < kMaxRealms; ++i) | 497 for (int i = 0; i < kMaxRealms; ++i) |
| 468 CheckRealmExistence(i, true); | 498 CheckRealmExistence(i, true); |
| 469 } | 499 } |
| 470 | 500 |
| 471 } // namespace net | 501 } // namespace net |
| OLD | NEW |