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