Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ssl/ssl_client_session_cache.h" | 5 #include "net/ssl/ssl_client_session_cache.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/test/simple_test_clock.h" | 10 #include "base/test/simple_test_clock.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 SSLClientSessionCache::Config config; | 43 SSLClientSessionCache::Config config; |
| 44 SSLClientSessionCache cache(config); | 44 SSLClientSessionCache cache(config); |
| 45 | 45 |
| 46 bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_new()); | 46 bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_new()); |
| 47 bssl::UniquePtr<SSL_SESSION> session2(SSL_SESSION_new()); | 47 bssl::UniquePtr<SSL_SESSION> session2(SSL_SESSION_new()); |
| 48 bssl::UniquePtr<SSL_SESSION> session3(SSL_SESSION_new()); | 48 bssl::UniquePtr<SSL_SESSION> session3(SSL_SESSION_new()); |
| 49 EXPECT_EQ(1u, session1->references); | 49 EXPECT_EQ(1u, session1->references); |
| 50 EXPECT_EQ(1u, session2->references); | 50 EXPECT_EQ(1u, session2->references); |
| 51 EXPECT_EQ(1u, session3->references); | 51 EXPECT_EQ(1u, session3->references); |
| 52 | 52 |
| 53 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); | 53 EXPECT_EQ(nullptr, cache.Lookup("key1", nullptr).get()); |
| 54 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); | 54 EXPECT_EQ(nullptr, cache.Lookup("key2", nullptr).get()); |
| 55 EXPECT_EQ(0u, cache.size()); | 55 EXPECT_EQ(0u, cache.size()); |
| 56 | 56 |
| 57 cache.Insert("key1", session1.get()); | 57 cache.Insert("key1", session1.get()); |
| 58 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); | 58 EXPECT_EQ(session1.get(), cache.Lookup("key1", nullptr).get()); |
| 59 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); | 59 EXPECT_EQ(nullptr, cache.Lookup("key2", nullptr).get()); |
| 60 EXPECT_EQ(1u, cache.size()); | 60 EXPECT_EQ(1u, cache.size()); |
| 61 | 61 |
| 62 cache.Insert("key2", session2.get()); | 62 cache.Insert("key2", session2.get()); |
| 63 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); | 63 EXPECT_EQ(session1.get(), cache.Lookup("key1", nullptr).get()); |
| 64 EXPECT_EQ(session2.get(), cache.Lookup("key2").get()); | 64 EXPECT_EQ(session2.get(), cache.Lookup("key2", nullptr).get()); |
| 65 EXPECT_EQ(2u, cache.size()); | 65 EXPECT_EQ(2u, cache.size()); |
| 66 | 66 |
| 67 EXPECT_EQ(2u, session1->references); | 67 EXPECT_EQ(2u, session1->references); |
| 68 EXPECT_EQ(2u, session2->references); | 68 EXPECT_EQ(2u, session2->references); |
| 69 | 69 |
| 70 cache.Insert("key1", session3.get()); | 70 cache.Insert("key1", session3.get()); |
| 71 EXPECT_EQ(session3.get(), cache.Lookup("key1").get()); | 71 EXPECT_EQ(session3.get(), cache.Lookup("key1", nullptr).get()); |
| 72 EXPECT_EQ(session2.get(), cache.Lookup("key2").get()); | 72 EXPECT_EQ(session2.get(), cache.Lookup("key2", nullptr).get()); |
| 73 EXPECT_EQ(2u, cache.size()); | 73 EXPECT_EQ(2u, cache.size()); |
| 74 | 74 |
| 75 EXPECT_EQ(1u, session1->references); | 75 EXPECT_EQ(1u, session1->references); |
| 76 EXPECT_EQ(2u, session2->references); | 76 EXPECT_EQ(2u, session2->references); |
| 77 EXPECT_EQ(2u, session3->references); | 77 EXPECT_EQ(2u, session3->references); |
| 78 | 78 |
| 79 cache.Flush(); | 79 cache.Flush(); |
| 80 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); | 80 EXPECT_EQ(nullptr, cache.Lookup("key1", nullptr).get()); |
| 81 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); | 81 EXPECT_EQ(nullptr, cache.Lookup("key2", nullptr).get()); |
| 82 EXPECT_EQ(nullptr, cache.Lookup("key3").get()); | 82 EXPECT_EQ(nullptr, cache.Lookup("key3", nullptr).get()); |
| 83 EXPECT_EQ(0u, cache.size()); | 83 EXPECT_EQ(0u, cache.size()); |
| 84 | 84 |
| 85 EXPECT_EQ(1u, session1->references); | 85 EXPECT_EQ(1u, session1->references); |
| 86 EXPECT_EQ(1u, session2->references); | 86 EXPECT_EQ(1u, session2->references); |
| 87 EXPECT_EQ(1u, session3->references); | 87 EXPECT_EQ(1u, session3->references); |
| 88 } | 88 } |
| 89 | 89 |
| 90 // Test that pairs of calls to Lookup/ResetLookupCount appropriately log to | |
| 91 // UMA. | |
| 92 TEST(SSLClientSessionCacheTest, LookupCountUMA) { | |
| 93 SSLClientSessionCache::Config config; | |
| 94 SSLClientSessionCache cache(config); | |
| 95 | |
| 96 bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_new()); | |
| 97 bssl::UniquePtr<SSL_SESSION> session2(SSL_SESSION_new()); | |
| 98 cache.Insert("key1", session1.get()); | |
| 99 cache.Insert("key2", session2.get()); | |
| 100 | |
| 101 // Test that the count returned increments when multiple lookups of the same | |
| 102 // key/session occur before the count is reset. | |
| 103 size_t count; | |
| 104 EXPECT_EQ(session1.get(), cache.Lookup("key1", &count).get()); | |
| 105 EXPECT_EQ(1u, count); | |
| 106 EXPECT_EQ(session1.get(), cache.Lookup("key1", &count).get()); | |
| 107 EXPECT_EQ(2u, count); | |
| 108 cache.ResetLookupCount("key1"); | |
| 109 EXPECT_EQ(session1.get(), cache.Lookup("key1", &count).get()); | |
| 110 cache.ResetLookupCount("key1"); | |
| 111 EXPECT_EQ(1u, count); | |
| 112 | |
| 113 // Test that independent keys are logged separately. | |
| 114 EXPECT_EQ(session1.get(), cache.Lookup("key1", &count).get()); | |
| 115 EXPECT_EQ(1u, count); | |
| 116 EXPECT_EQ(session2.get(), cache.Lookup("key2", &count).get()); | |
| 117 EXPECT_EQ(1u, count); | |
| 118 | |
| 119 // Test that the count is 0 if there is a cache miss. | |
| 120 EXPECT_EQ(nullptr, cache.Lookup("not present", &count).get()); | |
| 121 EXPECT_EQ(0u, count); | |
|
davidben
2017/01/19 21:56:12
For completeness, I'd probably add a test that Res
nharper
2017/01/19 22:09:07
Done.
| |
| 122 | |
| 123 EXPECT_EQ(2u, cache.size()); | |
| 124 } | |
| 125 | |
| 90 // Test that a session may be inserted at two different keys. This should never | 126 // Test that a session may be inserted at two different keys. This should never |
| 91 // be necessary, but the API doesn't prohibit it. | 127 // be necessary, but the API doesn't prohibit it. |
| 92 TEST(SSLClientSessionCacheTest, DoubleInsert) { | 128 TEST(SSLClientSessionCacheTest, DoubleInsert) { |
| 93 SSLClientSessionCache::Config config; | 129 SSLClientSessionCache::Config config; |
| 94 SSLClientSessionCache cache(config); | 130 SSLClientSessionCache cache(config); |
| 95 | 131 |
| 96 bssl::UniquePtr<SSL_SESSION> session(SSL_SESSION_new()); | 132 bssl::UniquePtr<SSL_SESSION> session(SSL_SESSION_new()); |
| 97 EXPECT_EQ(1u, session->references); | 133 EXPECT_EQ(1u, session->references); |
| 98 | 134 |
| 99 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); | 135 EXPECT_EQ(nullptr, cache.Lookup("key1", nullptr).get()); |
| 100 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); | 136 EXPECT_EQ(nullptr, cache.Lookup("key2", nullptr).get()); |
| 101 EXPECT_EQ(0u, cache.size()); | 137 EXPECT_EQ(0u, cache.size()); |
| 102 | 138 |
| 103 cache.Insert("key1", session.get()); | 139 cache.Insert("key1", session.get()); |
| 104 EXPECT_EQ(session.get(), cache.Lookup("key1").get()); | 140 EXPECT_EQ(session.get(), cache.Lookup("key1", nullptr).get()); |
| 105 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); | 141 EXPECT_EQ(nullptr, cache.Lookup("key2", nullptr).get()); |
| 106 EXPECT_EQ(1u, cache.size()); | 142 EXPECT_EQ(1u, cache.size()); |
| 107 | 143 |
| 108 EXPECT_EQ(2u, session->references); | 144 EXPECT_EQ(2u, session->references); |
| 109 | 145 |
| 110 cache.Insert("key2", session.get()); | 146 cache.Insert("key2", session.get()); |
| 111 EXPECT_EQ(session.get(), cache.Lookup("key1").get()); | 147 EXPECT_EQ(session.get(), cache.Lookup("key1", nullptr).get()); |
| 112 EXPECT_EQ(session.get(), cache.Lookup("key2").get()); | 148 EXPECT_EQ(session.get(), cache.Lookup("key2", nullptr).get()); |
| 113 EXPECT_EQ(2u, cache.size()); | 149 EXPECT_EQ(2u, cache.size()); |
| 114 | 150 |
| 115 EXPECT_EQ(3u, session->references); | 151 EXPECT_EQ(3u, session->references); |
| 116 | 152 |
| 117 cache.Flush(); | 153 cache.Flush(); |
| 118 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); | 154 EXPECT_EQ(nullptr, cache.Lookup("key1", nullptr).get()); |
| 119 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); | 155 EXPECT_EQ(nullptr, cache.Lookup("key2", nullptr).get()); |
| 120 EXPECT_EQ(0u, cache.size()); | 156 EXPECT_EQ(0u, cache.size()); |
| 121 | 157 |
| 122 EXPECT_EQ(1u, session->references); | 158 EXPECT_EQ(1u, session->references); |
| 123 } | 159 } |
| 124 | 160 |
| 125 // Tests that the session cache's size is correctly bounded. | 161 // Tests that the session cache's size is correctly bounded. |
| 126 TEST(SSLClientSessionCacheTest, MaxEntries) { | 162 TEST(SSLClientSessionCacheTest, MaxEntries) { |
| 127 SSLClientSessionCache::Config config; | 163 SSLClientSessionCache::Config config; |
| 128 config.max_entries = 3; | 164 config.max_entries = 3; |
| 129 SSLClientSessionCache cache(config); | 165 SSLClientSessionCache cache(config); |
| 130 | 166 |
| 131 bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_new()); | 167 bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_new()); |
| 132 bssl::UniquePtr<SSL_SESSION> session2(SSL_SESSION_new()); | 168 bssl::UniquePtr<SSL_SESSION> session2(SSL_SESSION_new()); |
| 133 bssl::UniquePtr<SSL_SESSION> session3(SSL_SESSION_new()); | 169 bssl::UniquePtr<SSL_SESSION> session3(SSL_SESSION_new()); |
| 134 bssl::UniquePtr<SSL_SESSION> session4(SSL_SESSION_new()); | 170 bssl::UniquePtr<SSL_SESSION> session4(SSL_SESSION_new()); |
| 135 | 171 |
| 136 // Insert three entries. | 172 // Insert three entries. |
| 137 cache.Insert("key1", session1.get()); | 173 cache.Insert("key1", session1.get()); |
| 138 cache.Insert("key2", session2.get()); | 174 cache.Insert("key2", session2.get()); |
| 139 cache.Insert("key3", session3.get()); | 175 cache.Insert("key3", session3.get()); |
| 140 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); | 176 EXPECT_EQ(session1.get(), cache.Lookup("key1", nullptr).get()); |
| 141 EXPECT_EQ(session2.get(), cache.Lookup("key2").get()); | 177 EXPECT_EQ(session2.get(), cache.Lookup("key2", nullptr).get()); |
| 142 EXPECT_EQ(session3.get(), cache.Lookup("key3").get()); | 178 EXPECT_EQ(session3.get(), cache.Lookup("key3", nullptr).get()); |
| 143 EXPECT_EQ(3u, cache.size()); | 179 EXPECT_EQ(3u, cache.size()); |
| 144 | 180 |
| 145 // On insertion of a fourth, the first is removed. | 181 // On insertion of a fourth, the first is removed. |
| 146 cache.Insert("key4", session4.get()); | 182 cache.Insert("key4", session4.get()); |
| 147 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); | 183 EXPECT_EQ(nullptr, cache.Lookup("key1", nullptr).get()); |
| 148 EXPECT_EQ(session4.get(), cache.Lookup("key4").get()); | 184 EXPECT_EQ(session4.get(), cache.Lookup("key4", nullptr).get()); |
| 149 EXPECT_EQ(session3.get(), cache.Lookup("key3").get()); | 185 EXPECT_EQ(session3.get(), cache.Lookup("key3", nullptr).get()); |
| 150 EXPECT_EQ(session2.get(), cache.Lookup("key2").get()); | 186 EXPECT_EQ(session2.get(), cache.Lookup("key2", nullptr).get()); |
| 151 EXPECT_EQ(3u, cache.size()); | 187 EXPECT_EQ(3u, cache.size()); |
| 152 | 188 |
| 153 // Despite being newest, the next to be removed is session4 as it was accessed | 189 // Despite being newest, the next to be removed is session4 as it was accessed |
| 154 // least. recently. | 190 // least. recently. |
| 155 cache.Insert("key1", session1.get()); | 191 cache.Insert("key1", session1.get()); |
| 156 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); | 192 EXPECT_EQ(session1.get(), cache.Lookup("key1", nullptr).get()); |
| 157 EXPECT_EQ(session2.get(), cache.Lookup("key2").get()); | 193 EXPECT_EQ(session2.get(), cache.Lookup("key2", nullptr).get()); |
| 158 EXPECT_EQ(session3.get(), cache.Lookup("key3").get()); | 194 EXPECT_EQ(session3.get(), cache.Lookup("key3", nullptr).get()); |
| 159 EXPECT_EQ(nullptr, cache.Lookup("key4").get()); | 195 EXPECT_EQ(nullptr, cache.Lookup("key4", nullptr).get()); |
| 160 EXPECT_EQ(3u, cache.size()); | 196 EXPECT_EQ(3u, cache.size()); |
| 161 } | 197 } |
| 162 | 198 |
| 163 // Tests that session expiration works properly. | 199 // Tests that session expiration works properly. |
| 164 TEST(SSLClientSessionCacheTest, Expiration) { | 200 TEST(SSLClientSessionCacheTest, Expiration) { |
| 165 const size_t kNumEntries = 20; | 201 const size_t kNumEntries = 20; |
| 166 const size_t kExpirationCheckCount = 10; | 202 const size_t kExpirationCheckCount = 10; |
| 167 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); | 203 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); |
| 168 | 204 |
| 169 SSLClientSessionCache::Config config; | 205 SSLClientSessionCache::Config config; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 185 bssl::UniquePtr<SSL_SESSION> session = | 221 bssl::UniquePtr<SSL_SESSION> session = |
| 186 MakeTestSession(clock->Now(), kTimeout); | 222 MakeTestSession(clock->Now(), kTimeout); |
| 187 cache.Insert("key", session.get()); | 223 cache.Insert("key", session.get()); |
| 188 | 224 |
| 189 // All entries are still in the cache. | 225 // All entries are still in the cache. |
| 190 EXPECT_EQ(kNumEntries, cache.size()); | 226 EXPECT_EQ(kNumEntries, cache.size()); |
| 191 | 227 |
| 192 // Perform one fewer lookup than needed to trigger the expiration check. This | 228 // Perform one fewer lookup than needed to trigger the expiration check. This |
| 193 // shall not expire any session. | 229 // shall not expire any session. |
| 194 for (size_t i = 0; i < kExpirationCheckCount - 1; i++) | 230 for (size_t i = 0; i < kExpirationCheckCount - 1; i++) |
| 195 cache.Lookup("key"); | 231 cache.Lookup("key", nullptr); |
| 196 | 232 |
| 197 // All entries are still in the cache. | 233 // All entries are still in the cache. |
| 198 EXPECT_EQ(kNumEntries, cache.size()); | 234 EXPECT_EQ(kNumEntries, cache.size()); |
| 199 | 235 |
| 200 // Perform one more lookup. This will expire all sessions but the last one. | 236 // Perform one more lookup. This will expire all sessions but the last one. |
| 201 cache.Lookup("key"); | 237 cache.Lookup("key", nullptr); |
| 202 EXPECT_EQ(1u, cache.size()); | 238 EXPECT_EQ(1u, cache.size()); |
| 203 EXPECT_EQ(session.get(), cache.Lookup("key").get()); | 239 EXPECT_EQ(session.get(), cache.Lookup("key", nullptr).get()); |
| 204 for (size_t i = 0; i < kNumEntries - 1; i++) { | 240 for (size_t i = 0; i < kNumEntries - 1; i++) { |
| 205 SCOPED_TRACE(i); | 241 SCOPED_TRACE(i); |
| 206 EXPECT_EQ(nullptr, cache.Lookup(base::SizeTToString(i))); | 242 EXPECT_EQ(nullptr, cache.Lookup(base::SizeTToString(i), nullptr)); |
| 207 } | 243 } |
| 208 } | 244 } |
| 209 | 245 |
| 210 // Tests that Lookup performs an expiration check before returning a cached | 246 // Tests that Lookup performs an expiration check before returning a cached |
| 211 // session. | 247 // session. |
| 212 TEST(SSLClientSessionCacheTest, LookupExpirationCheck) { | 248 TEST(SSLClientSessionCacheTest, LookupExpirationCheck) { |
| 213 // kExpirationCheckCount is set to a suitably large number so the automated | 249 // kExpirationCheckCount is set to a suitably large number so the automated |
| 214 // pruning never triggers. | 250 // pruning never triggers. |
| 215 const size_t kExpirationCheckCount = 1000; | 251 const size_t kExpirationCheckCount = 1000; |
| 216 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); | 252 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); |
| 217 | 253 |
| 218 SSLClientSessionCache::Config config; | 254 SSLClientSessionCache::Config config; |
| 219 config.expiration_check_count = kExpirationCheckCount; | 255 config.expiration_check_count = kExpirationCheckCount; |
| 220 SSLClientSessionCache cache(config); | 256 SSLClientSessionCache cache(config); |
| 221 base::SimpleTestClock* clock = MakeTestClock().release(); | 257 base::SimpleTestClock* clock = MakeTestClock().release(); |
| 222 cache.SetClockForTesting(base::WrapUnique(clock)); | 258 cache.SetClockForTesting(base::WrapUnique(clock)); |
| 223 | 259 |
| 224 // Insert an entry into the session cache. | 260 // Insert an entry into the session cache. |
| 225 bssl::UniquePtr<SSL_SESSION> session = | 261 bssl::UniquePtr<SSL_SESSION> session = |
| 226 MakeTestSession(clock->Now(), kTimeout); | 262 MakeTestSession(clock->Now(), kTimeout); |
| 227 cache.Insert("key", session.get()); | 263 cache.Insert("key", session.get()); |
| 228 EXPECT_EQ(session.get(), cache.Lookup("key").get()); | 264 EXPECT_EQ(session.get(), cache.Lookup("key", nullptr).get()); |
| 229 EXPECT_EQ(1u, cache.size()); | 265 EXPECT_EQ(1u, cache.size()); |
| 230 | 266 |
| 231 // Expire the session. | 267 // Expire the session. |
| 232 clock->Advance(kTimeout * 2); | 268 clock->Advance(kTimeout * 2); |
| 233 | 269 |
| 234 // The entry has not been removed yet. | 270 // The entry has not been removed yet. |
| 235 EXPECT_EQ(1u, cache.size()); | 271 EXPECT_EQ(1u, cache.size()); |
| 236 | 272 |
| 237 // But it will not be returned on lookup and gets pruned at that point. | 273 // But it will not be returned on lookup and gets pruned at that point. |
| 238 EXPECT_EQ(nullptr, cache.Lookup("key").get()); | 274 EXPECT_EQ(nullptr, cache.Lookup("key", nullptr).get()); |
| 239 EXPECT_EQ(0u, cache.size()); | 275 EXPECT_EQ(0u, cache.size()); |
| 240 | 276 |
| 241 // Re-inserting a session does not refresh the lifetime. The expiration | 277 // Re-inserting a session does not refresh the lifetime. The expiration |
| 242 // information in the session is used. | 278 // information in the session is used. |
| 243 cache.Insert("key", session.get()); | 279 cache.Insert("key", session.get()); |
| 244 EXPECT_EQ(nullptr, cache.Lookup("key").get()); | 280 EXPECT_EQ(nullptr, cache.Lookup("key", nullptr).get()); |
| 245 EXPECT_EQ(0u, cache.size()); | 281 EXPECT_EQ(0u, cache.size()); |
| 246 | 282 |
| 247 // Re-insert a fresh copy of the session. | 283 // Re-insert a fresh copy of the session. |
| 248 session = MakeTestSession(clock->Now(), kTimeout); | 284 session = MakeTestSession(clock->Now(), kTimeout); |
| 249 cache.Insert("key", session.get()); | 285 cache.Insert("key", session.get()); |
| 250 EXPECT_EQ(session.get(), cache.Lookup("key").get()); | 286 EXPECT_EQ(session.get(), cache.Lookup("key", nullptr).get()); |
| 251 EXPECT_EQ(1u, cache.size()); | 287 EXPECT_EQ(1u, cache.size()); |
| 252 | 288 |
| 253 // Sessions also are treated as expired if the clock rewinds. | 289 // Sessions also are treated as expired if the clock rewinds. |
| 254 clock->Advance(base::TimeDelta::FromSeconds(-1)); | 290 clock->Advance(base::TimeDelta::FromSeconds(-1)); |
| 255 EXPECT_EQ(nullptr, cache.Lookup("key").get()); | 291 EXPECT_EQ(nullptr, cache.Lookup("key", nullptr).get()); |
| 256 EXPECT_EQ(0u, cache.size()); | 292 EXPECT_EQ(0u, cache.size()); |
| 257 } | 293 } |
| 258 | 294 |
| 259 // Test that SSL cache is flushed on low memory notifications | 295 // Test that SSL cache is flushed on low memory notifications |
| 260 TEST(SSLClientSessionCacheTest, TestFlushOnMemoryNotifications) { | 296 TEST(SSLClientSessionCacheTest, TestFlushOnMemoryNotifications) { |
| 261 // kExpirationCheckCount is set to a suitably large number so the automated | 297 // kExpirationCheckCount is set to a suitably large number so the automated |
| 262 // pruning never triggers. | 298 // pruning never triggers. |
| 263 const size_t kExpirationCheckCount = 1000; | 299 const size_t kExpirationCheckCount = 1000; |
| 264 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); | 300 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); |
| 265 | 301 |
| 266 SSLClientSessionCache::Config config; | 302 SSLClientSessionCache::Config config; |
| 267 config.expiration_check_count = kExpirationCheckCount; | 303 config.expiration_check_count = kExpirationCheckCount; |
| 268 SSLClientSessionCache cache(config); | 304 SSLClientSessionCache cache(config); |
| 269 base::SimpleTestClock* clock = MakeTestClock().release(); | 305 base::SimpleTestClock* clock = MakeTestClock().release(); |
| 270 cache.SetClockForTesting(base::WrapUnique(clock)); | 306 cache.SetClockForTesting(base::WrapUnique(clock)); |
| 271 | 307 |
| 272 // Insert an entry into the session cache. | 308 // Insert an entry into the session cache. |
| 273 bssl::UniquePtr<SSL_SESSION> session1 = | 309 bssl::UniquePtr<SSL_SESSION> session1 = |
| 274 MakeTestSession(clock->Now(), kTimeout); | 310 MakeTestSession(clock->Now(), kTimeout); |
| 275 cache.Insert("key1", session1.get()); | 311 cache.Insert("key1", session1.get()); |
| 276 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); | 312 EXPECT_EQ(session1.get(), cache.Lookup("key1", nullptr).get()); |
| 277 EXPECT_EQ(1u, cache.size()); | 313 EXPECT_EQ(1u, cache.size()); |
| 278 | 314 |
| 279 // Expire the session. | 315 // Expire the session. |
| 280 clock->Advance(kTimeout * 2); | 316 clock->Advance(kTimeout * 2); |
| 281 // Add one more session. | 317 // Add one more session. |
| 282 bssl::UniquePtr<SSL_SESSION> session2 = | 318 bssl::UniquePtr<SSL_SESSION> session2 = |
| 283 MakeTestSession(clock->Now(), kTimeout); | 319 MakeTestSession(clock->Now(), kTimeout); |
| 284 cache.Insert("key2", session2.get()); | 320 cache.Insert("key2", session2.get()); |
| 285 EXPECT_EQ(2u, cache.size()); | 321 EXPECT_EQ(2u, cache.size()); |
| 286 | 322 |
| 287 // Fire a notification that will flush expired sessions. | 323 // Fire a notification that will flush expired sessions. |
| 288 base::MemoryPressureListener::NotifyMemoryPressure( | 324 base::MemoryPressureListener::NotifyMemoryPressure( |
| 289 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); | 325 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
| 290 base::RunLoop().RunUntilIdle(); | 326 base::RunLoop().RunUntilIdle(); |
| 291 | 327 |
| 292 // Expired session's cache should be flushed. | 328 // Expired session's cache should be flushed. |
| 293 // Lookup returns nullptr, when cache entry not found. | 329 // Lookup returns nullptr, when cache entry not found. |
| 294 EXPECT_FALSE(cache.Lookup("key1")); | 330 EXPECT_FALSE(cache.Lookup("key1", nullptr)); |
| 295 EXPECT_TRUE(cache.Lookup("key2")); | 331 EXPECT_TRUE(cache.Lookup("key2", nullptr)); |
| 296 EXPECT_EQ(1u, cache.size()); | 332 EXPECT_EQ(1u, cache.size()); |
| 297 | 333 |
| 298 // Fire notification that will flush everything. | 334 // Fire notification that will flush everything. |
| 299 base::MemoryPressureListener::NotifyMemoryPressure( | 335 base::MemoryPressureListener::NotifyMemoryPressure( |
| 300 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); | 336 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 301 base::RunLoop().RunUntilIdle(); | 337 base::RunLoop().RunUntilIdle(); |
| 302 EXPECT_EQ(0u, cache.size()); | 338 EXPECT_EQ(0u, cache.size()); |
| 303 } | 339 } |
| 304 | 340 |
| 305 // Basic test for dumping memory stats. | 341 // Basic test for dumping memory stats. |
| 306 TEST(SSLClientSessionCacheTest, TestDumpMemoryStats) { | 342 TEST(SSLClientSessionCacheTest, TestDumpMemoryStats) { |
| 307 SSLClientSessionCache::Config config; | 343 SSLClientSessionCache::Config config; |
| 308 SSLClientSessionCache cache(config); | 344 SSLClientSessionCache cache(config); |
| 309 | 345 |
| 310 bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_new()); | 346 bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_new()); |
| 311 bssl::UniquePtr<SSL_SESSION> session2(SSL_SESSION_new()); | 347 bssl::UniquePtr<SSL_SESSION> session2(SSL_SESSION_new()); |
| 312 bssl::UniquePtr<SSL_SESSION> session3(SSL_SESSION_new()); | 348 bssl::UniquePtr<SSL_SESSION> session3(SSL_SESSION_new()); |
| 313 | 349 |
| 314 // Insert three entries. | 350 // Insert three entries. |
| 315 cache.Insert("key1", session1.get()); | 351 cache.Insert("key1", session1.get()); |
| 316 cache.Insert("key2", session2.get()); | 352 cache.Insert("key2", session2.get()); |
| 317 cache.Insert("key3", session3.get()); | 353 cache.Insert("key3", session3.get()); |
| 318 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); | 354 EXPECT_EQ(session1.get(), cache.Lookup("key1", nullptr).get()); |
| 319 EXPECT_EQ(session2.get(), cache.Lookup("key2").get()); | 355 EXPECT_EQ(session2.get(), cache.Lookup("key2", nullptr).get()); |
| 320 EXPECT_EQ(session3.get(), cache.Lookup("key3").get()); | 356 EXPECT_EQ(session3.get(), cache.Lookup("key3", nullptr).get()); |
| 321 EXPECT_EQ(3u, cache.size()); | 357 EXPECT_EQ(3u, cache.size()); |
| 322 | 358 |
| 323 base::trace_event::MemoryDumpArgs dump_args = { | 359 base::trace_event::MemoryDumpArgs dump_args = { |
| 324 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; | 360 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
| 325 std::unique_ptr<base::trace_event::ProcessMemoryDump> process_memory_dump( | 361 std::unique_ptr<base::trace_event::ProcessMemoryDump> process_memory_dump( |
| 326 new base::trace_event::ProcessMemoryDump(nullptr, dump_args)); | 362 new base::trace_event::ProcessMemoryDump(nullptr, dump_args)); |
| 327 cache.DumpMemoryStats(process_memory_dump.get()); | 363 cache.DumpMemoryStats(process_memory_dump.get()); |
| 328 | 364 |
| 329 const base::trace_event::MemoryAllocatorDump* dump = | 365 const base::trace_event::MemoryAllocatorDump* dump = |
| 330 process_memory_dump->GetAllocatorDump("net/ssl_session_cache"); | 366 process_memory_dump->GetAllocatorDump("net/ssl_session_cache"); |
| 331 ASSERT_NE(nullptr, dump); | 367 ASSERT_NE(nullptr, dump); |
| 332 std::unique_ptr<base::Value> raw_attrs = | 368 std::unique_ptr<base::Value> raw_attrs = |
| 333 dump->attributes_for_testing()->ToBaseValue(); | 369 dump->attributes_for_testing()->ToBaseValue(); |
| 334 base::DictionaryValue* attrs; | 370 base::DictionaryValue* attrs; |
| 335 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs)); | 371 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs)); |
| 336 ASSERT_TRUE(attrs->HasKey("cert_count")); | 372 ASSERT_TRUE(attrs->HasKey("cert_count")); |
| 337 ASSERT_TRUE(attrs->HasKey("serialized_cert_size")); | 373 ASSERT_TRUE(attrs->HasKey("serialized_cert_size")); |
| 338 ASSERT_TRUE(attrs->HasKey(base::trace_event::MemoryAllocatorDump::kNameSize)); | 374 ASSERT_TRUE(attrs->HasKey(base::trace_event::MemoryAllocatorDump::kNameSize)); |
| 339 } | 375 } |
| 340 | 376 |
| 341 } // namespace net | 377 } // namespace net |
| OLD | NEW |