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