| 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 |
|
| 19 |
#include "src/core/tsi/ssl/session_cache/ssl_session_cache.h" |
| 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 |
|
| 29 |
#include "test/core/util/test_config.h" |
| 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*)) { |
| 74 |
return tsi::SslSessionPtr(cb(ssl_context_)); |
| 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 |
|
| 84 |
SSL_CTX* ssl_context_; |
| 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 = |
| 104 |
tsi::SslSessionLRUCache::Create(3); |
| 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 |
} |