Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(951)

Side by Side Diff: net/base/expiring_cache_unittest.cc

Issue 10556022: Consider the verification time as well as the expiration time when caching certificate verification… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Now with less const Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/expiring_cache.h ('k') | net/base/host_cache.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/expiring_cache.h" 5 #include "net/base/expiring_cache.h"
6 6
7 #include <functional>
8 #include <string>
9
7 #include "base/stl_util.h" 10 #include "base/stl_util.h"
8 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "base/time.h"
9 #include "testing/gmock/include/gmock/gmock.h" 13 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
11 15
12 using testing::Pointee; 16 using testing::Pointee;
13 using testing::StrEq; 17 using testing::StrEq;
14 18
15 namespace net { 19 namespace net {
16 20
17 namespace { 21 namespace {
18 22
19 const int kMaxCacheEntries = 10; 23 const int kMaxCacheEntries = 10;
20 typedef ExpiringCache<std::string, std::string> Cache; 24 typedef ExpiringCache<std::string, std::string, base::TimeTicks,
25 std::less<base::TimeTicks> > Cache;
26
27 struct TestFunctor {
28 bool operator()(const std::string& now,
29 const std::string& expiration) const {
30 return now != expiration;
31 }
32 };
21 33
22 } // namespace 34 } // namespace
23 35
24 TEST(ExpiringCacheTest, Basic) { 36 TEST(ExpiringCacheTest, Basic) {
25 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 37 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
26 38
27 Cache cache(kMaxCacheEntries); 39 Cache cache(kMaxCacheEntries);
28 40
29 // Start at t=0. 41 // Start at t=0.
30 base::TimeTicks now; 42 base::TimeTicks now;
31 EXPECT_EQ(0U, cache.size()); 43 EXPECT_EQ(0U, cache.size());
32 44
33 // Add an entry at t=0 45 // Add an entry at t=0
34 EXPECT_FALSE(cache.Get("entry1", now)); 46 EXPECT_FALSE(cache.Get("entry1", now));
35 cache.Put("entry1", "test1", now, kTTL); 47 cache.Put("entry1", "test1", now, now + kTTL);
36 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1"))); 48 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
37 EXPECT_EQ(1U, cache.size()); 49 EXPECT_EQ(1U, cache.size());
38 50
39 // Advance to t=5. 51 // Advance to t=5.
40 now += base::TimeDelta::FromSeconds(5); 52 now += base::TimeDelta::FromSeconds(5);
41 53
42 // Add an entry at t=5. 54 // Add an entry at t=5.
43 EXPECT_FALSE(cache.Get("entry2", now)); 55 EXPECT_FALSE(cache.Get("entry2", now));
44 cache.Put("entry2", "test2", now, kTTL); 56 cache.Put("entry2", "test2", now, now + kTTL);
45 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2"))); 57 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
46 EXPECT_EQ(2U, cache.size()); 58 EXPECT_EQ(2U, cache.size());
47 59
48 // Advance to t=9. 60 // Advance to t=9.
49 now += base::TimeDelta::FromSeconds(4); 61 now += base::TimeDelta::FromSeconds(4);
50 62
51 // Verify that the entries added are still retrievable and usable. 63 // Verify that the entries added are still retrievable and usable.
52 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1"))); 64 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
53 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2"))); 65 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
54 66
55 // Advance to t=10; entry1 is now expired. 67 // Advance to t=10; entry1 is now expired.
56 now += base::TimeDelta::FromSeconds(1); 68 now += base::TimeDelta::FromSeconds(1);
57 69
58 EXPECT_FALSE(cache.Get("entry1", now)); 70 EXPECT_FALSE(cache.Get("entry1", now));
59 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2"))); 71 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
60 72
61 // The expired element should no longer be in the cache. 73 // The expired element should no longer be in the cache.
62 EXPECT_EQ(1U, cache.size()); 74 EXPECT_EQ(1U, cache.size());
63 75
64 // Update entry1 so it is no longer expired. 76 // Update entry1 so it is no longer expired.
65 cache.Put("entry1", "test1", now, kTTL); 77 cache.Put("entry1", "test1", now, now + kTTL);
66 78
67 // Both entries should be retrievable and usable. 79 // Both entries should be retrievable and usable.
68 EXPECT_EQ(2U, cache.size()); 80 EXPECT_EQ(2U, cache.size());
69 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1"))); 81 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
70 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2"))); 82 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
71 83
72 // Advance to t=20; both entries are now expired. 84 // Advance to t=20; both entries are now expired.
73 now += base::TimeDelta::FromSeconds(10); 85 now += base::TimeDelta::FromSeconds(10);
74 86
75 EXPECT_FALSE(cache.Get("entry1", now)); 87 EXPECT_FALSE(cache.Get("entry1", now));
76 EXPECT_FALSE(cache.Get("entry2", now)); 88 EXPECT_FALSE(cache.Get("entry2", now));
77 } 89 }
78 90
79 TEST(ExpiringCacheTest, Compact) { 91 TEST(ExpiringCacheTest, Compact) {
80 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 92 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
81 93
82 Cache cache(kMaxCacheEntries); 94 Cache cache(kMaxCacheEntries);
83 95
84 // Start at t=0. 96 // Start at t=0.
85 base::TimeTicks now; 97 base::TimeTicks now;
86 EXPECT_EQ(0U, cache.size()); 98 EXPECT_EQ(0U, cache.size());
87 99
88 // Add five valid entries at t=10. 100 // Add five valid entries at t=10 that expire at t=20.
89 base::TimeTicks t10 = now + kTTL; 101 base::TimeTicks t10 = now + kTTL;
90 for (int i = 0; i < 5; ++i) { 102 for (int i = 0; i < 5; ++i) {
91 std::string name = base::StringPrintf("valid%d", i); 103 std::string name = base::StringPrintf("valid%d", i);
92 cache.Put(name, "I'm valid!", t10, kTTL); // Expire at t=20. 104 cache.Put(name, "I'm valid!", t10, t10 + kTTL);
93 } 105 }
94 EXPECT_EQ(5U, cache.size()); 106 EXPECT_EQ(5U, cache.size());
95 107
96 // Add three expired entries at t=10. 108 // Add three entries at t=0 that expire at t=10.
97 for (int i = 0; i < 3; ++i) { 109 for (int i = 0; i < 3; ++i) {
98 std::string name = base::StringPrintf("expired%d", i); 110 std::string name = base::StringPrintf("expired%d", i);
99 cache.Put(name, "I'm expired.", now - kTTL, kTTL); // Expire at t=10. 111 cache.Put(name, "I'm expired.", now, t10);
100 } 112 }
101 EXPECT_EQ(8U, cache.size()); 113 EXPECT_EQ(8U, cache.size());
102 114
103 // Add two negative (instantly expired) entriies at t=10. 115 // Add two negative (instantly expired) entries at t=0 that expire at t=0.
104 for (int i = 0; i < 2; ++i) { 116 for (int i = 0; i < 2; ++i) {
105 std::string name = base::StringPrintf("negative%d", i); 117 std::string name = base::StringPrintf("negative%d", i);
106 cache.Put(name, "I was never valid.", now, base::TimeDelta::FromSeconds(0)); 118 cache.Put(name, "I was never valid.", now, now);
107 } 119 }
108 EXPECT_EQ(10U, cache.size()); 120 EXPECT_EQ(10U, cache.size());
109 121
110 EXPECT_TRUE(ContainsKey(cache.entries_, "valid0")); 122 EXPECT_TRUE(ContainsKey(cache.entries_, "valid0"));
111 EXPECT_TRUE(ContainsKey(cache.entries_, "valid1")); 123 EXPECT_TRUE(ContainsKey(cache.entries_, "valid1"));
112 EXPECT_TRUE(ContainsKey(cache.entries_, "valid2")); 124 EXPECT_TRUE(ContainsKey(cache.entries_, "valid2"));
113 EXPECT_TRUE(ContainsKey(cache.entries_, "valid3")); 125 EXPECT_TRUE(ContainsKey(cache.entries_, "valid3"));
114 EXPECT_TRUE(ContainsKey(cache.entries_, "valid4")); 126 EXPECT_TRUE(ContainsKey(cache.entries_, "valid4"));
115 EXPECT_TRUE(ContainsKey(cache.entries_, "expired0")); 127 EXPECT_TRUE(ContainsKey(cache.entries_, "expired0"));
116 EXPECT_TRUE(ContainsKey(cache.entries_, "expired1")); 128 EXPECT_TRUE(ContainsKey(cache.entries_, "expired1"));
(...skipping 27 matching lines...) Expand all
144 156
145 // Add entries while the cache is at capacity, causing evictions. 157 // Add entries while the cache is at capacity, causing evictions.
146 TEST(ExpiringCacheTest, SetWithCompact) { 158 TEST(ExpiringCacheTest, SetWithCompact) {
147 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 159 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
148 160
149 Cache cache(3); 161 Cache cache(3);
150 162
151 // t=10 163 // t=10
152 base::TimeTicks now = base::TimeTicks() + kTTL; 164 base::TimeTicks now = base::TimeTicks() + kTTL;
153 165
154 cache.Put("test1", "test1", now, kTTL); 166 cache.Put("test1", "test1", now, now + kTTL);
155 cache.Put("test2", "test2", now, kTTL); 167 cache.Put("test2", "test2", now, now + kTTL);
156 cache.Put("expired", "expired", now, base::TimeDelta::FromSeconds(0)); 168 cache.Put("expired", "expired", now, now);
157 169
158 EXPECT_EQ(3U, cache.size()); 170 EXPECT_EQ(3U, cache.size());
159 171
160 // Should all be retrievable except "expired". 172 // Should all be retrievable except "expired".
161 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1"))); 173 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1")));
162 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2"))); 174 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2")));
163 EXPECT_FALSE(cache.Get("expired", now)); 175 EXPECT_FALSE(cache.Get("expired", now));
164 176
165 // Adding the fourth entry will cause "expired" to be evicted. 177 // Adding the fourth entry will cause "expired" to be evicted.
166 cache.Put("test3", "test3", now, kTTL); 178 cache.Put("test3", "test3", now, now + kTTL);
167 EXPECT_EQ(3U, cache.size()); 179 EXPECT_EQ(3U, cache.size());
168 180
169 EXPECT_FALSE(cache.Get("expired", now)); 181 EXPECT_FALSE(cache.Get("expired", now));
170 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1"))); 182 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1")));
171 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2"))); 183 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2")));
172 EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("test3"))); 184 EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("test3")));
173 185
174 // Add two more entries. Something should be evicted, however "test5" 186 // Add two more entries. Something should be evicted, however "test5"
175 // should definitely be in there (since it was last inserted). 187 // should definitely be in there (since it was last inserted).
176 cache.Put("test4", "test4", now, kTTL); 188 cache.Put("test4", "test4", now, now + kTTL);
177 EXPECT_EQ(3U, cache.size()); 189 EXPECT_EQ(3U, cache.size());
178 cache.Put("test5", "test5", now, kTTL); 190 cache.Put("test5", "test5", now, now + kTTL);
179 EXPECT_EQ(3U, cache.size()); 191 EXPECT_EQ(3U, cache.size());
180 EXPECT_THAT(cache.Get("test5", now), Pointee(StrEq("test5"))); 192 EXPECT_THAT(cache.Get("test5", now), Pointee(StrEq("test5")));
181 } 193 }
182 194
183 TEST(ExpiringCacheTest, Clear) { 195 TEST(ExpiringCacheTest, Clear) {
184 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 196 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
185 197
186 Cache cache(kMaxCacheEntries); 198 Cache cache(kMaxCacheEntries);
187 199
188 // Start at t=0. 200 // Start at t=0.
189 base::TimeTicks now; 201 base::TimeTicks now;
190 EXPECT_EQ(0U, cache.size()); 202 EXPECT_EQ(0U, cache.size());
191 203
192 // Add three entries. 204 // Add three entries.
193 cache.Put("test1", "foo", now, kTTL); 205 cache.Put("test1", "foo", now, now + kTTL);
194 cache.Put("test2", "foo", now, kTTL); 206 cache.Put("test2", "foo", now, now + kTTL);
195 cache.Put("test3", "foo", now, kTTL); 207 cache.Put("test3", "foo", now, now + kTTL);
196 EXPECT_EQ(3U, cache.size()); 208 EXPECT_EQ(3U, cache.size());
197 209
198 cache.Clear(); 210 cache.Clear();
199 211
200 EXPECT_EQ(0U, cache.size()); 212 EXPECT_EQ(0U, cache.size());
201 } 213 }
202 214
203 TEST(ExpiringCache, GetTruncatesExpiredEntries) { 215 TEST(ExpiringCacheTest, GetTruncatesExpiredEntries) {
204 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 216 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
205 217
206 Cache cache(kMaxCacheEntries); 218 Cache cache(kMaxCacheEntries);
207 219
208 // Start at t=0. 220 // Start at t=0.
209 base::TimeTicks now; 221 base::TimeTicks now;
210 EXPECT_EQ(0U, cache.size()); 222 EXPECT_EQ(0U, cache.size());
211 223
212 // Add three entries at t=0. 224 // Add three entries at t=0.
213 cache.Put("test1", "foo1", now, kTTL); 225 cache.Put("test1", "foo1", now, now + kTTL);
214 cache.Put("test2", "foo2", now, kTTL); 226 cache.Put("test2", "foo2", now, now + kTTL);
215 cache.Put("test3", "foo3", now, kTTL); 227 cache.Put("test3", "foo3", now, now + kTTL);
216 EXPECT_EQ(3U, cache.size()); 228 EXPECT_EQ(3U, cache.size());
217 229
218 // Ensure the entries were added. 230 // Ensure the entries were added.
219 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("foo1"))); 231 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("foo1")));
220 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("foo2"))); 232 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("foo2")));
221 EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("foo3"))); 233 EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("foo3")));
222 234
223 // Add five entries at t=10. 235 // Add five entries at t=10.
224 now += kTTL; 236 now += kTTL;
225 for (int i = 0; i < 5; ++i) { 237 for (int i = 0; i < 5; ++i) {
226 std::string name = base::StringPrintf("valid%d", i); 238 std::string name = base::StringPrintf("valid%d", i);
227 cache.Put(name, name, now, kTTL); // Expire at t=20. 239 cache.Put(name, name, now, now + kTTL); // Expire at t=20.
228 } 240 }
229 EXPECT_EQ(8U, cache.size()); 241 EXPECT_EQ(8U, cache.size());
230 242
231 // Now access two expired entries and ensure the cache size goes down. 243 // Now access two expired entries and ensure the cache size goes down.
232 EXPECT_FALSE(cache.Get("test1", now)); 244 EXPECT_FALSE(cache.Get("test1", now));
233 EXPECT_FALSE(cache.Get("test2", now)); 245 EXPECT_FALSE(cache.Get("test2", now));
234 EXPECT_EQ(6U, cache.size()); 246 EXPECT_EQ(6U, cache.size());
235 247
236 // Accessing non-expired entries should return entries and not adjust the 248 // Accessing non-expired entries should return entries and not adjust the
237 // cache size. 249 // cache size.
238 for (int i = 0; i < 5; ++i) { 250 for (int i = 0; i < 5; ++i) {
239 std::string name = base::StringPrintf("valid%d", i); 251 std::string name = base::StringPrintf("valid%d", i);
240 EXPECT_THAT(cache.Get(name, now), Pointee(StrEq(name))); 252 EXPECT_THAT(cache.Get(name, now), Pointee(StrEq(name)));
241 } 253 }
242 EXPECT_EQ(6U, cache.size()); 254 EXPECT_EQ(6U, cache.size());
243 } 255 }
244 256
257 TEST(ExpiringCacheTest, CustomFunctor) {
258 ExpiringCache<std::string, std::string, std::string, TestFunctor> cache(5);
259
260 const std::string kNow("Now");
261 const std::string kLater("A little bit later");
262 const std::string kMuchLater("Much later");
263 const std::string kHeatDeath("The heat death of the universe");
264
265 EXPECT_EQ(0u, cache.size());
266
267 // Add three entries at t=kNow that expire at kLater.
268 cache.Put("test1", "foo1", kNow, kLater);
269 cache.Put("test2", "foo2", kNow, kLater);
270 cache.Put("test3", "foo3", kNow, kLater);
271 EXPECT_EQ(3U, cache.size());
272
273 // Add two entries at t=kNow that expire at kMuchLater
274 cache.Put("test4", "foo4", kNow, kMuchLater);
275 cache.Put("test5", "foo5", kNow, kMuchLater);
276 EXPECT_EQ(5U, cache.size());
277
278 // Ensure the entries were added.
279 EXPECT_THAT(cache.Get("test1", kNow), Pointee(StrEq("foo1")));
280 EXPECT_THAT(cache.Get("test2", kNow), Pointee(StrEq("foo2")));
281 EXPECT_THAT(cache.Get("test3", kNow), Pointee(StrEq("foo3")));
282 EXPECT_THAT(cache.Get("test4", kNow), Pointee(StrEq("foo4")));
283 EXPECT_THAT(cache.Get("test5", kNow), Pointee(StrEq("foo5")));
284
285 // Add one entry at t=kLater that expires at kHeatDeath, which will expire
286 // one of test1-3.
287 cache.Put("test6", "foo6", kLater, kHeatDeath);
288 EXPECT_THAT(cache.Get("test6", kLater), Pointee(StrEq("foo6")));
289 EXPECT_EQ(3U, cache.size());
290
291 // Now compact at kMuchLater, which should remove all but "test6".
292 cache.max_entries_ = 2;
293 cache.Compact(kMuchLater);
294
295 EXPECT_EQ(1U, cache.size());
296 EXPECT_THAT(cache.Get("test6", kMuchLater), Pointee(StrEq("foo6")));
297
298 // Finally, "test6" should not be valid at the end of the universe.
299 EXPECT_FALSE(cache.Get("test6", kHeatDeath));
300
301 // Because comparison is based on equality, not strict weak ordering, we
302 // should be able to add something at kHeatDeath that expires at kMuchLater.
303 cache.Put("test7", "foo7", kHeatDeath, kMuchLater);
304 EXPECT_EQ(1U, cache.size());
305 EXPECT_THAT(cache.Get("test7", kNow), Pointee(StrEq("foo7")));
306 EXPECT_THAT(cache.Get("test7", kLater), Pointee(StrEq("foo7")));
307 EXPECT_THAT(cache.Get("test7", kHeatDeath), Pointee(StrEq("foo7")));
308 EXPECT_FALSE(cache.Get("test7", kMuchLater));
309 }
310
245 } // namespace net 311 } // namespace net
OLDNEW
« no previous file with comments | « net/base/expiring_cache.h ('k') | net/base/host_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698