ssl_session_cache_test.cc
Go to the documentation of this file.
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 
20 
21 #include <string>
22 #include <unordered_set>
23 
24 #include <gtest/gtest.h>
25 
26 #include <grpc/grpc.h>
27 #include <grpc/support/log.h>
28 
30 
31 namespace grpc_core {
32 
33 namespace {
34 
35 class SessionTracker;
36 
37 struct SessionExDataId {
38  SessionTracker* tracker;
39  long id;
40 };
41 
42 class SessionTracker {
43  public:
44  SessionTracker() { ssl_context_ = SSL_CTX_new(TLSv1_2_method()); }
45 
46  ~SessionTracker() { SSL_CTX_free(ssl_context_); }
47 
48  tsi::SslSessionPtr NewSession(long id) {
49  static int ex_data_id = SSL_SESSION_get_ex_new_index(
50  0, nullptr, nullptr, nullptr, DestroyExData);
51  GPR_ASSERT(ex_data_id != -1);
52  // OpenSSL and different version of BoringSSL don't agree on API
53  // so try both.
54  tsi::SslSessionPtr session = NewSessionInternal(SSL_SESSION_new);
55  SessionExDataId* data = new SessionExDataId{this, id};
56  int result = SSL_SESSION_set_ex_data(session.get(), ex_data_id, data);
57  EXPECT_EQ(result, 1);
58  alive_sessions_.insert(id);
59  return session;
60  }
61 
62  bool IsAlive(long id) const {
63  return alive_sessions_.find(id) != alive_sessions_.end();
64  }
65 
66  size_t AliveCount() const { return alive_sessions_.size(); }
67 
68  private:
69  tsi::SslSessionPtr NewSessionInternal(SSL_SESSION* (*cb)()) {
70  return tsi::SslSessionPtr(cb());
71  }
72 
73  tsi::SslSessionPtr NewSessionInternal(SSL_SESSION* (*cb)(const SSL_CTX*)) {
75  }
76 
77  static void DestroyExData(void* /*parent*/, void* ptr, CRYPTO_EX_DATA* /*ad*/,
78  int /*index*/, long /*argl*/, void* /*argp*/) {
79  SessionExDataId* data = static_cast<SessionExDataId*>(ptr);
80  data->tracker->alive_sessions_.erase(data->id);
81  delete data;
82  }
83 
85  std::unordered_set<long> alive_sessions_;
86 };
87 
88 TEST(SslSessionCacheTest, InitialState) {
89  SessionTracker tracker;
90  // Verify session initial state.
91  {
92  tsi::SslSessionPtr tmp_sess = tracker.NewSession(1);
93  EXPECT_TRUE(tracker.IsAlive(1));
94  EXPECT_EQ(tracker.AliveCount(), 1);
95  }
96  EXPECT_FALSE(tracker.IsAlive(1));
97  EXPECT_EQ(tracker.AliveCount(), 0);
98 }
99 
100 TEST(SslSessionCacheTest, LruCache) {
101  SessionTracker tracker;
102  {
103  RefCountedPtr<tsi::SslSessionLRUCache> cache =
105  tsi::SslSessionPtr sess2 = tracker.NewSession(2);
106  SSL_SESSION* sess2_ptr = sess2.get();
107  cache->Put("first.dropbox.com", std::move(sess2));
108  EXPECT_EQ(cache->Get("first.dropbox.com").get(), sess2_ptr);
109  EXPECT_TRUE(tracker.IsAlive(2));
110  EXPECT_EQ(tracker.AliveCount(), 1);
111  // Putting element with the same key destroys old session.
112  tsi::SslSessionPtr sess3 = tracker.NewSession(3);
113  SSL_SESSION* sess3_ptr = sess3.get();
114  cache->Put("first.dropbox.com", std::move(sess3));
115  EXPECT_FALSE(tracker.IsAlive(2));
116  EXPECT_EQ(cache->Get("first.dropbox.com").get(), sess3_ptr);
117  EXPECT_TRUE(tracker.IsAlive(3));
118  EXPECT_EQ(tracker.AliveCount(), 1);
119  // Putting three more elements discards current one.
120  for (long id = 4; id < 7; id++) {
121  EXPECT_TRUE(tracker.IsAlive(3));
122  std::string domain = std::to_string(id) + ".random.domain";
123  cache->Put(domain.c_str(), tracker.NewSession(id));
124  }
125  EXPECT_EQ(cache->Size(), 3);
126  EXPECT_FALSE(tracker.IsAlive(3));
127  EXPECT_EQ(tracker.AliveCount(), 3);
128  // Accessing element moves it into front of the queue.
129  EXPECT_TRUE(cache->Get("4.random.domain"));
130  EXPECT_TRUE(tracker.IsAlive(4));
131  EXPECT_TRUE(tracker.IsAlive(5));
132  EXPECT_TRUE(tracker.IsAlive(6));
133  // One element has to be evicted from cache->
134  cache->Put("7.random.domain", tracker.NewSession(7));
135  EXPECT_TRUE(tracker.IsAlive(4));
136  EXPECT_FALSE(tracker.IsAlive(5));
137  EXPECT_TRUE(tracker.IsAlive(6));
138  EXPECT_TRUE(tracker.IsAlive(7));
139  EXPECT_EQ(tracker.AliveCount(), 3);
140  }
141  // Cache destructor destroys all sessions.
142  EXPECT_EQ(tracker.AliveCount(), 0);
143 }
144 
145 } // namespace
146 } // namespace grpc_core
147 
148 int main(int argc, char** argv) {
149  ::testing::InitGoogleTest(&argc, argv);
150  grpc::testing::TestEnvironment env(&argc, argv);
151  grpc_init();
152  int ret = RUN_ALL_TESTS();
153  grpc_shutdown();
154  return ret;
155 }
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
log.h
generate.env
env
Definition: generate.py:37
grpc_core
Definition: call_metric_recorder.h:31
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::TEST
TEST(AvlTest, NoOp)
Definition: avl_test.cc:21
TLSv1_2_method
#define TLSv1_2_method
Definition: boringssl_prefix_symbols.h:545
SSL_SESSION_get_ex_new_index
#define SSL_SESSION_get_ex_new_index
Definition: boringssl_prefix_symbols.h:252
ssl_ctx_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3404
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
SSL_SESSION_new
SSL_SESSION * SSL_SESSION_new(const SSL_CTX *ctx)
Definition: ssl_session.cc:957
id
long id
Definition: ssl_session_cache_test.cc:39
SSL_CTX_free
#define SSL_CTX_free
Definition: boringssl_prefix_symbols.h:84
SSL_CTX_new
#define SSL_CTX_new
Definition: boringssl_prefix_symbols.h:115
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
tsi::SslSessionPtr
std::unique_ptr< SSL_SESSION, SslSessionDeleter > SslSessionPtr
Definition: ssl_session.h:46
ssl_context_
SSL_CTX * ssl_context_
Definition: ssl_session_cache_test.cc:84
grpc.h
ssl_session_cache.h
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
test_config.h
ssl_session_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3787
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
SSL_SESSION_set_ex_data
#define SSL_SESSION_set_ex_data
Definition: boringssl_prefix_symbols.h:266
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
alive_sessions_
std::unordered_set< long > alive_sessions_
Definition: ssl_session_cache_test.cc:85
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
tracker
SessionTracker * tracker
Definition: ssl_session_cache_test.cc:38
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
crypto_ex_data_st
Definition: ex_data.h:194
tsi::SslSessionLRUCache::Create
static grpc_core::RefCountedPtr< SslSessionLRUCache > Create(size_t capacity)
Create new LRU cache with the given capacity.
Definition: ssl_session_cache.h:50
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
main
int main(int argc, char **argv)
Definition: ssl_session_cache_test.cc:148


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:21