proxygen
SSLSessionCacheDataTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <chrono>
17 #include <vector>
18 
19 #include <folly/DynamicConverter.h>
20 #include <gtest/gtest.h>
25 #include <wangle/ssl/SSLUtil.h>
26 
27 using namespace std::chrono;
28 using namespace testing;
29 using namespace wangle;
30 
32 using SSLCtxPtr = std::unique_ptr<SSL_CTX, SSLCtxDeleter>;
33 
34 class SSLSessionCacheDataTest : public Test {
35  public:
36  void SetUp() override {
37  sessions_ = getSessions();
38  }
39 
40  void TearDown() override {
41  for (const auto& it : sessions_) {
42  SSL_SESSION_free(it.first);
43  }
44  sessions_.clear();
45  }
46 
47  protected:
48  std::vector<std::pair<SSL_SESSION*, size_t>> sessions_;
49 };
50 
53  data.sessionData = folly::fbstring("some session data");
55  data.serviceIdentity = "some service";
56 
57  auto d = folly::toDynamic(data);
58  auto deserializedData = folly::convertTo<SSLSessionCacheData>(d);
59 
60  EXPECT_EQ(deserializedData.sessionData, data.sessionData);
61  EXPECT_EQ(deserializedData.addedTime, data.addedTime);
62  EXPECT_EQ(deserializedData.serviceIdentity, data.serviceIdentity);
63 }
64 
65 TEST_F(SSLSessionCacheDataTest, CloneSSLSession) {
66  for (auto& it : sessions_) {
67  auto sess = SSLSessionPtr(cloneSSLSession(it.first));
68  EXPECT_TRUE(sess);
69  }
70 }
71 
72 TEST_F(SSLSessionCacheDataTest, ServiceIdentity) {
73  auto sessionPtr = SSLSessionPtr(cloneSSLSession(sessions_[0].first));
74  auto session = sessionPtr.get();
75  auto ident = getSessionServiceIdentity(session);
76  EXPECT_FALSE(ident);
77 
78  std::string id("serviceId");
80  ident = getSessionServiceIdentity(session);
81  EXPECT_TRUE(ident);
82  EXPECT_EQ(ident.value(), id);
83 
84  auto cloned = SSLSessionPtr(cloneSSLSession(session));
85  EXPECT_TRUE(cloned);
86  ident = getSessionServiceIdentity(cloned.get());
87  EXPECT_TRUE(ident);
88  EXPECT_EQ(ident.value(), id);
89 
90  auto cacheDataOpt = getCacheDataForSession(session);
91  EXPECT_TRUE(cacheDataOpt);
92  auto& cacheData = cacheDataOpt.value();
93  EXPECT_EQ(id, cacheData.serviceIdentity);
94 
95  auto deserialized = SSLSessionPtr(getSessionFromCacheData(cacheData));
96  EXPECT_TRUE(deserialized);
97  ident = getSessionServiceIdentity(deserialized.get());
98  EXPECT_TRUE(ident);
99  EXPECT_EQ(ident.value(), id);
100 }
bool setSessionServiceIdentity(SSL_SESSION *session, const std::string &str)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
folly::Optional< std::string > getSessionServiceIdentity(SSL_SESSION *session)
std::chrono::steady_clock::time_point now()
folly::Optional< SSLSessionCacheData > getCacheDataForSession(SSL_SESSION *sess)
std::unique_ptr< SSL_SESSION, SessionDestructor > SSLSessionPtr
Definition: SSLSession.h:32
std::vector< std::pair< SSL_SESSION *, size_t > > getSessions()
Definition: TestUtil.cpp:320
SSL_SESSION * getSessionFromCacheData(const SSLSessionCacheData &data)
std::chrono::time_point< std::chrono::system_clock > addedTime
std::unique_ptr< SSL_CTX, SSLCtxDeleter > SSLCtxPtr
SSL_SESSION * cloneSSLSession(SSL_SESSION *toClone)
TEST_F(SSLSessionCacheDataTest, Basic)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const char * string
Definition: Conv.cpp:212
std::vector< std::pair< SSL_SESSION *, size_t > > sessions_
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
basic_fbstring< char > fbstring
Definition: FBString.h:2904
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
dynamic toDynamic(const T &)
constexpr detail::First first
Definition: Base-inl.h:2553