This source file includes following definitions.
- Get
- Set
- GetInstance
- GetValue
- SetValue
- KeyDup
- KeyFree
- ResetConfig
- NewSSL
- ResetSessionID
- AddToCache
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "net/socket/ssl_session_cache_openssl.h"
#include <openssl/ssl.h>
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "crypto/openssl_util.h"
#include "testing/gtest/include/gtest/gtest.h"
extern "C" int ssl_get_new_session(SSL* s, int session);
extern "C" void ssl_update_cache(SSL* s, int mode);
namespace net {
namespace {
typedef crypto::ScopedOpenSSL<SSL, SSL_free> ScopedSSL;
class SSLKeyHelper {
public:
static std::string Get(const SSL* ssl) {
return GetInstance()->GetValue(ssl);
}
static void Set(SSL* ssl, const std::string& value) {
GetInstance()->SetValue(ssl, value);
}
static SSLKeyHelper* GetInstance() {
static base::LazyInstance<SSLKeyHelper>::Leaky s_instance =
LAZY_INSTANCE_INITIALIZER;
return s_instance.Pointer();
}
SSLKeyHelper() {
ex_index_ = SSL_get_ex_new_index(0, NULL, NULL, KeyDup, KeyFree);
CHECK_NE(-1, ex_index_);
}
std::string GetValue(const SSL* ssl) {
std::string* value =
reinterpret_cast<std::string*>(SSL_get_ex_data(ssl, ex_index_));
if (!value)
return std::string();
return *value;
}
void SetValue(SSL* ssl, const std::string& value) {
int ret = SSL_set_ex_data(ssl, ex_index_, new std::string(value));
CHECK_EQ(1, ret);
}
static int KeyDup(CRYPTO_EX_DATA* to,
CRYPTO_EX_DATA* from,
void* from_fd,
int idx,
long argl,
void* argp) {
std::string** ptr = reinterpret_cast<std::string**>(from_fd);
std::string* old_string = *ptr;
std::string* new_string = new std::string(*old_string);
*ptr = new_string;
return 0;
}
static void KeyFree(void* parent,
void* ptr,
CRYPTO_EX_DATA* ad,
int index,
long argl,
void* argp) {
std::string* value = reinterpret_cast<std::string*>(ptr);
delete value;
}
int ex_index_;
};
}
class SSLSessionCacheOpenSSLTest : public testing::Test {
public:
SSLSessionCacheOpenSSLTest() {
crypto::EnsureOpenSSLInit();
ctx_.reset(SSL_CTX_new(SSLv23_client_method()));
cache_.Reset(ctx_.get(), kDefaultConfig);
}
void ResetConfig(const SSLSessionCacheOpenSSL::Config& config) {
cache_.Reset(ctx_.get(), config);
}
SSL* NewSSL(const std::string& cache_key) {
SSL* ssl = SSL_new(ctx_.get());
if (!ssl)
return NULL;
SSLKeyHelper::Set(ssl, cache_key);
ResetSessionID(ssl);
return ssl;
}
static void ResetSessionID(SSL* ssl) { ssl_get_new_session(ssl, 1); }
void AddToCache(SSL* ssl) {
ssl_update_cache(ssl, ctx_.get()->session_cache_mode);
}
static const SSLSessionCacheOpenSSL::Config kDefaultConfig;
protected:
crypto::ScopedOpenSSL<SSL_CTX, SSL_CTX_free> ctx_;
SSLSessionCacheOpenSSL cache_;
};
const SSLSessionCacheOpenSSL::Config
SSLSessionCacheOpenSSLTest::kDefaultConfig = {
&SSLKeyHelper::Get,
1024,
256,
60 * 60,
};
TEST_F(SSLSessionCacheOpenSSLTest, EmptyCacheCreation) {
EXPECT_EQ(0U, cache_.size());
}
TEST_F(SSLSessionCacheOpenSSLTest, CacheOneSession) {
ScopedSSL ssl(NewSSL("hello"));
EXPECT_EQ(0U, cache_.size());
AddToCache(ssl.get());
EXPECT_EQ(1U, cache_.size());
ssl.reset(NULL);
EXPECT_EQ(1U, cache_.size());
}
TEST_F(SSLSessionCacheOpenSSLTest, CacheMultipleSessions) {
const size_t kNumItems = 100;
int local_id = 1;
for (size_t n = 0; n < kNumItems; ++n) {
std::string local_id_string = base::StringPrintf("%d", local_id++);
ScopedSSL ssl(NewSSL(local_id_string));
AddToCache(ssl.get());
EXPECT_EQ(n + 1, cache_.size());
}
}
TEST_F(SSLSessionCacheOpenSSLTest, Flush) {
const size_t kNumItems = 100;
int local_id = 1;
for (size_t n = 0; n < kNumItems; ++n) {
std::string local_id_string = base::StringPrintf("%d", local_id++);
ScopedSSL ssl(NewSSL(local_id_string));
AddToCache(ssl.get());
}
EXPECT_EQ(kNumItems, cache_.size());
cache_.Flush();
EXPECT_EQ(0U, cache_.size());
}
TEST_F(SSLSessionCacheOpenSSLTest, SetSSLSession) {
const std::string key("hello");
ScopedSSL ssl(NewSSL(key));
EXPECT_FALSE(cache_.SetSSLSession(ssl.get()));
SSL_SESSION* session = ssl.get()->session;
EXPECT_TRUE(session);
EXPECT_EQ(1, session->references);
AddToCache(ssl.get());
EXPECT_EQ(2, session->references);
cache_.MarkSSLSessionAsGood(ssl.get());
ssl.reset(NULL);
EXPECT_EQ(1, session->references);
ScopedSSL ssl2(NewSSL(key));
EXPECT_TRUE(cache_.SetSSLSession(ssl2.get()));
EXPECT_EQ(session, ssl2.get()->session);
EXPECT_EQ(2, session->references);
}
TEST_F(SSLSessionCacheOpenSSLTest, SetSSLSessionWithKey) {
const std::string key("hello");
ScopedSSL ssl(NewSSL(key));
AddToCache(ssl.get());
cache_.MarkSSLSessionAsGood(ssl.get());
ssl.reset(NULL);
ScopedSSL ssl2(NewSSL(key));
EXPECT_TRUE(cache_.SetSSLSessionWithKey(ssl2.get(), key));
}
TEST_F(SSLSessionCacheOpenSSLTest, CheckSessionReplacement) {
const std::string common_key("common-key");
ScopedSSL ssl1(NewSSL(common_key));
ScopedSSL ssl2(NewSSL(common_key));
AddToCache(ssl1.get());
EXPECT_EQ(1U, cache_.size());
EXPECT_EQ(2, ssl1.get()->session->references);
AddToCache(ssl2.get());
EXPECT_EQ(1U, cache_.size());
EXPECT_EQ(1, ssl1.get()->session->references);
EXPECT_EQ(2, ssl2.get()->session->references);
}
TEST_F(SSLSessionCacheOpenSSLTest, CheckSessionReplacementWhenNotGood) {
const std::string key("hello");
ScopedSSL ssl(NewSSL(key));
EXPECT_FALSE(cache_.SetSSLSession(ssl.get()));
SSL_SESSION* session = ssl.get()->session;
ASSERT_TRUE(session);
EXPECT_EQ(1, session->references);
AddToCache(ssl.get());
EXPECT_EQ(2, session->references);
ScopedSSL ssl2(NewSSL(key));
EXPECT_FALSE(cache_.SetSSLSession(ssl2.get()));
SSL_SESSION* session2 = ssl2.get()->session;
ASSERT_TRUE(session2);
EXPECT_EQ(1, session2->references);
EXPECT_NE(session, session2);
AddToCache(ssl2.get());
EXPECT_EQ(1, session->references);
EXPECT_EQ(2, session2->references);
cache_.MarkSSLSessionAsGood(ssl.get());
ScopedSSL ssl3(NewSSL(key));
EXPECT_FALSE(cache_.SetSSLSession(ssl3.get()));
EXPECT_NE(session, ssl3.get()->session);
EXPECT_NE(session2, ssl3.get()->session);
EXPECT_EQ(1, ssl3.get()->session->references);
}
TEST_F(SSLSessionCacheOpenSSLTest, CheckEviction) {
const size_t kMaxItems = 20;
int local_id = 1;
SSLSessionCacheOpenSSL::Config config = kDefaultConfig;
config.max_entries = kMaxItems;
ResetConfig(config);
for (size_t n = 0; n < kMaxItems; ++n) {
std::string local_id_string = base::StringPrintf("%d", local_id++);
ScopedSSL ssl(NewSSL(local_id_string));
AddToCache(ssl.get());
EXPECT_EQ(n + 1, cache_.size());
}
for (size_t n = 0; n < kMaxItems; ++n) {
std::string local_id_string = base::StringPrintf("%d", local_id++);
ScopedSSL ssl(NewSSL(local_id_string));
AddToCache(ssl.get());
EXPECT_EQ(kMaxItems, cache_.size());
}
}
TEST_F(SSLSessionCacheOpenSSLTest, CheckExpiration) {
const size_t kMaxCheckCount = 10;
const size_t kNumEntries = 20;
SSLSessionCacheOpenSSL::Config config = kDefaultConfig;
config.expiration_check_count = kMaxCheckCount;
config.timeout_seconds = 1000;
ResetConfig(config);
for (size_t n = 0; n < kNumEntries - 1U; ++n) {
std::string key = base::StringPrintf("%d", static_cast<int>(n));
ScopedSSL ssl(NewSSL(key));
ssl.get()->session->time = static_cast<long>(n);
AddToCache(ssl.get());
}
EXPECT_EQ(kNumEntries - 1U, cache_.size());
ScopedSSL good_ssl(NewSSL("good-key"));
AddToCache(good_ssl.get());
good_ssl.reset(NULL);
EXPECT_EQ(kNumEntries, cache_.size());
for (size_t n = 0; n < kMaxCheckCount - 1U; ++n) {
ScopedSSL ssl(NewSSL("unknown-key"));
cache_.SetSSLSession(ssl.get());
EXPECT_EQ(kNumEntries, cache_.size());
}
ScopedSSL bad_ssl(NewSSL("unknown-key"));
cache_.SetSSLSession(bad_ssl.get());
bad_ssl.reset(NULL);
EXPECT_EQ(1U, cache_.size());
}
}