ssl_session_cache_test.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "src/core/tsi/ssl/session_cache/ssl_session_cache.h"
  19. #include <string>
  20. #include <unordered_set>
  21. #include <gtest/gtest.h>
  22. #include <grpc/grpc.h>
  23. #include <grpc/support/log.h>
  24. #include "test/core/util/test_config.h"
  25. namespace grpc_core {
  26. namespace {
  27. class SessionTracker;
  28. struct SessionExDataId {
  29. SessionTracker* tracker;
  30. long id;
  31. };
  32. class SessionTracker {
  33. public:
  34. SessionTracker() { ssl_context_ = SSL_CTX_new(TLSv1_2_method()); }
  35. ~SessionTracker() { SSL_CTX_free(ssl_context_); }
  36. tsi::SslSessionPtr NewSession(long id) {
  37. static int ex_data_id = SSL_SESSION_get_ex_new_index(
  38. 0, nullptr, nullptr, nullptr, DestroyExData);
  39. GPR_ASSERT(ex_data_id != -1);
  40. // OpenSSL and different version of BoringSSL don't agree on API
  41. // so try both.
  42. tsi::SslSessionPtr session = NewSessionInternal(SSL_SESSION_new);
  43. SessionExDataId* data = new SessionExDataId{this, id};
  44. int result = SSL_SESSION_set_ex_data(session.get(), ex_data_id, data);
  45. EXPECT_EQ(result, 1);
  46. alive_sessions_.insert(id);
  47. return session;
  48. }
  49. bool IsAlive(long id) const {
  50. return alive_sessions_.find(id) != alive_sessions_.end();
  51. }
  52. size_t AliveCount() const { return alive_sessions_.size(); }
  53. private:
  54. tsi::SslSessionPtr NewSessionInternal(SSL_SESSION* (*cb)()) {
  55. return tsi::SslSessionPtr(cb());
  56. }
  57. tsi::SslSessionPtr NewSessionInternal(SSL_SESSION* (*cb)(const SSL_CTX*)) {
  58. return tsi::SslSessionPtr(cb(ssl_context_));
  59. }
  60. static void DestroyExData(void* /*parent*/, void* ptr, CRYPTO_EX_DATA* /*ad*/,
  61. int /*index*/, long /*argl*/, void* /*argp*/) {
  62. SessionExDataId* data = static_cast<SessionExDataId*>(ptr);
  63. data->tracker->alive_sessions_.erase(data->id);
  64. delete data;
  65. }
  66. SSL_CTX* ssl_context_;
  67. std::unordered_set<long> alive_sessions_;
  68. };
  69. TEST(SslSessionCacheTest, InitialState) {
  70. SessionTracker tracker;
  71. // Verify session initial state.
  72. {
  73. tsi::SslSessionPtr tmp_sess = tracker.NewSession(1);
  74. EXPECT_TRUE(tracker.IsAlive(1));
  75. EXPECT_EQ(tracker.AliveCount(), 1);
  76. }
  77. EXPECT_FALSE(tracker.IsAlive(1));
  78. EXPECT_EQ(tracker.AliveCount(), 0);
  79. }
  80. TEST(SslSessionCacheTest, LruCache) {
  81. SessionTracker tracker;
  82. {
  83. RefCountedPtr<tsi::SslSessionLRUCache> cache =
  84. tsi::SslSessionLRUCache::Create(3);
  85. tsi::SslSessionPtr sess2 = tracker.NewSession(2);
  86. SSL_SESSION* sess2_ptr = sess2.get();
  87. cache->Put("first.dropbox.com", std::move(sess2));
  88. EXPECT_EQ(cache->Get("first.dropbox.com").get(), sess2_ptr);
  89. EXPECT_TRUE(tracker.IsAlive(2));
  90. EXPECT_EQ(tracker.AliveCount(), 1);
  91. // Putting element with the same key destroys old session.
  92. tsi::SslSessionPtr sess3 = tracker.NewSession(3);
  93. SSL_SESSION* sess3_ptr = sess3.get();
  94. cache->Put("first.dropbox.com", std::move(sess3));
  95. EXPECT_FALSE(tracker.IsAlive(2));
  96. EXPECT_EQ(cache->Get("first.dropbox.com").get(), sess3_ptr);
  97. EXPECT_TRUE(tracker.IsAlive(3));
  98. EXPECT_EQ(tracker.AliveCount(), 1);
  99. // Putting three more elements discards current one.
  100. for (long id = 4; id < 7; id++) {
  101. EXPECT_TRUE(tracker.IsAlive(3));
  102. std::string domain = std::to_string(id) + ".random.domain";
  103. cache->Put(domain.c_str(), tracker.NewSession(id));
  104. }
  105. EXPECT_EQ(cache->Size(), 3);
  106. EXPECT_FALSE(tracker.IsAlive(3));
  107. EXPECT_EQ(tracker.AliveCount(), 3);
  108. // Accessing element moves it into front of the queue.
  109. EXPECT_TRUE(cache->Get("4.random.domain"));
  110. EXPECT_TRUE(tracker.IsAlive(4));
  111. EXPECT_TRUE(tracker.IsAlive(5));
  112. EXPECT_TRUE(tracker.IsAlive(6));
  113. // One element has to be evicted from cache->
  114. cache->Put("7.random.domain", tracker.NewSession(7));
  115. EXPECT_TRUE(tracker.IsAlive(4));
  116. EXPECT_FALSE(tracker.IsAlive(5));
  117. EXPECT_TRUE(tracker.IsAlive(6));
  118. EXPECT_TRUE(tracker.IsAlive(7));
  119. EXPECT_EQ(tracker.AliveCount(), 3);
  120. }
  121. // Cache destructor destroys all sessions.
  122. EXPECT_EQ(tracker.AliveCount(), 0);
  123. }
  124. } // namespace
  125. } // namespace grpc_core
  126. int main(int argc, char** argv) {
  127. ::testing::InitGoogleTest(&argc, argv);
  128. grpc::testing::TestEnvironment env(argc, argv);
  129. grpc_init();
  130. int ret = RUN_ALL_TESTS();
  131. grpc_shutdown();
  132. return ret;
  133. }