OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 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 |
11 #include <memory> | 11 #include <memory> |
12 | 12 |
13 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | |
14 #include "webrtc/p2p/base/jseptransport.h" | |
15 #include "webrtc/p2p/base/relayport.h" | |
16 #include "webrtc/p2p/base/stunport.h" | |
17 #include "webrtc/p2p/base/tcpport.h" | |
18 #include "webrtc/p2p/base/testrelayserver.h" | |
19 #include "webrtc/p2p/base/teststunserver.h" | |
20 #include "webrtc/p2p/base/testturnserver.h" | |
21 #include "webrtc/p2p/base/turnport.h" | |
22 #include "webrtc/base/arraysize.h" | 13 #include "webrtc/base/arraysize.h" |
23 #include "webrtc/base/buffer.h" | 14 #include "webrtc/base/buffer.h" |
24 #include "webrtc/base/crc32.h" | 15 #include "webrtc/base/crc32.h" |
25 #include "webrtc/base/gunit.h" | 16 #include "webrtc/base/gunit.h" |
26 #include "webrtc/base/helpers.h" | 17 #include "webrtc/base/helpers.h" |
27 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
28 #include "webrtc/base/natserver.h" | 19 #include "webrtc/base/natserver.h" |
29 #include "webrtc/base/natsocketfactory.h" | 20 #include "webrtc/base/natsocketfactory.h" |
30 #include "webrtc/base/physicalsocketserver.h" | 21 #include "webrtc/base/physicalsocketserver.h" |
| 22 #include "webrtc/base/ptr_util.h" |
31 #include "webrtc/base/socketaddress.h" | 23 #include "webrtc/base/socketaddress.h" |
32 #include "webrtc/base/ssladapter.h" | 24 #include "webrtc/base/ssladapter.h" |
33 #include "webrtc/base/stringutils.h" | 25 #include "webrtc/base/stringutils.h" |
34 #include "webrtc/base/thread.h" | 26 #include "webrtc/base/thread.h" |
35 #include "webrtc/base/virtualsocketserver.h" | 27 #include "webrtc/base/virtualsocketserver.h" |
| 28 #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
| 29 #include "webrtc/p2p/base/jseptransport.h" |
| 30 #include "webrtc/p2p/base/relayport.h" |
| 31 #include "webrtc/p2p/base/stunport.h" |
| 32 #include "webrtc/p2p/base/tcpport.h" |
| 33 #include "webrtc/p2p/base/testrelayserver.h" |
| 34 #include "webrtc/p2p/base/teststunserver.h" |
| 35 #include "webrtc/p2p/base/testturnserver.h" |
| 36 #include "webrtc/p2p/base/turnport.h" |
36 | 37 |
37 using rtc::AsyncPacketSocket; | 38 using rtc::AsyncPacketSocket; |
38 using rtc::Buffer; | 39 using rtc::Buffer; |
39 using rtc::ByteBufferReader; | 40 using rtc::ByteBufferReader; |
40 using rtc::ByteBufferWriter; | 41 using rtc::ByteBufferWriter; |
41 using rtc::NATType; | 42 using rtc::NATType; |
42 using rtc::NAT_OPEN_CONE; | 43 using rtc::NAT_OPEN_CONE; |
43 using rtc::NAT_ADDR_RESTRICTED; | 44 using rtc::NAT_ADDR_RESTRICTED; |
44 using rtc::NAT_PORT_RESTRICTED; | 45 using rtc::NAT_PORT_RESTRICTED; |
45 using rtc::NAT_SYMMETRIC; | 46 using rtc::NAT_SYMMETRIC; |
(...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 IceMessage* CreateStunMessage(int type) { | 754 IceMessage* CreateStunMessage(int type) { |
754 IceMessage* msg = new IceMessage(); | 755 IceMessage* msg = new IceMessage(); |
755 msg->SetType(type); | 756 msg->SetType(type); |
756 msg->SetTransactionID("TESTTESTTEST"); | 757 msg->SetTransactionID("TESTTESTTEST"); |
757 return msg; | 758 return msg; |
758 } | 759 } |
759 IceMessage* CreateStunMessageWithUsername(int type, | 760 IceMessage* CreateStunMessageWithUsername(int type, |
760 const std::string& username) { | 761 const std::string& username) { |
761 IceMessage* msg = CreateStunMessage(type); | 762 IceMessage* msg = CreateStunMessage(type); |
762 msg->AddAttribute( | 763 msg->AddAttribute( |
763 new StunByteStringAttribute(STUN_ATTR_USERNAME, username)); | 764 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USERNAME, username)); |
764 return msg; | 765 return msg; |
765 } | 766 } |
766 TestPort* CreateTestPort(const rtc::SocketAddress& addr, | 767 TestPort* CreateTestPort(const rtc::SocketAddress& addr, |
767 const std::string& username, | 768 const std::string& username, |
768 const std::string& password) { | 769 const std::string& password) { |
769 TestPort* port = new TestPort(main_, "test", &socket_factory_, &network_, | 770 TestPort* port = new TestPort(main_, "test", &socket_factory_, &network_, |
770 addr.ipaddr(), 0, 0, username, password); | 771 addr.ipaddr(), 0, 0, username, password); |
771 port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); | 772 port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); |
772 return port; | 773 return port; |
773 } | 774 } |
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1396 Port::ORIGIN_MESSAGE); | 1397 Port::ORIGIN_MESSAGE); |
1397 conn1->Ping(0); | 1398 conn1->Ping(0); |
1398 | 1399 |
1399 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); | 1400 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
1400 msg = lport->last_stun_msg(); | 1401 msg = lport->last_stun_msg(); |
1401 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); | 1402 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
1402 std::unique_ptr<IceMessage> modified_req( | 1403 std::unique_ptr<IceMessage> modified_req( |
1403 CreateStunMessage(STUN_BINDING_REQUEST)); | 1404 CreateStunMessage(STUN_BINDING_REQUEST)); |
1404 const StunByteStringAttribute* username_attr = msg->GetByteString( | 1405 const StunByteStringAttribute* username_attr = msg->GetByteString( |
1405 STUN_ATTR_USERNAME); | 1406 STUN_ATTR_USERNAME); |
1406 modified_req->AddAttribute(new StunByteStringAttribute( | 1407 modified_req->AddAttribute(rtc::MakeUnique<StunByteStringAttribute>( |
1407 STUN_ATTR_USERNAME, username_attr->GetString())); | 1408 STUN_ATTR_USERNAME, username_attr->GetString())); |
1408 // To make sure we receive error response, adding tiebreaker less than | 1409 // To make sure we receive error response, adding tiebreaker less than |
1409 // what's present in request. | 1410 // what's present in request. |
1410 modified_req->AddAttribute(new StunUInt64Attribute( | 1411 modified_req->AddAttribute(rtc::MakeUnique<StunUInt64Attribute>( |
1411 STUN_ATTR_ICE_CONTROLLING, kTiebreaker1 - 1)); | 1412 STUN_ATTR_ICE_CONTROLLING, kTiebreaker1 - 1)); |
1412 modified_req->AddMessageIntegrity("lpass"); | 1413 modified_req->AddMessageIntegrity("lpass"); |
1413 modified_req->AddFingerprint(); | 1414 modified_req->AddFingerprint(); |
1414 | 1415 |
1415 lport->Reset(); | 1416 lport->Reset(); |
1416 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); | 1417 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); |
1417 WriteStunMessage(modified_req.get(), buf.get()); | 1418 WriteStunMessage(modified_req.get(), buf.get()); |
1418 conn1->OnReadPacket(buf->Data(), buf->Length(), rtc::PacketTime()); | 1419 conn1->OnReadPacket(buf->Data(), buf->Length(), rtc::PacketTime()); |
1419 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); | 1420 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
1420 msg = lport->last_stun_msg(); | 1421 msg = lport->last_stun_msg(); |
(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2053 in_msg->AddMessageIntegrity("rpass"); | 2054 in_msg->AddMessageIntegrity("rpass"); |
2054 in_msg->AddFingerprint(); | 2055 in_msg->AddFingerprint(); |
2055 WriteStunMessage(in_msg.get(), buf.get()); | 2056 WriteStunMessage(in_msg.get(), buf.get()); |
2056 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, | 2057 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
2057 &username)); | 2058 &username)); |
2058 EXPECT_TRUE(out_msg.get() != NULL); | 2059 EXPECT_TRUE(out_msg.get() != NULL); |
2059 EXPECT_EQ("lfrag", username); | 2060 EXPECT_EQ("lfrag", username); |
2060 | 2061 |
2061 // BINDING-RESPONSE without username, with MESSAGE-INTEGRITY and FINGERPRINT. | 2062 // BINDING-RESPONSE without username, with MESSAGE-INTEGRITY and FINGERPRINT. |
2062 in_msg.reset(CreateStunMessage(STUN_BINDING_RESPONSE)); | 2063 in_msg.reset(CreateStunMessage(STUN_BINDING_RESPONSE)); |
2063 in_msg->AddAttribute( | 2064 in_msg->AddAttribute(rtc::MakeUnique<StunXorAddressAttribute>( |
2064 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); | 2065 STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); |
2065 in_msg->AddMessageIntegrity("rpass"); | 2066 in_msg->AddMessageIntegrity("rpass"); |
2066 in_msg->AddFingerprint(); | 2067 in_msg->AddFingerprint(); |
2067 WriteStunMessage(in_msg.get(), buf.get()); | 2068 WriteStunMessage(in_msg.get(), buf.get()); |
2068 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, | 2069 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
2069 &username)); | 2070 &username)); |
2070 EXPECT_TRUE(out_msg.get() != NULL); | 2071 EXPECT_TRUE(out_msg.get() != NULL); |
2071 EXPECT_EQ("", username); | 2072 EXPECT_EQ("", username); |
2072 | 2073 |
2073 // BINDING-ERROR-RESPONSE without username, with error, M-I, and FINGERPRINT. | 2074 // BINDING-ERROR-RESPONSE without username, with error, M-I, and FINGERPRINT. |
2074 in_msg.reset(CreateStunMessage(STUN_BINDING_ERROR_RESPONSE)); | 2075 in_msg.reset(CreateStunMessage(STUN_BINDING_ERROR_RESPONSE)); |
2075 in_msg->AddAttribute(new StunErrorCodeAttribute(STUN_ATTR_ERROR_CODE, | 2076 in_msg->AddAttribute(rtc::MakeUnique<StunErrorCodeAttribute>( |
2076 STUN_ERROR_SERVER_ERROR, STUN_ERROR_REASON_SERVER_ERROR)); | 2077 STUN_ATTR_ERROR_CODE, STUN_ERROR_SERVER_ERROR, |
| 2078 STUN_ERROR_REASON_SERVER_ERROR)); |
2077 in_msg->AddFingerprint(); | 2079 in_msg->AddFingerprint(); |
2078 WriteStunMessage(in_msg.get(), buf.get()); | 2080 WriteStunMessage(in_msg.get(), buf.get()); |
2079 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, | 2081 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
2080 &username)); | 2082 &username)); |
2081 EXPECT_TRUE(out_msg.get() != NULL); | 2083 EXPECT_TRUE(out_msg.get() != NULL); |
2082 EXPECT_EQ("", username); | 2084 EXPECT_EQ("", username); |
2083 ASSERT_TRUE(out_msg->GetErrorCode() != NULL); | 2085 ASSERT_TRUE(out_msg->GetErrorCode() != NULL); |
2084 EXPECT_EQ(STUN_ERROR_SERVER_ERROR, out_msg->GetErrorCode()->code()); | 2086 EXPECT_EQ(STUN_ERROR_SERVER_ERROR, out_msg->GetErrorCode()->code()); |
2085 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), | 2087 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), |
2086 out_msg->GetErrorCode()->reason()); | 2088 out_msg->GetErrorCode()->reason()); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2216 // Now, add a fingerprint, but munge the message so it's not valid. | 2218 // Now, add a fingerprint, but munge the message so it's not valid. |
2217 in_msg->AddFingerprint(); | 2219 in_msg->AddFingerprint(); |
2218 in_msg->SetTransactionID("TESTTESTBADD"); | 2220 in_msg->SetTransactionID("TESTTESTBADD"); |
2219 WriteStunMessage(in_msg.get(), buf.get()); | 2221 WriteStunMessage(in_msg.get(), buf.get()); |
2220 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, | 2222 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
2221 &username)); | 2223 &username)); |
2222 EXPECT_EQ(0, port->last_stun_error_code()); | 2224 EXPECT_EQ(0, port->last_stun_error_code()); |
2223 | 2225 |
2224 // Valid BINDING-RESPONSE, except no FINGERPRINT. | 2226 // Valid BINDING-RESPONSE, except no FINGERPRINT. |
2225 in_msg.reset(CreateStunMessage(STUN_BINDING_RESPONSE)); | 2227 in_msg.reset(CreateStunMessage(STUN_BINDING_RESPONSE)); |
2226 in_msg->AddAttribute( | 2228 in_msg->AddAttribute(rtc::MakeUnique<StunXorAddressAttribute>( |
2227 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); | 2229 STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); |
2228 in_msg->AddMessageIntegrity("rpass"); | 2230 in_msg->AddMessageIntegrity("rpass"); |
2229 WriteStunMessage(in_msg.get(), buf.get()); | 2231 WriteStunMessage(in_msg.get(), buf.get()); |
2230 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, | 2232 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
2231 &username)); | 2233 &username)); |
2232 EXPECT_EQ(0, port->last_stun_error_code()); | 2234 EXPECT_EQ(0, port->last_stun_error_code()); |
2233 | 2235 |
2234 // Now, add a fingerprint, but munge the message so it's not valid. | 2236 // Now, add a fingerprint, but munge the message so it's not valid. |
2235 in_msg->AddFingerprint(); | 2237 in_msg->AddFingerprint(); |
2236 in_msg->SetTransactionID("TESTTESTBADD"); | 2238 in_msg->SetTransactionID("TESTTESTBADD"); |
2237 WriteStunMessage(in_msg.get(), buf.get()); | 2239 WriteStunMessage(in_msg.get(), buf.get()); |
2238 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, | 2240 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
2239 &username)); | 2241 &username)); |
2240 EXPECT_EQ(0, port->last_stun_error_code()); | 2242 EXPECT_EQ(0, port->last_stun_error_code()); |
2241 | 2243 |
2242 // Valid BINDING-ERROR-RESPONSE, except no FINGERPRINT. | 2244 // Valid BINDING-ERROR-RESPONSE, except no FINGERPRINT. |
2243 in_msg.reset(CreateStunMessage(STUN_BINDING_ERROR_RESPONSE)); | 2245 in_msg.reset(CreateStunMessage(STUN_BINDING_ERROR_RESPONSE)); |
2244 in_msg->AddAttribute(new StunErrorCodeAttribute(STUN_ATTR_ERROR_CODE, | 2246 in_msg->AddAttribute(rtc::MakeUnique<StunErrorCodeAttribute>( |
2245 STUN_ERROR_SERVER_ERROR, STUN_ERROR_REASON_SERVER_ERROR)); | 2247 STUN_ATTR_ERROR_CODE, STUN_ERROR_SERVER_ERROR, |
| 2248 STUN_ERROR_REASON_SERVER_ERROR)); |
2246 in_msg->AddMessageIntegrity("rpass"); | 2249 in_msg->AddMessageIntegrity("rpass"); |
2247 WriteStunMessage(in_msg.get(), buf.get()); | 2250 WriteStunMessage(in_msg.get(), buf.get()); |
2248 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, | 2251 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
2249 &username)); | 2252 &username)); |
2250 EXPECT_EQ(0, port->last_stun_error_code()); | 2253 EXPECT_EQ(0, port->last_stun_error_code()); |
2251 | 2254 |
2252 // Now, add a fingerprint, but munge the message so it's not valid. | 2255 // Now, add a fingerprint, but munge the message so it's not valid. |
2253 in_msg->AddFingerprint(); | 2256 in_msg->AddFingerprint(); |
2254 in_msg->SetTransactionID("TESTTESTBADD"); | 2257 in_msg->SetTransactionID("TESTTESTBADD"); |
2255 WriteStunMessage(in_msg.get(), buf.get()); | 2258 WriteStunMessage(in_msg.get(), buf.get()); |
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2878 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); | 2881 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); |
2879 EXPECT_NE(conn1, conn2); | 2882 EXPECT_NE(conn1, conn2); |
2880 conn_in_use = port->GetConnection(address); | 2883 conn_in_use = port->GetConnection(address); |
2881 EXPECT_EQ(conn2, conn_in_use); | 2884 EXPECT_EQ(conn2, conn_in_use); |
2882 EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); | 2885 EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); |
2883 | 2886 |
2884 // Make sure the new connection was not deleted. | 2887 // Make sure the new connection was not deleted. |
2885 rtc::Thread::Current()->ProcessMessages(300); | 2888 rtc::Thread::Current()->ProcessMessages(300); |
2886 EXPECT_TRUE(port->GetConnection(address) != nullptr); | 2889 EXPECT_TRUE(port->GetConnection(address) != nullptr); |
2887 } | 2890 } |
OLD | NEW |