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