1 // Copyright (c) 2009 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/string_util.h" 8 #include "googleurl/src/gurl.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 using net::FtpAuthCache; 12 13 TEST(FtpAuthCacheTest, LookupAddRemove) { 14 FtpAuthCache cache; 15 16 GURL origin1("ftp://foo1"); 17 GURL origin2("ftp://foo2"); 18 19 // Lookup non-existent entry. 20 EXPECT_TRUE(cache.Lookup(origin1) == NULL); 21 22 // Add entry for origin1. 23 cache.Add(origin1, L"username1", L"password1"); 24 FtpAuthCache::Entry* entry1 = cache.Lookup(origin1); 25 ASSERT_TRUE(entry1); 26 EXPECT_EQ(origin1, entry1->origin); 27 EXPECT_EQ(L"username1", entry1->username); 28 EXPECT_EQ(L"password1", entry1->password); 29 30 // Add an entry for origin2. 31 cache.Add(origin2, L"username2", L"password2"); 32 FtpAuthCache::Entry* entry2 = cache.Lookup(origin2); 33 ASSERT_TRUE(entry2); 34 EXPECT_EQ(origin2, entry2->origin); 35 EXPECT_EQ(L"username2", entry2->username); 36 EXPECT_EQ(L"password2", entry2->password); 37 38 // The original entry1 should still be there. 39 EXPECT_EQ(entry1, cache.Lookup(origin1)); 40 41 // Overwrite the entry for origin1. 42 cache.Add(origin1, L"username3", L"password3"); 43 FtpAuthCache::Entry* entry3 = cache.Lookup(origin1); 44 ASSERT_TRUE(entry3); 45 EXPECT_EQ(origin1, entry3->origin); 46 EXPECT_EQ(L"username3", entry3->username); 47 EXPECT_EQ(L"password3", entry3->password); 48 49 // Remove entry of origin1. 50 cache.Remove(origin1, L"username3", L"password3"); 51 EXPECT_TRUE(cache.Lookup(origin1) == NULL); 52 53 // Remove non-existent entry. 54 cache.Remove(origin1, L"username3", L"password3"); 55 EXPECT_TRUE(cache.Lookup(origin1) == NULL); 56 } 57 58 // Check that if the origin differs only by port number, it is considered 59 // a separate origin. 60 TEST(FtpAuthCacheTest, LookupWithPort) { 61 FtpAuthCache cache; 62 63 GURL origin1("ftp://foo:80"); 64 GURL origin2("ftp://foo:21"); 65 66 cache.Add(origin1, L"username", L"password"); 67 cache.Add(origin2, L"username", L"password"); 68 69 EXPECT_NE(cache.Lookup(origin1), cache.Lookup(origin2)); 70 } 71 72 TEST(FtpAuthCacheTest, NormalizedKey) { 73 // GURL is automatically canonicalized. Hence the following variations in 74 // url format should all map to the same entry (case insensitive host, 75 // default port of 21). 76 77 FtpAuthCache cache; 78 79 // Add. 80 cache.Add(GURL("ftp://HoSt:21"), L"username", L"password"); 81 82 // Lookup. 83 FtpAuthCache::Entry* entry1 = cache.Lookup(GURL("ftp://HoSt:21")); 84 ASSERT_TRUE(entry1); 85 EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host:21"))); 86 EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host"))); 87 88 // Overwrite. 89 cache.Add(GURL("ftp://host"), L"othername", L"otherword"); 90 FtpAuthCache::Entry* entry2 = cache.Lookup(GURL("ftp://HoSt:21")); 91 ASSERT_TRUE(entry2); 92 EXPECT_EQ(GURL("ftp://host"), entry2->origin); 93 EXPECT_EQ(L"othername", entry2->username); 94 EXPECT_EQ(L"otherword", entry2->password); 95 96 // Remove 97 cache.Remove(GURL("ftp://HOsT"), L"othername", L"otherword"); 98 EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL); 99 } 100 101 TEST(FtpAuthCacheTest, OnlyRemoveMatching) { 102 FtpAuthCache cache; 103 104 cache.Add(GURL("ftp://host"), L"username", L"password"); 105 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); 106 107 // Auth data doesn't match, shouldn't remove. 108 cache.Remove(GURL("ftp://host"), L"bogus", L"bogus"); 109 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); 110 111 // Auth data matches, should remove. 112 cache.Remove(GURL("ftp://host"), L"username", L"password"); 113 EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL); 114 } 115 116 TEST(FtpAuthCacheTest, EvictOldEntries) { 117 FtpAuthCache cache; 118 119 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) 120 cache.Add(GURL("ftp://host" + IntToString(i)), L"username", L"password"); 121 122 // No entries should be evicted before reaching the limit. 123 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) { 124 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + IntToString(i)))); 125 } 126 127 // Adding one entry should cause eviction of the first entry. 128 cache.Add(GURL("ftp://last_host"), L"username", L"password"); 129 EXPECT_TRUE(cache.Lookup(GURL("ftp://host0")) == NULL); 130 131 // Remaining entries should not get evicted. 132 for (size_t i = 1; i < FtpAuthCache::kMaxEntries; i++) { 133 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + IntToString(i)))); 134 } 135 EXPECT_TRUE(cache.Lookup(GURL("ftp://last_host"))); 136 } 137