Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -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 "pc/dtlssrtptransport.h" |
| 12 | |
| 13 | #include <memory> |
| 14 | #include <utility> |
| 15 | |
| 16 | #include "media/base/fakertp.h" |
| 17 | #include "p2p/base/dtlstransportinternal.h" |
| 18 | #include "p2p/base/fakedtlstransport.h" |
| 19 | #include "p2p/base/fakepackettransport.h" |
| 20 | #include "p2p/base/p2pconstants.h" |
| 21 | #include "pc/rtptransport.h" |
| 22 | #include "pc/rtptransporttestutil.h" |
| 23 | #include "rtc_base/asyncpacketsocket.h" |
| 24 | #include "rtc_base/gunit.h" |
| 25 | #include "rtc_base/ptr_util.h" |
| 26 | #include "rtc_base/sslstreamadapter.h" |
| 27 | |
| 28 | using cricket::FakeDtlsTransport; |
| 29 | using cricket::FakeIceTransport; |
| 30 | using webrtc::DtlsSrtpTransport; |
| 31 | using webrtc::SrtpTransport; |
| 32 | using webrtc::RtpTransport; |
| 33 | |
| 34 | const int kRtpAuthTagLen = 10; |
| 35 | |
| 36 | class TransportObserver : public sigslot::has_slots<> { |
| 37 | public: |
| 38 | void OnPacketReceived(bool rtcp, |
| 39 | rtc::CopyOnWriteBuffer* packet, |
| 40 | const rtc::PacketTime& packet_time) { |
| 41 | rtcp ? last_recv_rtcp_packet_ = *packet : last_recv_rtp_packet_ = *packet; |
| 42 | } |
| 43 | |
| 44 | void OnReadyToSend(bool ready) { ready_to_send_ = ready; } |
| 45 | |
| 46 | rtc::CopyOnWriteBuffer last_recv_rtp_packet() { |
| 47 | return last_recv_rtp_packet_; |
| 48 | } |
| 49 | |
| 50 | rtc::CopyOnWriteBuffer last_recv_rtcp_packet() { |
| 51 | return last_recv_rtcp_packet_; |
| 52 | } |
| 53 | |
| 54 | bool ready_to_send() { return ready_to_send_; } |
| 55 | |
| 56 | private: |
| 57 | rtc::CopyOnWriteBuffer last_recv_rtp_packet_; |
| 58 | rtc::CopyOnWriteBuffer last_recv_rtcp_packet_; |
| 59 | bool ready_to_send_ = false; |
| 60 | }; |
| 61 | |
| 62 | class DtlsSrtpTransportTest : public testing::Test, |
| 63 | public sigslot::has_slots<> { |
| 64 | protected: |
| 65 | DtlsSrtpTransportTest() {} |
| 66 | |
| 67 | std::unique_ptr<DtlsSrtpTransport> MakeDtlsSrtpTransport( |
| 68 | FakeDtlsTransport* rtp_dtls, |
| 69 | FakeDtlsTransport* rtcp_dtls, |
| 70 | bool rtcp_mux_enabled) { |
| 71 | auto rtp_transport = rtc::MakeUnique<RtpTransport>(rtcp_mux_enabled); |
| 72 | |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 73 | rtp_transport->AddHandledPayloadType(0x00); |
| 74 | rtp_transport->AddHandledPayloadType(0xc9); |
| 75 | |
| 76 | auto srtp_transport = |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 77 | rtc::MakeUnique<SrtpTransport>(std::move(rtp_transport)); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 78 | auto dtls_srtp_transport = |
| 79 | rtc::MakeUnique<DtlsSrtpTransport>(std::move(srtp_transport)); |
| 80 | |
| 81 | dtls_srtp_transport->SetDtlsTransports(rtp_dtls, rtcp_dtls); |
| 82 | |
| 83 | return dtls_srtp_transport; |
| 84 | } |
| 85 | |
| 86 | void MakeDtlsSrtpTransports(FakeDtlsTransport* rtp_dtls1, |
| 87 | FakeDtlsTransport* rtcp_dtls1, |
| 88 | FakeDtlsTransport* rtp_dtls2, |
| 89 | FakeDtlsTransport* rtcp_dtls2, |
| 90 | bool rtcp_mux_enabled) { |
| 91 | dtls_srtp_transport1_ = |
| 92 | MakeDtlsSrtpTransport(rtp_dtls1, rtcp_dtls1, rtcp_mux_enabled); |
| 93 | dtls_srtp_transport2_ = |
| 94 | MakeDtlsSrtpTransport(rtp_dtls2, rtcp_dtls2, rtcp_mux_enabled); |
| 95 | |
| 96 | dtls_srtp_transport1_->SignalPacketReceived.connect( |
| 97 | &transport_observer1_, &TransportObserver::OnPacketReceived); |
| 98 | dtls_srtp_transport1_->SignalReadyToSend.connect( |
| 99 | &transport_observer1_, &TransportObserver::OnReadyToSend); |
| 100 | |
| 101 | dtls_srtp_transport2_->SignalPacketReceived.connect( |
| 102 | &transport_observer2_, &TransportObserver::OnPacketReceived); |
| 103 | dtls_srtp_transport2_->SignalReadyToSend.connect( |
| 104 | &transport_observer2_, &TransportObserver::OnReadyToSend); |
| 105 | } |
| 106 | |
| 107 | void CompleteDtlsHandshake(FakeDtlsTransport* fake_dtls1, |
| 108 | FakeDtlsTransport* fake_dtls2) { |
| 109 | auto cert1 = rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>( |
| 110 | rtc::SSLIdentity::Generate("session1", rtc::KT_DEFAULT))); |
| 111 | fake_dtls1->SetLocalCertificate(cert1); |
| 112 | auto cert2 = rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>( |
| 113 | rtc::SSLIdentity::Generate("session1", rtc::KT_DEFAULT))); |
| 114 | fake_dtls2->SetLocalCertificate(cert2); |
| 115 | fake_dtls1->SetDestination(fake_dtls2); |
| 116 | } |
| 117 | |
| 118 | void SendRecvRtpPackets() { |
| 119 | ASSERT_TRUE(dtls_srtp_transport1_); |
| 120 | ASSERT_TRUE(dtls_srtp_transport2_); |
| 121 | ASSERT_TRUE(dtls_srtp_transport1_->IsActive()); |
| 122 | ASSERT_TRUE(dtls_srtp_transport2_->IsActive()); |
| 123 | |
| 124 | size_t rtp_len = sizeof(kPcmuFrame); |
| 125 | size_t packet_size = rtp_len + kRtpAuthTagLen; |
| 126 | rtc::Buffer rtp_packet_buffer(packet_size); |
| 127 | char* rtp_packet_data = rtp_packet_buffer.data<char>(); |
| 128 | memcpy(rtp_packet_data, kPcmuFrame, rtp_len); |
| 129 | // In order to be able to run this test function multiple times we can not |
| 130 | // use the same sequence number twice. Increase the sequence number by one. |
| 131 | rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_data) + 2, |
| 132 | ++sequence_number_); |
| 133 | rtc::CopyOnWriteBuffer rtp_packet1to2(rtp_packet_data, rtp_len, |
| 134 | packet_size); |
| 135 | rtc::CopyOnWriteBuffer rtp_packet2to1(rtp_packet_data, rtp_len, |
| 136 | packet_size); |
| 137 | |
| 138 | rtc::PacketOptions options; |
| 139 | // Send a packet from |srtp_transport1_| to |srtp_transport2_| and verify |
| 140 | // that the packet can be successfully received and decrypted. |
| 141 | ASSERT_TRUE(dtls_srtp_transport1_->SendRtpPacket(&rtp_packet1to2, options, |
| 142 | cricket::PF_SRTP_BYPASS)); |
| 143 | ASSERT_TRUE(transport_observer2_.last_recv_rtp_packet().data()); |
| 144 | EXPECT_EQ(0, memcmp(transport_observer2_.last_recv_rtp_packet().data(), |
| 145 | kPcmuFrame, rtp_len)); |
| 146 | ASSERT_TRUE(dtls_srtp_transport2_->SendRtpPacket(&rtp_packet2to1, options, |
| 147 | cricket::PF_SRTP_BYPASS)); |
| 148 | ASSERT_TRUE(transport_observer1_.last_recv_rtp_packet().data()); |
| 149 | EXPECT_EQ(0, memcmp(transport_observer1_.last_recv_rtp_packet().data(), |
| 150 | kPcmuFrame, rtp_len)); |
| 151 | } |
| 152 | |
| 153 | void SendRecvRtcpPackets() { |
| 154 | size_t rtcp_len = sizeof(kRtcpReport); |
| 155 | size_t packet_size = rtcp_len + 4 + kRtpAuthTagLen; |
| 156 | rtc::Buffer rtcp_packet_buffer(packet_size); |
| 157 | |
| 158 | // TODO(zhihuang): Remove the extra copy when the SendRtpPacket method |
| 159 | // doesn't take the CopyOnWriteBuffer by pointer. |
| 160 | rtc::CopyOnWriteBuffer rtcp_packet1to2(kRtcpReport, rtcp_len, packet_size); |
| 161 | rtc::CopyOnWriteBuffer rtcp_packet2to1(kRtcpReport, rtcp_len, packet_size); |
| 162 | |
| 163 | rtc::PacketOptions options; |
| 164 | // Send a packet from |srtp_transport1_| to |srtp_transport2_| and verify |
| 165 | // that the packet can be successfully received and decrypted. |
| 166 | ASSERT_TRUE(dtls_srtp_transport1_->SendRtcpPacket(&rtcp_packet1to2, options, |
| 167 | cricket::PF_SRTP_BYPASS)); |
| 168 | ASSERT_TRUE(transport_observer2_.last_recv_rtcp_packet().data()); |
| 169 | EXPECT_EQ(0, memcmp(transport_observer2_.last_recv_rtcp_packet().data(), |
| 170 | kRtcpReport, rtcp_len)); |
| 171 | |
| 172 | // Do the same thing in the opposite direction; |
| 173 | ASSERT_TRUE(dtls_srtp_transport2_->SendRtcpPacket(&rtcp_packet2to1, options, |
| 174 | cricket::PF_SRTP_BYPASS)); |
| 175 | ASSERT_TRUE(transport_observer1_.last_recv_rtcp_packet().data()); |
| 176 | EXPECT_EQ(0, memcmp(transport_observer1_.last_recv_rtcp_packet().data(), |
| 177 | kRtcpReport, rtcp_len)); |
| 178 | } |
| 179 | |
| 180 | void SendRecvRtpPacketsWithHeaderExtension( |
| 181 | const std::vector<int>& encrypted_header_ids) { |
| 182 | ASSERT_TRUE(dtls_srtp_transport1_); |
| 183 | ASSERT_TRUE(dtls_srtp_transport2_); |
| 184 | ASSERT_TRUE(dtls_srtp_transport1_->IsActive()); |
| 185 | ASSERT_TRUE(dtls_srtp_transport2_->IsActive()); |
| 186 | |
| 187 | size_t rtp_len = sizeof(kPcmuFrameWithExtensions); |
| 188 | size_t packet_size = rtp_len + kRtpAuthTagLen; |
| 189 | rtc::Buffer rtp_packet_buffer(packet_size); |
| 190 | char* rtp_packet_data = rtp_packet_buffer.data<char>(); |
| 191 | memcpy(rtp_packet_data, kPcmuFrameWithExtensions, rtp_len); |
| 192 | // In order to be able to run this test function multiple times we can not |
| 193 | // use the same sequence number twice. Increase the sequence number by one. |
| 194 | rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_data) + 2, |
| 195 | ++sequence_number_); |
| 196 | rtc::CopyOnWriteBuffer rtp_packet1to2(rtp_packet_data, rtp_len, |
| 197 | packet_size); |
| 198 | rtc::CopyOnWriteBuffer rtp_packet2to1(rtp_packet_data, rtp_len, |
| 199 | packet_size); |
| 200 | |
| 201 | char original_rtp_data[sizeof(kPcmuFrameWithExtensions)]; |
| 202 | memcpy(original_rtp_data, rtp_packet_data, rtp_len); |
| 203 | |
| 204 | rtc::PacketOptions options; |
| 205 | // Send a packet from |srtp_transport1_| to |srtp_transport2_| and verify |
| 206 | // that the packet can be successfully received and decrypted. |
| 207 | ASSERT_TRUE(dtls_srtp_transport1_->SendRtpPacket(&rtp_packet1to2, options, |
| 208 | cricket::PF_SRTP_BYPASS)); |
| 209 | ASSERT_TRUE(transport_observer2_.last_recv_rtp_packet().data()); |
| 210 | EXPECT_EQ(0, memcmp(transport_observer2_.last_recv_rtp_packet().data(), |
| 211 | original_rtp_data, rtp_len)); |
| 212 | // Get the encrypted packet from underneath packet transport and verify the |
| 213 | // data and header extension are actually encrypted. |
| 214 | auto fake_dtls_transport = static_cast<FakeDtlsTransport*>( |
| 215 | dtls_srtp_transport1_->rtp_packet_transport()); |
| 216 | auto fake_ice_transport = |
| 217 | static_cast<FakeIceTransport*>(fake_dtls_transport->ice_transport()); |
| 218 | EXPECT_NE(0, memcmp(fake_ice_transport->last_sent_packet().data(), |
| 219 | original_rtp_data, rtp_len)); |
| 220 | CompareHeaderExtensions(reinterpret_cast<const char*>( |
| 221 | fake_ice_transport->last_sent_packet().data()), |
| 222 | fake_ice_transport->last_sent_packet().size(), |
| 223 | original_rtp_data, rtp_len, encrypted_header_ids, |
| 224 | false); |
| 225 | |
| 226 | // Do the same thing in the opposite direction. |
| 227 | ASSERT_TRUE(dtls_srtp_transport2_->SendRtpPacket(&rtp_packet2to1, options, |
| 228 | cricket::PF_SRTP_BYPASS)); |
| 229 | ASSERT_TRUE(transport_observer1_.last_recv_rtp_packet().data()); |
| 230 | EXPECT_EQ(0, memcmp(transport_observer1_.last_recv_rtp_packet().data(), |
| 231 | original_rtp_data, rtp_len)); |
| 232 | // Get the encrypted packet from underneath packet transport and verify the |
| 233 | // data and header extension are actually encrypted. |
| 234 | fake_dtls_transport = static_cast<FakeDtlsTransport*>( |
| 235 | dtls_srtp_transport2_->rtp_packet_transport()); |
| 236 | fake_ice_transport = |
| 237 | static_cast<FakeIceTransport*>(fake_dtls_transport->ice_transport()); |
| 238 | EXPECT_NE(0, memcmp(fake_ice_transport->last_sent_packet().data(), |
| 239 | original_rtp_data, rtp_len)); |
| 240 | CompareHeaderExtensions(reinterpret_cast<const char*>( |
| 241 | fake_ice_transport->last_sent_packet().data()), |
| 242 | fake_ice_transport->last_sent_packet().size(), |
| 243 | original_rtp_data, rtp_len, encrypted_header_ids, |
| 244 | false); |
| 245 | } |
| 246 | |
| 247 | void SendRecvPackets() { |
| 248 | SendRecvRtpPackets(); |
| 249 | SendRecvRtcpPackets(); |
| 250 | } |
| 251 | |
| 252 | std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport1_; |
| 253 | std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport2_; |
| 254 | TransportObserver transport_observer1_; |
| 255 | TransportObserver transport_observer2_; |
| 256 | |
| 257 | int sequence_number_ = 0; |
| 258 | }; |
| 259 | |
| 260 | // Tests that if RTCP muxing is enabled and transports are set after RTP |
| 261 | // transport finished the handshake, SRTP is set up. |
| 262 | TEST_F(DtlsSrtpTransportTest, SetTransportsAfterHandshakeCompleteWithRtcpMux) { |
| 263 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 264 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 265 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 266 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 267 | |
| 268 | MakeDtlsSrtpTransports(rtp_dtls1.get(), nullptr, rtp_dtls2.get(), nullptr, |
| 269 | /*rtcp_mux_enabled=*/true); |
| 270 | |
| 271 | auto rtp_dtls3 = rtc::MakeUnique<FakeDtlsTransport>( |
| 272 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 273 | auto rtp_dtls4 = rtc::MakeUnique<FakeDtlsTransport>( |
| 274 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 275 | |
| 276 | CompleteDtlsHandshake(rtp_dtls3.get(), rtp_dtls4.get()); |
| 277 | |
| 278 | dtls_srtp_transport1_->SetDtlsTransports(rtp_dtls3.get(), nullptr); |
| 279 | dtls_srtp_transport2_->SetDtlsTransports(rtp_dtls4.get(), nullptr); |
| 280 | |
| 281 | SendRecvPackets(); |
| 282 | } |
| 283 | |
| 284 | // Tests that if RTCP muxing is not enabled and transports are set after both |
| 285 | // RTP and RTCP transports finished the handshake, SRTP is set up. |
| 286 | TEST_F(DtlsSrtpTransportTest, |
| 287 | SetTransportsAfterHandshakeCompleteWithoutRtcpMux) { |
| 288 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 289 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 290 | auto rtcp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 291 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 292 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 293 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 294 | auto rtcp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 295 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 296 | |
| 297 | MakeDtlsSrtpTransports(rtp_dtls1.get(), rtcp_dtls1.get(), rtp_dtls2.get(), |
| 298 | rtcp_dtls2.get(), /*rtcp_mux_enabled=*/false); |
| 299 | |
| 300 | auto rtp_dtls3 = rtc::MakeUnique<FakeDtlsTransport>( |
| 301 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 302 | auto rtcp_dtls3 = rtc::MakeUnique<FakeDtlsTransport>( |
| 303 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 304 | auto rtp_dtls4 = rtc::MakeUnique<FakeDtlsTransport>( |
| 305 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 306 | auto rtcp_dtls4 = rtc::MakeUnique<FakeDtlsTransport>( |
| 307 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 308 | CompleteDtlsHandshake(rtp_dtls3.get(), rtp_dtls4.get()); |
| 309 | CompleteDtlsHandshake(rtcp_dtls3.get(), rtcp_dtls4.get()); |
| 310 | |
| 311 | dtls_srtp_transport1_->SetDtlsTransports(rtp_dtls3.get(), rtcp_dtls3.get()); |
| 312 | dtls_srtp_transport2_->SetDtlsTransports(rtp_dtls4.get(), rtcp_dtls4.get()); |
| 313 | |
| 314 | SendRecvPackets(); |
| 315 | } |
| 316 | |
| 317 | // Tests if RTCP muxing is enabled, SRTP is set up as soon as the RTP DTLS |
| 318 | // handshake is finished. |
| 319 | TEST_F(DtlsSrtpTransportTest, SetTransportsBeforeHandshakeCompleteWithRtcpMux) { |
| 320 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 321 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 322 | auto rtcp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 323 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 324 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 325 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 326 | auto rtcp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 327 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 328 | |
| 329 | MakeDtlsSrtpTransports(rtp_dtls1.get(), rtcp_dtls1.get(), rtp_dtls2.get(), |
| 330 | rtcp_dtls2.get(), |
| 331 | /*rtcp_mux_enabled=*/false); |
| 332 | |
| 333 | dtls_srtp_transport1_->SetRtcpMuxEnabled(true); |
| 334 | dtls_srtp_transport2_->SetRtcpMuxEnabled(true); |
| 335 | CompleteDtlsHandshake(rtp_dtls1.get(), rtp_dtls2.get()); |
| 336 | SendRecvPackets(); |
| 337 | } |
| 338 | |
| 339 | // Tests if RTCP muxing is not enabled, SRTP is set up when both the RTP and |
| 340 | // RTCP DTLS handshake are finished. |
| 341 | TEST_F(DtlsSrtpTransportTest, |
| 342 | SetTransportsBeforeHandshakeCompleteWithoutRtcpMux) { |
| 343 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 344 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 345 | auto rtcp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 346 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 347 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 348 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 349 | auto rtcp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 350 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 351 | |
| 352 | MakeDtlsSrtpTransports(rtp_dtls1.get(), rtcp_dtls1.get(), rtp_dtls2.get(), |
| 353 | rtcp_dtls2.get(), /*rtcp_mux_enabled=*/false); |
| 354 | |
| 355 | CompleteDtlsHandshake(rtp_dtls1.get(), rtp_dtls2.get()); |
| 356 | EXPECT_FALSE(dtls_srtp_transport1_->IsActive()); |
| 357 | EXPECT_FALSE(dtls_srtp_transport2_->IsActive()); |
| 358 | CompleteDtlsHandshake(rtcp_dtls1.get(), rtcp_dtls2.get()); |
| 359 | SendRecvPackets(); |
| 360 | } |
| 361 | |
| 362 | // Tests that if the DtlsTransport underneath is changed, the previous DTLS-SRTP |
| 363 | // context will be reset and will be re-setup once the new transports' handshake |
| 364 | // complete. |
| 365 | TEST_F(DtlsSrtpTransportTest, DtlsSrtpResetAfterDtlsTransportChange) { |
| 366 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 367 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 368 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 369 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 370 | |
| 371 | MakeDtlsSrtpTransports(rtp_dtls1.get(), nullptr, rtp_dtls2.get(), nullptr, |
| 372 | /*rtcp_mux_enabled=*/true); |
| 373 | |
| 374 | CompleteDtlsHandshake(rtp_dtls1.get(), rtp_dtls2.get()); |
| 375 | EXPECT_TRUE(dtls_srtp_transport1_->IsActive()); |
| 376 | EXPECT_TRUE(dtls_srtp_transport2_->IsActive()); |
| 377 | |
| 378 | auto rtp_dtls3 = rtc::MakeUnique<FakeDtlsTransport>( |
| 379 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 380 | auto rtp_dtls4 = rtc::MakeUnique<FakeDtlsTransport>( |
| 381 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 382 | |
| 383 | // The previous context is reset. |
| 384 | dtls_srtp_transport1_->SetDtlsTransports(rtp_dtls3.get(), nullptr); |
| 385 | dtls_srtp_transport2_->SetDtlsTransports(rtp_dtls4.get(), nullptr); |
| 386 | EXPECT_FALSE(dtls_srtp_transport1_->IsActive()); |
| 387 | EXPECT_FALSE(dtls_srtp_transport2_->IsActive()); |
| 388 | |
| 389 | // Re-setup. |
| 390 | CompleteDtlsHandshake(rtp_dtls3.get(), rtp_dtls4.get()); |
| 391 | SendRecvPackets(); |
| 392 | } |
| 393 | |
| 394 | // Tests if only the RTP DTLS handshake complete, and then RTCP muxing is |
| 395 | // enabled, SRTP is set up. |
| 396 | TEST_F(DtlsSrtpTransportTest, |
| 397 | RtcpMuxEnabledAfterRtpTransportHandshakeComplete) { |
| 398 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 399 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 400 | auto rtcp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 401 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 402 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 403 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 404 | auto rtcp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 405 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 406 | |
| 407 | MakeDtlsSrtpTransports(rtp_dtls1.get(), rtcp_dtls1.get(), rtp_dtls2.get(), |
| 408 | rtcp_dtls2.get(), /*rtcp_mux_enabled=*/false); |
| 409 | |
| 410 | CompleteDtlsHandshake(rtp_dtls1.get(), rtp_dtls2.get()); |
| 411 | // Inactive because the RTCP transport handshake didn't complete. |
| 412 | EXPECT_FALSE(dtls_srtp_transport1_->IsActive()); |
| 413 | EXPECT_FALSE(dtls_srtp_transport2_->IsActive()); |
| 414 | |
| 415 | dtls_srtp_transport1_->SetRtcpMuxEnabled(true); |
| 416 | dtls_srtp_transport2_->SetRtcpMuxEnabled(true); |
| 417 | // The transports should be active and be able to send packets when the |
| 418 | // RTCP muxing is enabled. |
| 419 | SendRecvPackets(); |
| 420 | } |
| 421 | |
| 422 | // Tests that when SetSend/RecvEncryptedHeaderExtensionIds is called, the SRTP |
| 423 | // sessions are updated with new encryped header extension IDs immediately. |
| 424 | TEST_F(DtlsSrtpTransportTest, EncryptedHeaderExtensionIdUpdated) { |
| 425 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 426 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 427 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 428 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 429 | |
| 430 | MakeDtlsSrtpTransports(rtp_dtls1.get(), nullptr, rtp_dtls2.get(), nullptr, |
| 431 | /*rtcp_mux_enabled=*/true); |
| 432 | CompleteDtlsHandshake(rtp_dtls1.get(), rtp_dtls2.get()); |
| 433 | |
| 434 | std::vector<int> encrypted_headers; |
| 435 | encrypted_headers.push_back(kHeaderExtensionIDs[0]); |
| 436 | encrypted_headers.push_back(kHeaderExtensionIDs[1]); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 437 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 438 | dtls_srtp_transport1_->UpdateSendEncryptedHeaderExtensionIds( |
| 439 | encrypted_headers); |
| 440 | dtls_srtp_transport1_->UpdateRecvEncryptedHeaderExtensionIds( |
| 441 | encrypted_headers); |
| 442 | dtls_srtp_transport2_->UpdateSendEncryptedHeaderExtensionIds( |
| 443 | encrypted_headers); |
| 444 | dtls_srtp_transport2_->UpdateRecvEncryptedHeaderExtensionIds( |
| 445 | encrypted_headers); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // Tests if RTCP muxing is enabled. DtlsSrtpTransport is ready to send once the |
| 449 | // RTP DtlsTransport is ready. |
| 450 | TEST_F(DtlsSrtpTransportTest, SignalReadyToSendFiredWithRtcpMux) { |
| 451 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 452 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 453 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 454 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 455 | |
| 456 | MakeDtlsSrtpTransports(rtp_dtls1.get(), nullptr, rtp_dtls2.get(), nullptr, |
| 457 | /*rtcp_mux_enabled=*/true); |
| 458 | |
| 459 | rtp_dtls1->SetDestination(rtp_dtls2.get()); |
| 460 | EXPECT_TRUE(transport_observer1_.ready_to_send()); |
| 461 | EXPECT_TRUE(transport_observer2_.ready_to_send()); |
| 462 | } |
| 463 | |
| 464 | // Tests if RTCP muxing is not enabled. DtlsSrtpTransport is ready to send once |
| 465 | // both the RTP and RTCP DtlsTransport are ready. |
| 466 | TEST_F(DtlsSrtpTransportTest, SignalReadyToSendFiredWithoutRtcpMux) { |
| 467 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 468 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 469 | auto rtcp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 470 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 471 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 472 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 473 | auto rtcp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 474 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 475 | |
| 476 | MakeDtlsSrtpTransports(rtp_dtls1.get(), rtcp_dtls1.get(), rtp_dtls2.get(), |
| 477 | rtcp_dtls2.get(), /*rtcp_mux_enabled=*/false); |
| 478 | |
| 479 | rtp_dtls1->SetDestination(rtp_dtls2.get()); |
| 480 | EXPECT_FALSE(transport_observer1_.ready_to_send()); |
| 481 | EXPECT_FALSE(transport_observer2_.ready_to_send()); |
| 482 | |
| 483 | rtcp_dtls1->SetDestination(rtcp_dtls2.get()); |
| 484 | EXPECT_TRUE(transport_observer1_.ready_to_send()); |
| 485 | EXPECT_TRUE(transport_observer2_.ready_to_send()); |
| 486 | } |