| 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/run_loop.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/test/simple_test_clock.h" | 12 #include "base/test/simple_test_clock.h" |
| 13 #include "net/ssl/scoped_openssl_types.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 14 |
| 16 namespace net { | 15 namespace net { |
| 17 | 16 |
| 18 // Test basic insertion and lookup operations. | 17 // Test basic insertion and lookup operations. |
| 19 TEST(SSLClientSessionCacheTest, Basic) { | 18 TEST(SSLClientSessionCacheTest, Basic) { |
| 20 SSLClientSessionCache::Config config; | 19 SSLClientSessionCache::Config config; |
| 21 SSLClientSessionCache cache(config); | 20 SSLClientSessionCache cache(config); |
| 22 | 21 |
| 23 ScopedSSL_SESSION session1(SSL_SESSION_new()); | 22 bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_new()); |
| 24 ScopedSSL_SESSION session2(SSL_SESSION_new()); | 23 bssl::UniquePtr<SSL_SESSION> session2(SSL_SESSION_new()); |
| 25 ScopedSSL_SESSION session3(SSL_SESSION_new()); | 24 bssl::UniquePtr<SSL_SESSION> session3(SSL_SESSION_new()); |
| 26 EXPECT_EQ(1u, session1->references); | 25 EXPECT_EQ(1u, session1->references); |
| 27 EXPECT_EQ(1u, session2->references); | 26 EXPECT_EQ(1u, session2->references); |
| 28 EXPECT_EQ(1u, session3->references); | 27 EXPECT_EQ(1u, session3->references); |
| 29 | 28 |
| 30 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); | 29 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); |
| 31 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); | 30 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); |
| 32 EXPECT_EQ(0u, cache.size()); | 31 EXPECT_EQ(0u, cache.size()); |
| 33 | 32 |
| 34 cache.Insert("key1", session1.get()); | 33 cache.Insert("key1", session1.get()); |
| 35 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); | 34 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 63 EXPECT_EQ(1u, session2->references); | 62 EXPECT_EQ(1u, session2->references); |
| 64 EXPECT_EQ(1u, session3->references); | 63 EXPECT_EQ(1u, session3->references); |
| 65 } | 64 } |
| 66 | 65 |
| 67 // Test that a session may be inserted at two different keys. This should never | 66 // Test that a session may be inserted at two different keys. This should never |
| 68 // be necessary, but the API doesn't prohibit it. | 67 // be necessary, but the API doesn't prohibit it. |
| 69 TEST(SSLClientSessionCacheTest, DoubleInsert) { | 68 TEST(SSLClientSessionCacheTest, DoubleInsert) { |
| 70 SSLClientSessionCache::Config config; | 69 SSLClientSessionCache::Config config; |
| 71 SSLClientSessionCache cache(config); | 70 SSLClientSessionCache cache(config); |
| 72 | 71 |
| 73 ScopedSSL_SESSION session(SSL_SESSION_new()); | 72 bssl::UniquePtr<SSL_SESSION> session(SSL_SESSION_new()); |
| 74 EXPECT_EQ(1u, session->references); | 73 EXPECT_EQ(1u, session->references); |
| 75 | 74 |
| 76 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); | 75 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); |
| 77 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); | 76 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); |
| 78 EXPECT_EQ(0u, cache.size()); | 77 EXPECT_EQ(0u, cache.size()); |
| 79 | 78 |
| 80 cache.Insert("key1", session.get()); | 79 cache.Insert("key1", session.get()); |
| 81 EXPECT_EQ(session.get(), cache.Lookup("key1").get()); | 80 EXPECT_EQ(session.get(), cache.Lookup("key1").get()); |
| 82 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); | 81 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); |
| 83 EXPECT_EQ(1u, cache.size()); | 82 EXPECT_EQ(1u, cache.size()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 98 | 97 |
| 99 EXPECT_EQ(1u, session->references); | 98 EXPECT_EQ(1u, session->references); |
| 100 } | 99 } |
| 101 | 100 |
| 102 // Tests that the session cache's size is correctly bounded. | 101 // Tests that the session cache's size is correctly bounded. |
| 103 TEST(SSLClientSessionCacheTest, MaxEntries) { | 102 TEST(SSLClientSessionCacheTest, MaxEntries) { |
| 104 SSLClientSessionCache::Config config; | 103 SSLClientSessionCache::Config config; |
| 105 config.max_entries = 3; | 104 config.max_entries = 3; |
| 106 SSLClientSessionCache cache(config); | 105 SSLClientSessionCache cache(config); |
| 107 | 106 |
| 108 ScopedSSL_SESSION session1(SSL_SESSION_new()); | 107 bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_new()); |
| 109 ScopedSSL_SESSION session2(SSL_SESSION_new()); | 108 bssl::UniquePtr<SSL_SESSION> session2(SSL_SESSION_new()); |
| 110 ScopedSSL_SESSION session3(SSL_SESSION_new()); | 109 bssl::UniquePtr<SSL_SESSION> session3(SSL_SESSION_new()); |
| 111 ScopedSSL_SESSION session4(SSL_SESSION_new()); | 110 bssl::UniquePtr<SSL_SESSION> session4(SSL_SESSION_new()); |
| 112 | 111 |
| 113 // Insert three entries. | 112 // Insert three entries. |
| 114 cache.Insert("key1", session1.get()); | 113 cache.Insert("key1", session1.get()); |
| 115 cache.Insert("key2", session2.get()); | 114 cache.Insert("key2", session2.get()); |
| 116 cache.Insert("key3", session3.get()); | 115 cache.Insert("key3", session3.get()); |
| 117 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); | 116 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); |
| 118 EXPECT_EQ(session2.get(), cache.Lookup("key2").get()); | 117 EXPECT_EQ(session2.get(), cache.Lookup("key2").get()); |
| 119 EXPECT_EQ(session3.get(), cache.Lookup("key3").get()); | 118 EXPECT_EQ(session3.get(), cache.Lookup("key3").get()); |
| 120 EXPECT_EQ(3u, cache.size()); | 119 EXPECT_EQ(3u, cache.size()); |
| 121 | 120 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 145 | 144 |
| 146 SSLClientSessionCache::Config config; | 145 SSLClientSessionCache::Config config; |
| 147 config.expiration_check_count = kExpirationCheckCount; | 146 config.expiration_check_count = kExpirationCheckCount; |
| 148 config.timeout = kTimeout; | 147 config.timeout = kTimeout; |
| 149 SSLClientSessionCache cache(config); | 148 SSLClientSessionCache cache(config); |
| 150 base::SimpleTestClock* clock = new base::SimpleTestClock; | 149 base::SimpleTestClock* clock = new base::SimpleTestClock; |
| 151 cache.SetClockForTesting(base::WrapUnique(clock)); | 150 cache.SetClockForTesting(base::WrapUnique(clock)); |
| 152 | 151 |
| 153 // Add |kNumEntries - 1| entries. | 152 // Add |kNumEntries - 1| entries. |
| 154 for (size_t i = 0; i < kNumEntries - 1; i++) { | 153 for (size_t i = 0; i < kNumEntries - 1; i++) { |
| 155 ScopedSSL_SESSION session(SSL_SESSION_new()); | 154 bssl::UniquePtr<SSL_SESSION> session(SSL_SESSION_new()); |
| 156 cache.Insert(base::SizeTToString(i), session.get()); | 155 cache.Insert(base::SizeTToString(i), session.get()); |
| 157 } | 156 } |
| 158 EXPECT_EQ(kNumEntries - 1, cache.size()); | 157 EXPECT_EQ(kNumEntries - 1, cache.size()); |
| 159 | 158 |
| 160 // Expire all the previous entries and insert one more entry. | 159 // Expire all the previous entries and insert one more entry. |
| 161 clock->Advance(kTimeout * 2); | 160 clock->Advance(kTimeout * 2); |
| 162 ScopedSSL_SESSION session(SSL_SESSION_new()); | 161 bssl::UniquePtr<SSL_SESSION> session(SSL_SESSION_new()); |
| 163 cache.Insert("key", session.get()); | 162 cache.Insert("key", session.get()); |
| 164 | 163 |
| 165 // All entries are still in the cache. | 164 // All entries are still in the cache. |
| 166 EXPECT_EQ(kNumEntries, cache.size()); | 165 EXPECT_EQ(kNumEntries, cache.size()); |
| 167 | 166 |
| 168 // Perform one fewer lookup than needed to trigger the expiration check. This | 167 // Perform one fewer lookup than needed to trigger the expiration check. This |
| 169 // shall not expire any session. | 168 // shall not expire any session. |
| 170 for (size_t i = 0; i < kExpirationCheckCount - 1; i++) | 169 for (size_t i = 0; i < kExpirationCheckCount - 1; i++) |
| 171 cache.Lookup("key"); | 170 cache.Lookup("key"); |
| 172 | 171 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 192 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); | 191 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); |
| 193 | 192 |
| 194 SSLClientSessionCache::Config config; | 193 SSLClientSessionCache::Config config; |
| 195 config.expiration_check_count = kExpirationCheckCount; | 194 config.expiration_check_count = kExpirationCheckCount; |
| 196 config.timeout = kTimeout; | 195 config.timeout = kTimeout; |
| 197 SSLClientSessionCache cache(config); | 196 SSLClientSessionCache cache(config); |
| 198 base::SimpleTestClock* clock = new base::SimpleTestClock; | 197 base::SimpleTestClock* clock = new base::SimpleTestClock; |
| 199 cache.SetClockForTesting(base::WrapUnique(clock)); | 198 cache.SetClockForTesting(base::WrapUnique(clock)); |
| 200 | 199 |
| 201 // Insert an entry into the session cache. | 200 // Insert an entry into the session cache. |
| 202 ScopedSSL_SESSION session(SSL_SESSION_new()); | 201 bssl::UniquePtr<SSL_SESSION> session(SSL_SESSION_new()); |
| 203 cache.Insert("key", session.get()); | 202 cache.Insert("key", session.get()); |
| 204 EXPECT_EQ(session.get(), cache.Lookup("key").get()); | 203 EXPECT_EQ(session.get(), cache.Lookup("key").get()); |
| 205 EXPECT_EQ(1u, cache.size()); | 204 EXPECT_EQ(1u, cache.size()); |
| 206 | 205 |
| 207 // Expire the session. | 206 // Expire the session. |
| 208 clock->Advance(kTimeout * 2); | 207 clock->Advance(kTimeout * 2); |
| 209 | 208 |
| 210 // The entry has not been removed yet. | 209 // The entry has not been removed yet. |
| 211 EXPECT_EQ(1u, cache.size()); | 210 EXPECT_EQ(1u, cache.size()); |
| 212 | 211 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 233 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); | 232 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); |
| 234 | 233 |
| 235 SSLClientSessionCache::Config config; | 234 SSLClientSessionCache::Config config; |
| 236 config.expiration_check_count = kExpirationCheckCount; | 235 config.expiration_check_count = kExpirationCheckCount; |
| 237 config.timeout = kTimeout; | 236 config.timeout = kTimeout; |
| 238 SSLClientSessionCache cache(config); | 237 SSLClientSessionCache cache(config); |
| 239 base::SimpleTestClock* clock = new base::SimpleTestClock; | 238 base::SimpleTestClock* clock = new base::SimpleTestClock; |
| 240 cache.SetClockForTesting(base::WrapUnique(clock)); | 239 cache.SetClockForTesting(base::WrapUnique(clock)); |
| 241 | 240 |
| 242 // Insert an entry into the session cache. | 241 // Insert an entry into the session cache. |
| 243 ScopedSSL_SESSION session1(SSL_SESSION_new()); | 242 bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_new()); |
| 244 cache.Insert("key1", session1.get()); | 243 cache.Insert("key1", session1.get()); |
| 245 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); | 244 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); |
| 246 EXPECT_EQ(1u, cache.size()); | 245 EXPECT_EQ(1u, cache.size()); |
| 247 | 246 |
| 248 // Expire the session. | 247 // Expire the session. |
| 249 clock->Advance(kTimeout * 2); | 248 clock->Advance(kTimeout * 2); |
| 250 // Add one more session. | 249 // Add one more session. |
| 251 ScopedSSL_SESSION session2(SSL_SESSION_new()); | 250 bssl::UniquePtr<SSL_SESSION> session2(SSL_SESSION_new()); |
| 252 cache.Insert("key2", session2.get()); | 251 cache.Insert("key2", session2.get()); |
| 253 EXPECT_EQ(2u, cache.size()); | 252 EXPECT_EQ(2u, cache.size()); |
| 254 | 253 |
| 255 // Fire a notification that will flush expired sessions. | 254 // Fire a notification that will flush expired sessions. |
| 256 base::MemoryPressureListener::NotifyMemoryPressure( | 255 base::MemoryPressureListener::NotifyMemoryPressure( |
| 257 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); | 256 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
| 258 base::RunLoop().RunUntilIdle(); | 257 base::RunLoop().RunUntilIdle(); |
| 259 | 258 |
| 260 // Expired session's cache should be flushed. | 259 // Expired session's cache should be flushed. |
| 261 // Lookup returns nullptr, when cache entry not found. | 260 // Lookup returns nullptr, when cache entry not found. |
| 262 EXPECT_FALSE(cache.Lookup("key1")); | 261 EXPECT_FALSE(cache.Lookup("key1")); |
| 263 EXPECT_TRUE(cache.Lookup("key2")); | 262 EXPECT_TRUE(cache.Lookup("key2")); |
| 264 EXPECT_EQ(1u, cache.size()); | 263 EXPECT_EQ(1u, cache.size()); |
| 265 | 264 |
| 266 // Fire notification that will flush everything. | 265 // Fire notification that will flush everything. |
| 267 base::MemoryPressureListener::NotifyMemoryPressure( | 266 base::MemoryPressureListener::NotifyMemoryPressure( |
| 268 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); | 267 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 269 base::RunLoop().RunUntilIdle(); | 268 base::RunLoop().RunUntilIdle(); |
| 270 EXPECT_EQ(0u, cache.size()); | 269 EXPECT_EQ(0u, cache.size()); |
| 271 } | 270 } |
| 272 | 271 |
| 273 } // namespace net | 272 } // namespace net |
| OLD | NEW |