blob: ea9b28a0c5967247cd9b6b61b828434106cd99d4 [file] [log] [blame]
zhihuang01c181d2017-03-03 22:39:061/*
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 <memory>
12
zhihuang01c181d2017-03-03 22:39:0613#include "webrtc/media/base/fakemediaengine.h"
14#include "webrtc/ortc/ortcfactory.h"
15#include "webrtc/ortc/testrtpparameters.h"
16#include "webrtc/p2p/base/fakepackettransport.h"
Edward Lemur76de83e2017-07-06 17:44:3417#include "webrtc/rtc_base/gunit.h"
zhihuang01c181d2017-03-03 22:39:0618
19namespace webrtc {
20
21static const char kTestSha1KeyParams1[] =
22 "inline:WVNfX19zZW1jdGwgKCkgewkyMjA7fQp9CnVubGVz";
23static const char kTestSha1KeyParams2[] =
24 "inline:PS1uQCVeeCFCanVmcjkpPywjNWhcYD0mXXtxaVBR";
25static const char kTestGcmKeyParams3[] =
26 "inline:e166KFlKzJsGW0d5apX+rrI05vxbrvMJEzFI14aTDCa63IRTlLK4iH66uOI=";
27
28static const cricket::CryptoParams kTestSha1CryptoParams1(
29 1,
30 "AES_CM_128_HMAC_SHA1_80",
31 kTestSha1KeyParams1,
32 "");
33static const cricket::CryptoParams kTestSha1CryptoParams2(
34 1,
35 "AES_CM_128_HMAC_SHA1_80",
36 kTestSha1KeyParams2,
37 "");
38static const cricket::CryptoParams kTestGcmCryptoParams3(1,
39 "AEAD_AES_256_GCM",
40 kTestGcmKeyParams3,
41 "");
42
43// This test uses fake packet transports and a fake media engine, in order to
44// test the SrtpTransport at only an API level. Any end-to-end test should go in
45// ortcfactory_integrationtest.cc instead.
46class SrtpTransportTest : public testing::Test {
47 public:
48 SrtpTransportTest() {
49 fake_media_engine_ = new cricket::FakeMediaEngine();
50 // Note: This doesn't need to use fake network classes, since it uses
51 // FakePacketTransports.
52 auto result = OrtcFactory::Create(
53 nullptr, nullptr, nullptr, nullptr, nullptr,
54 std::unique_ptr<cricket::MediaEngineInterface>(fake_media_engine_));
55 ortc_factory_ = result.MoveValue();
56 rtp_transport_controller_ =
57 ortc_factory_->CreateRtpTransportController().MoveValue();
58
59 fake_packet_transport_.reset(new rtc::FakePacketTransport("fake"));
60 auto srtp_transport_result = ortc_factory_->CreateSrtpTransport(
sprang6ab33cf2017-08-09 13:42:3261 rtp_transport_parameters_, fake_packet_transport_.get(), nullptr,
zhihuang01c181d2017-03-03 22:39:0662 rtp_transport_controller_.get());
63 srtp_transport_ = srtp_transport_result.MoveValue();
64 }
65
66 protected:
67 // Owned by |ortc_factory_|.
68 cricket::FakeMediaEngine* fake_media_engine_;
69 std::unique_ptr<OrtcFactoryInterface> ortc_factory_;
70 std::unique_ptr<RtpTransportControllerInterface> rtp_transport_controller_;
71 std::unique_ptr<SrtpTransportInterface> srtp_transport_;
sprang6ab33cf2017-08-09 13:42:3272 RtpTransportParameters rtp_transport_parameters_;
zhihuang01c181d2017-03-03 22:39:0673 std::unique_ptr<rtc::FakePacketTransport> fake_packet_transport_;
74};
75
76// Tests that setting the SRTP send/receive key succeeds.
77TEST_F(SrtpTransportTest, SetSrtpSendAndReceiveKey) {
78 EXPECT_TRUE(srtp_transport_->SetSrtpSendKey(kTestSha1CryptoParams1).ok());
79 EXPECT_TRUE(srtp_transport_->SetSrtpReceiveKey(kTestSha1CryptoParams2).ok());
80 auto sender_result = ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_AUDIO,
81 srtp_transport_.get());
82 EXPECT_TRUE(sender_result.ok());
83 auto receiver_result = ortc_factory_->CreateRtpReceiver(
84 cricket::MEDIA_TYPE_AUDIO, srtp_transport_.get());
85 EXPECT_TRUE(receiver_result.ok());
86}
87
88// Tests that setting the SRTP send/receive key twice is not supported.
89TEST_F(SrtpTransportTest, SetSrtpSendAndReceiveKeyTwice) {
90 EXPECT_TRUE(srtp_transport_->SetSrtpSendKey(kTestSha1CryptoParams1).ok());
91 EXPECT_TRUE(srtp_transport_->SetSrtpReceiveKey(kTestSha1CryptoParams2).ok());
92 EXPECT_EQ(RTCErrorType::UNSUPPORTED_OPERATION,
93 srtp_transport_->SetSrtpSendKey(kTestSha1CryptoParams2).type());
94 EXPECT_EQ(RTCErrorType::UNSUPPORTED_OPERATION,
95 srtp_transport_->SetSrtpReceiveKey(kTestSha1CryptoParams1).type());
96 // Ensure that the senders and receivers can be created despite the previous
97 // errors.
98 auto sender_result = ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_AUDIO,
99 srtp_transport_.get());
100 EXPECT_TRUE(sender_result.ok());
101 auto receiver_result = ortc_factory_->CreateRtpReceiver(
102 cricket::MEDIA_TYPE_AUDIO, srtp_transport_.get());
103 EXPECT_TRUE(receiver_result.ok());
104}
105
106// Test that the SRTP send key and receive key must have the same cipher suite.
107TEST_F(SrtpTransportTest, SetSrtpSendAndReceiveKeyDifferentCipherSuite) {
108 EXPECT_TRUE(srtp_transport_->SetSrtpSendKey(kTestSha1CryptoParams1).ok());
109 EXPECT_EQ(RTCErrorType::UNSUPPORTED_OPERATION,
110 srtp_transport_->SetSrtpReceiveKey(kTestGcmCryptoParams3).type());
111 EXPECT_TRUE(srtp_transport_->SetSrtpReceiveKey(kTestSha1CryptoParams2).ok());
112 // Ensure that the senders and receivers can be created despite the previous
113 // error.
114 auto sender_result = ortc_factory_->CreateRtpSender(cricket::MEDIA_TYPE_AUDIO,
115 srtp_transport_.get());
116 EXPECT_TRUE(sender_result.ok());
117 auto receiver_result = ortc_factory_->CreateRtpReceiver(
118 cricket::MEDIA_TYPE_AUDIO, srtp_transport_.get());
119 EXPECT_TRUE(receiver_result.ok());
120}
121
122class SrtpTransportTestWithMediaType
123 : public SrtpTransportTest,
124 public ::testing::WithParamInterface<cricket::MediaType> {};
125
126// Tests that the senders cannot be created before setting the keys.
127TEST_P(SrtpTransportTestWithMediaType, CreateSenderBeforeSettingKeys) {
128 auto sender_result =
129 ortc_factory_->CreateRtpSender(GetParam(), srtp_transport_.get());
130 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER, sender_result.error().type());
131 EXPECT_TRUE(srtp_transport_->SetSrtpSendKey(kTestSha1CryptoParams1).ok());
132 sender_result =
133 ortc_factory_->CreateRtpSender(GetParam(), srtp_transport_.get());
134 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER, sender_result.error().type());
135 EXPECT_TRUE(srtp_transport_->SetSrtpReceiveKey(kTestSha1CryptoParams2).ok());
136 // Ensure that after the keys are set, a sender can be created, despite the
137 // previous errors.
138 sender_result =
139 ortc_factory_->CreateRtpSender(GetParam(), srtp_transport_.get());
140 EXPECT_TRUE(sender_result.ok());
141}
142
143// Tests that the receivers cannot be created before setting the keys.
144TEST_P(SrtpTransportTestWithMediaType, CreateReceiverBeforeSettingKeys) {
145 auto receiver_result =
146 ortc_factory_->CreateRtpReceiver(GetParam(), srtp_transport_.get());
147 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER,
148 receiver_result.error().type());
149 EXPECT_TRUE(srtp_transport_->SetSrtpSendKey(kTestSha1CryptoParams1).ok());
150 receiver_result =
151 ortc_factory_->CreateRtpReceiver(GetParam(), srtp_transport_.get());
152 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER,
153 receiver_result.error().type());
154 EXPECT_TRUE(srtp_transport_->SetSrtpReceiveKey(kTestSha1CryptoParams2).ok());
155 // Ensure that after the keys are set, a receiver can be created, despite the
156 // previous errors.
157 receiver_result =
158 ortc_factory_->CreateRtpReceiver(GetParam(), srtp_transport_.get());
159 EXPECT_TRUE(receiver_result.ok());
160}
161
162INSTANTIATE_TEST_CASE_P(SenderCreatationTest,
163 SrtpTransportTestWithMediaType,
164 ::testing::Values(cricket::MEDIA_TYPE_AUDIO,
165 cricket::MEDIA_TYPE_VIDEO));
166
167} // namespace webrtc