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/base/host_cache.h" | 5 #include "net/base/host_cache.h" |
6 | 6 |
7 #include "base/stl_util-inl.h" | 7 #include "base/stl_util-inl.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 // Start at t=0. | 22 // Start at t=0. |
23 base::TimeTicks now; | 23 base::TimeTicks now; |
24 | 24 |
25 const HostCache::Entry* entry1 = NULL; // Entry for foobar.com. | 25 const HostCache::Entry* entry1 = NULL; // Entry for foobar.com. |
26 const HostCache::Entry* entry2 = NULL; // Entry for foobar2.com. | 26 const HostCache::Entry* entry2 = NULL; // Entry for foobar2.com. |
27 | 27 |
28 EXPECT_EQ(0U, cache.size()); | 28 EXPECT_EQ(0U, cache.size()); |
29 | 29 |
30 // Add an entry for "foobar.com" at t=0. | 30 // Add an entry for "foobar.com" at t=0. |
31 EXPECT_TRUE(NULL == cache.Lookup("foobar.com", base::TimeTicks())); | 31 EXPECT_EQ(NULL, cache.Lookup("foobar.com", base::TimeTicks())); |
32 cache.Set("foobar.com", OK, AddressList(), now); | 32 cache.Set("foobar.com", OK, AddressList(), now); |
33 entry1 = cache.Lookup("foobar.com", base::TimeTicks()); | 33 entry1 = cache.Lookup("foobar.com", base::TimeTicks()); |
34 EXPECT_FALSE(NULL == entry1); | 34 EXPECT_FALSE(NULL == entry1); |
35 EXPECT_EQ(1U, cache.size()); | 35 EXPECT_EQ(1U, cache.size()); |
36 | 36 |
37 // Advance to t=5. | 37 // Advance to t=5. |
38 now += base::TimeDelta::FromSeconds(5); | 38 now += base::TimeDelta::FromSeconds(5); |
39 | 39 |
40 // Add an entry for "foobar2.com" at t=5. | 40 // Add an entry for "foobar2.com" at t=5. |
41 EXPECT_TRUE(NULL == cache.Lookup("foobar2.com", base::TimeTicks())); | 41 EXPECT_EQ(NULL, cache.Lookup("foobar2.com", base::TimeTicks())); |
42 cache.Set("foobar2.com", OK, AddressList(), now); | 42 cache.Set("foobar2.com", OK, AddressList(), now); |
43 entry2 = cache.Lookup("foobar2.com", base::TimeTicks()); | 43 entry2 = cache.Lookup("foobar2.com", base::TimeTicks()); |
44 EXPECT_FALSE(NULL == entry1); | 44 EXPECT_FALSE(NULL == entry1); |
45 EXPECT_EQ(2U, cache.size()); | 45 EXPECT_EQ(2U, cache.size()); |
46 | 46 |
47 // Advance to t=9 | 47 // Advance to t=9 |
48 now += base::TimeDelta::FromSeconds(4); | 48 now += base::TimeDelta::FromSeconds(4); |
49 | 49 |
50 // Verify that the entries we added are still retrievable, and usable. | 50 // Verify that the entries we added are still retrievable, and usable. |
51 EXPECT_EQ(entry1, cache.Lookup("foobar.com", now)); | 51 EXPECT_EQ(entry1, cache.Lookup("foobar.com", now)); |
52 EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now)); | 52 EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now)); |
53 | 53 |
54 // Advance to t=10; entry1 is now expired. | 54 // Advance to t=10; entry1 is now expired. |
55 now += base::TimeDelta::FromSeconds(1); | 55 now += base::TimeDelta::FromSeconds(1); |
56 | 56 |
57 EXPECT_TRUE(NULL == cache.Lookup("foobar.com", now)); | 57 EXPECT_EQ(NULL, cache.Lookup("foobar.com", now)); |
58 EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now)); | 58 EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now)); |
59 | 59 |
60 // Update entry1, so it is no longer expired. | 60 // Update entry1, so it is no longer expired. |
61 cache.Set("foobar.com", OK, AddressList(), now); | 61 cache.Set("foobar.com", OK, AddressList(), now); |
62 // Re-uses existing entry storage. | 62 // Re-uses existing entry storage. |
63 EXPECT_EQ(entry1, cache.Lookup("foobar.com", now)); | 63 EXPECT_EQ(entry1, cache.Lookup("foobar.com", now)); |
64 EXPECT_EQ(2U, cache.size()); | 64 EXPECT_EQ(2U, cache.size()); |
65 | 65 |
66 // Both entries should still be retrievable and usable. | 66 // Both entries should still be retrievable and usable. |
67 EXPECT_EQ(entry1, cache.Lookup("foobar.com", now)); | 67 EXPECT_EQ(entry1, cache.Lookup("foobar.com", now)); |
68 EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now)); | 68 EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now)); |
69 | 69 |
70 // Advance to t=20; both entries are now expired. | 70 // Advance to t=20; both entries are now expired. |
71 now += base::TimeDelta::FromSeconds(10); | 71 now += base::TimeDelta::FromSeconds(10); |
72 | 72 |
73 EXPECT_TRUE(NULL == cache.Lookup("foobar.com", now)); | 73 EXPECT_EQ(NULL, cache.Lookup("foobar.com", now)); |
74 EXPECT_TRUE(NULL == cache.Lookup("foobar2.com", now)); | 74 EXPECT_EQ(NULL, cache.Lookup("foobar2.com", now)); |
75 } | 75 } |
76 | 76 |
77 // Try caching entries for a failed resolve attempt. | 77 // Try caching entries for a failed resolve attempt. |
78 TEST(HostCacheTest, NegativeEntry) { | 78 TEST(HostCacheTest, NegativeEntry) { |
79 HostCache cache(kMaxCacheEntries, kCacheDurationMs); | 79 HostCache cache(kMaxCacheEntries, kCacheDurationMs); |
80 | 80 |
81 // Set t=0. | 81 // Set t=0. |
82 base::TimeTicks now; | 82 base::TimeTicks now; |
83 | 83 |
84 EXPECT_TRUE(NULL == cache.Lookup("foobar.com", base::TimeTicks())); | 84 EXPECT_EQ(NULL, cache.Lookup("foobar.com", base::TimeTicks())); |
85 cache.Set("foobar.com", ERR_NAME_NOT_RESOLVED, AddressList(), now); | 85 cache.Set("foobar.com", ERR_NAME_NOT_RESOLVED, AddressList(), now); |
86 EXPECT_EQ(1U, cache.size()); | 86 EXPECT_EQ(1U, cache.size()); |
87 | 87 |
88 // We disallow use of negative entries. | 88 // We disallow use of negative entries. |
89 EXPECT_TRUE(NULL == cache.Lookup("foobar.com", now)); | 89 EXPECT_EQ(NULL, cache.Lookup("foobar.com", now)); |
90 | 90 |
91 // Now overwrite with a valid entry, and then overwrite with negative entry | 91 // Now overwrite with a valid entry, and then overwrite with negative entry |
92 // again -- the valid entry should be kicked out. | 92 // again -- the valid entry should be kicked out. |
93 cache.Set("foobar.com", OK, AddressList(), now); | 93 cache.Set("foobar.com", OK, AddressList(), now); |
94 EXPECT_FALSE(NULL == cache.Lookup("foobar.com", now)); | 94 EXPECT_FALSE(NULL == cache.Lookup("foobar.com", now)); |
95 cache.Set("foobar.com", ERR_NAME_NOT_RESOLVED, AddressList(), now); | 95 cache.Set("foobar.com", ERR_NAME_NOT_RESOLVED, AddressList(), now); |
96 EXPECT_TRUE(NULL == cache.Lookup("foobar.com", now)); | 96 EXPECT_EQ(NULL, cache.Lookup("foobar.com", now)); |
97 } | 97 } |
98 | 98 |
99 TEST(HostCacheTest, Compact) { | 99 TEST(HostCacheTest, Compact) { |
100 // Initial entries limit is big enough to accomadate everything we add. | 100 // Initial entries limit is big enough to accomadate everything we add. |
101 net::HostCache cache(kMaxCacheEntries, kCacheDurationMs); | 101 net::HostCache cache(kMaxCacheEntries, kCacheDurationMs); |
102 | 102 |
103 EXPECT_EQ(0U, cache.size()); | 103 EXPECT_EQ(0U, cache.size()); |
104 | 104 |
105 // t=10 | 105 // t=10 |
106 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromSeconds(10); | 106 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromSeconds(10); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 EXPECT_EQ(3U, cache.size()); | 178 EXPECT_EQ(3U, cache.size()); |
179 | 179 |
180 // Should all be retrievable except "expired". | 180 // Should all be retrievable except "expired". |
181 EXPECT_FALSE(NULL == cache.Lookup("host1", now)); | 181 EXPECT_FALSE(NULL == cache.Lookup("host1", now)); |
182 EXPECT_FALSE(NULL == cache.Lookup("host2", now)); | 182 EXPECT_FALSE(NULL == cache.Lookup("host2", now)); |
183 EXPECT_TRUE(NULL == cache.Lookup("expired", now)); | 183 EXPECT_TRUE(NULL == cache.Lookup("expired", now)); |
184 | 184 |
185 // Adding the fourth entry will cause "expired" to be evicted. | 185 // Adding the fourth entry will cause "expired" to be evicted. |
186 cache.Set("host3", OK, AddressList(), now); | 186 cache.Set("host3", OK, AddressList(), now); |
187 EXPECT_EQ(3U, cache.size()); | 187 EXPECT_EQ(3U, cache.size()); |
188 EXPECT_TRUE(NULL == cache.Lookup("expired", now)); | 188 EXPECT_EQ(NULL, cache.Lookup("expired", now)); |
189 EXPECT_FALSE(NULL == cache.Lookup("host1", now)); | 189 EXPECT_FALSE(NULL == cache.Lookup("host1", now)); |
190 EXPECT_FALSE(NULL == cache.Lookup("host2", now)); | 190 EXPECT_FALSE(NULL == cache.Lookup("host2", now)); |
191 EXPECT_FALSE(NULL == cache.Lookup("host3", now)); | 191 EXPECT_FALSE(NULL == cache.Lookup("host3", now)); |
192 | 192 |
193 // Add two more entries. Something should be evicted, however "host5" | 193 // Add two more entries. Something should be evicted, however "host5" |
194 // should definitely be in there (since it was last inserted). | 194 // should definitely be in there (since it was last inserted). |
195 cache.Set("host4", OK, AddressList(), now); | 195 cache.Set("host4", OK, AddressList(), now); |
196 EXPECT_EQ(3U, cache.size()); | 196 EXPECT_EQ(3U, cache.size()); |
197 cache.Set("host5", OK, AddressList(), now); | 197 cache.Set("host5", OK, AddressList(), now); |
198 EXPECT_EQ(3U, cache.size()); | 198 EXPECT_EQ(3U, cache.size()); |
199 EXPECT_FALSE(NULL == cache.Lookup("host5", now)); | 199 EXPECT_FALSE(NULL == cache.Lookup("host5", now)); |
200 } | 200 } |
201 | 201 |
202 TEST(HostCacheTest, NoCache) { | 202 TEST(HostCacheTest, NoCache) { |
203 // Disable caching. | 203 // Disable caching. |
204 HostCache cache(0, kCacheDurationMs); | 204 HostCache cache(0, kCacheDurationMs); |
205 EXPECT_TRUE(cache.caching_is_disabled()); | 205 EXPECT_TRUE(cache.caching_is_disabled()); |
206 | 206 |
207 // Set t=0. | 207 // Set t=0. |
208 base::TimeTicks now; | 208 base::TimeTicks now; |
209 | 209 |
210 // Lookup and Set should have no effect. | 210 // Lookup and Set should have no effect. |
211 EXPECT_TRUE(NULL == cache.Lookup("foobar.com", base::TimeTicks())); | 211 EXPECT_EQ(NULL, cache.Lookup("foobar.com", base::TimeTicks())); |
212 cache.Set("foobar.com", OK, AddressList(), now); | 212 cache.Set("foobar.com", OK, AddressList(), now); |
213 EXPECT_TRUE(NULL == cache.Lookup("foobar.com", base::TimeTicks())); | 213 EXPECT_EQ(NULL, cache.Lookup("foobar.com", base::TimeTicks())); |
214 | 214 |
215 EXPECT_EQ(0U, cache.size()); | 215 EXPECT_EQ(0U, cache.size()); |
216 } | 216 } |
217 | 217 |
218 } // namespace net | 218 } // namespace net |
OLD | NEW |