Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / ftp / ftp_auth_cache_unittest.cc
blobbd47b647d6005239b4635a39a5b9ca83daf2e46a
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"
12 #include "url/gurl.h"
14 using base::ASCIIToUTF16;
15 using net::FtpAuthCache;
17 namespace {
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"));
31 } // namespace
33 TEST(FtpAuthCacheTest, LookupAddRemove) {
34 FtpAuthCache cache;
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);
45 ASSERT_TRUE(entry1);
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);
53 ASSERT_TRUE(entry2);
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);
64 ASSERT_TRUE(entry3);
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
79 // a separate origin.
80 TEST(FtpAuthCacheTest, LookupWithPort) {
81 FtpAuthCache cache;
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).
97 FtpAuthCache cache;
99 // Add.
100 cache.Add(GURL("ftp://HoSt:21"), net::AuthCredentials(kUsername, kPassword));
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")));
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());
116 // Remove
117 cache.Remove(GURL("ftp://HOsT"),
118 net::AuthCredentials(kOthername, kOtherword));
119 EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL);
122 TEST(FtpAuthCacheTest, OnlyRemoveMatching) {
123 FtpAuthCache cache;
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) {
138 FtpAuthCache cache;
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")));