| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/ftp/ftp_auth_cache.h" | 5 #include "net/ftp/ftp_auth_cache.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 using net::FtpAuthCache; | 11 using net::FtpAuthCache; |
| 12 | 12 |
| 13 TEST(FtpAuthCacheTest, LookupAddRemove) { | 13 TEST(FtpAuthCacheTest, LookupAddRemove) { |
| 14 FtpAuthCache cache; | 14 FtpAuthCache cache; |
| 15 | 15 |
| 16 GURL origin1("ftp://foo1"); | 16 GURL origin1("ftp://foo1"); |
| 17 GURL origin2("ftp://foo2"); | 17 GURL origin2("ftp://foo2"); |
| 18 | 18 |
| 19 // Lookup non-existent entry. | 19 // Lookup non-existent entry. |
| 20 EXPECT_EQ(NULL, cache.Lookup(origin1)); | 20 EXPECT_TRUE(cache.Lookup(origin1) == NULL); |
| 21 | 21 |
| 22 // Add entry for origin1. | 22 // Add entry for origin1. |
| 23 cache.Add(origin1, L"username1", L"password1"); | 23 cache.Add(origin1, L"username1", L"password1"); |
| 24 FtpAuthCache::Entry* entry1 = cache.Lookup(origin1); | 24 FtpAuthCache::Entry* entry1 = cache.Lookup(origin1); |
| 25 ASSERT_TRUE(entry1); | 25 ASSERT_TRUE(entry1); |
| 26 EXPECT_EQ(origin1, entry1->origin); | 26 EXPECT_EQ(origin1, entry1->origin); |
| 27 EXPECT_EQ(L"username1", entry1->username); | 27 EXPECT_EQ(L"username1", entry1->username); |
| 28 EXPECT_EQ(L"password1", entry1->password); | 28 EXPECT_EQ(L"password1", entry1->password); |
| 29 | 29 |
| 30 // Add an entry for origin2. | 30 // Add an entry for origin2. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 41 // Overwrite the entry for origin1. | 41 // Overwrite the entry for origin1. |
| 42 cache.Add(origin1, L"username3", L"password3"); | 42 cache.Add(origin1, L"username3", L"password3"); |
| 43 FtpAuthCache::Entry* entry3 = cache.Lookup(origin1); | 43 FtpAuthCache::Entry* entry3 = cache.Lookup(origin1); |
| 44 ASSERT_TRUE(entry3); | 44 ASSERT_TRUE(entry3); |
| 45 EXPECT_EQ(origin1, entry3->origin); | 45 EXPECT_EQ(origin1, entry3->origin); |
| 46 EXPECT_EQ(L"username3", entry3->username); | 46 EXPECT_EQ(L"username3", entry3->username); |
| 47 EXPECT_EQ(L"password3", entry3->password); | 47 EXPECT_EQ(L"password3", entry3->password); |
| 48 | 48 |
| 49 // Remove entry of origin1. | 49 // Remove entry of origin1. |
| 50 cache.Remove(origin1, L"username3", L"password3"); | 50 cache.Remove(origin1, L"username3", L"password3"); |
| 51 EXPECT_EQ(NULL, cache.Lookup(origin1)); | 51 EXPECT_TRUE(cache.Lookup(origin1) == NULL); |
| 52 | 52 |
| 53 // Remove non-existent entry. | 53 // Remove non-existent entry. |
| 54 cache.Remove(origin1, L"username3", L"password3"); | 54 cache.Remove(origin1, L"username3", L"password3"); |
| 55 EXPECT_EQ(NULL, cache.Lookup(origin1)); | 55 EXPECT_TRUE(cache.Lookup(origin1) == NULL); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Check that if the origin differs only by port number, it is considered | 58 // Check that if the origin differs only by port number, it is considered |
| 59 // a separate origin. | 59 // a separate origin. |
| 60 TEST(FtpAuthCacheTest, LookupWithPort) { | 60 TEST(FtpAuthCacheTest, LookupWithPort) { |
| 61 FtpAuthCache cache; | 61 FtpAuthCache cache; |
| 62 | 62 |
| 63 GURL origin1("ftp://foo:80"); | 63 GURL origin1("ftp://foo:80"); |
| 64 GURL origin2("ftp://foo:21"); | 64 GURL origin2("ftp://foo:21"); |
| 65 | 65 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 88 // Overwrite. | 88 // Overwrite. |
| 89 cache.Add(GURL("ftp://host"), L"othername", L"otherword"); | 89 cache.Add(GURL("ftp://host"), L"othername", L"otherword"); |
| 90 FtpAuthCache::Entry* entry2 = cache.Lookup(GURL("ftp://HoSt:21")); | 90 FtpAuthCache::Entry* entry2 = cache.Lookup(GURL("ftp://HoSt:21")); |
| 91 ASSERT_TRUE(entry2); | 91 ASSERT_TRUE(entry2); |
| 92 EXPECT_EQ(GURL("ftp://host"), entry2->origin); | 92 EXPECT_EQ(GURL("ftp://host"), entry2->origin); |
| 93 EXPECT_EQ(L"othername", entry2->username); | 93 EXPECT_EQ(L"othername", entry2->username); |
| 94 EXPECT_EQ(L"otherword", entry2->password); | 94 EXPECT_EQ(L"otherword", entry2->password); |
| 95 | 95 |
| 96 // Remove | 96 // Remove |
| 97 cache.Remove(GURL("ftp://HOsT"), L"othername", L"otherword"); | 97 cache.Remove(GURL("ftp://HOsT"), L"othername", L"otherword"); |
| 98 EXPECT_EQ(NULL, cache.Lookup(GURL("ftp://host"))); | 98 EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL); |
| 99 } | 99 } |
| 100 | 100 |
| 101 TEST(FtpAuthCacheTest, OnlyRemoveMatching) { | 101 TEST(FtpAuthCacheTest, OnlyRemoveMatching) { |
| 102 FtpAuthCache cache; | 102 FtpAuthCache cache; |
| 103 | 103 |
| 104 cache.Add(GURL("ftp://host"), L"username", L"password"); | 104 cache.Add(GURL("ftp://host"), L"username", L"password"); |
| 105 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); | 105 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); |
| 106 | 106 |
| 107 // Auth data doesn't match, shouldn't remove. | 107 // Auth data doesn't match, shouldn't remove. |
| 108 cache.Remove(GURL("ftp://host"), L"bogus", L"bogus"); | 108 cache.Remove(GURL("ftp://host"), L"bogus", L"bogus"); |
| 109 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); | 109 EXPECT_TRUE(cache.Lookup(GURL("ftp://host"))); |
| 110 | 110 |
| 111 // Auth data matches, should remove. | 111 // Auth data matches, should remove. |
| 112 cache.Remove(GURL("ftp://host"), L"username", L"password"); | 112 cache.Remove(GURL("ftp://host"), L"username", L"password"); |
| 113 EXPECT_EQ(NULL, cache.Lookup(GURL("ftp://host"))); | 113 EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL); |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST(FtpAuthCacheTest, EvictOldEntries) { | 116 TEST(FtpAuthCacheTest, EvictOldEntries) { |
| 117 FtpAuthCache cache; | 117 FtpAuthCache cache; |
| 118 | 118 |
| 119 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) | 119 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) |
| 120 cache.Add(GURL("ftp://host" + IntToString(i)), L"username", L"password"); | 120 cache.Add(GURL("ftp://host" + IntToString(i)), L"username", L"password"); |
| 121 | 121 |
| 122 // No entries should be evicted before reaching the limit. | 122 // No entries should be evicted before reaching the limit. |
| 123 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) { | 123 for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) { |
| 124 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + IntToString(i)))); | 124 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + IntToString(i)))); |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Adding one entry should cause eviction of the first entry. | 127 // Adding one entry should cause eviction of the first entry. |
| 128 cache.Add(GURL("ftp://last_host"), L"username", L"password"); | 128 cache.Add(GURL("ftp://last_host"), L"username", L"password"); |
| 129 EXPECT_EQ(NULL, cache.Lookup(GURL("ftp://host0"))); | 129 EXPECT_TRUE(cache.Lookup(GURL("ftp://host0")) == NULL); |
| 130 | 130 |
| 131 // Remaining entries should not get evicted. | 131 // Remaining entries should not get evicted. |
| 132 for (size_t i = 1; i < FtpAuthCache::kMaxEntries; i++) { | 132 for (size_t i = 1; i < FtpAuthCache::kMaxEntries; i++) { |
| 133 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + IntToString(i)))); | 133 EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + IntToString(i)))); |
| 134 } | 134 } |
| 135 EXPECT_TRUE(cache.Lookup(GURL("ftp://last_host"))); | 135 EXPECT_TRUE(cache.Lookup(GURL("ftp://last_host"))); |
| 136 } | 136 } |
| OLD | NEW |