OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 "base/string_util.h" | 5 #include "base/string_util.h" |
6 #include "net/http/http_auth_cache.h" | 6 #include "net/http/http_auth_cache.h" |
7 | 7 |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace net { | 10 namespace net { |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 class MockAuthHandler : public HttpAuthHandler { | 14 class MockAuthHandler : public HttpAuthHandler { |
15 public: | 15 public: |
16 MockAuthHandler(const char* scheme, const std::string& realm, | 16 MockAuthHandler(const char* scheme, const std::string& realm, |
17 HttpAuth::Target target) { | 17 HttpAuth::Target target) { |
18 // Can't use initializer list since these are members of the base class. | 18 // Can't use initializer list since these are members of the base class. |
19 scheme_ = scheme; | 19 scheme_ = scheme; |
20 realm_ = realm; | 20 realm_ = realm; |
21 score_ = 1; | 21 score_ = 1; |
22 target_ = target; | 22 target_ = target; |
| 23 properties_ = 0; |
23 } | 24 } |
24 | 25 |
25 virtual std::string GenerateCredentials(const std::wstring&, | 26 virtual std::string GenerateCredentials(const std::wstring&, |
26 const std::wstring&, | 27 const std::wstring&, |
27 const HttpRequestInfo*, | 28 const HttpRequestInfo*, |
28 const ProxyInfo*) { | 29 const ProxyInfo*) { |
29 return "mock-credentials"; // Unused. | 30 return "mock-credentials"; // Unused. |
30 } | 31 } |
31 | 32 |
32 protected: | 33 protected: |
33 virtual bool Init(std::string::const_iterator, std::string::const_iterator) { | 34 virtual bool Init(std::string::const_iterator, std::string::const_iterator) { |
34 return false; // Unused. | 35 return false; // Unused. |
35 } | 36 } |
36 }; | 37 }; |
37 | 38 |
38 } // namespace | 39 } // namespace |
39 | 40 |
40 // Test adding and looking-up cache entries (both by realm and by path). | 41 // Test adding and looking-up cache entries (both by realm and by path). |
41 TEST(HttpAuthCacheTest, Basic) { | 42 TEST(HttpAuthCacheTest, Basic) { |
42 GURL origin("http://www.google.com"); | 43 GURL origin("http://www.google.com"); |
43 HttpAuthCache cache; | 44 HttpAuthCache cache; |
44 HttpAuthCache::Entry* entry; | 45 HttpAuthCache::Entry* entry; |
45 | 46 |
46 // Add cache entries for 3 realms: "Realm1", "Realm2", "Realm3" | 47 // Add cache entries for 3 realms: "Realm1", "Realm2", "Realm3" |
47 | 48 |
48 scoped_refptr<HttpAuthHandler> realm1_handler = | 49 scoped_refptr<HttpAuthHandler> realm1_handler = |
49 new MockAuthHandler("basic", "Realm1", HttpAuth::AUTH_SERVER); | 50 new MockAuthHandler("basic", "Realm1", HttpAuth::AUTH_SERVER); |
50 cache.Add(origin, realm1_handler, L"realm1-user", L"realm1-password", | 51 cache.Add(origin, realm1_handler, L"realm1-user", L"realm1-password", |
51 "/foo/bar/index.html"); | 52 "/foo/bar/index.html"); |
52 | 53 |
53 scoped_refptr<HttpAuthHandler> realm2_handler = | 54 scoped_refptr<HttpAuthHandler> realm2_handler = |
54 new MockAuthHandler("basic", "Realm2", HttpAuth::AUTH_SERVER); | 55 new MockAuthHandler("basic", "Realm2", HttpAuth::AUTH_SERVER); |
55 cache.Add(origin, realm2_handler, L"realm2-user", L"realm2-password", | 56 cache.Add(origin, realm2_handler, L"realm2-user", L"realm2-password", |
(...skipping 17 matching lines...) Expand all Loading... |
73 EXPECT_TRUE(entry->handler() == realm3_handler.get()); | 74 EXPECT_TRUE(entry->handler() == realm3_handler.get()); |
74 EXPECT_EQ(L"realm3-user", entry->username()); | 75 EXPECT_EQ(L"realm3-user", entry->username()); |
75 EXPECT_EQ(L"realm3-password", entry->password()); | 76 EXPECT_EQ(L"realm3-password", entry->password()); |
76 | 77 |
77 // Valid lookup by realm. | 78 // Valid lookup by realm. |
78 entry = cache.LookupByRealm(origin, "Realm2"); | 79 entry = cache.LookupByRealm(origin, "Realm2"); |
79 EXPECT_FALSE(NULL == entry); | 80 EXPECT_FALSE(NULL == entry); |
80 EXPECT_TRUE(entry->handler() == realm2_handler.get()); | 81 EXPECT_TRUE(entry->handler() == realm2_handler.get()); |
81 EXPECT_EQ(L"realm2-user", entry->username()); | 82 EXPECT_EQ(L"realm2-user", entry->username()); |
82 EXPECT_EQ(L"realm2-password", entry->password()); | 83 EXPECT_EQ(L"realm2-password", entry->password()); |
83 | 84 |
84 // Check that subpaths are recognized. | 85 // Check that subpaths are recognized. |
85 HttpAuthCache::Entry* realm2Entry = cache.LookupByRealm(origin, "Realm2"); | 86 HttpAuthCache::Entry* realm2Entry = cache.LookupByRealm(origin, "Realm2"); |
86 EXPECT_FALSE(NULL == realm2Entry); | 87 EXPECT_FALSE(NULL == realm2Entry); |
87 // Positive tests: | 88 // Positive tests: |
88 entry = cache.LookupByPath(origin, "/foo2/index.html"); | 89 entry = cache.LookupByPath(origin, "/foo2/index.html"); |
89 EXPECT_TRUE(realm2Entry == entry); | 90 EXPECT_TRUE(realm2Entry == entry); |
90 entry = cache.LookupByPath(origin, "/foo2/foobar.html"); | 91 entry = cache.LookupByPath(origin, "/foo2/foobar.html"); |
91 EXPECT_TRUE(realm2Entry == entry); | 92 EXPECT_TRUE(realm2Entry == entry); |
92 entry = cache.LookupByPath(origin, "/foo2/bar/index.html"); | 93 entry = cache.LookupByPath(origin, "/foo2/bar/index.html"); |
93 EXPECT_TRUE(realm2Entry == entry); | 94 EXPECT_TRUE(realm2Entry == entry); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 HttpAuthCache::Entry* orig_entry = cache.Add( | 155 HttpAuthCache::Entry* orig_entry = cache.Add( |
155 origin, handler, L"user1", L"password1", "/x/y/z/"); | 156 origin, handler, L"user1", L"password1", "/x/y/z/"); |
156 cache.Add(origin, handler, L"user2", L"password2", "/z/y/x/"); | 157 cache.Add(origin, handler, L"user2", L"password2", "/z/y/x/"); |
157 cache.Add(origin, handler, L"user3", L"password3", "/z/y"); | 158 cache.Add(origin, handler, L"user3", L"password3", "/z/y"); |
158 | 159 |
159 HttpAuthCache::Entry* entry = cache.LookupByRealm(origin, "MyRealm"); | 160 HttpAuthCache::Entry* entry = cache.LookupByRealm(origin, "MyRealm"); |
160 | 161 |
161 EXPECT_TRUE(entry == orig_entry); | 162 EXPECT_TRUE(entry == orig_entry); |
162 EXPECT_EQ(L"user3", entry->username()); | 163 EXPECT_EQ(L"user3", entry->username()); |
163 EXPECT_EQ(L"password3", entry->password()); | 164 EXPECT_EQ(L"password3", entry->password()); |
164 | 165 |
165 EXPECT_EQ(2U, entry->paths_.size()); | 166 EXPECT_EQ(2U, entry->paths_.size()); |
166 EXPECT_EQ("/z/", entry->paths_.front()); | 167 EXPECT_EQ("/z/", entry->paths_.front()); |
167 EXPECT_EQ("/x/y/z/", entry->paths_.back()); | 168 EXPECT_EQ("/x/y/z/", entry->paths_.back()); |
168 } | 169 } |
169 | 170 |
170 TEST(HttpAuthCacheTest, Remove) { | 171 TEST(HttpAuthCacheTest, Remove) { |
171 GURL origin("http://foobar2.com"); | 172 GURL origin("http://foobar2.com"); |
172 | 173 |
173 scoped_refptr<HttpAuthHandler> realm1_handler = | 174 scoped_refptr<HttpAuthHandler> realm1_handler = |
174 new MockAuthHandler("basic", "Realm1", HttpAuth::AUTH_SERVER); | 175 new MockAuthHandler("basic", "Realm1", HttpAuth::AUTH_SERVER); |
175 | 176 |
176 scoped_refptr<HttpAuthHandler> realm2_handler = | 177 scoped_refptr<HttpAuthHandler> realm2_handler = |
177 new MockAuthHandler("basic", "Realm2", HttpAuth::AUTH_SERVER); | 178 new MockAuthHandler("basic", "Realm2", HttpAuth::AUTH_SERVER); |
178 | 179 |
179 scoped_refptr<HttpAuthHandler> realm3_handler = | 180 scoped_refptr<HttpAuthHandler> realm3_handler = |
180 new MockAuthHandler("basic", "Realm3", HttpAuth::AUTH_SERVER); | 181 new MockAuthHandler("basic", "Realm3", HttpAuth::AUTH_SERVER); |
181 | 182 |
182 HttpAuthCache cache; | 183 HttpAuthCache cache; |
183 cache.Add(origin, realm1_handler, L"alice", L"123", "/"); | 184 cache.Add(origin, realm1_handler, L"alice", L"123", "/"); |
184 cache.Add(origin, realm2_handler, L"bob", L"princess", "/"); | 185 cache.Add(origin, realm2_handler, L"bob", L"princess", "/"); |
185 cache.Add(origin, realm3_handler, L"admin", L"password", "/"); | 186 cache.Add(origin, realm3_handler, L"admin", L"password", "/"); |
186 | 187 |
187 // Fails, because there is no realm "Realm4". | 188 // Fails, because there is no realm "Realm4". |
188 EXPECT_FALSE(cache.Remove(origin, "Realm4", L"alice", L"123")); | 189 EXPECT_FALSE(cache.Remove(origin, "Realm4", L"alice", L"123")); |
189 | 190 |
190 // Fails because the origin is wrong. | 191 // Fails because the origin is wrong. |
191 EXPECT_FALSE(cache.Remove( | 192 EXPECT_FALSE(cache.Remove( |
192 GURL("http://foobar2.com:100"), "Realm1", L"alice", L"123")); | 193 GURL("http://foobar2.com:100"), "Realm1", L"alice", L"123")); |
193 | 194 |
194 // Fails because the username is wrong. | 195 // Fails because the username is wrong. |
195 EXPECT_FALSE(cache.Remove(origin, "Realm1", L"alice2", L"123")); | 196 EXPECT_FALSE(cache.Remove(origin, "Realm1", L"alice2", L"123")); |
196 | 197 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 // full this causes FIFO eviction of the first three entries. | 264 // full this causes FIFO eviction of the first three entries. |
264 TEST_F(HttpAuthCacheEvictionTest, RealmEntryEviction) { | 265 TEST_F(HttpAuthCacheEvictionTest, RealmEntryEviction) { |
265 for (int i = 0; i < kMaxRealms; ++i) | 266 for (int i = 0; i < kMaxRealms; ++i) |
266 AddRealm(i); | 267 AddRealm(i); |
267 | 268 |
268 for (int i = 0; i < kMaxRealms; ++i) | 269 for (int i = 0; i < kMaxRealms; ++i) |
269 CheckRealmExistence(i, true); | 270 CheckRealmExistence(i, true); |
270 | 271 |
271 for (int i = 0; i < 3; ++i) | 272 for (int i = 0; i < 3; ++i) |
272 AddRealm(i + kMaxRealms); | 273 AddRealm(i + kMaxRealms); |
273 | 274 |
274 for (int i = 0; i < 3; ++i) | 275 for (int i = 0; i < 3; ++i) |
275 CheckRealmExistence(i, false); | 276 CheckRealmExistence(i, false); |
276 | 277 |
277 for (int i = 0; i < kMaxRealms; ++i) | 278 for (int i = 0; i < kMaxRealms; ++i) |
278 CheckRealmExistence(i + 3, true); | 279 CheckRealmExistence(i + 3, true); |
279 } | 280 } |
280 | 281 |
281 // Add the maximum number of paths to a single realm entry. Each of these | 282 // Add the maximum number of paths to a single realm entry. Each of these |
282 // paths should be retrievable. Next add 3 more paths -- since the cache is | 283 // paths should be retrievable. Next add 3 more paths -- since the cache is |
283 // full this causes FIFO eviction of the first three paths. | 284 // full this causes FIFO eviction of the first three paths. |
(...skipping 10 matching lines...) Expand all Loading... |
294 for (int i = 0; i < 3; ++i) | 295 for (int i = 0; i < 3; ++i) |
295 CheckPathExistence(0, i, false); | 296 CheckPathExistence(0, i, false); |
296 | 297 |
297 for (int i = 0; i < kMaxPaths; ++i) | 298 for (int i = 0; i < kMaxPaths; ++i) |
298 CheckPathExistence(0, i + 3, true); | 299 CheckPathExistence(0, i + 3, true); |
299 | 300 |
300 for (int i = 0; i < kMaxRealms; ++i) | 301 for (int i = 0; i < kMaxRealms; ++i) |
301 CheckRealmExistence(i, true); | 302 CheckRealmExistence(i, true); |
302 } | 303 } |
303 | 304 |
304 } // namespace net | 305 } // namespace net |
OLD | NEW |