This source file includes following definitions.
- EVP_PKEY_get_refcount
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "net/ssl/openssl_client_key_store.h"
#include "base/memory/ref_counted.h"
#include "net/base/test_data_directory.h"
#include "net/test/cert_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
typedef OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY;
int EVP_PKEY_get_refcount(EVP_PKEY* pkey) {
return pkey->references;
}
class OpenSSLClientKeyStoreTest : public ::testing::Test {
public:
OpenSSLClientKeyStoreTest()
: store_(OpenSSLClientKeyStore::GetInstance()) {
}
virtual ~OpenSSLClientKeyStoreTest() {
if (store_)
store_->Flush();
}
protected:
OpenSSLClientKeyStore* store_;
};
TEST_F(OpenSSLClientKeyStoreTest, GetInstance) {
ASSERT_TRUE(store_);
}
TEST_F(OpenSSLClientKeyStoreTest, Flush) {
ASSERT_TRUE(store_);
scoped_refptr<X509Certificate> cert_1(
ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
ASSERT_TRUE(cert_1.get());
ScopedEVP_PKEY priv_key(EVP_PKEY_new());
ASSERT_TRUE(priv_key.get());
ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(),
priv_key.get()));
store_->Flush();
ScopedEVP_PKEY pkey;
ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey));
ASSERT_FALSE(pkey.get());
}
TEST_F(OpenSSLClientKeyStoreTest, FetchEmptyPrivateKey) {
ASSERT_TRUE(store_);
scoped_refptr<X509Certificate> cert_1(
ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
ASSERT_TRUE(cert_1.get());
ScopedEVP_PKEY pkey;
ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey));
ASSERT_FALSE(pkey.get());
}
TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchPrivateKey) {
ASSERT_TRUE(store_);
scoped_refptr<X509Certificate> cert_1(
ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
ASSERT_TRUE(cert_1.get());
ScopedEVP_PKEY priv_key(EVP_PKEY_new());
ASSERT_TRUE(priv_key.get());
ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key.get()));
ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(),
priv_key.get()));
ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key.get()));
ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(),
priv_key.get()));
ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key.get()));
ScopedEVP_PKEY pkey2;
ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey2));
ASSERT_EQ(pkey2.get(), priv_key.get());
ASSERT_EQ(3, EVP_PKEY_get_refcount(priv_key.get()));
store_->Flush();
ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key.get()));
}
TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchTwoPrivateKeys) {
scoped_refptr<X509Certificate> cert_1(
ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
ASSERT_TRUE(cert_1.get());
scoped_refptr<X509Certificate> cert_2(
ImportCertFromFile(GetTestCertsDirectory(), "client_2.pem"));
ASSERT_TRUE(cert_2.get());
ScopedEVP_PKEY priv_key1(EVP_PKEY_new());
ASSERT_TRUE(priv_key1.get());
ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key1.get()));
ScopedEVP_PKEY priv_key2(EVP_PKEY_new());
ASSERT_TRUE(priv_key2.get());
ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key2.get()));
ASSERT_NE(priv_key1.get(), priv_key2.get());
EXPECT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(),
priv_key1.get()));
EXPECT_TRUE(store_->RecordClientCertPrivateKey(cert_2.get(),
priv_key2.get()));
EXPECT_EQ(2, EVP_PKEY_get_refcount(priv_key1.get()));
EXPECT_EQ(2, EVP_PKEY_get_refcount(priv_key2.get()));
ScopedEVP_PKEY fetch_key1;
ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_1.get(),
&fetch_key1));
ScopedEVP_PKEY fetch_key2;
ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_2.get(),
&fetch_key2));
EXPECT_TRUE(fetch_key1.get());
EXPECT_TRUE(fetch_key2.get());
EXPECT_EQ(fetch_key1.get(), priv_key1.get());
EXPECT_EQ(fetch_key2.get(), priv_key2.get());
EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key1.get()));
EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key2.get()));
}
}
}