Benjamin Wright | 19aab2e | 2018-04-05 22:39:06 | [diff] [blame] | 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 <openssl/ssl.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | |
| 17 | #include "rtc_base/gunit.h" |
| 18 | #include "rtc_base/openssl.h" |
| 19 | #include "rtc_base/opensslsessioncache.h" |
| 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | TEST(OpenSSLSessionCache, DTLSModeSetCorrectly) { |
| 24 | SSL_CTX* ssl_ctx = SSL_CTX_new(DTLSv1_2_client_method()); |
| 25 | |
| 26 | OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx); |
| 27 | EXPECT_EQ(session_cache.GetSSLMode(), SSL_MODE_DTLS); |
| 28 | |
| 29 | SSL_CTX_free(ssl_ctx); |
| 30 | } |
| 31 | |
| 32 | TEST(OpenSSLSessionCache, TLSModeSetCorrectly) { |
| 33 | SSL_CTX* ssl_ctx = SSL_CTX_new(TLSv1_2_client_method()); |
| 34 | |
| 35 | OpenSSLSessionCache session_cache(SSL_MODE_TLS, ssl_ctx); |
| 36 | EXPECT_EQ(session_cache.GetSSLMode(), SSL_MODE_TLS); |
| 37 | |
| 38 | SSL_CTX_free(ssl_ctx); |
| 39 | } |
| 40 | |
| 41 | TEST(OpenSSLSessionCache, SSLContextSetCorrectly) { |
| 42 | SSL_CTX* ssl_ctx = SSL_CTX_new(DTLSv1_2_client_method()); |
| 43 | |
| 44 | OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx); |
| 45 | EXPECT_EQ(session_cache.GetSSLContext(), ssl_ctx); |
| 46 | |
| 47 | SSL_CTX_free(ssl_ctx); |
| 48 | } |
| 49 | |
| 50 | TEST(OpenSSLSessionCache, InvalidLookupReturnsNullptr) { |
| 51 | SSL_CTX* ssl_ctx = SSL_CTX_new(DTLSv1_2_client_method()); |
| 52 | |
| 53 | OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx); |
| 54 | EXPECT_EQ(session_cache.LookupSession("Invalid"), nullptr); |
| 55 | EXPECT_EQ(session_cache.LookupSession(""), nullptr); |
| 56 | EXPECT_EQ(session_cache.LookupSession("."), nullptr); |
| 57 | |
| 58 | SSL_CTX_free(ssl_ctx); |
| 59 | } |
| 60 | |
| 61 | TEST(OpenSSLSessionCache, SimpleValidSessionLookup) { |
| 62 | SSL_CTX* ssl_ctx = SSL_CTX_new(DTLSv1_2_client_method()); |
| 63 | SSL_SESSION* ssl_session = SSL_SESSION_new(ssl_ctx); |
| 64 | |
| 65 | OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx); |
| 66 | session_cache.AddSession("webrtc.org", ssl_session); |
| 67 | EXPECT_EQ(session_cache.LookupSession("webrtc.org"), ssl_session); |
| 68 | |
| 69 | SSL_CTX_free(ssl_ctx); |
| 70 | } |
| 71 | |
| 72 | TEST(OpenSSLSessionCache, AddToExistingReplacesPrevious) { |
| 73 | SSL_CTX* ssl_ctx = SSL_CTX_new(DTLSv1_2_client_method()); |
| 74 | SSL_SESSION* ssl_session_1 = SSL_SESSION_new(ssl_ctx); |
| 75 | SSL_SESSION* ssl_session_2 = SSL_SESSION_new(ssl_ctx); |
| 76 | |
| 77 | OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx); |
| 78 | session_cache.AddSession("webrtc.org", ssl_session_1); |
| 79 | session_cache.AddSession("webrtc.org", ssl_session_2); |
| 80 | EXPECT_EQ(session_cache.LookupSession("webrtc.org"), ssl_session_2); |
| 81 | |
| 82 | SSL_CTX_free(ssl_ctx); |
| 83 | } |
| 84 | |
| 85 | } // namespace rtc |