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