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
;
20 const base::string16
kBogus(ASCIIToUTF16("bogus"));
21 const base::string16
kOthername(ASCIIToUTF16("othername"));
22 const base::string16
kOtherword(ASCIIToUTF16("otherword"));
23 const base::string16
kPassword(ASCIIToUTF16("password"));
24 const base::string16
kPassword1(ASCIIToUTF16("password1"));
25 const base::string16
kPassword2(ASCIIToUTF16("password2"));
26 const base::string16
kPassword3(ASCIIToUTF16("password3"));
27 const base::string16
kUsername(ASCIIToUTF16("username"));
28 const base::string16
kUsername1(ASCIIToUTF16("username1"));
29 const base::string16
kUsername2(ASCIIToUTF16("username2"));
30 const base::string16
kUsername3(ASCIIToUTF16("username3"));
34 TEST(FtpAuthCacheTest
, LookupAddRemove
) {
37 GURL
origin1("ftp://foo1");
38 GURL
origin2("ftp://foo2");
40 // Lookup non-existent entry.
41 EXPECT_TRUE(cache
.Lookup(origin1
) == NULL
);
43 // Add entry for origin1.
44 cache
.Add(origin1
, AuthCredentials(kUsername1
, kPassword1
));
45 FtpAuthCache::Entry
* entry1
= cache
.Lookup(origin1
);
47 EXPECT_EQ(origin1
, entry1
->origin
);
48 EXPECT_EQ(kUsername1
, entry1
->credentials
.username());
49 EXPECT_EQ(kPassword1
, entry1
->credentials
.password());
51 // Add an entry for origin2.
52 cache
.Add(origin2
, AuthCredentials(kUsername2
, kPassword2
));
53 FtpAuthCache::Entry
* entry2
= cache
.Lookup(origin2
);
55 EXPECT_EQ(origin2
, entry2
->origin
);
56 EXPECT_EQ(kUsername2
, entry2
->credentials
.username());
57 EXPECT_EQ(kPassword2
, entry2
->credentials
.password());
59 // The original entry1 should still be there.
60 EXPECT_EQ(entry1
, cache
.Lookup(origin1
));
62 // Overwrite the entry for origin1.
63 cache
.Add(origin1
, AuthCredentials(kUsername3
, kPassword3
));
64 FtpAuthCache::Entry
* entry3
= cache
.Lookup(origin1
);
66 EXPECT_EQ(origin1
, entry3
->origin
);
67 EXPECT_EQ(kUsername3
, entry3
->credentials
.username());
68 EXPECT_EQ(kPassword3
, entry3
->credentials
.password());
70 // Remove entry of origin1.
71 cache
.Remove(origin1
, AuthCredentials(kUsername3
, kPassword3
));
72 EXPECT_TRUE(cache
.Lookup(origin1
) == NULL
);
74 // Remove non-existent entry.
75 cache
.Remove(origin1
, AuthCredentials(kUsername3
, kPassword3
));
76 EXPECT_TRUE(cache
.Lookup(origin1
) == NULL
);
79 // Check that if the origin differs only by port number, it is considered
81 TEST(FtpAuthCacheTest
, LookupWithPort
) {
84 GURL
origin1("ftp://foo:80");
85 GURL
origin2("ftp://foo:21");
87 cache
.Add(origin1
, AuthCredentials(kUsername
, kPassword
));
88 cache
.Add(origin2
, AuthCredentials(kUsername
, kPassword
));
90 EXPECT_NE(cache
.Lookup(origin1
), cache
.Lookup(origin2
));
93 TEST(FtpAuthCacheTest
, NormalizedKey
) {
94 // GURL is automatically canonicalized. Hence the following variations in
95 // url format should all map to the same entry (case insensitive host,
96 // default port of 21).
101 cache
.Add(GURL("ftp://HoSt:21"), AuthCredentials(kUsername
, kPassword
));
104 FtpAuthCache::Entry
* entry1
= cache
.Lookup(GURL("ftp://HoSt:21"));
106 EXPECT_EQ(entry1
, cache
.Lookup(GURL("ftp://host:21")));
107 EXPECT_EQ(entry1
, cache
.Lookup(GURL("ftp://host")));
110 cache
.Add(GURL("ftp://host"), AuthCredentials(kOthername
, kOtherword
));
111 FtpAuthCache::Entry
* entry2
= cache
.Lookup(GURL("ftp://HoSt:21"));
113 EXPECT_EQ(GURL("ftp://host"), entry2
->origin
);
114 EXPECT_EQ(kOthername
, entry2
->credentials
.username());
115 EXPECT_EQ(kOtherword
, entry2
->credentials
.password());
118 cache
.Remove(GURL("ftp://HOsT"), AuthCredentials(kOthername
, kOtherword
));
119 EXPECT_TRUE(cache
.Lookup(GURL("ftp://host")) == NULL
);
122 TEST(FtpAuthCacheTest
, OnlyRemoveMatching
) {
125 cache
.Add(GURL("ftp://host"), 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"), AuthCredentials(kBogus
, kBogus
));
130 EXPECT_TRUE(cache
.Lookup(GURL("ftp://host")));
132 // Auth data matches, should remove.
133 cache
.Remove(GURL("ftp://host"), 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 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"), AuthCredentials(kUsername
, kPassword
));
152 EXPECT_TRUE(cache
.Lookup(GURL("ftp://host0")) == NULL
);
154 // Remaining entries should not get evicted.
155 for (size_t i
= 1; i
< FtpAuthCache::kMaxEntries
; i
++) {
156 EXPECT_TRUE(cache
.Lookup(GURL("ftp://host" + base::IntToString(i
))));
158 EXPECT_TRUE(cache
.Lookup(GURL("ftp://last_host")));