blob: 4db00e561bd84d34b156ccce1c58a5fbfd142330 [file] [log] [blame]
Diogo Real1dca9d52017-08-29 12:18:32 -07001/*
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 Wiberg918f50c2018-07-05 11:40:33 +020015#include "absl/memory/memory.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070016#include "rtc_base/asyncsocket.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/gunit.h"
18#include "rtc_base/openssladapter.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070019#include "test/gmock.h"
Diogo Real1dca9d52017-08-29 12:18:32 -070020
21namespace rtc {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070022namespace {
23
24class 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
45class MockCertVerifier : public SSLCertificateVerifier {
46 public:
47 virtual ~MockCertVerifier() = default;
48 MOCK_METHOD1(Verify, bool(const SSLCertificate&));
49};
50
51} // namespace
52
53using ::testing::_;
54using ::testing::Return;
Diogo Real1dca9d52017-08-29 12:18:32 -070055
56TEST(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 Wrightd6f86e82018-05-08 13:12:25 -070077// Verifies that SSLStart works when OpenSSLAdapter is started in standalone
78// mode.
79TEST(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.
86TEST(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.
96TEST(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 Real1dca9d52017-08-29 12:18:32 -0700109} // namespace rtc