You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.9 KiB

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