OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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 "webrtc/p2p/quic/quictransport.h" |
| 12 |
| 13 #include <string> |
| 14 #include <vector> |
| 15 |
| 16 #include "webrtc/base/gunit.h" |
| 17 #include "webrtc/base/rtccertificate.h" |
| 18 #include "webrtc/base/sslidentity.h" |
| 19 |
| 20 using cricket::TransportChannelImpl; |
| 21 using cricket::QuicTransport; |
| 22 using cricket::Transport; |
| 23 using cricket::TransportDescription; |
| 24 |
| 25 static const char kIceUfrag1[] = "TESTICEUFRAG0001"; |
| 26 static const char kIcePwd1[] = "TESTICEPWD00000000000001"; |
| 27 |
| 28 static const char kIceUfrag2[] = "TESTICEUFRAG0002"; |
| 29 static const char kIcePwd2[] = "TESTICEPWD00000000000002"; |
| 30 |
| 31 static rtc::scoped_refptr<rtc::RTCCertificate> CreateCertificate( |
| 32 std::string name) { |
| 33 return rtc::RTCCertificate::Create(rtc::scoped_ptr<rtc::SSLIdentity>( |
| 34 rtc::SSLIdentity::Generate(name, rtc::KT_DEFAULT))); |
| 35 } |
| 36 |
| 37 static rtc::scoped_ptr<rtc::SSLFingerprint> CreateFingerprint( |
| 38 rtc::RTCCertificate* cert) { |
| 39 std::string digest_algorithm; |
| 40 cert->ssl_certificate().GetSignatureDigestAlgorithm(&digest_algorithm); |
| 41 return rtc::scoped_ptr<rtc::SSLFingerprint>( |
| 42 rtc::SSLFingerprint::Create(digest_algorithm, cert->identity())); |
| 43 } |
| 44 |
| 45 class QuicTransportTest : public testing::Test { |
| 46 public: |
| 47 QuicTransportTest() : transport_("testing", nullptr, nullptr) {} |
| 48 |
| 49 void SetTransportDescription(cricket::ConnectionRole local_role, |
| 50 cricket::ConnectionRole remote_role, |
| 51 cricket::ContentAction local_action, |
| 52 cricket::ContentAction remote_action, |
| 53 rtc::SSLRole expected_ssl_role) { |
| 54 TransportChannelImpl* channel = transport_.CreateChannel(1); |
| 55 ASSERT_NE(nullptr, channel); |
| 56 |
| 57 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate( |
| 58 CreateCertificate("local")); |
| 59 ASSERT_NE(nullptr, local_certificate); |
| 60 transport_.SetLocalCertificate(local_certificate); |
| 61 |
| 62 rtc::scoped_ptr<rtc::SSLFingerprint> local_fingerprint = |
| 63 CreateFingerprint(local_certificate.get()); |
| 64 ASSERT_NE(nullptr, local_fingerprint); |
| 65 TransportDescription local_desc(std::vector<std::string>(), kIceUfrag1, |
| 66 kIcePwd1, cricket::ICEMODE_FULL, local_role, |
| 67 local_fingerprint.get()); |
| 68 ASSERT_TRUE(transport_.SetLocalTransportDescription(local_desc, |
| 69 local_action, nullptr)); |
| 70 // The certificate is applied to QuicTransportChannel when the local |
| 71 // description is set. |
| 72 rtc::scoped_refptr<rtc::RTCCertificate> channel_local_certificate = |
| 73 channel->GetLocalCertificate(); |
| 74 ASSERT_NE(nullptr, channel_local_certificate); |
| 75 EXPECT_EQ(local_certificate, channel_local_certificate); |
| 76 rtc::scoped_ptr<rtc::SSLFingerprint> remote_fingerprint = |
| 77 CreateFingerprint(CreateCertificate("remote").get()); |
| 78 // NegotiateTransportDescription was not called yet. The SSL role should |
| 79 // not be set and neither should the remote fingerprint. |
| 80 rtc::scoped_ptr<rtc::SSLRole> role(new rtc::SSLRole()); |
| 81 EXPECT_FALSE(channel->GetSslRole(role.get())); |
| 82 // Setting the remote description should set the SSL role. |
| 83 ASSERT_NE(nullptr, remote_fingerprint); |
| 84 TransportDescription remote_desc(std::vector<std::string>(), kIceUfrag2, |
| 85 kIcePwd2, cricket::ICEMODE_FULL, |
| 86 remote_role, remote_fingerprint.get()); |
| 87 ASSERT_TRUE(transport_.SetRemoteTransportDescription( |
| 88 remote_desc, remote_action, nullptr)); |
| 89 ASSERT_TRUE(channel->GetSslRole(role.get())); |
| 90 // SSL role should be client because the remote description is an ANSWER. |
| 91 EXPECT_EQ(expected_ssl_role, *role); |
| 92 } |
| 93 |
| 94 protected: |
| 95 QuicTransport transport_; |
| 96 }; |
| 97 |
| 98 // Test setting the local certificate. |
| 99 TEST_F(QuicTransportTest, SetLocalCertificate) { |
| 100 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate( |
| 101 CreateCertificate("local")); |
| 102 ASSERT_NE(nullptr, local_certificate); |
| 103 rtc::scoped_refptr<rtc::RTCCertificate> transport_local_certificate; |
| 104 EXPECT_FALSE(transport_.GetLocalCertificate(&transport_local_certificate)); |
| 105 transport_.SetLocalCertificate(local_certificate); |
| 106 ASSERT_TRUE(transport_.GetLocalCertificate(&transport_local_certificate)); |
| 107 ASSERT_NE(nullptr, transport_local_certificate); |
| 108 EXPECT_EQ(local_certificate, transport_local_certificate); |
| 109 } |
| 110 |
| 111 // Test setting the ICE role. |
| 112 TEST_F(QuicTransportTest, SetIceRole) { |
| 113 TransportChannelImpl* channel1 = transport_.CreateChannel(1); |
| 114 ASSERT_NE(nullptr, channel1); |
| 115 transport_.SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 116 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, transport_.ice_role()); |
| 117 TransportChannelImpl* channel2 = transport_.CreateChannel(2); |
| 118 ASSERT_NE(nullptr, channel2); |
| 119 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, channel1->GetIceRole()); |
| 120 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, channel2->GetIceRole()); |
| 121 } |
| 122 |
| 123 // Test setting the ICE tie breaker. |
| 124 TEST_F(QuicTransportTest, SetIceTiebreaker) { |
| 125 transport_.SetIceTiebreaker(1u); |
| 126 EXPECT_EQ(1u, transport_.IceTiebreaker()); |
| 127 } |
| 128 |
| 129 // Test setting the local and remote descriptions for a SSL client. |
| 130 TEST_F(QuicTransportTest, SetLocalAndRemoteTransportDescriptionClient) { |
| 131 SetTransportDescription(cricket::CONNECTIONROLE_ACTPASS, |
| 132 cricket::CONNECTIONROLE_PASSIVE, cricket::CA_OFFER, |
| 133 cricket::CA_ANSWER, rtc::SSL_CLIENT); |
| 134 } |
| 135 |
| 136 // Test setting the local and remote descriptions for a SSL server. |
| 137 TEST_F(QuicTransportTest, SetLocalAndRemoteTransportDescriptionServer) { |
| 138 SetTransportDescription(cricket::CONNECTIONROLE_ACTPASS, |
| 139 cricket::CONNECTIONROLE_ACTIVE, cricket::CA_OFFER, |
| 140 cricket::CA_ANSWER, rtc::SSL_SERVER); |
| 141 } |
| 142 |
| 143 // Test creation and destruction of channels. |
| 144 TEST_F(QuicTransportTest, CreateAndDestroyChannels) { |
| 145 TransportChannelImpl* channel1 = transport_.CreateChannel(1); |
| 146 ASSERT_NE(nullptr, channel1); |
| 147 EXPECT_TRUE(transport_.HasChannel(1)); |
| 148 EXPECT_EQ(channel1, transport_.GetChannel(1)); |
| 149 TransportChannelImpl* channel2 = transport_.CreateChannel(2); |
| 150 ASSERT_NE(nullptr, channel2); |
| 151 EXPECT_TRUE(transport_.HasChannel(2)); |
| 152 EXPECT_EQ(channel2, transport_.GetChannel(2)); |
| 153 transport_.DestroyChannel(1); |
| 154 EXPECT_FALSE(transport_.HasChannel(1)); |
| 155 EXPECT_EQ(nullptr, transport_.GetChannel(1)); |
| 156 transport_.DestroyChannel(2); |
| 157 EXPECT_FALSE(transport_.HasChannel(2)); |
| 158 EXPECT_EQ(nullptr, transport_.GetChannel(2)); |
| 159 } |
OLD | NEW |