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