| 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 "net/ftp/ftp_auth_cache.h" | 5 #include "net/ftp/ftp_auth_cache.h" |
| 6 | 6 |
| 7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| 11 #include "net/base/auth.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 using net::FtpAuthCache; | 14 using net::FtpAuthCache; |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 const string16 kBogus(ASCIIToUTF16("bogus")); | 18 const string16 kBogus(ASCIIToUTF16("bogus")); |
| 18 const string16 kOthername(ASCIIToUTF16("othername")); | 19 const string16 kOthername(ASCIIToUTF16("othername")); |
| 19 const string16 kOtherword(ASCIIToUTF16("otherword")); | 20 const string16 kOtherword(ASCIIToUTF16("otherword")); |
| 20 const string16 kPassword(ASCIIToUTF16("password")); | 21 const string16 kPassword(ASCIIToUTF16("password")); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 31 TEST(FtpAuthCacheTest, LookupAddRemove) { | 32 TEST(FtpAuthCacheTest, LookupAddRemove) { |
| 32 FtpAuthCache cache; | 33 FtpAuthCache cache; |
| 33 | 34 |
| 34 GURL origin1("ftp://foo1"); | 35 GURL origin1("ftp://foo1"); |
| 35 GURL origin2("ftp://foo2"); | 36 GURL origin2("ftp://foo2"); |
| 36 | 37 |
| 37 // Lookup non-existent entry. | 38 // Lookup non-existent entry. |
| 38 EXPECT_TRUE(cache.Lookup(origin1) == NULL); | 39 EXPECT_TRUE(cache.Lookup(origin1) == NULL); |
| 39 | 40 |
| 40 // Add entry for origin1. | 41 // Add entry for origin1. |
| 41 cache.Add(origin1, kUsername1, kPassword1); | 42 cache.Add(origin1, net::AuthCredentials(kUsername1, kPassword1)); |
| 42 FtpAuthCache::Entry* entry1 = cache.Lookup(origin1); | 43 FtpAuthCache::Entry* entry1 = cache.Lookup(origin1); |
| 43 ASSERT_TRUE(entry1); | 44 ASSERT_TRUE(entry1); |
| 44 EXPECT_EQ(origin1, entry1->origin); | 45 EXPECT_EQ(origin1, entry1->origin); |
| 45 EXPECT_EQ(kUsername1, entry1->username); | 46 EXPECT_EQ(kUsername1, entry1->credentials.username()); |
| 46 EXPECT_EQ(kPassword1, entry1->password); | 47 EXPECT_EQ(kPassword1, entry1->credentials.password()); |
| 47 | 48 |
| 48 // Add an entry for origin2. | 49 // Add an entry for origin2. |
| 49 cache.Add(origin2, kUsername2, kPassword2); | 50 cache.Add(origin2, net::AuthCredentials(kUsername2, kPassword2)); |
| 50 FtpAuthCache::Entry* entry2 = cache.Lookup(origin2); | 51 FtpAuthCache::Entry* entry2 = cache.Lookup(origin2); |
| 51 ASSERT_TRUE(entry2); | 52 ASSERT_TRUE(entry2); |
| 52 EXPECT_EQ(origin2, entry2->origin); | 53 EXPECT_EQ(origin2, entry2->origin); |
| 53 EXPECT_EQ(kUsername2, entry2->username); | 54 EXPECT_EQ(kUsername2, entry2->credentials.username()); |
| 54 EXPECT_EQ(kPassword2, entry2->password); | 55 EXPECT_EQ(kPassword2, entry2->credentials.password()); |
| 55 | 56 |
| 56 // The original entry1 should still be there. | 57 // The original entry1 should still be there. |
| 57 EXPECT_EQ(entry1, cache.Lookup(origin1)); | 58 EXPECT_EQ(entry1, cache.Lookup(origin1)); |
| 58 | 59 |
| 59 // Overwrite the entry for origin1. | 60 // Overwrite the entry for origin1. |
| 60 cache.Add(origin1, kUsername3, kPassword3); | 61 cache.Add(origin1, net::AuthCredentials(kUsername3, kPassword3)); |
| 61 FtpAuthCache::Entry* entry3 = cache.Lookup(origin1); | 62 FtpAuthCache::Entry* entry3 = cache.Lookup(origin1); |
| 62 ASSERT_TRUE(entry3); | 63 ASSERT_TRUE(entry3); |
| 63 EXPECT_EQ(origin1, entry3->origin); | 64 EXPECT_EQ(origin1, entry3->origin); |
| 64 EXPECT_EQ(kUsername3, entry3->username); | 65 EXPECT_EQ(kUsername3, entry3->credentials.username()); |
| 65 EXPECT_EQ(kPassword3, entry3->password); | 66 EXPECT_EQ(kPassword3, entry3->credentials.password()); |
| 66 | 67 |
| 67 // Remove entry of origin1. | 68 // Remove entry of origin1. |
| 68 cache.Remove(origin1, kUsername3, kPassword3); | 69 cache.Remove(origin1, net::AuthCredentials(kUsername3, kPassword3)); |
| 69 EXPECT_TRUE(cache.Lookup(origin1) == NULL); | 70 EXPECT_TRUE(cache.Lookup(origin1) == NULL); |
| 70 | 71 |
| 71 // Remove non-existent entry. | 72 // Remove non-existent entry. |
| 72 cache.Remove(origin1, kUsername3, kPassword3); | 73 cache.Remove(origin1, net::AuthCredentials(kUsername3, kPassword3)); |
| 73 EXPECT_TRUE(cache.Lookup(origin1) == NULL); | 74 EXPECT_TRUE(cache.Lookup(origin1) == NULL); |
| 74 } | 75 } |
| 75 | 76 |
| 76 // Check that if the origin differs only by port number, it is considered | 77 // Check that if the origin differs only by port number, it is considered |
| 77 // a separate origin. | 78 // a separate origin. |
| 78 TEST(FtpAuthCacheTest, LookupWithPort) { | 79 TEST(FtpAuthCacheTest, LookupWithPort) { |
| 79 FtpAuthCache cache; | 80 FtpAuthCache cache; |
| 80 | 81 |
| 81 GURL origin1("ftp://foo:80"); | 82 GURL origin1("ftp://foo:80"); |
| 82 GURL origin2("ftp://foo:21"); | 83 GURL origin2("ftp://foo:21"); |
| 83 | 84 |
| 84 cache.Add(origin1, kUsername, kPassword); | 85 cache.Add(origin1, net::AuthCredentials(kUsername, kPassword)); |
| 85 cache.Add(origin2, kUsername, kPassword); | 86 cache.Add(origin2, net::AuthCredentials(kUsername, kPassword)); |
| 86 | 87 |
| 87 EXPECT_NE(cache.Lookup(origin1), cache.Lookup(origin2)); | 88 EXPECT_NE(cache.Lookup(origin1), cache.Lookup(origin2)); |
| 88 } | 89 } |
| 89 | 90 |
| 90 TEST(FtpAuthCacheTest, NormalizedKey) { | 91 TEST(FtpAuthCacheTest, NormalizedKey) { |
| 91 // GURL is automatically canonicalized. Hence the following variations in | 92 // GURL is automatically canonicalized. Hence the following variations in |
| 92 // url format should all map to the same entry (case insensitive host, | 93 // url format should all map to the same entry (case insensitive host, |
| 93 // default port of 21). | 94 // default port of 21). |
| 94 | 95 |
| 95 FtpAuthCache cache; | 96 FtpAuthCache cache; |
| 96 | 97 |
| 97 // Add. | 98 // Add. |
| 98 cache.Add(GURL("ftp://HoSt:21"), kUsername, kPassword); | 99 cache.Add(GURL("ftp://HoSt:21"), net::AuthCredentials(kUsername, kPassword)); |
| 99 | 100 |
| 100 // Lookup. | 101 // Lookup. |
| 101 FtpAuthCache::Entry* entry1 = cache.Lookup(GURL("ftp://HoSt:21")); | 102 FtpAuthCache::Entry* entry1 = cache.Lookup(GURL("ftp://HoSt:21")); |
| 102 ASSERT_TRUE(entry1); | 103 ASSERT_TRUE(entry1); |
| 103 EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host:21"))); | 104 EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host:21"))); |
| 104 EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host"))); | 105 EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host"))); |
| 105 | 106 |
| 106 // Overwrite. | 107 // Overwrite. |
| 107 cache.Add(GURL("ftp://host"), kOthername, kOtherword); | 108 cache.Add(GURL("ftp://host"), net::AuthCredentials(kOthername, kOtherword)); |
| 108 FtpAuthCache::Entry* entry2 = cache.Lookup(GURL("ftp://HoSt:21")); | 109 FtpAuthCache::Entry* entry2 = cache.Lookup(GURL("ftp://HoSt:21")); |
| 109 ASSERT_TRUE(entry2); | 110 ASSERT_TRUE(entry2); |
| 110 EXPECT_EQ(GURL("ftp://host"), entry2->origin); | 111 EXPECT_EQ(GURL("ftp://host"), entry2->origin); |
| 111 EXPECT_EQ(kOthername, entry2->username); | 112 EXPECT_EQ(kOthername, entry2->credentials.username()); |
| 112 EXPECT_EQ(kOtherword, entry2->password); | 113 EXPECT_EQ(kOtherword, entry2->credentials.password()); |
| 113 | 114 |
| 114 // Remove | 115 // Remove |
| 115 cache.Remove(GURL("ftp://HOsT"), kOthername, kOtherword); | 116 cache.Remove(GURL("ftp://HOsT"), |
| 117 net::AuthCredentials(kOthername, kOtherword)); |
| 116 EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL); | 118 EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL); |
| 117 } | 119 } |
| 118 | 120 |
| 119 TEST(FtpAuthCacheTest, OnlyRemoveMatching) { | 121 TEST(FtpAuthCacheTest, OnlyRemoveMatching) { |
| 120 FtpAuthCache cache; | 122 FtpAuthCache cache; |
| 121 | 123 |
| 122 cache.Add(GURL("ftp://host"), kUsername, kPassword); | 124 cache.Add(GURL("ftp://host"), net::AuthCredentials(kUsername, kPassword)); |
| 123 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); | 125 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); |
| 124 | 126 |
| 125 // Auth data doesn't match, shouldn't remove. | 127 // Auth data doesn't match, shouldn't remove. |
| 126 cache.Remove(GURL("ftp://host"), kBogus, kBogus); | 128 cache.Remove(GURL("ftp://host"), net::AuthCredentials(kBogus, kBogus)); |
| 127 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); | 129 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); |
| 128 | 130 |
| 129 // Auth data matches, should remove. | 131 // Auth data matches, should remove. |
| 130 cache.Remove(GURL("ftp://host"), kUsername, kPassword); | 132 cache.Remove(GURL("ftp://host"), net::AuthCredentials(kUsername, kPassword)); |
| 131 EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL); | 133 EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL); |
| 132 } | 134 } |
| 133 | 135 |
| 134 TEST(FtpAuthCacheTest, EvictOldEntries) { | 136 TEST(FtpAuthCacheTest, EvictOldEntries) { |
| 135 FtpAuthCache cache; | 137 FtpAuthCache cache; |
| 136 | 138 |
| 137 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) { | 139 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) { |
| 138 cache.Add(GURL("ftp://host" + base::IntToString(i)), | 140 cache.Add(GURL("ftp://host" + base::IntToString(i)), |
| 139 kUsername, kPassword); | 141 net::AuthCredentials(kUsername, kPassword)); |
| 140 } | 142 } |
| 141 | 143 |
| 142 // No entries should be evicted before reaching the limit. | 144 // No entries should be evicted before reaching the limit. |
| 143 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) { | 145 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) { |
| 144 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + base::IntToString(i)))); | 146 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + base::IntToString(i)))); |
| 145 } | 147 } |
| 146 | 148 |
| 147 // Adding one entry should cause eviction of the first entry. | 149 // Adding one entry should cause eviction of the first entry. |
| 148 cache.Add(GURL("ftp://last_host"), kUsername, kPassword); | 150 cache.Add(GURL("ftp://last_host"), |
| 151 net::AuthCredentials(kUsername, kPassword)); |
| 149 EXPECT_TRUE(cache.Lookup(GURL("ftp://host0")) == NULL); | 152 EXPECT_TRUE(cache.Lookup(GURL("ftp://host0")) == NULL); |
| 150 | 153 |
| 151 // Remaining entries should not get evicted. | 154 // Remaining entries should not get evicted. |
| 152 for (size_t i = 1; i < FtpAuthCache::kMaxEntries; i++) { | 155 for (size_t i = 1; i < FtpAuthCache::kMaxEntries; i++) { |
| 153 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + base::IntToString(i)))); | 156 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + base::IntToString(i)))); |
| 154 } | 157 } |
| 155 EXPECT_TRUE(cache.Lookup(GURL("ftp://last_host"))); | 158 EXPECT_TRUE(cache.Lookup(GURL("ftp://last_host"))); |
| 156 } | 159 } |
| OLD | NEW |