| 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> |
| 6 |
| 7 #include "base/string16.h" |
| 5 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 6 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 7 #include "net/http/http_auth_cache.h" | 10 #include "net/http/http_auth_cache.h" |
| 8 #include "net/http/http_auth_handler.h" | 11 #include "net/http/http_auth_handler.h" |
| 9 | 12 |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 14 |
| 12 namespace net { | 15 namespace net { |
| 13 | 16 |
| 14 namespace { | 17 namespace { |
| 15 | 18 |
| 16 class MockAuthHandler : public HttpAuthHandler { | 19 class MockAuthHandler : public HttpAuthHandler { |
| 17 public: | 20 public: |
| 18 MockAuthHandler(const char* scheme, const std::string& realm, | 21 MockAuthHandler(const char* scheme, const std::string& realm, |
| 19 HttpAuth::Target target) { | 22 HttpAuth::Target target) { |
| 20 // Can't use initializer list since these are members of the base class. | 23 // Can't use initializer list since these are members of the base class. |
| 21 scheme_ = scheme; | 24 scheme_ = scheme; |
| 22 realm_ = realm; | 25 realm_ = realm; |
| 23 score_ = 1; | 26 score_ = 1; |
| 24 target_ = target; | 27 target_ = target; |
| 25 properties_ = 0; | 28 properties_ = 0; |
| 26 } | 29 } |
| 27 | 30 |
| 28 protected: | 31 protected: |
| 29 virtual bool Init(HttpAuth::ChallengeTokenizer* challenge) { | 32 virtual bool Init(HttpAuth::ChallengeTokenizer* challenge) { |
| 30 return false; // Unused. | 33 return false; // Unused. |
| 31 } | 34 } |
| 32 | 35 |
| 33 virtual int GenerateAuthTokenImpl(const std::wstring*, | 36 virtual int GenerateAuthTokenImpl(const string16*, |
| 34 const std::wstring*, | 37 const string16*, |
| 35 const HttpRequestInfo*, | 38 const HttpRequestInfo*, |
| 36 CompletionCallback* callback, | 39 CompletionCallback* callback, |
| 37 std::string* auth_token) { | 40 std::string* auth_token) { |
| 38 *auth_token = "mock-credentials"; | 41 *auth_token = "mock-credentials"; |
| 39 return OK; | 42 return OK; |
| 40 } | 43 } |
| 41 | 44 |
| 42 | 45 |
| 43 private: | 46 private: |
| 44 ~MockAuthHandler() {} | 47 ~MockAuthHandler() {} |
| 45 }; | 48 }; |
| 46 | 49 |
| 50 const char* kBasic = "basic"; |
| 51 const char* kDigest = "digest"; |
| 52 const char* kRealm1 = "Realm1"; |
| 53 const char* kRealm2 = "Realm2"; |
| 54 const char* kRealm3 = "Realm3"; |
| 55 const char* kRealm4 = "Realm4"; |
| 56 const string16 k123(ASCIIToUTF16("123")); |
| 57 const string16 k1234(ASCIIToUTF16("1234")); |
| 58 const string16 kAdmin(ASCIIToUTF16("admin")); |
| 59 const string16 kAlice(ASCIIToUTF16("alice")); |
| 60 const string16 kAlice2(ASCIIToUTF16("alice2")); |
| 61 const string16 kPassword(ASCIIToUTF16("password")); |
| 62 const string16 kRoot(ASCIIToUTF16("root")); |
| 63 const string16 kUsername(ASCIIToUTF16("username")); |
| 64 const string16 kWileCoyote(ASCIIToUTF16("wilecoyote")); |
| 65 |
| 47 } // namespace | 66 } // namespace |
| 48 | 67 |
| 49 // Test adding and looking-up cache entries (both by realm and by path). | 68 // Test adding and looking-up cache entries (both by realm and by path). |
| 50 TEST(HttpAuthCacheTest, Basic) { | 69 TEST(HttpAuthCacheTest, Basic) { |
| 51 GURL origin("http://www.google.com"); | 70 GURL origin("http://www.google.com"); |
| 52 HttpAuthCache cache; | 71 HttpAuthCache cache; |
| 53 HttpAuthCache::Entry* entry; | 72 HttpAuthCache::Entry* entry; |
| 54 | 73 |
| 55 // Add cache entries for 3 realms: "Realm1", "Realm2", "Realm3" | 74 // Add cache entries for 3 realms: "Realm1", "Realm2", "Realm3" |
| 56 | 75 |
| 57 scoped_ptr<HttpAuthHandler> realm1_handler( | 76 scoped_ptr<HttpAuthHandler> realm1_handler( |
| 58 new MockAuthHandler("basic", "Realm1", HttpAuth::AUTH_SERVER)); | 77 new MockAuthHandler(kBasic, kRealm1, HttpAuth::AUTH_SERVER)); |
| 59 cache.Add(origin, realm1_handler->realm(), realm1_handler->scheme(), | 78 cache.Add(origin, realm1_handler->realm(), realm1_handler->scheme(), |
| 60 "Basic realm=Realm1", L"realm1-user", L"realm1-password", | 79 "Basic realm=Realm1", ASCIIToUTF16("realm1-user"), |
| 61 "/foo/bar/index.html"); | 80 ASCIIToUTF16("realm1-password"), "/foo/bar/index.html"); |
| 62 | 81 |
| 63 scoped_ptr<HttpAuthHandler> realm2_handler( | 82 scoped_ptr<HttpAuthHandler> realm2_handler( |
| 64 new MockAuthHandler("basic", "Realm2", HttpAuth::AUTH_SERVER)); | 83 new MockAuthHandler(kBasic, kRealm2, HttpAuth::AUTH_SERVER)); |
| 65 cache.Add(origin, realm2_handler->realm(), realm2_handler->scheme(), | 84 cache.Add(origin, realm2_handler->realm(), realm2_handler->scheme(), |
| 66 "Basic realm=Realm2", L"realm2-user", L"realm2-password", | 85 "Basic realm=Realm2", ASCIIToUTF16("realm2-user"), |
| 67 "/foo2/index.html"); | 86 ASCIIToUTF16("realm2-password"), "/foo2/index.html"); |
| 68 | 87 |
| 69 scoped_ptr<HttpAuthHandler> realm3_basic_handler( | 88 scoped_ptr<HttpAuthHandler> realm3_basic_handler( |
| 70 new MockAuthHandler("basic", "Realm3", HttpAuth::AUTH_PROXY)); | 89 new MockAuthHandler(kBasic, kRealm3, HttpAuth::AUTH_PROXY)); |
| 71 cache.Add(origin, realm3_basic_handler->realm(), | 90 cache.Add(origin, realm3_basic_handler->realm(), |
| 72 realm3_basic_handler->scheme(), "Basic realm=Realm3", | 91 realm3_basic_handler->scheme(), "Basic realm=Realm3", |
| 73 L"realm3-basic-user", L"realm3-basic-password", ""); | 92 ASCIIToUTF16("realm3-basic-user"), |
| 93 ASCIIToUTF16("realm3-basic-password"), ""); |
| 74 | 94 |
| 75 scoped_ptr<HttpAuthHandler> realm3_digest_handler( | 95 scoped_ptr<HttpAuthHandler> realm3_digest_handler( |
| 76 new MockAuthHandler("digest", "Realm3", HttpAuth::AUTH_PROXY)); | 96 new MockAuthHandler(kDigest, kRealm3, HttpAuth::AUTH_PROXY)); |
| 77 cache.Add(origin, realm3_digest_handler->realm(), | 97 cache.Add(origin, realm3_digest_handler->realm(), |
| 78 realm3_digest_handler->scheme(), "Digest realm=Realm3", | 98 realm3_digest_handler->scheme(), "Digest realm=Realm3", |
| 79 L"realm3-digest-user", L"realm3-digest-password", | 99 ASCIIToUTF16("realm3-digest-user"), |
| 80 "/baz/index.html"); | 100 ASCIIToUTF16("realm3-digest-password"), "/baz/index.html"); |
| 81 | 101 |
| 82 // There is no Realm4 | 102 // There is no Realm4 |
| 83 entry = cache.Lookup(origin, "Realm4", "basic"); | 103 entry = cache.Lookup(origin, kRealm4, kBasic); |
| 84 EXPECT_TRUE(NULL == entry); | 104 EXPECT_TRUE(NULL == entry); |
| 85 | 105 |
| 86 // While Realm3 does exist, the origin scheme is wrong. | 106 // While Realm3 does exist, the origin scheme is wrong. |
| 87 entry = cache.Lookup(GURL("https://www.google.com"), "Realm3", | 107 entry = cache.Lookup(GURL("https://www.google.com"), kRealm3, |
| 88 "basic"); | 108 kBasic); |
| 89 EXPECT_TRUE(NULL == entry); | 109 EXPECT_TRUE(NULL == entry); |
| 90 | 110 |
| 91 // Realm, origin scheme ok, authentication scheme wrong | 111 // Realm, origin scheme ok, authentication scheme wrong |
| 92 entry = cache.Lookup(GURL("http://www.google.com"), "Realm1", "digest"); | 112 entry = cache.Lookup(GURL("http://www.google.com"), kRealm1, kDigest); |
| 93 EXPECT_TRUE(NULL == entry); | 113 EXPECT_TRUE(NULL == entry); |
| 94 | 114 |
| 95 // Valid lookup by origin, realm, scheme. | 115 // Valid lookup by origin, realm, scheme. |
| 96 entry = cache.Lookup(GURL("http://www.google.com:80"), "Realm3", "basic"); | 116 entry = cache.Lookup(GURL("http://www.google.com:80"), kRealm3, kBasic); |
| 97 ASSERT_FALSE(NULL == entry); | 117 ASSERT_FALSE(NULL == entry); |
| 98 EXPECT_EQ("basic", entry->scheme()); | 118 EXPECT_EQ(kBasic, entry->scheme()); |
| 99 EXPECT_EQ("Realm3", entry->realm()); | 119 EXPECT_EQ(kRealm3, entry->realm()); |
| 100 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge()); | 120 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge()); |
| 101 EXPECT_EQ(L"realm3-basic-user", entry->username()); | 121 EXPECT_EQ(ASCIIToUTF16("realm3-basic-user"), entry->username()); |
| 102 EXPECT_EQ(L"realm3-basic-password", entry->password()); | 122 EXPECT_EQ(ASCIIToUTF16("realm3-basic-password"), entry->password()); |
| 103 | 123 |
| 104 // Valid lookup by origin, realm, scheme when there's a duplicate | 124 // Valid lookup by origin, realm, scheme when there's a duplicate |
| 105 // origin, realm in the cache | 125 // origin, realm in the cache |
| 106 entry = cache.Lookup(GURL("http://www.google.com:80"), "Realm3", "digest"); | 126 entry = cache.Lookup(GURL("http://www.google.com:80"), kRealm3, kDigest); |
| 107 ASSERT_FALSE(NULL == entry); | 127 ASSERT_FALSE(NULL == entry); |
| 108 EXPECT_EQ("digest", entry->scheme()); | 128 EXPECT_EQ(kDigest, entry->scheme()); |
| 109 EXPECT_EQ("Realm3", entry->realm()); | 129 EXPECT_EQ(kRealm3, entry->realm()); |
| 110 EXPECT_EQ("Digest realm=Realm3", entry->auth_challenge()); | 130 EXPECT_EQ("Digest realm=Realm3", entry->auth_challenge()); |
| 111 EXPECT_EQ(L"realm3-digest-user", entry->username()); | 131 EXPECT_EQ(ASCIIToUTF16("realm3-digest-user"), entry->username()); |
| 112 EXPECT_EQ(L"realm3-digest-password", entry->password()); | 132 EXPECT_EQ(ASCIIToUTF16("realm3-digest-password"), entry->password()); |
| 113 | 133 |
| 114 // Valid lookup by realm. | 134 // Valid lookup by realm. |
| 115 entry = cache.Lookup(origin, "Realm2", "basic"); | 135 entry = cache.Lookup(origin, kRealm2, kBasic); |
| 116 ASSERT_FALSE(NULL == entry); | 136 ASSERT_FALSE(NULL == entry); |
| 117 EXPECT_EQ("basic", entry->scheme()); | 137 EXPECT_EQ(kBasic, entry->scheme()); |
| 118 EXPECT_EQ("Realm2", entry->realm()); | 138 EXPECT_EQ(kRealm2, entry->realm()); |
| 119 EXPECT_EQ("Basic realm=Realm2", entry->auth_challenge()); | 139 EXPECT_EQ("Basic realm=Realm2", entry->auth_challenge()); |
| 120 EXPECT_EQ(L"realm2-user", entry->username()); | 140 EXPECT_EQ(ASCIIToUTF16("realm2-user"), entry->username()); |
| 121 EXPECT_EQ(L"realm2-password", entry->password()); | 141 EXPECT_EQ(ASCIIToUTF16("realm2-password"), entry->password()); |
| 122 | 142 |
| 123 // Check that subpaths are recognized. | 143 // Check that subpaths are recognized. |
| 124 HttpAuthCache::Entry* realm2_entry = cache.Lookup(origin, "Realm2", "basic"); | 144 HttpAuthCache::Entry* realm2_entry = cache.Lookup(origin, kRealm2, kBasic); |
| 125 EXPECT_FALSE(NULL == realm2_entry); | 145 EXPECT_FALSE(NULL == realm2_entry); |
| 126 // Positive tests: | 146 // Positive tests: |
| 127 entry = cache.LookupByPath(origin, "/foo2/index.html"); | 147 entry = cache.LookupByPath(origin, "/foo2/index.html"); |
| 128 EXPECT_TRUE(realm2_entry == entry); | 148 EXPECT_TRUE(realm2_entry == entry); |
| 129 entry = cache.LookupByPath(origin, "/foo2/foobar.html"); | 149 entry = cache.LookupByPath(origin, "/foo2/foobar.html"); |
| 130 EXPECT_TRUE(realm2_entry == entry); | 150 EXPECT_TRUE(realm2_entry == entry); |
| 131 entry = cache.LookupByPath(origin, "/foo2/bar/index.html"); | 151 entry = cache.LookupByPath(origin, "/foo2/bar/index.html"); |
| 132 EXPECT_TRUE(realm2_entry == entry); | 152 EXPECT_TRUE(realm2_entry == entry); |
| 133 entry = cache.LookupByPath(origin, "/foo2/"); | 153 entry = cache.LookupByPath(origin, "/foo2/"); |
| 134 EXPECT_TRUE(realm2_entry == entry); | 154 EXPECT_TRUE(realm2_entry == entry); |
| 135 | 155 |
| 136 // Negative tests: | 156 // Negative tests: |
| 137 entry = cache.LookupByPath(origin, "/foo2"); | 157 entry = cache.LookupByPath(origin, "/foo2"); |
| 138 EXPECT_FALSE(realm2_entry == entry); | 158 EXPECT_FALSE(realm2_entry == entry); |
| 139 entry = cache.LookupByPath(origin, "/foo3/index.html"); | 159 entry = cache.LookupByPath(origin, "/foo3/index.html"); |
| 140 EXPECT_FALSE(realm2_entry == entry); | 160 EXPECT_FALSE(realm2_entry == entry); |
| 141 entry = cache.LookupByPath(origin, ""); | 161 entry = cache.LookupByPath(origin, ""); |
| 142 EXPECT_FALSE(realm2_entry == entry); | 162 EXPECT_FALSE(realm2_entry == entry); |
| 143 entry = cache.LookupByPath(origin, "/"); | 163 entry = cache.LookupByPath(origin, "/"); |
| 144 EXPECT_FALSE(realm2_entry == entry); | 164 EXPECT_FALSE(realm2_entry == entry); |
| 145 | 165 |
| 146 // Confirm we find the same realm, different auth scheme by path lookup | 166 // Confirm we find the same realm, different auth scheme by path lookup |
| 147 HttpAuthCache::Entry* realm3_digest_entry = | 167 HttpAuthCache::Entry* realm3_digest_entry = |
| 148 cache.Lookup(origin, "Realm3", "digest"); | 168 cache.Lookup(origin, kRealm3, kDigest); |
| 149 EXPECT_FALSE(NULL == realm3_digest_entry); | 169 EXPECT_FALSE(NULL == realm3_digest_entry); |
| 150 entry = cache.LookupByPath(origin, "/baz/index.html"); | 170 entry = cache.LookupByPath(origin, "/baz/index.html"); |
| 151 EXPECT_TRUE(realm3_digest_entry == entry); | 171 EXPECT_TRUE(realm3_digest_entry == entry); |
| 152 entry = cache.LookupByPath(origin, "/baz/"); | 172 entry = cache.LookupByPath(origin, "/baz/"); |
| 153 EXPECT_TRUE(realm3_digest_entry == entry); | 173 EXPECT_TRUE(realm3_digest_entry == entry); |
| 154 entry = cache.LookupByPath(origin, "/baz"); | 174 entry = cache.LookupByPath(origin, "/baz"); |
| 155 EXPECT_FALSE(realm3_digest_entry == entry); | 175 EXPECT_FALSE(realm3_digest_entry == entry); |
| 156 | 176 |
| 157 // Confirm we find the same realm, different auth scheme by path lookup | 177 // Confirm we find the same realm, different auth scheme by path lookup |
| 158 HttpAuthCache::Entry* realm3DigestEntry = | 178 HttpAuthCache::Entry* realm3DigestEntry = |
| 159 cache.Lookup(origin, "Realm3", "digest"); | 179 cache.Lookup(origin, kRealm3, kDigest); |
| 160 EXPECT_FALSE(NULL == realm3DigestEntry); | 180 EXPECT_FALSE(NULL == realm3DigestEntry); |
| 161 entry = cache.LookupByPath(origin, "/baz/index.html"); | 181 entry = cache.LookupByPath(origin, "/baz/index.html"); |
| 162 EXPECT_TRUE(realm3DigestEntry == entry); | 182 EXPECT_TRUE(realm3DigestEntry == entry); |
| 163 entry = cache.LookupByPath(origin, "/baz/"); | 183 entry = cache.LookupByPath(origin, "/baz/"); |
| 164 EXPECT_TRUE(realm3DigestEntry == entry); | 184 EXPECT_TRUE(realm3DigestEntry == entry); |
| 165 entry = cache.LookupByPath(origin, "/baz"); | 185 entry = cache.LookupByPath(origin, "/baz"); |
| 166 EXPECT_FALSE(realm3DigestEntry == entry); | 186 EXPECT_FALSE(realm3DigestEntry == entry); |
| 167 | 187 |
| 168 // Lookup using empty path (may be used for proxy). | 188 // Lookup using empty path (may be used for proxy). |
| 169 entry = cache.LookupByPath(origin, ""); | 189 entry = cache.LookupByPath(origin, ""); |
| 170 EXPECT_FALSE(NULL == entry); | 190 EXPECT_FALSE(NULL == entry); |
| 171 EXPECT_EQ("basic", entry->scheme()); | 191 EXPECT_EQ(kBasic, entry->scheme()); |
| 172 EXPECT_EQ("Realm3", entry->realm()); | 192 EXPECT_EQ(kRealm3, entry->realm()); |
| 173 } | 193 } |
| 174 | 194 |
| 175 TEST(HttpAuthCacheTest, AddPath) { | 195 TEST(HttpAuthCacheTest, AddPath) { |
| 176 HttpAuthCache::Entry entry; | 196 HttpAuthCache::Entry entry; |
| 177 | 197 |
| 178 // All of these paths have a common root /1/2/2/4/5/ | 198 // All of these paths have a common root /1/2/2/4/5/ |
| 179 entry.AddPath("/1/2/3/4/5/x.txt"); | 199 entry.AddPath("/1/2/3/4/5/x.txt"); |
| 180 entry.AddPath("/1/2/3/4/5/y.txt"); | 200 entry.AddPath("/1/2/3/4/5/y.txt"); |
| 181 entry.AddPath("/1/2/3/4/5/z.txt"); | 201 entry.AddPath("/1/2/3/4/5/z.txt"); |
| 182 | 202 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 205 } | 225 } |
| 206 | 226 |
| 207 // Calling Add when the realm entry already exists, should append that | 227 // Calling Add when the realm entry already exists, should append that |
| 208 // path. | 228 // path. |
| 209 TEST(HttpAuthCacheTest, AddToExistingEntry) { | 229 TEST(HttpAuthCacheTest, AddToExistingEntry) { |
| 210 HttpAuthCache cache; | 230 HttpAuthCache cache; |
| 211 GURL origin("http://www.foobar.com:70"); | 231 GURL origin("http://www.foobar.com:70"); |
| 212 const std::string auth_challenge = "Basic realm=MyRealm"; | 232 const std::string auth_challenge = "Basic realm=MyRealm"; |
| 213 | 233 |
| 214 scoped_ptr<HttpAuthHandler> handler( | 234 scoped_ptr<HttpAuthHandler> handler( |
| 215 new MockAuthHandler("basic", "MyRealm", HttpAuth::AUTH_SERVER)); | 235 new MockAuthHandler(kBasic, "MyRealm", HttpAuth::AUTH_SERVER)); |
| 216 | 236 |
| 217 HttpAuthCache::Entry* orig_entry = cache.Add( | 237 HttpAuthCache::Entry* orig_entry = cache.Add( |
| 218 origin, handler->realm(), handler->scheme(), auth_challenge, | 238 origin, handler->realm(), handler->scheme(), auth_challenge, |
| 219 L"user1", L"password1", "/x/y/z/"); | 239 ASCIIToUTF16("user1"), ASCIIToUTF16("password1"), "/x/y/z/"); |
| 220 cache.Add(origin, handler->realm(), handler->scheme(), auth_challenge, | 240 cache.Add(origin, handler->realm(), handler->scheme(), auth_challenge, |
| 221 L"user2", L"password2", "/z/y/x/"); | 241 ASCIIToUTF16("user2"), ASCIIToUTF16("password2"), "/z/y/x/"); |
| 222 cache.Add(origin, handler->realm(), handler->scheme(), auth_challenge, | 242 cache.Add(origin, handler->realm(), handler->scheme(), auth_challenge, |
| 223 L"user3", L"password3", "/z/y"); | 243 ASCIIToUTF16("user3"), ASCIIToUTF16("password3"), "/z/y"); |
| 224 | 244 |
| 225 HttpAuthCache::Entry* entry = cache.Lookup(origin, "MyRealm", "basic"); | 245 HttpAuthCache::Entry* entry = cache.Lookup(origin, "MyRealm", kBasic); |
| 226 | 246 |
| 227 EXPECT_TRUE(entry == orig_entry); | 247 EXPECT_TRUE(entry == orig_entry); |
| 228 EXPECT_EQ(L"user3", entry->username()); | 248 EXPECT_EQ(ASCIIToUTF16("user3"), entry->username()); |
| 229 EXPECT_EQ(L"password3", entry->password()); | 249 EXPECT_EQ(ASCIIToUTF16("password3"), entry->password()); |
| 230 | 250 |
| 231 EXPECT_EQ(2U, entry->paths_.size()); | 251 EXPECT_EQ(2U, entry->paths_.size()); |
| 232 EXPECT_EQ("/z/", entry->paths_.front()); | 252 EXPECT_EQ("/z/", entry->paths_.front()); |
| 233 EXPECT_EQ("/x/y/z/", entry->paths_.back()); | 253 EXPECT_EQ("/x/y/z/", entry->paths_.back()); |
| 234 } | 254 } |
| 235 | 255 |
| 236 TEST(HttpAuthCacheTest, Remove) { | 256 TEST(HttpAuthCacheTest, Remove) { |
| 237 GURL origin("http://foobar2.com"); | 257 GURL origin("http://foobar2.com"); |
| 238 | 258 |
| 239 scoped_ptr<HttpAuthHandler> realm1_handler( | 259 scoped_ptr<HttpAuthHandler> realm1_handler( |
| 240 new MockAuthHandler("basic", "Realm1", HttpAuth::AUTH_SERVER)); | 260 new MockAuthHandler(kBasic, kRealm1, HttpAuth::AUTH_SERVER)); |
| 241 | 261 |
| 242 scoped_ptr<HttpAuthHandler> realm2_handler( | 262 scoped_ptr<HttpAuthHandler> realm2_handler( |
| 243 new MockAuthHandler("basic", "Realm2", HttpAuth::AUTH_SERVER)); | 263 new MockAuthHandler(kBasic, kRealm2, HttpAuth::AUTH_SERVER)); |
| 244 | 264 |
| 245 scoped_ptr<HttpAuthHandler> realm3_basic_handler( | 265 scoped_ptr<HttpAuthHandler> realm3_basic_handler( |
| 246 new MockAuthHandler("basic", "Realm3", HttpAuth::AUTH_SERVER)); | 266 new MockAuthHandler(kBasic, kRealm3, HttpAuth::AUTH_SERVER)); |
| 247 | 267 |
| 248 scoped_ptr<HttpAuthHandler> realm3_digest_handler( | 268 scoped_ptr<HttpAuthHandler> realm3_digest_handler( |
| 249 new MockAuthHandler("digest", "Realm3", HttpAuth::AUTH_SERVER)); | 269 new MockAuthHandler(kDigest, kRealm3, HttpAuth::AUTH_SERVER)); |
| 250 | 270 |
| 251 HttpAuthCache cache; | 271 HttpAuthCache cache; |
| 252 cache.Add(origin, realm1_handler->realm(), realm1_handler->scheme(), | 272 cache.Add(origin, realm1_handler->realm(), realm1_handler->scheme(), |
| 253 "basic realm=Realm1", L"alice", L"123", "/"); | 273 "basic realm=Realm1", kAlice, k123, "/"); |
| 254 cache.Add(origin, realm2_handler->realm(), realm2_handler->scheme(), | 274 cache.Add(origin, realm2_handler->realm(), realm2_handler->scheme(), |
| 255 "basic realm=Realm2", L"bob", L"princess", "/"); | 275 "basic realm=Realm2", ASCIIToUTF16("bob"), ASCIIToUTF16("princess"), |
| 276 "/"); |
| 256 cache.Add(origin, realm3_basic_handler->realm(), | 277 cache.Add(origin, realm3_basic_handler->realm(), |
| 257 realm3_basic_handler->scheme(), "basic realm=Realm3", L"admin", | 278 realm3_basic_handler->scheme(), "basic realm=Realm3", |
| 258 L"password", "/"); | 279 kAdmin, kPassword, "/"); |
| 259 cache.Add(origin, realm3_digest_handler->realm(), | 280 cache.Add(origin, realm3_digest_handler->realm(), |
| 260 realm3_digest_handler->scheme(), "digest realm=Realm3", L"root", | 281 realm3_digest_handler->scheme(), "digest realm=Realm3", |
| 261 L"wilecoyote", "/"); | 282 kRoot, kWileCoyote, "/"); |
| 262 | 283 |
| 263 // Fails, because there is no realm "Realm4". | 284 // Fails, because there is no realm "Realm4". |
| 264 EXPECT_FALSE(cache.Remove(origin, "Realm4", "basic", L"alice", L"123")); | 285 EXPECT_FALSE(cache.Remove(origin, kRealm4, kBasic, kAlice, k123)); |
| 265 | 286 |
| 266 // Fails because the origin is wrong. | 287 // Fails because the origin is wrong. |
| 267 EXPECT_FALSE(cache.Remove( | 288 EXPECT_FALSE(cache.Remove( |
| 268 GURL("http://foobar2.com:100"), "Realm1", "basic", L"alice", L"123")); | 289 GURL("http://foobar2.com:100"), kRealm1, kBasic, kAlice, k123)); |
| 269 | 290 |
| 270 // Fails because the username is wrong. | 291 // Fails because the username is wrong. |
| 271 EXPECT_FALSE(cache.Remove(origin, "Realm1", "basic", L"alice2", L"123")); | 292 EXPECT_FALSE(cache.Remove(origin, kRealm1, kBasic, kAlice2, k123)); |
| 272 | 293 |
| 273 // Fails because the password is wrong. | 294 // Fails because the password is wrong. |
| 274 EXPECT_FALSE(cache.Remove(origin, "Realm1", "basic", L"alice", L"1234")); | 295 EXPECT_FALSE(cache.Remove(origin, kRealm1, kBasic, kAlice, k1234)); |
| 275 | 296 |
| 276 // Fails because the authentication type is wrong. | 297 // Fails because the authentication type is wrong. |
| 277 EXPECT_FALSE(cache.Remove(origin, "Realm1", "digest", L"alice", L"123")); | 298 EXPECT_FALSE(cache.Remove(origin, kRealm1, kDigest, kAlice, k123)); |
| 278 | 299 |
| 279 // Succeeds. | 300 // Succeeds. |
| 280 EXPECT_TRUE(cache.Remove(origin, "Realm1", "basic", L"alice", L"123")); | 301 EXPECT_TRUE(cache.Remove(origin, kRealm1, kBasic, kAlice, k123)); |
| 281 | 302 |
| 282 // Fails because we just deleted the entry! | 303 // Fails because we just deleted the entry! |
| 283 EXPECT_FALSE(cache.Remove(origin, "Realm1", "basic", L"alice", L"123")); | 304 EXPECT_FALSE(cache.Remove(origin, kRealm1, kBasic, kAlice, k123)); |
| 284 | 305 |
| 285 // Succeed when there are two authentication types for the same origin,realm. | 306 // Succeed when there are two authentication types for the same origin,realm. |
| 286 EXPECT_TRUE(cache.Remove(origin, "Realm3", "digest", L"root", L"wilecoyote")); | 307 EXPECT_TRUE(cache.Remove(origin, kRealm3, kDigest, kRoot, kWileCoyote)); |
| 287 | 308 |
| 288 // Succeed as above, but when entries were added in opposite order | 309 // Succeed as above, but when entries were added in opposite order |
| 289 cache.Add(origin, realm3_digest_handler->realm(), | 310 cache.Add(origin, realm3_digest_handler->realm(), |
| 290 realm3_digest_handler->scheme(), "digest realm=Realm3", L"root", | 311 realm3_digest_handler->scheme(), "digest realm=Realm3", |
| 291 L"wilecoyote", "/"); | 312 kRoot, kWileCoyote, "/"); |
| 292 EXPECT_TRUE(cache.Remove(origin, "Realm3", "basic", L"admin", L"password")); | 313 EXPECT_TRUE(cache.Remove(origin, kRealm3, kBasic, kAdmin, kPassword)); |
| 293 | 314 |
| 294 // Make sure that removing one entry still leaves the other available for | 315 // Make sure that removing one entry still leaves the other available for |
| 295 // lookup. | 316 // lookup. |
| 296 HttpAuthCache::Entry* entry = cache.Lookup(origin, "Realm3", "digest"); | 317 HttpAuthCache::Entry* entry = cache.Lookup(origin, kRealm3, kDigest); |
| 297 EXPECT_FALSE(NULL == entry); | 318 EXPECT_FALSE(NULL == entry); |
| 298 } | 319 } |
| 299 | 320 |
| 300 // Test fixture class for eviction tests (contains helpers for bulk | 321 // Test fixture class for eviction tests (contains helpers for bulk |
| 301 // insertion and existence testing). | 322 // insertion and existence testing). |
| 302 class HttpAuthCacheEvictionTest : public testing::Test { | 323 class HttpAuthCacheEvictionTest : public testing::Test { |
| 303 protected: | 324 protected: |
| 304 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") { } | 325 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") { } |
| 305 | 326 |
| 306 std::string GenerateRealm(int realm_i) { | 327 std::string GenerateRealm(int realm_i) { |
| 307 return StringPrintf("Realm %d", realm_i); | 328 return StringPrintf("Realm %d", realm_i); |
| 308 } | 329 } |
| 309 | 330 |
| 310 std::string GeneratePath(int realm_i, int path_i) { | 331 std::string GeneratePath(int realm_i, int path_i) { |
| 311 return StringPrintf("/%d/%d/x/y", realm_i, path_i); | 332 return StringPrintf("/%d/%d/x/y", realm_i, path_i); |
| 312 } | 333 } |
| 313 | 334 |
| 314 void AddRealm(int realm_i) { | 335 void AddRealm(int realm_i) { |
| 315 AddPathToRealm(realm_i, 0); | 336 AddPathToRealm(realm_i, 0); |
| 316 } | 337 } |
| 317 | 338 |
| 318 void AddPathToRealm(int realm_i, int path_i) { | 339 void AddPathToRealm(int realm_i, int path_i) { |
| 319 cache_.Add(origin_, GenerateRealm(realm_i), "basic", "", | 340 cache_.Add(origin_, GenerateRealm(realm_i), kBasic, "", |
| 320 L"username", L"password", GeneratePath(realm_i, path_i)); | 341 kUsername, kPassword, GeneratePath(realm_i, path_i)); |
| 321 } | 342 } |
| 322 | 343 |
| 323 void CheckRealmExistence(int realm_i, bool exists) { | 344 void CheckRealmExistence(int realm_i, bool exists) { |
| 324 const HttpAuthCache::Entry* entry = | 345 const HttpAuthCache::Entry* entry = |
| 325 cache_.Lookup(origin_, GenerateRealm(realm_i), "basic"); | 346 cache_.Lookup(origin_, GenerateRealm(realm_i), kBasic); |
| 326 if (exists) { | 347 if (exists) { |
| 327 EXPECT_FALSE(entry == NULL); | 348 EXPECT_FALSE(entry == NULL); |
| 328 EXPECT_EQ(GenerateRealm(realm_i), entry->realm()); | 349 EXPECT_EQ(GenerateRealm(realm_i), entry->realm()); |
| 329 } else { | 350 } else { |
| 330 EXPECT_TRUE(entry == NULL); | 351 EXPECT_TRUE(entry == NULL); |
| 331 } | 352 } |
| 332 } | 353 } |
| 333 | 354 |
| 334 void CheckPathExistence(int realm_i, int path_i, bool exists) { | 355 void CheckPathExistence(int realm_i, int path_i, bool exists) { |
| 335 const HttpAuthCache::Entry* entry = | 356 const HttpAuthCache::Entry* entry = |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 CheckPathExistence(0, i, false); | 407 CheckPathExistence(0, i, false); |
| 387 | 408 |
| 388 for (int i = 0; i < kMaxPaths; ++i) | 409 for (int i = 0; i < kMaxPaths; ++i) |
| 389 CheckPathExistence(0, i + 3, true); | 410 CheckPathExistence(0, i + 3, true); |
| 390 | 411 |
| 391 for (int i = 0; i < kMaxRealms; ++i) | 412 for (int i = 0; i < kMaxRealms; ++i) |
| 392 CheckRealmExistence(i, true); | 413 CheckRealmExistence(i, true); |
| 393 } | 414 } |
| 394 | 415 |
| 395 } // namespace net | 416 } // namespace net |
| OLD | NEW |