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/string_number_conversions.h"
8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h"
10 #include "googleurl/src/gurl.h"
11 #include "net/base/auth.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using net::FtpAuthCache
;
18 const string16
kBogus(ASCIIToUTF16("bogus"));
19 const string16
kOthername(ASCIIToUTF16("othername"));
20 const string16
kOtherword(ASCIIToUTF16("otherword"));
21 const string16
kPassword(ASCIIToUTF16("password"));
22 const string16
kPassword1(ASCIIToUTF16("password1"));
23 const string16
kPassword2(ASCIIToUTF16("password2"));
24 const string16
kPassword3(ASCIIToUTF16("password3"));
25 const string16
kUsername(ASCIIToUTF16("username"));
26 const string16
kUsername1(ASCIIToUTF16("username1"));
27 const string16
kUsername2(ASCIIToUTF16("username2"));
28 const string16
kUsername3(ASCIIToUTF16("username3"));
32 TEST(FtpAuthCacheTest
, LookupAddRemove
) {
35 GURL
origin1("ftp://foo1");
36 GURL
origin2("ftp://foo2");
38 // Lookup non-existent entry.
39 EXPECT_TRUE(cache
.Lookup(origin1
) == NULL
);
41 // Add entry for origin1.
42 cache
.Add(origin1
, net::AuthCredentials(kUsername1
, kPassword1
));
43 FtpAuthCache::Entry
* entry1
= cache
.Lookup(origin1
);
45 EXPECT_EQ(origin1
, entry1
->origin
);
46 EXPECT_EQ(kUsername1
, entry1
->credentials
.username());
47 EXPECT_EQ(kPassword1
, entry1
->credentials
.password());
49 // Add an entry for origin2.
50 cache
.Add(origin2
, net::AuthCredentials(kUsername2
, kPassword2
));
51 FtpAuthCache::Entry
* entry2
= cache
.Lookup(origin2
);
53 EXPECT_EQ(origin2
, entry2
->origin
);
54 EXPECT_EQ(kUsername2
, entry2
->credentials
.username());
55 EXPECT_EQ(kPassword2
, entry2
->credentials
.password());
57 // The original entry1 should still be there.
58 EXPECT_EQ(entry1
, cache
.Lookup(origin1
));
60 // Overwrite the entry for origin1.
61 cache
.Add(origin1
, net::AuthCredentials(kUsername3
, kPassword3
));
62 FtpAuthCache::Entry
* entry3
= cache
.Lookup(origin1
);
64 EXPECT_EQ(origin1
, entry3
->origin
);
65 EXPECT_EQ(kUsername3
, entry3
->credentials
.username());
66 EXPECT_EQ(kPassword3
, entry3
->credentials
.password());
68 // Remove entry of origin1.
69 cache
.Remove(origin1
, net::AuthCredentials(kUsername3
, kPassword3
));
70 EXPECT_TRUE(cache
.Lookup(origin1
) == NULL
);
72 // Remove non-existent entry.
73 cache
.Remove(origin1
, net::AuthCredentials(kUsername3
, kPassword3
));
74 EXPECT_TRUE(cache
.Lookup(origin1
) == NULL
);
77 // Check that if the origin differs only by port number, it is considered
79 TEST(FtpAuthCacheTest
, LookupWithPort
) {
82 GURL
origin1("ftp://foo:80");
83 GURL
origin2("ftp://foo:21");
85 cache
.Add(origin1
, net::AuthCredentials(kUsername
, kPassword
));
86 cache
.Add(origin2
, net::AuthCredentials(kUsername
, kPassword
));
88 EXPECT_NE(cache
.Lookup(origin1
), cache
.Lookup(origin2
));
91 TEST(FtpAuthCacheTest
, NormalizedKey
) {
92 // GURL is automatically canonicalized. Hence the following variations in
93 // url format should all map to the same entry (case insensitive host,
94 // default port of 21).
99 cache
.Add(GURL("ftp://HoSt:21"), net::AuthCredentials(kUsername
, kPassword
));
102 FtpAuthCache::Entry
* entry1
= cache
.Lookup(GURL("ftp://HoSt:21"));
104 EXPECT_EQ(entry1
, cache
.Lookup(GURL("ftp://host:21")));
105 EXPECT_EQ(entry1
, cache
.Lookup(GURL("ftp://host")));
108 cache
.Add(GURL("ftp://host"), net::AuthCredentials(kOthername
, kOtherword
));
109 FtpAuthCache::Entry
* entry2
= cache
.Lookup(GURL("ftp://HoSt:21"));
111 EXPECT_EQ(GURL("ftp://host"), entry2
->origin
);
112 EXPECT_EQ(kOthername
, entry2
->credentials
.username());
113 EXPECT_EQ(kOtherword
, entry2
->credentials
.password());
116 cache
.Remove(GURL("ftp://HOsT"),
117 net::AuthCredentials(kOthername
, kOtherword
));
118 EXPECT_TRUE(cache
.Lookup(GURL("ftp://host")) == NULL
);
121 TEST(FtpAuthCacheTest
, OnlyRemoveMatching
) {
124 cache
.Add(GURL("ftp://host"), net::AuthCredentials(kUsername
, kPassword
));
125 EXPECT_TRUE(cache
.Lookup(GURL("ftp://host")));
127 // Auth data doesn't match, shouldn't remove.
128 cache
.Remove(GURL("ftp://host"), net::AuthCredentials(kBogus
, kBogus
));
129 EXPECT_TRUE(cache
.Lookup(GURL("ftp://host")));
131 // Auth data matches, should remove.
132 cache
.Remove(GURL("ftp://host"), net::AuthCredentials(kUsername
, kPassword
));
133 EXPECT_TRUE(cache
.Lookup(GURL("ftp://host")) == NULL
);
136 TEST(FtpAuthCacheTest
, EvictOldEntries
) {
139 for (size_t i
= 0; i
< FtpAuthCache::kMaxEntries
; i
++) {
140 cache
.Add(GURL("ftp://host" + base::IntToString(i
)),
141 net::AuthCredentials(kUsername
, kPassword
));
144 // No entries should be evicted before reaching the limit.
145 for (size_t i
= 0; i
< FtpAuthCache::kMaxEntries
; i
++) {
146 EXPECT_TRUE(cache
.Lookup(GURL("ftp://host" + base::IntToString(i
))));
149 // Adding one entry should cause eviction of the first entry.
150 cache
.Add(GURL("ftp://last_host"),
151 net::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")));