Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 <sstream> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 15 | #include "absl/memory/memory.h" |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 16 | #include "rtc_base/asyncsocket.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/gunit.h" |
| 18 | #include "rtc_base/openssladapter.h" |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 19 | #include "test/gmock.h" |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 20 | |
| 21 | namespace rtc { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | class MockAsyncSocket : public AsyncSocket { |
| 25 | public: |
| 26 | virtual ~MockAsyncSocket() = default; |
| 27 | MOCK_METHOD1(Accept, AsyncSocket*(SocketAddress*)); |
| 28 | MOCK_CONST_METHOD0(GetLocalAddress, SocketAddress()); |
| 29 | MOCK_CONST_METHOD0(GetRemoteAddress, SocketAddress()); |
| 30 | MOCK_METHOD1(Bind, int(const SocketAddress&)); |
| 31 | MOCK_METHOD1(Connect, int(const SocketAddress&)); |
| 32 | MOCK_METHOD2(Send, int(const void*, size_t)); |
| 33 | MOCK_METHOD3(SendTo, int(const void*, size_t, const SocketAddress&)); |
| 34 | MOCK_METHOD3(Recv, int(void*, size_t, int64_t*)); |
| 35 | MOCK_METHOD4(RecvFrom, int(void*, size_t, SocketAddress*, int64_t*)); |
| 36 | MOCK_METHOD1(Listen, int(int)); |
| 37 | MOCK_METHOD0(Close, int()); |
| 38 | MOCK_CONST_METHOD0(GetError, int()); |
| 39 | MOCK_METHOD1(SetError, void(int)); |
| 40 | MOCK_CONST_METHOD0(GetState, ConnState()); |
| 41 | MOCK_METHOD2(GetOption, int(Option, int*)); |
| 42 | MOCK_METHOD2(SetOption, int(Option, int)); |
| 43 | }; |
| 44 | |
| 45 | class MockCertVerifier : public SSLCertificateVerifier { |
| 46 | public: |
| 47 | virtual ~MockCertVerifier() = default; |
| 48 | MOCK_METHOD1(Verify, bool(const SSLCertificate&)); |
| 49 | }; |
| 50 | |
| 51 | } // namespace |
| 52 | |
| 53 | using ::testing::_; |
| 54 | using ::testing::Return; |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 55 | |
| 56 | TEST(OpenSSLAdapterTest, TestTransformAlpnProtocols) { |
| 57 | EXPECT_EQ("", TransformAlpnProtocols(std::vector<std::string>())); |
| 58 | |
| 59 | // Protocols larger than 255 characters (whose size can't be fit in a byte), |
| 60 | // can't be converted, and an empty string will be returned. |
| 61 | std::string large_protocol(256, 'a'); |
| 62 | EXPECT_EQ("", |
| 63 | TransformAlpnProtocols(std::vector<std::string>{large_protocol})); |
| 64 | |
| 65 | // One protocol test. |
| 66 | std::vector<std::string> alpn_protos{"h2"}; |
| 67 | std::stringstream expected_response; |
| 68 | expected_response << static_cast<char>(2) << "h2"; |
| 69 | EXPECT_EQ(expected_response.str(), TransformAlpnProtocols(alpn_protos)); |
| 70 | |
| 71 | // Standard protocols test (h2,http/1.1). |
| 72 | alpn_protos.push_back("http/1.1"); |
| 73 | expected_response << static_cast<char>(8) << "http/1.1"; |
| 74 | EXPECT_EQ(expected_response.str(), TransformAlpnProtocols(alpn_protos)); |
| 75 | } |
| 76 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 77 | // Verifies that SSLStart works when OpenSSLAdapter is started in standalone |
| 78 | // mode. |
| 79 | TEST(OpenSSLAdapterTest, TestBeginSSLBeforeConnection) { |
| 80 | AsyncSocket* async_socket = new MockAsyncSocket(); |
| 81 | OpenSSLAdapter adapter(async_socket); |
| 82 | EXPECT_EQ(adapter.StartSSL("webrtc.org", false), 0); |
| 83 | } |
| 84 | |
| 85 | // Verifies that the adapter factory can create new adapters. |
| 86 | TEST(OpenSSLAdapterFactoryTest, CreateSingleOpenSSLAdapter) { |
| 87 | OpenSSLAdapterFactory adapter_factory; |
| 88 | AsyncSocket* async_socket = new MockAsyncSocket(); |
| 89 | auto simple_adapter = std::unique_ptr<OpenSSLAdapter>( |
| 90 | adapter_factory.CreateAdapter(async_socket)); |
| 91 | EXPECT_NE(simple_adapter, nullptr); |
| 92 | } |
| 93 | |
| 94 | // Verifies that setting a custom verifier still allows for adapters to be |
| 95 | // created. |
| 96 | TEST(OpenSSLAdapterFactoryTest, CreateWorksWithCustomVerifier) { |
| 97 | MockCertVerifier* mock_verifier = new MockCertVerifier(); |
| 98 | EXPECT_CALL(*mock_verifier, Verify(_)).WillRepeatedly(Return(true)); |
| 99 | auto cert_verifier = std::unique_ptr<SSLCertificateVerifier>(mock_verifier); |
| 100 | |
| 101 | OpenSSLAdapterFactory adapter_factory; |
| 102 | adapter_factory.SetCertVerifier(cert_verifier.get()); |
| 103 | AsyncSocket* async_socket = new MockAsyncSocket(); |
| 104 | auto simple_adapter = std::unique_ptr<OpenSSLAdapter>( |
| 105 | adapter_factory.CreateAdapter(async_socket)); |
| 106 | EXPECT_NE(simple_adapter, nullptr); |
| 107 | } |
| 108 | |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 109 | } // namespace rtc |