- add sources.
[platform/framework/web/crosswalk.git] / src / net / ftp / ftp_auth_cache_unittest.cc
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.
4
5 #include "net/ftp/ftp_auth_cache.h"
6
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"
13
14 using net::FtpAuthCache;
15
16 namespace {
17
18 const base::string16 kBogus(ASCIIToUTF16("bogus"));
19 const base::string16 kOthername(ASCIIToUTF16("othername"));
20 const base::string16 kOtherword(ASCIIToUTF16("otherword"));
21 const base::string16 kPassword(ASCIIToUTF16("password"));
22 const base::string16 kPassword1(ASCIIToUTF16("password1"));
23 const base::string16 kPassword2(ASCIIToUTF16("password2"));
24 const base::string16 kPassword3(ASCIIToUTF16("password3"));
25 const base::string16 kUsername(ASCIIToUTF16("username"));
26 const base::string16 kUsername1(ASCIIToUTF16("username1"));
27 const base::string16 kUsername2(ASCIIToUTF16("username2"));
28 const base::string16 kUsername3(ASCIIToUTF16("username3"));
29
30 }  // namespace
31
32 TEST(FtpAuthCacheTest, LookupAddRemove) {
33   FtpAuthCache cache;
34
35   GURL origin1("ftp://foo1");
36   GURL origin2("ftp://foo2");
37
38   // Lookup non-existent entry.
39   EXPECT_TRUE(cache.Lookup(origin1) == NULL);
40
41   // Add entry for origin1.
42   cache.Add(origin1, net::AuthCredentials(kUsername1, kPassword1));
43   FtpAuthCache::Entry* entry1 = cache.Lookup(origin1);
44   ASSERT_TRUE(entry1);
45   EXPECT_EQ(origin1, entry1->origin);
46   EXPECT_EQ(kUsername1, entry1->credentials.username());
47   EXPECT_EQ(kPassword1, entry1->credentials.password());
48
49   // Add an entry for origin2.
50   cache.Add(origin2, net::AuthCredentials(kUsername2, kPassword2));
51   FtpAuthCache::Entry* entry2 = cache.Lookup(origin2);
52   ASSERT_TRUE(entry2);
53   EXPECT_EQ(origin2, entry2->origin);
54   EXPECT_EQ(kUsername2, entry2->credentials.username());
55   EXPECT_EQ(kPassword2, entry2->credentials.password());
56
57   // The original entry1 should still be there.
58   EXPECT_EQ(entry1, cache.Lookup(origin1));
59
60   // Overwrite the entry for origin1.
61   cache.Add(origin1, net::AuthCredentials(kUsername3, kPassword3));
62   FtpAuthCache::Entry* entry3 = cache.Lookup(origin1);
63   ASSERT_TRUE(entry3);
64   EXPECT_EQ(origin1, entry3->origin);
65   EXPECT_EQ(kUsername3, entry3->credentials.username());
66   EXPECT_EQ(kPassword3, entry3->credentials.password());
67
68   // Remove entry of origin1.
69   cache.Remove(origin1, net::AuthCredentials(kUsername3, kPassword3));
70   EXPECT_TRUE(cache.Lookup(origin1) == NULL);
71
72   // Remove non-existent entry.
73   cache.Remove(origin1, net::AuthCredentials(kUsername3, kPassword3));
74   EXPECT_TRUE(cache.Lookup(origin1) == NULL);
75 }
76
77 // Check that if the origin differs only by port number, it is considered
78 // a separate origin.
79 TEST(FtpAuthCacheTest, LookupWithPort) {
80   FtpAuthCache cache;
81
82   GURL origin1("ftp://foo:80");
83   GURL origin2("ftp://foo:21");
84
85   cache.Add(origin1, net::AuthCredentials(kUsername, kPassword));
86   cache.Add(origin2, net::AuthCredentials(kUsername, kPassword));
87
88   EXPECT_NE(cache.Lookup(origin1), cache.Lookup(origin2));
89 }
90
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).
95
96   FtpAuthCache cache;
97
98   // Add.
99   cache.Add(GURL("ftp://HoSt:21"), net::AuthCredentials(kUsername, kPassword));
100
101   // Lookup.
102   FtpAuthCache::Entry* entry1 = cache.Lookup(GURL("ftp://HoSt:21"));
103   ASSERT_TRUE(entry1);
104   EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host:21")));
105   EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host")));
106
107   // Overwrite.
108   cache.Add(GURL("ftp://host"), net::AuthCredentials(kOthername, kOtherword));
109   FtpAuthCache::Entry* entry2 = cache.Lookup(GURL("ftp://HoSt:21"));
110   ASSERT_TRUE(entry2);
111   EXPECT_EQ(GURL("ftp://host"), entry2->origin);
112   EXPECT_EQ(kOthername, entry2->credentials.username());
113   EXPECT_EQ(kOtherword, entry2->credentials.password());
114
115   // Remove
116   cache.Remove(GURL("ftp://HOsT"),
117                net::AuthCredentials(kOthername, kOtherword));
118   EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL);
119 }
120
121 TEST(FtpAuthCacheTest, OnlyRemoveMatching) {
122   FtpAuthCache cache;
123
124   cache.Add(GURL("ftp://host"), net::AuthCredentials(kUsername, kPassword));
125   EXPECT_TRUE(cache.Lookup(GURL("ftp://host")));
126
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")));
130
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);
134 }
135
136 TEST(FtpAuthCacheTest, EvictOldEntries) {
137   FtpAuthCache cache;
138
139   for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) {
140     cache.Add(GURL("ftp://host" + base::IntToString(i)),
141               net::AuthCredentials(kUsername, kPassword));
142   }
143
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))));
147   }
148
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);
153
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))));
157   }
158   EXPECT_TRUE(cache.Lookup(GURL("ftp://last_host")));
159 }