| 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_openssl.h" | 5 #include "net/ssl/ssl_client_session_cache_openssl.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/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/test/simple_test_clock.h" | 11 #include "base/test/simple_test_clock.h" |
| 11 #include "net/ssl/scoped_openssl_types.h" | 12 #include "net/ssl/scoped_openssl_types.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace net { | 15 namespace net { |
| 15 | 16 |
| 16 // Test basic insertion and lookup operations. | 17 // Test basic insertion and lookup operations. |
| 17 TEST(SSLClientSessionCacheOpenSSLTest, Basic) { | 18 TEST(SSLClientSessionCacheOpenSSLTest, Basic) { |
| 18 SSLClientSessionCacheOpenSSL::Config config; | 19 SSLClientSessionCacheOpenSSL::Config config; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 TEST(SSLClientSessionCacheOpenSSLTest, Expiration) { | 140 TEST(SSLClientSessionCacheOpenSSLTest, Expiration) { |
| 140 const size_t kNumEntries = 20; | 141 const size_t kNumEntries = 20; |
| 141 const size_t kExpirationCheckCount = 10; | 142 const size_t kExpirationCheckCount = 10; |
| 142 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); | 143 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); |
| 143 | 144 |
| 144 SSLClientSessionCacheOpenSSL::Config config; | 145 SSLClientSessionCacheOpenSSL::Config config; |
| 145 config.expiration_check_count = kExpirationCheckCount; | 146 config.expiration_check_count = kExpirationCheckCount; |
| 146 config.timeout = kTimeout; | 147 config.timeout = kTimeout; |
| 147 SSLClientSessionCacheOpenSSL cache(config); | 148 SSLClientSessionCacheOpenSSL cache(config); |
| 148 base::SimpleTestClock* clock = new base::SimpleTestClock; | 149 base::SimpleTestClock* clock = new base::SimpleTestClock; |
| 149 cache.SetClockForTesting(make_scoped_ptr(clock)); | 150 cache.SetClockForTesting(base::WrapUnique(clock)); |
| 150 | 151 |
| 151 // Add |kNumEntries - 1| entries. | 152 // Add |kNumEntries - 1| entries. |
| 152 for (size_t i = 0; i < kNumEntries - 1; i++) { | 153 for (size_t i = 0; i < kNumEntries - 1; i++) { |
| 153 ScopedSSL_SESSION session(SSL_SESSION_new()); | 154 ScopedSSL_SESSION session(SSL_SESSION_new()); |
| 154 cache.Insert(base::SizeTToString(i), session.get()); | 155 cache.Insert(base::SizeTToString(i), session.get()); |
| 155 } | 156 } |
| 156 EXPECT_EQ(kNumEntries - 1, cache.size()); | 157 EXPECT_EQ(kNumEntries - 1, cache.size()); |
| 157 | 158 |
| 158 // Expire all the previous entries and insert one more entry. | 159 // Expire all the previous entries and insert one more entry. |
| 159 clock->Advance(kTimeout * 2); | 160 clock->Advance(kTimeout * 2); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 187 // kExpirationCheckCount is set to a suitably large number so the automated | 188 // kExpirationCheckCount is set to a suitably large number so the automated |
| 188 // pruning never triggers. | 189 // pruning never triggers. |
| 189 const size_t kExpirationCheckCount = 1000; | 190 const size_t kExpirationCheckCount = 1000; |
| 190 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); | 191 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(1000); |
| 191 | 192 |
| 192 SSLClientSessionCacheOpenSSL::Config config; | 193 SSLClientSessionCacheOpenSSL::Config config; |
| 193 config.expiration_check_count = kExpirationCheckCount; | 194 config.expiration_check_count = kExpirationCheckCount; |
| 194 config.timeout = kTimeout; | 195 config.timeout = kTimeout; |
| 195 SSLClientSessionCacheOpenSSL cache(config); | 196 SSLClientSessionCacheOpenSSL cache(config); |
| 196 base::SimpleTestClock* clock = new base::SimpleTestClock; | 197 base::SimpleTestClock* clock = new base::SimpleTestClock; |
| 197 cache.SetClockForTesting(make_scoped_ptr(clock)); | 198 cache.SetClockForTesting(base::WrapUnique(clock)); |
| 198 | 199 |
| 199 // Insert an entry into the session cache. | 200 // Insert an entry into the session cache. |
| 200 ScopedSSL_SESSION session(SSL_SESSION_new()); | 201 ScopedSSL_SESSION session(SSL_SESSION_new()); |
| 201 cache.Insert("key", session.get()); | 202 cache.Insert("key", session.get()); |
| 202 EXPECT_EQ(session.get(), cache.Lookup("key").get()); | 203 EXPECT_EQ(session.get(), cache.Lookup("key").get()); |
| 203 EXPECT_EQ(1u, cache.size()); | 204 EXPECT_EQ(1u, cache.size()); |
| 204 | 205 |
| 205 // Expire the session. | 206 // Expire the session. |
| 206 clock->Advance(kTimeout * 2); | 207 clock->Advance(kTimeout * 2); |
| 207 | 208 |
| 208 // The entry has not been removed yet. | 209 // The entry has not been removed yet. |
| 209 EXPECT_EQ(1u, cache.size()); | 210 EXPECT_EQ(1u, cache.size()); |
| 210 | 211 |
| 211 // But it will not be returned on lookup and gets pruned at that point. | 212 // But it will not be returned on lookup and gets pruned at that point. |
| 212 EXPECT_EQ(nullptr, cache.Lookup("key").get()); | 213 EXPECT_EQ(nullptr, cache.Lookup("key").get()); |
| 213 EXPECT_EQ(0u, cache.size()); | 214 EXPECT_EQ(0u, cache.size()); |
| 214 | 215 |
| 215 // Sessions also are treated as expired if the clock rewinds. | 216 // Sessions also are treated as expired if the clock rewinds. |
| 216 cache.Insert("key", session.get()); | 217 cache.Insert("key", session.get()); |
| 217 EXPECT_EQ(session.get(), cache.Lookup("key").get()); | 218 EXPECT_EQ(session.get(), cache.Lookup("key").get()); |
| 218 EXPECT_EQ(1u, cache.size()); | 219 EXPECT_EQ(1u, cache.size()); |
| 219 | 220 |
| 220 clock->Advance(-kTimeout * 2); | 221 clock->Advance(-kTimeout * 2); |
| 221 | 222 |
| 222 EXPECT_EQ(nullptr, cache.Lookup("key").get()); | 223 EXPECT_EQ(nullptr, cache.Lookup("key").get()); |
| 223 EXPECT_EQ(0u, cache.size()); | 224 EXPECT_EQ(0u, cache.size()); |
| 224 } | 225 } |
| 225 | 226 |
| 226 } // namespace net | 227 } // namespace net |
| OLD | NEW |