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.
5 #include "net/ftp/ftp_auth_cache.h"
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"
14 using base::ASCIIToUTF16
;
15 using net::FtpAuthCache
;
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"));
33 TEST(FtpAuthCacheTest
, LookupAddRemove
) {
36 GURL
origin1("ftp://foo1");
37 GURL
origin2("ftp://foo2");
39 // Lookup non-existent entry.
40 EXPECT_TRUE(cache
.Lookup(origin1
) == NULL
);
42 // Add entry for origin1.
43 cache
.Add(origin1
, net::AuthCredentials(kUsername1
, kPassword1
));
44 FtpAuthCache::Entry
* entry1
= cache
.Lookup(origin1
);
46 EXPECT_EQ(origin1
, entry1
->origin
);
47 EXPECT_EQ(kUsername1
, entry1
->credentials
.username());
48 EXPECT_EQ(kPassword1
, entry1
->credentials
.password());
50 // Add an entry for origin2.
51 cache
.Add(origin2
, net::AuthCredentials(kUsername2
, kPassword2
));
52 FtpAuthCache::Entry
* entry2
= cache
.Lookup(origin2
);
54 EXPECT_EQ(origin2
, entry2
->origin
);
55 EXPECT_EQ(kUsername2
, entry2
->credentials
.username());
56 EXPECT_EQ(kPassword2
, entry2
->credentials
.password());
58 // The original entry1 should still be there.
59 EXPECT_EQ(entry1
, cache
.Lookup(origin1
));
61 // Overwrite the entry for origin1.
62 cache
.Add(origin1
, net::AuthCredentials(kUsername3
, kPassword3
));
63 FtpAuthCache::Entry
* entry3
= cache
.Lookup(origin1
);
65 EXPECT_EQ(origin1
, entry3
->origin
);
66 EXPECT_EQ(kUsername3
, entry3
->credentials
.username());
67 EXPECT_EQ(kPassword3
, entry3
->credentials
.password());
69 // Remove entry of origin1.
70 cache
.Remove(origin1
, net::AuthCredentials(kUsername3
, kPassword3
));
71 EXPECT_TRUE(cache
.Lookup(origin1
) == NULL
);
73 // Remove non-existent entry.
74 cache
.Remove(origin1
, net::AuthCredentials(kUsername3
, kPassword3
));
75 EXPECT_TRUE(cache
.Lookup(origin1
) == NULL
);
78 // Check that if the origin differs only by port number, it is considered
80 TEST(FtpAuthCacheTest
, LookupWithPort
) {
83 GURL
origin1("ftp://foo:80");
84 GURL
origin2("ftp://foo:21");
86 cache
.Add(origin1
, net::AuthCredentials(kUsername
, kPassword
));
87 cache
.Add(origin2
, net::AuthCredentials(kUsername
, kPassword
));
89 EXPECT_NE(cache
.Lookup(origin1
), cache
.Lookup(origin2
));
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).
100 cache
.Add(GURL("ftp://HoSt:21"), net::AuthCredentials(kUsername
, kPassword
));
103 FtpAuthCache::Entry
* entry1
= cache
.Lookup(GURL("ftp://HoSt:21"));
105 EXPECT_EQ(entry1
, cache
.Lookup(GURL("ftp://host:21")));
106 EXPECT_EQ(entry1
, cache
.Lookup(GURL("ftp://host")));
109 cache
.Add(GURL("ftp://host"), net::AuthCredentials(kOthername
, kOtherword
));
110 FtpAuthCache::Entry
* entry2
= cache
.Lookup(GURL("ftp://HoSt:21"));
112 EXPECT_EQ(GURL("ftp://host"), entry2
->origin
);
113 EXPECT_EQ(kOthername
, entry2
->credentials
.username());
114 EXPECT_EQ(kOtherword
, entry2
->credentials
.password());
117 cache
.Remove(GURL("ftp://HOsT"),
118 net::AuthCredentials(kOthername
, kOtherword
));
119 EXPECT_TRUE(cache
.Lookup(GURL("ftp://host")) == NULL
);
122 TEST(FtpAuthCacheTest
, OnlyRemoveMatching
) {
125 cache
.Add(GURL("ftp://host"), net::AuthCredentials(kUsername
, kPassword
));
126 EXPECT_TRUE(cache
.Lookup(GURL("ftp://host")));
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")));
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
);
137 TEST(FtpAuthCacheTest
, EvictOldEntries
) {
140 for (size_t i
= 0; i
< FtpAuthCache::kMaxEntries
; i
++) {
141 cache
.Add(GURL("ftp://host" + base::IntToString(i
)),
142 net::AuthCredentials(kUsername
, kPassword
));
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
))));
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
);
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
))));
159 EXPECT_TRUE(cache
.Lookup(GURL("ftp://last_host")));