1 // Copyright (c) 2021 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 "quiche/quic/core/crypto/quic_client_session_cache.h"
6
7 #include <string>
8
9 #include "quiche/quic/platform/api/quic_test.h"
10 #include "quiche/quic/test_tools/mock_clock.h"
11 #include "quiche/common/quiche_text_utils.h"
12
13 namespace quic {
14 namespace test {
15 namespace {
16
17 const QuicTime::Delta kTimeout = QuicTime::Delta::FromSeconds(1000);
18 const QuicVersionLabel kFakeVersionLabel = 0x01234567;
19 const QuicVersionLabel kFakeVersionLabel2 = 0x89ABCDEF;
20 const uint64_t kFakeIdleTimeoutMilliseconds = 12012;
21 const uint8_t kFakeStatelessResetTokenData[16] = {
22 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
23 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F};
24 const uint64_t kFakeMaxPacketSize = 9001;
25 const uint64_t kFakeInitialMaxData = 101;
26 const bool kFakeDisableMigration = true;
27 const auto kCustomParameter1 =
28 static_cast<TransportParameters::TransportParameterId>(0xffcd);
29 const char* kCustomParameter1Value = "foo";
30 const auto kCustomParameter2 =
31 static_cast<TransportParameters::TransportParameterId>(0xff34);
32 const char* kCustomParameter2Value = "bar";
33
CreateFakeStatelessResetToken()34 std::vector<uint8_t> CreateFakeStatelessResetToken() {
35 return std::vector<uint8_t>(
36 kFakeStatelessResetTokenData,
37 kFakeStatelessResetTokenData + sizeof(kFakeStatelessResetTokenData));
38 }
39
40 TransportParameters::LegacyVersionInformation
CreateFakeLegacyVersionInformation()41 CreateFakeLegacyVersionInformation() {
42 TransportParameters::LegacyVersionInformation legacy_version_information;
43 legacy_version_information.version = kFakeVersionLabel;
44 legacy_version_information.supported_versions.push_back(kFakeVersionLabel);
45 legacy_version_information.supported_versions.push_back(kFakeVersionLabel2);
46 return legacy_version_information;
47 }
48
CreateFakeVersionInformation()49 TransportParameters::VersionInformation CreateFakeVersionInformation() {
50 TransportParameters::VersionInformation version_information;
51 version_information.chosen_version = kFakeVersionLabel;
52 version_information.other_versions.push_back(kFakeVersionLabel);
53 return version_information;
54 }
55
56 // Make a TransportParameters that has a few fields set to help test comparison.
MakeFakeTransportParams()57 std::unique_ptr<TransportParameters> MakeFakeTransportParams() {
58 auto params = std::make_unique<TransportParameters>();
59 params->perspective = Perspective::IS_CLIENT;
60 params->legacy_version_information = CreateFakeLegacyVersionInformation();
61 params->version_information = CreateFakeVersionInformation();
62 params->max_idle_timeout_ms.set_value(kFakeIdleTimeoutMilliseconds);
63 params->stateless_reset_token = CreateFakeStatelessResetToken();
64 params->max_udp_payload_size.set_value(kFakeMaxPacketSize);
65 params->initial_max_data.set_value(kFakeInitialMaxData);
66 params->disable_active_migration = kFakeDisableMigration;
67 params->custom_parameters[kCustomParameter1] = kCustomParameter1Value;
68 params->custom_parameters[kCustomParameter2] = kCustomParameter2Value;
69 return params;
70 }
71
72 // Generated by running TlsClientHandshakerTest.ZeroRttResumption and in
73 // TlsClientHandshaker::InsertSession calling SSL_SESSION_to_bytes to serialize
74 // the received 0-RTT capable ticket.
75 static const char kCachedSession[] =
76 "30820ad7020101020203040402130104206594ce84e61a866b56163c4ba09079aebf1d4f"
77 "6cbcbd38dc9d7066a38a76c9cf0420ec9062063582a4cc0a44f9ff93256a195153ba6032"
78 "0cf3c9189990932d838adaa10602046196f7b9a205020302a300a382039f3082039b3082"
79 "0183a00302010202021001300d06092a864886f70d010105050030623111300f06035504"
80 "030c08426f677573204941310b300906035504080c024d41310b30090603550406130255"
81 "533121301f06092a864886f70d0109011612626f67757340626f6775732d69612e636f6d"
82 "3110300e060355040a0c07426f6775734941301e170d3231303132383136323030315a17"
83 "0d3331303132363136323030315a3069311d301b06035504030c14746573745f6563632e"
84 "6578616d706c652e636f6d310b300906035504080c024d41310b30090603550406130255"
85 "53311e301c06092a864886f70d010901160f626f67757340626f6775732e636f6d310e30"
86 "0c060355040a0c05426f6775733059301306072a8648ce3d020106082a8648ce3d030107"
87 "034200041ba5e2b6f24e64990b9f24ae6d23473d8c77fbcfb7f554f36559529a69a57170"
88 "a10a81b7fe4a36ebf37b0a8c5e467a8443d8b8c002892aa5c1194bd843f42c9aa31f301d"
89 "301b0603551d11041430128210746573742e6578616d706c652e636f6d300d06092a8648"
90 "86f70d0101050500038202010019921d54ac06948763d609215f64f5d6540e3da886c6c9"
91 "61bc737a437719b4621416ef1229f39282d7d3234e1a5d57535473066233bd246eec8e96"
92 "1e0633cf4fe014c800e62599981820ec33d92e74ded0fa2953db1d81e19cb6890b6305b6"
93 "3ede8d3e9fcf3c09f3f57283acf08aa57be4ee9a68d00bb3e2ded5920c619b5d83e5194a"
94 "adb77ae5d61ed3e0a5670f0ae61cc3197329f0e71e3364dcab0405e9e4a6646adef8f022"
95 "6415ec16c8046307b1769029fe780bd576114dde2fa9b4a32aa70bc436549a24ee4907a9"
96 "045f6457ce8dfd8d62cc65315afe798ae1a948eefd70b035d415e73569c48fb20085de1a"
97 "87de039e6b0b9a5fcb4069df27f3a7a1409e72d1ac739c72f29ef786134207e61c79855f"
98 "c22e3ee5f6ad59a7b1ff0f18d79776f1c95efaebbebe381664132a58a1e7ff689945b7e0"
99 "88634b0872feeefbf6be020884b994c6a7ff435f2b3f609077ff97cb509cfa17ff479b34"
100 "e633e4b5bc46b20c5f27c80a2e2943f795a928acd5a3fc43c3af8425ad600c048b41d87e"
101 "6361bc72fc4e5e44680a3d325674ba6ffa760d2fc7d9e4847a8e0dd9d35a543324e18b94"
102 "2d42af6391ed1dd54a39e3f4a4c6b32486eb4ba72815dbd89c56fc053743a0b0483ce676"
103 "15defce6800c629b99d0cbc56da162487f475b7c246099eaf1e6d10a022b2f49c6af1da3"
104 "e8ed66096f267c4a76976b9572db7456ef90278330a4020400aa81b60481b3494e534543"
105 "55524500f3439e548c21d2ad6e5634cc1cc0045730819702010102020304040213010400"
106 "0420ec9062063582a4cc0a44f9ff93256a195153ba60320cf3c9189990932d838adaa106"
107 "02046196f7b9a205020302a300a4020400b20302011db5060404130800cdb807020500ff"
108 "ffffffb9050203093a80ba0404026833bb030101ffbc23042100d27d985bfce04833f02d"
109 "38366b219f4def42bc4ba1b01844d1778db11731487dbd020400be020400b20302011db3"
110 "8205da308205d6308203bea00302010202021000300d06092a864886f70d010105050030"
111 "62310b3009060355040613025553310b300906035504080c024d413110300e060355040a"
112 "0c07426f67757343413111300f06035504030c08426f6775732043413121301f06092a86"
113 "4886f70d0109011612626f67757340626f6775732d63612e636f6d3020170d3231303132"
114 "383136313935385a180f32303730303531313136313935385a30623111300f0603550403"
115 "0c08426f677573204941310b300906035504080c024d41310b3009060355040613025553"
116 "3121301f06092a864886f70d0109011612626f67757340626f6775732d69612e636f6d31"
117 "10300e060355040a0c07426f677573494130820222300d06092a864886f70d0101010500"
118 "0382020f003082020a028202010096c03a0ffc61bcedcd5ec9bf6f848b8a066b43f08377"
119 "3af518a6a0044f22e666e24d2ae741954e344302c4be04612185bd53bcd848eb322bf900"
120 "724eb0848047d647033ffbddb00f01d1de7c1cdb684f83c9bf5fd18ff60afad5a53b0d7d"
121 "2c2a50abc38df019cd7f50194d05bc4597a1ef8570ea04069a2c36d74496af126573ca18"
122 "8e470009b56250fadf2a04e837ee3837b36b1f08b7a0cfe2533d05f26484ce4e30203d01"
123 "517fffd3da63d0341079ddce16e9ab4dbf9d4049e5cc52326031e645dd682fe6220d9e0e"
124 "95451f5a82f3e1720dc13e8499466426a0bdbea9f6a76b3c9228dd3c79ab4dcc4c145ef0"
125 "e78d1ee8bfd4650692d7e28a54bed809d8f7b37fe24c586be59cc46638531cb291c8c156"
126 "8f08d67e768e51563e95a639c1f138b275ffad6a6a2a042ba9e26ad63c2ce63b600013f0"
127 "a6f0703ee51c4f457f7bab0391c2fc4c5bb3213742c9cf9941bff68cc2e1cc96139d35ed"
128 "1885244ddde0bf658416c486701841b81f7b17503d08c59a4db08a2a80755e007aa3b6c7"
129 "eadcaa9e07c8325f3689f100de23970b12c9d9f6d0a8fb35ba0fd75c64410318db4a13ac"
130 "3972ad16cdf6408af37013c7bcd7c42f20d6d04c3e39436c7531e8dafa219dd04b784ef0"
131 "3c70ee5a4782b33cafa925aa3deca62a14aed704f179b932efabc2b0c5c15a8a99bfc9e6"
132 "189dce7da50ea303594b6af9c933dd54b6e9d17c472d0203010001a38193308190300f06"
133 "03551d130101ff040530030101ff301d0603551d0e041604141a98e80029a80992b7e5e0"
134 "068ab9b3486cd839d6301f0603551d23041830168014780beeefe2fa419c48a438bdb30b"
135 "e37ef0b7a94e300b0603551d0f0404030202a430130603551d25040c300a06082b060105"
136 "05070301301b0603551d11041430128207426f67757343418207426f6775734941300d06"
137 "092a864886f70d010105050003820201009e822ed8064b1aabaddf1340010ea147f68c06"
138 "5a5a599ea305349f1b0e545a00817d6e55c7bf85560fab429ca72186c4d520b52f5cc121"
139 "abd068b06f3111494431d2522efa54642f907059e7db80b73bb5ecf621377195b8700bba"
140 "df798cece8c67a9571548d0e6592e81ae5d934877cb170aef18d3b97f635600fe0890d98"
141 "f88b33fe3d1fd34c1c915beae4e5c0b133f476c40b21d220f16ce9cdd9e8f97a36a31723"
142 "68875f052c9271648d9cb54687c6fdc3ea96f2908003bc5e5e79de00a21da7b8429f8b08"
143 "af4c4d34641e386d72eabf5f01f106363f2ffd18969bf0bb9a4d17627c6427ff772c4308"
144 "83c276feef5fc6dba9582c22fdbe9df7e8dfca375695f028ed588df54f3c86462dbf4c07"
145 "91d80ca738988a1419c86bb4dd8d738b746921f01f39422e5ffd488b6f00195b996e6392"
146 "3a820a32cd78b5989f339c0fcf4f269103964a30a16347d0ffdc8df1f3653ddc1515fa09"
147 "22c7aef1af1fbcb23e93ae7622ab1ee11fcfa98319bad4c37c091cad46bd0337b3cc78b5"
148 "5b9f1ea7994acc1f89c49a0b4cb540d2137e266fd43e56a9b5b778217b6f77df530e1eaf"
149 "b3417262b5ddb86d3c6c5ac51e3f326c650dcc2434473973b7182c66220d1f3871bde7ee"
150 "47d3f359d3d4c5bdd61baa684c03db4c75f9d6690c9e6e3abe6eaf5fa2c33c4daf26b373"
151 "d85a1e8a7d671ac4a0a97b14e36e81280de4593bbb12da7695b5060404130800cdb60301"
152 "0100b70402020403b807020500ffffffffb9050203093a80ba0404026833bb030101ffbd"
153 "020400be020400";
154
155 class QuicClientSessionCacheTest : public QuicTest {
156 public:
QuicClientSessionCacheTest()157 QuicClientSessionCacheTest() : ssl_ctx_(SSL_CTX_new(TLS_method())) {
158 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1));
159 }
160
161 protected:
NewSSLSession()162 bssl::UniquePtr<SSL_SESSION> NewSSLSession() {
163 std::string cached_session;
164 EXPECT_TRUE(absl::HexStringToBytes(kCachedSession, &cached_session));
165 SSL_SESSION* session = SSL_SESSION_from_bytes(
166 reinterpret_cast<const uint8_t*>(cached_session.data()),
167 cached_session.size(), ssl_ctx_.get());
168 QUICHE_DCHECK(session);
169 return bssl::UniquePtr<SSL_SESSION>(session);
170 }
171
MakeTestSession(QuicTime::Delta timeout=kTimeout)172 bssl::UniquePtr<SSL_SESSION> MakeTestSession(
173 QuicTime::Delta timeout = kTimeout) {
174 bssl::UniquePtr<SSL_SESSION> session = NewSSLSession();
175 SSL_SESSION_set_time(session.get(), clock_.WallNow().ToUNIXSeconds());
176 SSL_SESSION_set_timeout(session.get(), timeout.ToSeconds());
177 return session;
178 }
179
180 bssl::UniquePtr<SSL_CTX> ssl_ctx_;
181 MockClock clock_;
182 };
183
184 // Tests that simple insertion and lookup work correctly.
TEST_F(QuicClientSessionCacheTest,SingleSession)185 TEST_F(QuicClientSessionCacheTest, SingleSession) {
186 QuicClientSessionCache cache;
187
188 auto params = MakeFakeTransportParams();
189 auto session = MakeTestSession();
190 QuicServerId id1("a.com", 443);
191
192 auto params2 = MakeFakeTransportParams();
193 auto session2 = MakeTestSession();
194 SSL_SESSION* unowned2 = session2.get();
195 QuicServerId id2("b.com", 443);
196
197 EXPECT_EQ(nullptr, cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get()));
198 EXPECT_EQ(nullptr, cache.Lookup(id2, clock_.WallNow(), ssl_ctx_.get()));
199 EXPECT_EQ(0u, cache.size());
200
201 cache.Insert(id1, std::move(session), *params, nullptr);
202 EXPECT_EQ(1u, cache.size());
203 EXPECT_EQ(
204 *params,
205 *(cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get())->transport_params));
206 EXPECT_EQ(nullptr, cache.Lookup(id2, clock_.WallNow(), ssl_ctx_.get()));
207 // No session is available for id1, even though the entry exists.
208 EXPECT_EQ(1u, cache.size());
209 EXPECT_EQ(nullptr, cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get()));
210 // Lookup() will trigger a deletion of invalid entry.
211 EXPECT_EQ(0u, cache.size());
212
213 auto session3 = MakeTestSession();
214 SSL_SESSION* unowned3 = session3.get();
215 QuicServerId id3("c.com", 443);
216 cache.Insert(id3, std::move(session3), *params, nullptr);
217 cache.Insert(id2, std::move(session2), *params2, nullptr);
218 EXPECT_EQ(2u, cache.size());
219 EXPECT_EQ(
220 unowned2,
221 cache.Lookup(id2, clock_.WallNow(), ssl_ctx_.get())->tls_session.get());
222 EXPECT_EQ(
223 unowned3,
224 cache.Lookup(id3, clock_.WallNow(), ssl_ctx_.get())->tls_session.get());
225
226 // Verify that the cache is cleared after Lookups.
227 EXPECT_EQ(nullptr, cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get()));
228 EXPECT_EQ(nullptr, cache.Lookup(id2, clock_.WallNow(), ssl_ctx_.get()));
229 EXPECT_EQ(nullptr, cache.Lookup(id3, clock_.WallNow(), ssl_ctx_.get()));
230 EXPECT_EQ(0u, cache.size());
231 }
232
TEST_F(QuicClientSessionCacheTest,MultipleSessions)233 TEST_F(QuicClientSessionCacheTest, MultipleSessions) {
234 QuicClientSessionCache cache;
235
236 auto params = MakeFakeTransportParams();
237 auto session = MakeTestSession();
238 QuicServerId id1("a.com", 443);
239 auto session2 = MakeTestSession();
240 SSL_SESSION* unowned2 = session2.get();
241 auto session3 = MakeTestSession();
242 SSL_SESSION* unowned3 = session3.get();
243
244 cache.Insert(id1, std::move(session), *params, nullptr);
245 cache.Insert(id1, std::move(session2), *params, nullptr);
246 cache.Insert(id1, std::move(session3), *params, nullptr);
247 // The latest session is popped first.
248 EXPECT_EQ(
249 unowned3,
250 cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get())->tls_session.get());
251 EXPECT_EQ(
252 unowned2,
253 cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get())->tls_session.get());
254 // Only two sessions are cached.
255 EXPECT_EQ(nullptr, cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get()));
256 }
257
258 // Test that when a different TransportParameter is inserted for
259 // the same server id, the existing entry is removed.
TEST_F(QuicClientSessionCacheTest,DifferentTransportParams)260 TEST_F(QuicClientSessionCacheTest, DifferentTransportParams) {
261 QuicClientSessionCache cache;
262
263 auto params = MakeFakeTransportParams();
264 auto session = MakeTestSession();
265 QuicServerId id1("a.com", 443);
266 auto session2 = MakeTestSession();
267 auto session3 = MakeTestSession();
268 SSL_SESSION* unowned3 = session3.get();
269
270 cache.Insert(id1, std::move(session), *params, nullptr);
271 cache.Insert(id1, std::move(session2), *params, nullptr);
272 // tweak the transport parameters a little bit.
273 params->perspective = Perspective::IS_SERVER;
274 cache.Insert(id1, std::move(session3), *params, nullptr);
275 auto resumption_state = cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get());
276 EXPECT_EQ(unowned3, resumption_state->tls_session.get());
277 EXPECT_EQ(*params.get(), *resumption_state->transport_params);
278 EXPECT_EQ(nullptr, cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get()));
279 }
280
TEST_F(QuicClientSessionCacheTest,DifferentApplicationState)281 TEST_F(QuicClientSessionCacheTest, DifferentApplicationState) {
282 QuicClientSessionCache cache;
283
284 auto params = MakeFakeTransportParams();
285 auto session = MakeTestSession();
286 QuicServerId id1("a.com", 443);
287 auto session2 = MakeTestSession();
288 auto session3 = MakeTestSession();
289 SSL_SESSION* unowned3 = session3.get();
290 ApplicationState state;
291 state.push_back('a');
292
293 cache.Insert(id1, std::move(session), *params, &state);
294 cache.Insert(id1, std::move(session2), *params, &state);
295 cache.Insert(id1, std::move(session3), *params, nullptr);
296 auto resumption_state = cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get());
297 EXPECT_EQ(unowned3, resumption_state->tls_session.get());
298 EXPECT_EQ(nullptr, resumption_state->application_state);
299 EXPECT_EQ(nullptr, cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get()));
300 }
301
TEST_F(QuicClientSessionCacheTest,BothStatesDifferent)302 TEST_F(QuicClientSessionCacheTest, BothStatesDifferent) {
303 QuicClientSessionCache cache;
304
305 auto params = MakeFakeTransportParams();
306 auto session = MakeTestSession();
307 QuicServerId id1("a.com", 443);
308 auto session2 = MakeTestSession();
309 auto session3 = MakeTestSession();
310 SSL_SESSION* unowned3 = session3.get();
311 ApplicationState state;
312 state.push_back('a');
313
314 cache.Insert(id1, std::move(session), *params, &state);
315 cache.Insert(id1, std::move(session2), *params, &state);
316 params->perspective = Perspective::IS_SERVER;
317 cache.Insert(id1, std::move(session3), *params, nullptr);
318 auto resumption_state = cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get());
319 EXPECT_EQ(unowned3, resumption_state->tls_session.get());
320 EXPECT_EQ(*params.get(), *resumption_state->transport_params);
321 EXPECT_EQ(nullptr, resumption_state->application_state);
322 EXPECT_EQ(nullptr, cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get()));
323 }
324
325 // When the size limit is exceeded, the oldest entry should be erased.
TEST_F(QuicClientSessionCacheTest,SizeLimit)326 TEST_F(QuicClientSessionCacheTest, SizeLimit) {
327 QuicClientSessionCache cache(2);
328
329 auto params = MakeFakeTransportParams();
330 auto session = MakeTestSession();
331 QuicServerId id1("a.com", 443);
332
333 auto session2 = MakeTestSession();
334 SSL_SESSION* unowned2 = session2.get();
335 QuicServerId id2("b.com", 443);
336
337 auto session3 = MakeTestSession();
338 SSL_SESSION* unowned3 = session3.get();
339 QuicServerId id3("c.com", 443);
340
341 cache.Insert(id1, std::move(session), *params, nullptr);
342 cache.Insert(id2, std::move(session2), *params, nullptr);
343 cache.Insert(id3, std::move(session3), *params, nullptr);
344
345 EXPECT_EQ(2u, cache.size());
346 EXPECT_EQ(
347 unowned2,
348 cache.Lookup(id2, clock_.WallNow(), ssl_ctx_.get())->tls_session.get());
349 EXPECT_EQ(
350 unowned3,
351 cache.Lookup(id3, clock_.WallNow(), ssl_ctx_.get())->tls_session.get());
352 EXPECT_EQ(nullptr, cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get()));
353 }
354
TEST_F(QuicClientSessionCacheTest,ClearEarlyData)355 TEST_F(QuicClientSessionCacheTest, ClearEarlyData) {
356 QuicClientSessionCache cache;
357 SSL_CTX_set_early_data_enabled(ssl_ctx_.get(), 1);
358 auto params = MakeFakeTransportParams();
359 auto session = MakeTestSession();
360 QuicServerId id1("a.com", 443);
361 auto session2 = MakeTestSession();
362
363 EXPECT_TRUE(SSL_SESSION_early_data_capable(session.get()));
364 EXPECT_TRUE(SSL_SESSION_early_data_capable(session2.get()));
365
366 cache.Insert(id1, std::move(session), *params, nullptr);
367 cache.Insert(id1, std::move(session2), *params, nullptr);
368
369 cache.ClearEarlyData(id1);
370
371 auto resumption_state = cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get());
372 EXPECT_FALSE(
373 SSL_SESSION_early_data_capable(resumption_state->tls_session.get()));
374 resumption_state = cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get());
375 EXPECT_FALSE(
376 SSL_SESSION_early_data_capable(resumption_state->tls_session.get()));
377 EXPECT_EQ(nullptr, cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get()));
378 }
379
380 // Expired session isn't considered valid and nullptr will be returned upon
381 // Lookup.
TEST_F(QuicClientSessionCacheTest,Expiration)382 TEST_F(QuicClientSessionCacheTest, Expiration) {
383 QuicClientSessionCache cache;
384
385 auto params = MakeFakeTransportParams();
386 auto session = MakeTestSession();
387 QuicServerId id1("a.com", 443);
388
389 auto session2 = MakeTestSession(3 * kTimeout);
390 SSL_SESSION* unowned2 = session2.get();
391 QuicServerId id2("b.com", 443);
392
393 cache.Insert(id1, std::move(session), *params, nullptr);
394 cache.Insert(id2, std::move(session2), *params, nullptr);
395
396 EXPECT_EQ(2u, cache.size());
397 // Expire the session.
398 clock_.AdvanceTime(kTimeout * 2);
399 // The entry has not been removed yet.
400 EXPECT_EQ(2u, cache.size());
401
402 EXPECT_EQ(nullptr, cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get()));
403 EXPECT_EQ(1u, cache.size());
404 EXPECT_EQ(
405 unowned2,
406 cache.Lookup(id2, clock_.WallNow(), ssl_ctx_.get())->tls_session.get());
407 EXPECT_EQ(1u, cache.size());
408 }
409
TEST_F(QuicClientSessionCacheTest,RemoveExpiredEntriesAndClear)410 TEST_F(QuicClientSessionCacheTest, RemoveExpiredEntriesAndClear) {
411 QuicClientSessionCache cache;
412
413 auto params = MakeFakeTransportParams();
414 auto session = MakeTestSession();
415 quic::QuicServerId id1("a.com", 443);
416
417 auto session2 = MakeTestSession(3 * kTimeout);
418 quic::QuicServerId id2("b.com", 443);
419
420 cache.Insert(id1, std::move(session), *params, nullptr);
421 cache.Insert(id2, std::move(session2), *params, nullptr);
422
423 EXPECT_EQ(2u, cache.size());
424 // Expire the session.
425 clock_.AdvanceTime(kTimeout * 2);
426 // The entry has not been removed yet.
427 EXPECT_EQ(2u, cache.size());
428
429 // Flush expired sessions.
430 cache.RemoveExpiredEntries(clock_.WallNow());
431
432 // session is expired and should be flushed.
433 EXPECT_EQ(nullptr, cache.Lookup(id1, clock_.WallNow(), ssl_ctx_.get()));
434 EXPECT_EQ(1u, cache.size());
435
436 cache.Clear();
437 EXPECT_EQ(0u, cache.size());
438 }
439
440 } // namespace
441 } // namespace test
442 } // namespace quic
443