| 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/histogram_tester.h" |
| 10 #include "base/test/simple_test_clock.h" | 11 #include "base/test/simple_test_clock.h" |
| 11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 12 #include "base/trace_event/memory_allocator_dump.h" | 13 #include "base/trace_event/memory_allocator_dump.h" |
| 13 #include "base/trace_event/process_memory_dump.h" | 14 #include "base/trace_event/process_memory_dump.h" |
| 14 #include "base/trace_event/trace_event_argument.h" | 15 #include "base/trace_event/trace_event_argument.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/boringssl/src/include/openssl/ssl.h" | 17 #include "third_party/boringssl/src/include/openssl/ssl.h" |
| 17 | 18 |
| 18 namespace net { | 19 namespace net { |
| 19 | 20 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); | 81 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); |
| 81 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); | 82 EXPECT_EQ(nullptr, cache.Lookup("key2").get()); |
| 82 EXPECT_EQ(nullptr, cache.Lookup("key3").get()); | 83 EXPECT_EQ(nullptr, cache.Lookup("key3").get()); |
| 83 EXPECT_EQ(0u, cache.size()); | 84 EXPECT_EQ(0u, cache.size()); |
| 84 | 85 |
| 85 EXPECT_EQ(1u, session1->references); | 86 EXPECT_EQ(1u, session1->references); |
| 86 EXPECT_EQ(1u, session2->references); | 87 EXPECT_EQ(1u, session2->references); |
| 87 EXPECT_EQ(1u, session3->references); | 88 EXPECT_EQ(1u, session3->references); |
| 88 } | 89 } |
| 89 | 90 |
| 91 // Test that the Net.SSLSessionLookupCount histogram logs calls to Lookup. |
| 92 TEST(SSLClientSessionCacheTest, LookupCountUMA) { |
| 93 const char kLookupCountHistogram[] = "Net.SSLSessionLookupCount"; |
| 94 base::HistogramTester histograms; |
| 95 |
| 96 SSLClientSessionCache::Config config; |
| 97 SSLClientSessionCache cache(config); |
| 98 |
| 99 histograms.ExpectTotalCount(kLookupCountHistogram, 0); |
| 100 |
| 101 bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_new()); |
| 102 bssl::UniquePtr<SSL_SESSION> session2(SSL_SESSION_new()); |
| 103 cache.Insert("key1", session1.get()); |
| 104 cache.Insert("key2", session2.get()); |
| 105 histograms.ExpectBucketCount(kLookupCountHistogram, 0, 2); |
| 106 |
| 107 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); |
| 108 histograms.ExpectBucketCount(kLookupCountHistogram, 1, 1); |
| 109 |
| 110 EXPECT_EQ(session1.get(), cache.Lookup("key1").get()); |
| 111 histograms.ExpectBucketCount(kLookupCountHistogram, 2, 1); |
| 112 |
| 113 EXPECT_EQ(session2.get(), cache.Lookup("key2").get()); |
| 114 histograms.ExpectBucketCount(kLookupCountHistogram, 1, 2); |
| 115 // The total value is number of Inserts + number of Lookups, since "0 Lookups" |
| 116 // is recorded on Insert. |
| 117 histograms.ExpectTotalCount(kLookupCountHistogram, 5); |
| 118 |
| 119 EXPECT_EQ(2u, cache.size()); |
| 120 } |
| 121 |
| 90 // Test that a session may be inserted at two different keys. This should never | 122 // Test that a session may be inserted at two different keys. This should never |
| 91 // be necessary, but the API doesn't prohibit it. | 123 // be necessary, but the API doesn't prohibit it. |
| 92 TEST(SSLClientSessionCacheTest, DoubleInsert) { | 124 TEST(SSLClientSessionCacheTest, DoubleInsert) { |
| 93 SSLClientSessionCache::Config config; | 125 SSLClientSessionCache::Config config; |
| 94 SSLClientSessionCache cache(config); | 126 SSLClientSessionCache cache(config); |
| 95 | 127 |
| 96 bssl::UniquePtr<SSL_SESSION> session(SSL_SESSION_new()); | 128 bssl::UniquePtr<SSL_SESSION> session(SSL_SESSION_new()); |
| 97 EXPECT_EQ(1u, session->references); | 129 EXPECT_EQ(1u, session->references); |
| 98 | 130 |
| 99 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); | 131 EXPECT_EQ(nullptr, cache.Lookup("key1").get()); |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 std::unique_ptr<base::Value> raw_attrs = | 364 std::unique_ptr<base::Value> raw_attrs = |
| 333 dump->attributes_for_testing()->ToBaseValue(); | 365 dump->attributes_for_testing()->ToBaseValue(); |
| 334 base::DictionaryValue* attrs; | 366 base::DictionaryValue* attrs; |
| 335 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs)); | 367 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs)); |
| 336 ASSERT_TRUE(attrs->HasKey("cert_count")); | 368 ASSERT_TRUE(attrs->HasKey("cert_count")); |
| 337 ASSERT_TRUE(attrs->HasKey("serialized_cert_size")); | 369 ASSERT_TRUE(attrs->HasKey("serialized_cert_size")); |
| 338 ASSERT_TRUE(attrs->HasKey(base::trace_event::MemoryAllocatorDump::kNameSize)); | 370 ASSERT_TRUE(attrs->HasKey(base::trace_event::MemoryAllocatorDump::kNameSize)); |
| 339 } | 371 } |
| 340 | 372 |
| 341 } // namespace net | 373 } // namespace net |
| OLD | NEW |