| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 if (!(rtc::SSLStreamAdapter::feature())) { \ | 26 if (!(rtc::SSLStreamAdapter::feature())) { \ |
| 27 LOG(LS_INFO) << "Feature disabled... skipping"; \ | 27 LOG(LS_INFO) << "Feature disabled... skipping"; \ |
| 28 return; \ | 28 return; \ |
| 29 } | 29 } |
| 30 | 30 |
| 31 static const char AES_CM_128_HMAC_SHA1_80[] = "AES_CM_128_HMAC_SHA1_80"; | 31 static const char AES_CM_128_HMAC_SHA1_80[] = "AES_CM_128_HMAC_SHA1_80"; |
| 32 static const char kIceUfrag1[] = "TESTICEUFRAG0001"; | 32 static const char kIceUfrag1[] = "TESTICEUFRAG0001"; |
| 33 static const char kIcePwd1[] = "TESTICEPWD00000000000001"; | 33 static const char kIcePwd1[] = "TESTICEPWD00000000000001"; |
| 34 static const size_t kPacketNumOffset = 8; | 34 static const size_t kPacketNumOffset = 8; |
| 35 static const size_t kPacketHeaderLen = 12; | 35 static const size_t kPacketHeaderLen = 12; |
| 36 static const int kFakePacketId = 0x1234; |
| 36 | 37 |
| 37 static bool IsRtpLeadByte(uint8_t b) { | 38 static bool IsRtpLeadByte(uint8_t b) { |
| 38 return ((b & 0xC0) == 0x80); | 39 return ((b & 0xC0) == 0x80); |
| 39 } | 40 } |
| 40 | 41 |
| 41 using cricket::ConnectionRole; | 42 using cricket::ConnectionRole; |
| 42 | 43 |
| 43 enum Flags { NF_REOFFER = 0x1, NF_EXPECT_FAILURE = 0x2 }; | 44 enum Flags { NF_REOFFER = 0x1, NF_EXPECT_FAILURE = 0x2 }; |
| 44 | 45 |
| 45 class DtlsTestClient : public sigslot::has_slots<> { | 46 class DtlsTestClient : public sigslot::has_slots<> { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 for (int i = 0; i < count; ++i) { | 80 for (int i = 0; i < count; ++i) { |
| 80 cricket::DtlsTransportChannelWrapper* channel = | 81 cricket::DtlsTransportChannelWrapper* channel = |
| 81 static_cast<cricket::DtlsTransportChannelWrapper*>( | 82 static_cast<cricket::DtlsTransportChannelWrapper*>( |
| 82 transport_->CreateChannel(i)); | 83 transport_->CreateChannel(i)); |
| 83 ASSERT_TRUE(channel != NULL); | 84 ASSERT_TRUE(channel != NULL); |
| 84 channel->SetSslMaxProtocolVersion(ssl_max_version_); | 85 channel->SetSslMaxProtocolVersion(ssl_max_version_); |
| 85 channel->SignalWritableState.connect(this, | 86 channel->SignalWritableState.connect(this, |
| 86 &DtlsTestClient::OnTransportChannelWritableState); | 87 &DtlsTestClient::OnTransportChannelWritableState); |
| 87 channel->SignalReadPacket.connect(this, | 88 channel->SignalReadPacket.connect(this, |
| 88 &DtlsTestClient::OnTransportChannelReadPacket); | 89 &DtlsTestClient::OnTransportChannelReadPacket); |
| 90 channel->SignalSentPacket.connect( |
| 91 this, &DtlsTestClient::OnTransportChannelSentPacket); |
| 89 channels_.push_back(channel); | 92 channels_.push_back(channel); |
| 90 | 93 |
| 91 // Hook the raw packets so that we can verify they are encrypted. | 94 // Hook the raw packets so that we can verify they are encrypted. |
| 92 channel->channel()->SignalReadPacket.connect( | 95 channel->channel()->SignalReadPacket.connect( |
| 93 this, &DtlsTestClient::OnFakeTransportChannelReadPacket); | 96 this, &DtlsTestClient::OnFakeTransportChannelReadPacket); |
| 94 } | 97 } |
| 95 } | 98 } |
| 96 | 99 |
| 97 cricket::Transport* transport() { return transport_.get(); } | 100 cricket::Transport* transport() { return transport_.get(); } |
| 98 | 101 |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 // Fill the packet with a known value and a sequence number to check | 255 // Fill the packet with a known value and a sequence number to check |
| 253 // against, and make sure that it doesn't look like DTLS. | 256 // against, and make sure that it doesn't look like DTLS. |
| 254 memset(packet.get(), sent & 0xff, size); | 257 memset(packet.get(), sent & 0xff, size); |
| 255 packet[0] = (srtp) ? 0x80 : 0x00; | 258 packet[0] = (srtp) ? 0x80 : 0x00; |
| 256 rtc::SetBE32(packet.get() + kPacketNumOffset, | 259 rtc::SetBE32(packet.get() + kPacketNumOffset, |
| 257 static_cast<uint32_t>(sent)); | 260 static_cast<uint32_t>(sent)); |
| 258 | 261 |
| 259 // Only set the bypass flag if we've activated DTLS. | 262 // Only set the bypass flag if we've activated DTLS. |
| 260 int flags = (certificate_ && srtp) ? cricket::PF_SRTP_BYPASS : 0; | 263 int flags = (certificate_ && srtp) ? cricket::PF_SRTP_BYPASS : 0; |
| 261 rtc::PacketOptions packet_options; | 264 rtc::PacketOptions packet_options; |
| 265 packet_options.packet_id = kFakePacketId; |
| 262 int rv = channels_[channel]->SendPacket( | 266 int rv = channels_[channel]->SendPacket( |
| 263 packet.get(), size, packet_options, flags); | 267 packet.get(), size, packet_options, flags); |
| 264 ASSERT_GT(rv, 0); | 268 ASSERT_GT(rv, 0); |
| 265 ASSERT_EQ(size, static_cast<size_t>(rv)); | 269 ASSERT_EQ(size, static_cast<size_t>(rv)); |
| 266 ++sent; | 270 ++sent; |
| 267 } while (sent < count); | 271 } while (sent < count); |
| 268 } | 272 } |
| 269 | 273 |
| 270 int SendInvalidSrtpPacket(size_t channel, size_t size) { | 274 int SendInvalidSrtpPacket(size_t channel, size_t size) { |
| 271 ASSERT(channel < channels_.size()); | 275 ASSERT(channel < channels_.size()); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 int flags) { | 335 int flags) { |
| 332 uint32_t packet_num = 0; | 336 uint32_t packet_num = 0; |
| 333 ASSERT_TRUE(VerifyPacket(data, size, &packet_num)); | 337 ASSERT_TRUE(VerifyPacket(data, size, &packet_num)); |
| 334 received_.insert(packet_num); | 338 received_.insert(packet_num); |
| 335 // Only DTLS-SRTP packets should have the bypass flag set. | 339 // Only DTLS-SRTP packets should have the bypass flag set. |
| 336 int expected_flags = | 340 int expected_flags = |
| 337 (certificate_ && IsRtpLeadByte(data[0])) ? cricket::PF_SRTP_BYPASS : 0; | 341 (certificate_ && IsRtpLeadByte(data[0])) ? cricket::PF_SRTP_BYPASS : 0; |
| 338 ASSERT_EQ(expected_flags, flags); | 342 ASSERT_EQ(expected_flags, flags); |
| 339 } | 343 } |
| 340 | 344 |
| 345 void OnTransportChannelSentPacket(cricket::TransportChannel* channel, |
| 346 const rtc::SentPacket& sent_packet) { |
| 347 sent_packet_ = sent_packet; |
| 348 } |
| 349 |
| 350 rtc::SentPacket sent_packet() const { return sent_packet_; } |
| 351 |
| 341 // Hook into the raw packet stream to make sure DTLS packets are encrypted. | 352 // Hook into the raw packet stream to make sure DTLS packets are encrypted. |
| 342 void OnFakeTransportChannelReadPacket(cricket::TransportChannel* channel, | 353 void OnFakeTransportChannelReadPacket(cricket::TransportChannel* channel, |
| 343 const char* data, size_t size, | 354 const char* data, size_t size, |
| 344 const rtc::PacketTime& time, | 355 const rtc::PacketTime& time, |
| 345 int flags) { | 356 int flags) { |
| 346 // Flags shouldn't be set on the underlying TransportChannel packets. | 357 // Flags shouldn't be set on the underlying TransportChannel packets. |
| 347 ASSERT_EQ(0, flags); | 358 ASSERT_EQ(0, flags); |
| 348 | 359 |
| 349 // Look at the handshake packets to see what role we played. | 360 // Look at the handshake packets to see what role we played. |
| 350 // Check that non-handshake packets are DTLS data or SRTP bypass. | 361 // Check that non-handshake packets are DTLS data or SRTP bypass. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 371 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; | 382 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
| 372 rtc::scoped_ptr<cricket::FakeTransport> transport_; | 383 rtc::scoped_ptr<cricket::FakeTransport> transport_; |
| 373 std::vector<cricket::DtlsTransportChannelWrapper*> channels_; | 384 std::vector<cricket::DtlsTransportChannelWrapper*> channels_; |
| 374 size_t packet_size_; | 385 size_t packet_size_; |
| 375 std::set<int> received_; | 386 std::set<int> received_; |
| 376 bool use_dtls_srtp_; | 387 bool use_dtls_srtp_; |
| 377 rtc::SSLProtocolVersion ssl_max_version_; | 388 rtc::SSLProtocolVersion ssl_max_version_; |
| 378 bool negotiated_dtls_; | 389 bool negotiated_dtls_; |
| 379 bool received_dtls_client_hello_; | 390 bool received_dtls_client_hello_; |
| 380 bool received_dtls_server_hello_; | 391 bool received_dtls_server_hello_; |
| 392 rtc::SentPacket sent_packet_; |
| 381 }; | 393 }; |
| 382 | 394 |
| 383 | 395 |
| 384 class DtlsTransportChannelTest : public testing::Test { | 396 class DtlsTransportChannelTest : public testing::Test { |
| 385 public: | 397 public: |
| 386 DtlsTransportChannelTest() | 398 DtlsTransportChannelTest() |
| 387 : client1_("P1"), | 399 : client1_("P1"), |
| 388 client2_("P2"), | 400 client2_("P2"), |
| 389 channel_ct_(1), | 401 channel_ct_(1), |
| 390 use_dtls_(false), | 402 use_dtls_(false), |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, channel2->GetIceRole()); | 563 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, channel2->GetIceRole()); |
| 552 EXPECT_EQ(2U, channel2->IceTiebreaker()); | 564 EXPECT_EQ(2U, channel2->IceTiebreaker()); |
| 553 } | 565 } |
| 554 | 566 |
| 555 // Connect without DTLS, and transfer some data. | 567 // Connect without DTLS, and transfer some data. |
| 556 TEST_F(DtlsTransportChannelTest, TestTransfer) { | 568 TEST_F(DtlsTransportChannelTest, TestTransfer) { |
| 557 ASSERT_TRUE(Connect()); | 569 ASSERT_TRUE(Connect()); |
| 558 TestTransfer(0, 1000, 100, false); | 570 TestTransfer(0, 1000, 100, false); |
| 559 } | 571 } |
| 560 | 572 |
| 573 // Connect without DTLS, and transfer some data. |
| 574 TEST_F(DtlsTransportChannelTest, TestOnSentPacket) { |
| 575 ASSERT_TRUE(Connect()); |
| 576 EXPECT_EQ(client1_.sent_packet().send_time_ms, -1); |
| 577 TestTransfer(0, 1000, 100, false); |
| 578 EXPECT_EQ(kFakePacketId, client1_.sent_packet().packet_id); |
| 579 EXPECT_GE(client1_.sent_packet().send_time_ms, 0); |
| 580 } |
| 581 |
| 561 // Create two channels without DTLS, and transfer some data. | 582 // Create two channels without DTLS, and transfer some data. |
| 562 TEST_F(DtlsTransportChannelTest, TestTransferTwoChannels) { | 583 TEST_F(DtlsTransportChannelTest, TestTransferTwoChannels) { |
| 563 SetChannelCount(2); | 584 SetChannelCount(2); |
| 564 ASSERT_TRUE(Connect()); | 585 ASSERT_TRUE(Connect()); |
| 565 TestTransfer(0, 1000, 100, false); | 586 TestTransfer(0, 1000, 100, false); |
| 566 TestTransfer(1, 1000, 100, false); | 587 TestTransfer(1, 1000, 100, false); |
| 567 } | 588 } |
| 568 | 589 |
| 569 // Connect without DTLS, and transfer SRTP data. | 590 // Connect without DTLS, and transfer SRTP data. |
| 570 TEST_F(DtlsTransportChannelTest, TestTransferSrtp) { | 591 TEST_F(DtlsTransportChannelTest, TestTransferSrtp) { |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 864 // Each side's remote certificate is the other side's local certificate. | 885 // Each side's remote certificate is the other side's local certificate. |
| 865 ASSERT_TRUE( | 886 ASSERT_TRUE( |
| 866 client1_.transport()->GetRemoteSSLCertificate(remote_cert1.accept())); | 887 client1_.transport()->GetRemoteSSLCertificate(remote_cert1.accept())); |
| 867 ASSERT_EQ(remote_cert1->ToPEMString(), | 888 ASSERT_EQ(remote_cert1->ToPEMString(), |
| 868 certificate2->ssl_certificate().ToPEMString()); | 889 certificate2->ssl_certificate().ToPEMString()); |
| 869 ASSERT_TRUE( | 890 ASSERT_TRUE( |
| 870 client2_.transport()->GetRemoteSSLCertificate(remote_cert2.accept())); | 891 client2_.transport()->GetRemoteSSLCertificate(remote_cert2.accept())); |
| 871 ASSERT_EQ(remote_cert2->ToPEMString(), | 892 ASSERT_EQ(remote_cert2->ToPEMString(), |
| 872 certificate1->ssl_certificate().ToPEMString()); | 893 certificate1->ssl_certificate().ToPEMString()); |
| 873 } | 894 } |
| OLD | NEW |