| 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 <openssl/ssl.h> | 7 #include <openssl/ssl.h> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/run_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/test/simple_test_clock.h" | 12 #include "base/test/simple_test_clock.h" |
| 12 #include "net/ssl/scoped_openssl_types.h" | 13 #include "net/ssl/scoped_openssl_types.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 // Test basic insertion and lookup operations. | 18 // Test basic insertion and lookup operations. |
| 18 TEST(SSLClientSessionCacheTest, Basic) { | 19 TEST(SSLClientSessionCacheTest, Basic) { |
| 19 SSLClientSessionCache::Config config; | 20 SSLClientSessionCache::Config config; |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 cache.Insert("key", session.get()); | 218 cache.Insert("key", session.get()); |
| 218 EXPECT_EQ(session.get(), cache.Lookup("key").get()); | 219 EXPECT_EQ(session.get(), cache.Lookup("key").get()); |
| 219 EXPECT_EQ(1u, cache.size()); | 220 EXPECT_EQ(1u, cache.size()); |
| 220 | 221 |
| 221 clock->Advance(-kTimeout * 2); | 222 clock->Advance(-kTimeout * 2); |
| 222 | 223 |
| 223 EXPECT_EQ(nullptr, cache.Lookup("key").get()); | 224 EXPECT_EQ(nullptr, cache.Lookup("key").get()); |
| 224 EXPECT_EQ(0u, cache.size()); | 225 EXPECT_EQ(0u, cache.size()); |
| 225 } | 226 } |
| 226 | 227 |
| 228 // Test that SSL cache is flushed on low memory notifications |
| 229 TEST(SSLClientSessionCacheTest, TestFlushOnMemoryNotifications) { |
| 230 // kExpirationCheckCount is set to a suitably large number so the automated |
| 231 // pruning never triggers. |
| 232 const size_t kExpirationCheckCount = 1000; |
| 233 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); |
| 234 |
| 235 SSLClientSessionCache::Config config; |
| 236 config.expiration_check_count = kExpirationCheckCount; |
| 237 config.timeout = kTimeout; |
| 238 SSLClientSessionCache cache(config); |
| 239 base::SimpleTestClock* clock = new base::SimpleTestClock; |
| 240 cache.SetClockForTesting(base::WrapUnique(clock)); |
| 241 |
| 242 // Insert an entry into the session cache. |
| 243 ScopedSSL_SESSION session1(SSL_SESSION_new()); |
| 244 cache.Insert("key1", session1.get()); |
| 245 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); |
| 246 EXPECT_EQ(1u, cache.size()); |
| 247 |
| 248 // Expire the session. |
| 249 clock->Advance(kTimeout * 2); |
| 250 // Add one more session. |
| 251 ScopedSSL_SESSION session2(SSL_SESSION_new()); |
| 252 cache.Insert("key2", session2.get()); |
| 253 EXPECT_EQ(2u, cache.size()); |
| 254 |
| 255 // Fire a notification that will flush expired sessions. |
| 256 base::MemoryPressureListener::NotifyMemoryPressure( |
| 257 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
| 258 base::RunLoop().RunUntilIdle(); |
| 259 |
| 260 // Expired session's cache should be flushed. |
| 261 // Lookup returns nullptr, when cache entry not found. |
| 262 EXPECT_FALSE(cache.Lookup("key1")); |
| 263 EXPECT_TRUE(cache.Lookup("key2")); |
| 264 EXPECT_EQ(1u, cache.size()); |
| 265 |
| 266 // Fire notification that will flush everything. |
| 267 base::MemoryPressureListener::NotifyMemoryPressure( |
| 268 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 269 base::RunLoop().RunUntilIdle(); |
| 270 EXPECT_EQ(0u, cache.size()); |
| 271 } |
| 272 |
| 227 } // namespace net | 273 } // namespace net |
| OLD | NEW |