| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "net/ftp/ftp_auth_cache.h" | 5 #include "net/ftp/ftp_auth_cache.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 using net::AuthData; | |
| 12 using net::FtpAuthCache; | 11 using net::FtpAuthCache; |
| 13 | 12 |
| 14 TEST(FtpAuthCacheTest, LookupAddRemove) { | 13 TEST(FtpAuthCacheTest, LookupAddRemove) { |
| 15 FtpAuthCache cache; | 14 FtpAuthCache cache; |
| 16 | 15 |
| 17 GURL origin1("ftp://foo1"); | 16 GURL origin1("ftp://foo1"); |
| 18 scoped_refptr<AuthData> data1(new AuthData()); | |
| 19 | |
| 20 GURL origin2("ftp://foo2"); | 17 GURL origin2("ftp://foo2"); |
| 21 scoped_refptr<AuthData> data2(new AuthData()); | |
| 22 | |
| 23 GURL origin3("ftp://foo3"); | |
| 24 scoped_refptr<AuthData> data3(new AuthData()); | |
| 25 | 18 |
| 26 // Lookup non-existent entry. | 19 // Lookup non-existent entry. |
| 27 EXPECT_EQ(NULL, cache.Lookup(origin1)); | 20 EXPECT_EQ(NULL, cache.Lookup(origin1)); |
| 28 | 21 |
| 29 // Add entry for origin1. | 22 // Add entry for origin1. |
| 30 cache.Add(origin1, data1.get()); | 23 cache.Add(origin1, L"username1", L"password1"); |
| 31 EXPECT_EQ(data1.get(), cache.Lookup(origin1)); | 24 FtpAuthCache::Entry* entry1 = cache.Lookup(origin1); |
| 25 ASSERT_TRUE(entry1); |
| 26 EXPECT_EQ(origin1, entry1->origin); |
| 27 EXPECT_EQ(L"username1", entry1->username); |
| 28 EXPECT_EQ(L"password1", entry1->password); |
| 32 | 29 |
| 33 // Add an entry for origin2. | 30 // Add an entry for origin2. |
| 34 cache.Add(origin2, data2.get()); | 31 cache.Add(origin2, L"username2", L"password2"); |
| 35 EXPECT_EQ(data1.get(), cache.Lookup(origin1)); | 32 FtpAuthCache::Entry* entry2 = cache.Lookup(origin2); |
| 36 EXPECT_EQ(data2.get(), cache.Lookup(origin2)); | 33 ASSERT_TRUE(entry2); |
| 34 EXPECT_EQ(origin2, entry2->origin); |
| 35 EXPECT_EQ(L"username2", entry2->username); |
| 36 EXPECT_EQ(L"password2", entry2->password); |
| 37 |
| 38 // The original entry1 should still be there. |
| 39 EXPECT_EQ(entry1, cache.Lookup(origin1)); |
| 37 | 40 |
| 38 // Overwrite the entry for origin1. | 41 // Overwrite the entry for origin1. |
| 39 cache.Add(origin1, data3.get()); | 42 cache.Add(origin1, L"username3", L"password3"); |
| 40 EXPECT_EQ(data3.get(), cache.Lookup(origin1)); | 43 FtpAuthCache::Entry* entry3 = cache.Lookup(origin1); |
| 41 EXPECT_EQ(data2.get(), cache.Lookup(origin2)); | 44 ASSERT_TRUE(entry3); |
| 45 EXPECT_EQ(origin1, entry3->origin); |
| 46 EXPECT_EQ(L"username3", entry3->username); |
| 47 EXPECT_EQ(L"password3", entry3->password); |
| 42 | 48 |
| 43 // Remove entry of origin1. | 49 // Remove entry of origin1. |
| 44 cache.Remove(origin1); | 50 cache.Remove(origin1, L"username3", L"password3"); |
| 45 EXPECT_EQ(NULL, cache.Lookup(origin1)); | 51 EXPECT_EQ(NULL, cache.Lookup(origin1)); |
| 46 EXPECT_EQ(data2.get(), cache.Lookup(origin2)); | |
| 47 | 52 |
| 48 // Remove non-existent entry | 53 // Remove non-existent entry. |
| 49 cache.Remove(origin1); | 54 cache.Remove(origin1, L"username3", L"password3"); |
| 50 EXPECT_EQ(NULL, cache.Lookup(origin1)); | 55 EXPECT_EQ(NULL, cache.Lookup(origin1)); |
| 51 EXPECT_EQ(data2.get(), cache.Lookup(origin2)); | |
| 52 } | 56 } |
| 53 | 57 |
| 54 // Check that if the origin differs only by port number, it is considered | 58 // Check that if the origin differs only by port number, it is considered |
| 55 // a separate origin. | 59 // a separate origin. |
| 56 TEST(FtpAuthCacheTest, LookupWithPort) { | 60 TEST(FtpAuthCacheTest, LookupWithPort) { |
| 57 FtpAuthCache cache; | 61 FtpAuthCache cache; |
| 58 | 62 |
| 59 GURL origin1("ftp://foo:80"); | 63 GURL origin1("ftp://foo:80"); |
| 60 scoped_refptr<AuthData> data1(new AuthData()); | 64 GURL origin2("ftp://foo:21"); |
| 61 | 65 |
| 62 GURL origin2("ftp://foo:21"); | 66 cache.Add(origin1, L"username", L"password"); |
| 63 scoped_refptr<AuthData> data2(new AuthData()); | 67 cache.Add(origin2, L"username", L"password"); |
| 64 | 68 |
| 65 cache.Add(origin1, data1.get()); | 69 EXPECT_NE(cache.Lookup(origin1), cache.Lookup(origin2)); |
| 66 cache.Add(origin2, data2.get()); | |
| 67 | |
| 68 EXPECT_EQ(data1.get(), cache.Lookup(origin1)); | |
| 69 EXPECT_EQ(data2.get(), cache.Lookup(origin2)); | |
| 70 } | 70 } |
| 71 | 71 |
| 72 TEST(FtpAuthCacheTest, NormalizedKey) { | 72 TEST(FtpAuthCacheTest, NormalizedKey) { |
| 73 // GURL is automatically canonicalized. Hence the following variations in | 73 // GURL is automatically canonicalized. Hence the following variations in |
| 74 // url format should all map to the same entry (case insensitive host, | 74 // url format should all map to the same entry (case insensitive host, |
| 75 // default port of 21). | 75 // default port of 21). |
| 76 | 76 |
| 77 FtpAuthCache cache; | 77 FtpAuthCache cache; |
| 78 | 78 |
| 79 scoped_refptr<AuthData> data1(new AuthData()); | |
| 80 scoped_refptr<AuthData> data2(new AuthData()); | |
| 81 | |
| 82 // Add. | 79 // Add. |
| 83 cache.Add(GURL("ftp://HoSt:21"), data1.get()); | 80 cache.Add(GURL("ftp://HoSt:21"), L"username", L"password"); |
| 84 | 81 |
| 85 // Lookup. | 82 // Lookup. |
| 86 EXPECT_EQ(data1.get(), cache.Lookup(GURL("ftp://HoSt:21"))); | 83 FtpAuthCache::Entry* entry1 = cache.Lookup(GURL("ftp://HoSt:21")); |
| 87 EXPECT_EQ(data1.get(), cache.Lookup(GURL("ftp://host:21"))); | 84 ASSERT_TRUE(entry1); |
| 88 EXPECT_EQ(data1.get(), cache.Lookup(GURL("ftp://host"))); | 85 EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host:21"))); |
| 86 EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host"))); |
| 89 | 87 |
| 90 // Overwrite. | 88 // Overwrite. |
| 91 cache.Add(GURL("ftp://host"), data2.get()); | 89 cache.Add(GURL("ftp://host"), L"othername", L"otherword"); |
| 92 EXPECT_EQ(data2.get(), cache.Lookup(GURL("ftp://HoSt:21"))); | 90 FtpAuthCache::Entry* entry2 = cache.Lookup(GURL("ftp://HoSt:21")); |
| 91 ASSERT_TRUE(entry2); |
| 92 EXPECT_EQ(GURL("ftp://host"), entry2->origin); |
| 93 EXPECT_EQ(L"othername", entry2->username); |
| 94 EXPECT_EQ(L"otherword", entry2->password); |
| 93 | 95 |
| 94 // Remove | 96 // Remove |
| 95 cache.Remove(GURL("ftp://HOsT")); | 97 cache.Remove(GURL("ftp://HOsT"), L"othername", L"otherword"); |
| 98 EXPECT_EQ(NULL, cache.Lookup(GURL("ftp://host"))); |
| 99 } |
| 100 |
| 101 TEST(FtpAuthCacheTest, OnlyRemoveMatching) { |
| 102 FtpAuthCache cache; |
| 103 |
| 104 cache.Add(GURL("ftp://host"), L"username", L"password"); |
| 105 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); |
| 106 |
| 107 // Auth data doesn't match, shouldn't remove. |
| 108 cache.Remove(GURL("ftp://host"), L"bogus", L"bogus"); |
| 109 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); |
| 110 |
| 111 // Auth data matches, should remove. |
| 112 cache.Remove(GURL("ftp://host"), L"username", L"password"); |
| 96 EXPECT_EQ(NULL, cache.Lookup(GURL("ftp://host"))); | 113 EXPECT_EQ(NULL, cache.Lookup(GURL("ftp://host"))); |
| 97 } | 114 } |
| 98 | 115 |
| 99 TEST(FtpAuthCacheTest, EvictOldEntries) { | 116 TEST(FtpAuthCacheTest, EvictOldEntries) { |
| 100 FtpAuthCache cache; | 117 FtpAuthCache cache; |
| 101 | 118 |
| 102 scoped_refptr<AuthData> auth_data(new AuthData()); | |
| 103 | |
| 104 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) | 119 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) |
| 105 cache.Add(GURL("ftp://host" + IntToString(i)), auth_data.get()); | 120 cache.Add(GURL("ftp://host" + IntToString(i)), L"username", L"password"); |
| 106 | 121 |
| 107 // No entries should be evicted before reaching the limit. | 122 // No entries should be evicted before reaching the limit. |
| 108 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) { | 123 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) { |
| 109 EXPECT_EQ(auth_data.get(), | 124 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + IntToString(i)))); |
| 110 cache.Lookup(GURL("ftp://host" + IntToString(i)))); | |
| 111 } | 125 } |
| 112 | 126 |
| 113 // Adding one entry should cause eviction of the first entry. | 127 // Adding one entry should cause eviction of the first entry. |
| 114 cache.Add(GURL("ftp://last_host"), auth_data.get()); | 128 cache.Add(GURL("ftp://last_host"), L"username", L"password"); |
| 115 EXPECT_EQ(NULL, cache.Lookup(GURL("ftp://host0"))); | 129 EXPECT_EQ(NULL, cache.Lookup(GURL("ftp://host0"))); |
| 116 | 130 |
| 117 // Remaining entries should not get evicted. | 131 // Remaining entries should not get evicted. |
| 118 for (size_t i = 1; i < FtpAuthCache::kMaxEntries; i++) { | 132 for (size_t i = 1; i < FtpAuthCache::kMaxEntries; i++) { |
| 119 EXPECT_EQ(auth_data.get(), | 133 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + IntToString(i)))); |
| 120 cache.Lookup(GURL("ftp://host" + IntToString(i)))); | |
| 121 } | 134 } |
| 122 EXPECT_EQ(auth_data.get(), cache.Lookup(GURL("ftp://last_host"))); | 135 EXPECT_TRUE(cache.Lookup(GURL("ftp://last_host"))); |
| 123 } | 136 } |
| OLD | NEW |