• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2018 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "rtc_base/openssl_session_cache.h"
12 
13 #include <openssl/ssl.h>
14 #include <stdlib.h>
15 
16 #include <map>
17 #include <memory>
18 
19 #include "rtc_base/gunit.h"
20 #include "rtc_base/openssl.h"
21 
22 namespace {
23 // Use methods that avoid X509 objects if possible.
NewDtlsContext()24 SSL_CTX* NewDtlsContext() {
25 #ifdef OPENSSL_IS_BORINGSSL
26   return SSL_CTX_new(DTLS_with_buffers_method());
27 #else
28   return SSL_CTX_new(DTLS_method());
29 #endif
30 }
NewTlsContext()31 SSL_CTX* NewTlsContext() {
32 #ifdef OPENSSL_IS_BORINGSSL
33   return SSL_CTX_new(TLS_with_buffers_method());
34 #else
35   return SSL_CTX_new(TLS_method());
36 #endif
37 }
38 }  // namespace
39 
40 namespace rtc {
41 
TEST(OpenSSLSessionCache,DTLSModeSetCorrectly)42 TEST(OpenSSLSessionCache, DTLSModeSetCorrectly) {
43   SSL_CTX* ssl_ctx = NewDtlsContext();
44 
45   OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx);
46   EXPECT_EQ(session_cache.GetSSLMode(), SSL_MODE_DTLS);
47 
48   SSL_CTX_free(ssl_ctx);
49 }
50 
TEST(OpenSSLSessionCache,TLSModeSetCorrectly)51 TEST(OpenSSLSessionCache, TLSModeSetCorrectly) {
52   SSL_CTX* ssl_ctx = NewTlsContext();
53 
54   OpenSSLSessionCache session_cache(SSL_MODE_TLS, ssl_ctx);
55   EXPECT_EQ(session_cache.GetSSLMode(), SSL_MODE_TLS);
56 
57   SSL_CTX_free(ssl_ctx);
58 }
59 
TEST(OpenSSLSessionCache,SSLContextSetCorrectly)60 TEST(OpenSSLSessionCache, SSLContextSetCorrectly) {
61   SSL_CTX* ssl_ctx = NewDtlsContext();
62 
63   OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx);
64   EXPECT_EQ(session_cache.GetSSLContext(), ssl_ctx);
65 
66   SSL_CTX_free(ssl_ctx);
67 }
68 
TEST(OpenSSLSessionCache,InvalidLookupReturnsNullptr)69 TEST(OpenSSLSessionCache, InvalidLookupReturnsNullptr) {
70   SSL_CTX* ssl_ctx = NewDtlsContext();
71 
72   OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx);
73   EXPECT_EQ(session_cache.LookupSession("Invalid"), nullptr);
74   EXPECT_EQ(session_cache.LookupSession(""), nullptr);
75   EXPECT_EQ(session_cache.LookupSession("."), nullptr);
76 
77   SSL_CTX_free(ssl_ctx);
78 }
79 
TEST(OpenSSLSessionCache,SimpleValidSessionLookup)80 TEST(OpenSSLSessionCache, SimpleValidSessionLookup) {
81   SSL_CTX* ssl_ctx = NewDtlsContext();
82   SSL_SESSION* ssl_session = SSL_SESSION_new(ssl_ctx);
83 
84   OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx);
85   session_cache.AddSession("webrtc.org", ssl_session);
86   EXPECT_EQ(session_cache.LookupSession("webrtc.org"), ssl_session);
87 
88   SSL_CTX_free(ssl_ctx);
89 }
90 
TEST(OpenSSLSessionCache,AddToExistingReplacesPrevious)91 TEST(OpenSSLSessionCache, AddToExistingReplacesPrevious) {
92   SSL_CTX* ssl_ctx = NewDtlsContext();
93   SSL_SESSION* ssl_session_1 = SSL_SESSION_new(ssl_ctx);
94   SSL_SESSION* ssl_session_2 = SSL_SESSION_new(ssl_ctx);
95 
96   OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx);
97   session_cache.AddSession("webrtc.org", ssl_session_1);
98   session_cache.AddSession("webrtc.org", ssl_session_2);
99   EXPECT_EQ(session_cache.LookupSession("webrtc.org"), ssl_session_2);
100 
101   SSL_CTX_free(ssl_ctx);
102 }
103 
104 }  // namespace rtc
105