blob: 508e557159c9527bdfac3bfff031081fe212e779 [file] [log] [blame]
Harald Alvestrandd02541e2019-01-03 12:43:28 +01001/*
2 * Copyright 2018 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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/dtls_transport.h"
Harald Alvestrandd02541e2019-01-03 12:43:28 +010012
13#include <utility>
14#include <vector>
15
16#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "p2p/base/fake_dtls_transport.h"
Harald Alvestrandd02541e2019-01-03 12:43:28 +010018#include "rtc_base/gunit.h"
19#include "test/gmock.h"
20#include "test/gtest.h"
21
22constexpr int kDefaultTimeout = 1000; // milliseconds
23
24using cricket::FakeDtlsTransport;
25using ::testing::ElementsAre;
26
27namespace webrtc {
28
29class TestDtlsTransportObserver : public DtlsTransportObserverInterface {
30 public:
31 void OnStateChange(DtlsTransportInformation info) override {
32 state_change_called_ = true;
33 states_.push_back(info.state());
34 }
35
36 void OnError(RTCError error) override {}
37
Harald Alvestrandcdc30452019-01-08 18:08:04 +010038 DtlsTransportState state() {
39 if (states_.size() > 0) {
40 return states_[states_.size() - 1];
41 } else {
42 return DtlsTransportState::kNew;
43 }
44 }
45
Harald Alvestrandd02541e2019-01-03 12:43:28 +010046 bool state_change_called_ = false;
47 std::vector<DtlsTransportState> states_;
48};
49
50class DtlsTransportTest : public testing::Test {
51 public:
52 DtlsTransport* transport() { return transport_.get(); }
53 DtlsTransportObserverInterface* observer() { return &observer_; }
54
55 void CreateTransport() {
56 auto cricket_transport = absl::make_unique<FakeDtlsTransport>(
57 "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP);
58 transport_ =
59 new rtc::RefCountedObject<DtlsTransport>(std::move(cricket_transport));
60 }
61
62 void CompleteDtlsHandshake() {
63 auto fake_dtls1 = static_cast<FakeDtlsTransport*>(transport_->internal());
64 auto fake_dtls2 = absl::make_unique<FakeDtlsTransport>(
65 "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP);
66 auto cert1 = rtc::RTCCertificate::Create(absl::WrapUnique(
67 rtc::SSLIdentity::Generate("session1", rtc::KT_DEFAULT)));
68 fake_dtls1->SetLocalCertificate(cert1);
69 auto cert2 = rtc::RTCCertificate::Create(absl::WrapUnique(
70 rtc::SSLIdentity::Generate("session1", rtc::KT_DEFAULT)));
71 fake_dtls2->SetLocalCertificate(cert2);
72 fake_dtls1->SetDestination(fake_dtls2.get());
73 }
74
75 rtc::scoped_refptr<DtlsTransport> transport_;
76 TestDtlsTransportObserver observer_;
77};
78
79TEST_F(DtlsTransportTest, CreateClearDelete) {
80 auto cricket_transport = absl::make_unique<FakeDtlsTransport>(
81 "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP);
82 rtc::scoped_refptr<DtlsTransport> webrtc_transport =
83 new rtc::RefCountedObject<DtlsTransport>(std::move(cricket_transport));
84 ASSERT_TRUE(webrtc_transport->internal());
85 ASSERT_EQ(DtlsTransportState::kNew, webrtc_transport->Information().state());
86 webrtc_transport->Clear();
87 ASSERT_FALSE(webrtc_transport->internal());
88 ASSERT_EQ(DtlsTransportState::kClosed,
89 webrtc_transport->Information().state());
90}
91
92TEST_F(DtlsTransportTest, EventsObservedWhenConnecting) {
93 CreateTransport();
94 transport()->RegisterObserver(observer());
95 CompleteDtlsHandshake();
96 ASSERT_TRUE_WAIT(observer_.state_change_called_, kDefaultTimeout);
97 EXPECT_THAT(
98 observer_.states_,
99 ElementsAre( // FakeDtlsTransport doesn't signal the "connecting" state.
100 // TODO(hta): fix FakeDtlsTransport or file bug on it.
101 // DtlsTransportState::kConnecting,
102 DtlsTransportState::kConnected));
103}
104
Harald Alvestrandcdc30452019-01-08 18:08:04 +0100105TEST_F(DtlsTransportTest, CloseWhenClearing) {
106 CreateTransport();
107 transport()->RegisterObserver(observer());
108 CompleteDtlsHandshake();
109 ASSERT_TRUE_WAIT(observer_.state() == DtlsTransportState::kConnected,
110 kDefaultTimeout);
111 transport()->Clear();
112 ASSERT_TRUE_WAIT(observer_.state() == DtlsTransportState::kClosed,
113 kDefaultTimeout);
114}
115
Harald Alvestrandd02541e2019-01-03 12:43:28 +0100116} // namespace webrtc