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