henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 14 | #include "webrtc/p2p/base/relayport.h" |
| 15 | #include "webrtc/p2p/base/stunport.h" |
| 16 | #include "webrtc/p2p/base/tcpport.h" |
| 17 | #include "webrtc/p2p/base/testrelayserver.h" |
| 18 | #include "webrtc/p2p/base/teststunserver.h" |
| 19 | #include "webrtc/p2p/base/testturnserver.h" |
| 20 | #include "webrtc/p2p/base/transport.h" |
| 21 | #include "webrtc/p2p/base/turnport.h" |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 22 | #include "webrtc/base/arraysize.h" |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 23 | #include "webrtc/base/buffer.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 24 | #include "webrtc/base/crc32.h" |
| 25 | #include "webrtc/base/gunit.h" |
| 26 | #include "webrtc/base/helpers.h" |
| 27 | #include "webrtc/base/logging.h" |
| 28 | #include "webrtc/base/natserver.h" |
| 29 | #include "webrtc/base/natsocketfactory.h" |
| 30 | #include "webrtc/base/physicalsocketserver.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 31 | #include "webrtc/base/socketaddress.h" |
| 32 | #include "webrtc/base/ssladapter.h" |
| 33 | #include "webrtc/base/stringutils.h" |
| 34 | #include "webrtc/base/thread.h" |
| 35 | #include "webrtc/base/virtualsocketserver.h" |
| 36 | |
| 37 | using rtc::AsyncPacketSocket; |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 38 | using rtc::Buffer; |
| 39 | using rtc::ByteBufferReader; |
| 40 | using rtc::ByteBufferWriter; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 41 | using rtc::NATType; |
| 42 | using rtc::NAT_OPEN_CONE; |
| 43 | using rtc::NAT_ADDR_RESTRICTED; |
| 44 | using rtc::NAT_PORT_RESTRICTED; |
| 45 | using rtc::NAT_SYMMETRIC; |
| 46 | using rtc::PacketSocketFactory; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 47 | using rtc::Socket; |
| 48 | using rtc::SocketAddress; |
| 49 | using namespace cricket; |
| 50 | |
| 51 | static const int kTimeout = 1000; |
| 52 | static const SocketAddress kLocalAddr1("192.168.1.2", 0); |
| 53 | static const SocketAddress kLocalAddr2("192.168.1.3", 0); |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 54 | static const SocketAddress kNatAddr1("77.77.77.77", rtc::NAT_SERVER_UDP_PORT); |
| 55 | static const SocketAddress kNatAddr2("88.88.88.88", rtc::NAT_SERVER_UDP_PORT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 56 | static const SocketAddress kStunAddr("99.99.99.1", STUN_SERVER_PORT); |
| 57 | static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000); |
| 58 | static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001); |
| 59 | static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002); |
| 60 | static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003); |
| 61 | static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004); |
| 62 | static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005); |
| 63 | static const SocketAddress kTurnUdpIntAddr("99.99.99.4", STUN_SERVER_PORT); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 64 | static const SocketAddress kTurnTcpIntAddr("99.99.99.4", 5010); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 65 | static const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0); |
| 66 | static const RelayCredentials kRelayCredentials("test", "test"); |
| 67 | |
| 68 | // TODO: Update these when RFC5245 is completely supported. |
| 69 | // Magic value of 30 is from RFC3484, for IPv4 addresses. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 70 | static const uint32_t kDefaultPrflxPriority = |
| 71 | ICE_TYPE_PREFERENCE_PRFLX << 24 | 30 << 8 | |
| 72 | (256 - ICE_CANDIDATE_COMPONENT_DEFAULT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 73 | |
| 74 | static const int kTiebreaker1 = 11111; |
| 75 | static const int kTiebreaker2 = 22222; |
| 76 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 77 | static const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
| 78 | |
zhihuang | 6d0d4bf | 2016-05-24 10:13:32 -0700 | [diff] [blame] | 79 | static const int kGturnUserNameLength = 16; |
| 80 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 81 | static Candidate GetCandidate(Port* port) { |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 82 | assert(port->Candidates().size() >= 1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 83 | return port->Candidates()[0]; |
| 84 | } |
| 85 | |
| 86 | static SocketAddress GetAddress(Port* port) { |
| 87 | return GetCandidate(port).address(); |
| 88 | } |
| 89 | |
| 90 | static IceMessage* CopyStunMessage(const IceMessage* src) { |
| 91 | IceMessage* dst = new IceMessage(); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 92 | ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 93 | src->Write(&buf); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 94 | ByteBufferReader read_buf(buf); |
| 95 | dst->Read(&read_buf); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 96 | return dst; |
| 97 | } |
| 98 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 99 | static bool WriteStunMessage(const StunMessage* msg, ByteBufferWriter* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 100 | buf->Resize(0); // clear out any existing buffer contents |
| 101 | return msg->Write(buf); |
| 102 | } |
| 103 | |
| 104 | // Stub port class for testing STUN generation and processing. |
| 105 | class TestPort : public Port { |
| 106 | public: |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 107 | TestPort(rtc::Thread* thread, |
| 108 | const std::string& type, |
| 109 | rtc::PacketSocketFactory* factory, |
| 110 | rtc::Network* network, |
| 111 | const rtc::IPAddress& ip, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 112 | uint16_t min_port, |
| 113 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 114 | const std::string& username_fragment, |
| 115 | const std::string& password) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 116 | : Port(thread, |
| 117 | type, |
| 118 | factory, |
| 119 | network, |
| 120 | ip, |
| 121 | min_port, |
| 122 | max_port, |
| 123 | username_fragment, |
| 124 | password) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 125 | ~TestPort() {} |
| 126 | |
| 127 | // Expose GetStunMessage so that we can test it. |
| 128 | using cricket::Port::GetStunMessage; |
| 129 | |
| 130 | // The last StunMessage that was sent on this Port. |
| 131 | // TODO: Make these const; requires changes to SendXXXXResponse. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 132 | Buffer* last_stun_buf() { return last_stun_buf_.get(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 133 | IceMessage* last_stun_msg() { return last_stun_msg_.get(); } |
| 134 | int last_stun_error_code() { |
| 135 | int code = 0; |
| 136 | if (last_stun_msg_) { |
| 137 | const StunErrorCodeAttribute* error_attr = last_stun_msg_->GetErrorCode(); |
| 138 | if (error_attr) { |
| 139 | code = error_attr->code(); |
| 140 | } |
| 141 | } |
| 142 | return code; |
| 143 | } |
| 144 | |
| 145 | virtual void PrepareAddress() { |
| 146 | rtc::SocketAddress addr(ip(), min_port()); |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 147 | AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 148 | ICE_TYPE_PREFERENCE_HOST, 0, true); |
| 149 | } |
| 150 | |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 151 | virtual bool SupportsProtocol(const std::string& protocol) const { |
| 152 | return true; |
| 153 | } |
| 154 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 155 | // Exposed for testing candidate building. |
| 156 | void AddCandidateAddress(const rtc::SocketAddress& addr) { |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 157 | AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 158 | type_preference_, 0, false); |
| 159 | } |
| 160 | void AddCandidateAddress(const rtc::SocketAddress& addr, |
| 161 | const rtc::SocketAddress& base_address, |
| 162 | const std::string& type, |
| 163 | int type_preference, |
| 164 | bool final) { |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 165 | AddAddress(addr, base_address, rtc::SocketAddress(), "udp", "", "", type, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 166 | type_preference, 0, final); |
| 167 | } |
| 168 | |
| 169 | virtual Connection* CreateConnection(const Candidate& remote_candidate, |
| 170 | CandidateOrigin origin) { |
| 171 | Connection* conn = new ProxyConnection(this, 0, remote_candidate); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame^] | 172 | AddOrReplaceConnection(conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 173 | // Set use-candidate attribute flag as this will add USE-CANDIDATE attribute |
| 174 | // in STUN binding requests. |
| 175 | conn->set_use_candidate_attr(true); |
| 176 | return conn; |
| 177 | } |
| 178 | virtual int SendTo( |
| 179 | const void* data, size_t size, const rtc::SocketAddress& addr, |
| 180 | const rtc::PacketOptions& options, bool payload) { |
| 181 | if (!payload) { |
| 182 | IceMessage* msg = new IceMessage; |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 183 | Buffer* buf = new Buffer(static_cast<const char*>(data), size); |
| 184 | ByteBufferReader read_buf(*buf); |
| 185 | if (!msg->Read(&read_buf)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 186 | delete msg; |
| 187 | delete buf; |
| 188 | return -1; |
| 189 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 190 | last_stun_buf_.reset(buf); |
| 191 | last_stun_msg_.reset(msg); |
| 192 | } |
| 193 | return static_cast<int>(size); |
| 194 | } |
| 195 | virtual int SetOption(rtc::Socket::Option opt, int value) { |
| 196 | return 0; |
| 197 | } |
| 198 | virtual int GetOption(rtc::Socket::Option opt, int* value) { |
| 199 | return -1; |
| 200 | } |
| 201 | virtual int GetError() { |
| 202 | return 0; |
| 203 | } |
| 204 | void Reset() { |
| 205 | last_stun_buf_.reset(); |
| 206 | last_stun_msg_.reset(); |
| 207 | } |
| 208 | void set_type_preference(int type_preference) { |
| 209 | type_preference_ = type_preference; |
| 210 | } |
| 211 | |
| 212 | private: |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 213 | void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 214 | const rtc::SentPacket& sent_packet) { |
| 215 | PortInterface::SignalSentPacket(sent_packet); |
| 216 | } |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 217 | std::unique_ptr<Buffer> last_stun_buf_; |
| 218 | std::unique_ptr<IceMessage> last_stun_msg_; |
pbos | 7640ffa | 2015-11-30 09:16:59 -0800 | [diff] [blame] | 219 | int type_preference_ = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | class TestChannel : public sigslot::has_slots<> { |
| 223 | public: |
| 224 | // Takes ownership of |p1| (but not |p2|). |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 225 | TestChannel(Port* p1) |
| 226 | : ice_mode_(ICEMODE_FULL), |
| 227 | port_(p1), |
| 228 | complete_count_(0), |
| 229 | conn_(NULL), |
| 230 | remote_request_(), |
| 231 | nominated_(false) { |
| 232 | port_->SignalPortComplete.connect(this, &TestChannel::OnPortComplete); |
| 233 | port_->SignalUnknownAddress.connect(this, &TestChannel::OnUnknownAddress); |
| 234 | port_->SignalDestroyed.connect(this, &TestChannel::OnSrcPortDestroyed); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | int complete_count() { return complete_count_; } |
| 238 | Connection* conn() { return conn_; } |
| 239 | const SocketAddress& remote_address() { return remote_address_; } |
| 240 | const std::string remote_fragment() { return remote_frag_; } |
| 241 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 242 | void Start() { port_->PrepareAddress(); } |
| 243 | void CreateConnection(const Candidate& remote_candidate) { |
| 244 | conn_ = port_->CreateConnection(remote_candidate, Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 245 | IceMode remote_ice_mode = |
| 246 | (ice_mode_ == ICEMODE_FULL) ? ICEMODE_LITE : ICEMODE_FULL; |
| 247 | conn_->set_remote_ice_mode(remote_ice_mode); |
| 248 | conn_->set_use_candidate_attr(remote_ice_mode == ICEMODE_FULL); |
| 249 | conn_->SignalStateChange.connect( |
| 250 | this, &TestChannel::OnConnectionStateChange); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 251 | conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed); |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 252 | conn_->SignalReadyToSend.connect(this, |
| 253 | &TestChannel::OnConnectionReadyToSend); |
| 254 | connection_ready_to_send_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 255 | } |
| 256 | void OnConnectionStateChange(Connection* conn) { |
| 257 | if (conn->write_state() == Connection::STATE_WRITABLE) { |
| 258 | conn->set_use_candidate_attr(true); |
| 259 | nominated_ = true; |
| 260 | } |
| 261 | } |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 262 | void AcceptConnection(const Candidate& remote_candidate) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 263 | ASSERT_TRUE(remote_request_.get() != NULL); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 264 | Candidate c = remote_candidate; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 265 | c.set_address(remote_address_); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 266 | conn_ = port_->CreateConnection(c, Port::ORIGIN_MESSAGE); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 267 | conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 268 | port_->SendBindingResponse(remote_request_.get(), remote_address_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 269 | remote_request_.reset(); |
| 270 | } |
| 271 | void Ping() { |
| 272 | Ping(0); |
| 273 | } |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 274 | void Ping(int64_t now) { conn_->Ping(now); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 275 | void Stop() { |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 276 | if (conn_) { |
| 277 | conn_->Destroy(); |
| 278 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | void OnPortComplete(Port* port) { |
| 282 | complete_count_++; |
| 283 | } |
| 284 | void SetIceMode(IceMode ice_mode) { |
| 285 | ice_mode_ = ice_mode; |
| 286 | } |
| 287 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 288 | int SendData(const char* data, size_t len) { |
| 289 | rtc::PacketOptions options; |
| 290 | return conn_->Send(data, len, options); |
| 291 | } |
| 292 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 293 | void OnUnknownAddress(PortInterface* port, const SocketAddress& addr, |
| 294 | ProtocolType proto, |
| 295 | IceMessage* msg, const std::string& rf, |
| 296 | bool /*port_muxed*/) { |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 297 | ASSERT_EQ(port_.get(), port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 298 | if (!remote_address_.IsNil()) { |
| 299 | ASSERT_EQ(remote_address_, addr); |
| 300 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 301 | const cricket::StunUInt32Attribute* priority_attr = |
| 302 | msg->GetUInt32(STUN_ATTR_PRIORITY); |
| 303 | const cricket::StunByteStringAttribute* mi_attr = |
| 304 | msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); |
| 305 | const cricket::StunUInt32Attribute* fingerprint_attr = |
| 306 | msg->GetUInt32(STUN_ATTR_FINGERPRINT); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 307 | EXPECT_TRUE(priority_attr != NULL); |
| 308 | EXPECT_TRUE(mi_attr != NULL); |
| 309 | EXPECT_TRUE(fingerprint_attr != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 310 | remote_address_ = addr; |
| 311 | remote_request_.reset(CopyStunMessage(msg)); |
| 312 | remote_frag_ = rf; |
| 313 | } |
| 314 | |
| 315 | void OnDestroyed(Connection* conn) { |
| 316 | ASSERT_EQ(conn_, conn); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 317 | LOG(INFO) << "OnDestroy connection " << conn << " deleted"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 318 | conn_ = NULL; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 319 | // When the connection is destroyed, also clear these fields so future |
| 320 | // connections are possible. |
| 321 | remote_request_.reset(); |
| 322 | remote_address_.Clear(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void OnSrcPortDestroyed(PortInterface* port) { |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 326 | Port* destroyed_src = port_.release(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 327 | ASSERT_EQ(destroyed_src, port); |
| 328 | } |
| 329 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 330 | Port* port() { return port_.get(); } |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 331 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 332 | bool nominated() const { return nominated_; } |
| 333 | |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 334 | void set_connection_ready_to_send(bool ready) { |
| 335 | connection_ready_to_send_ = ready; |
| 336 | } |
| 337 | bool connection_ready_to_send() const { |
| 338 | return connection_ready_to_send_; |
| 339 | } |
| 340 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 341 | private: |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 342 | // ReadyToSend will only issue after a Connection recovers from EWOULDBLOCK. |
| 343 | void OnConnectionReadyToSend(Connection* conn) { |
| 344 | ASSERT_EQ(conn, conn_); |
| 345 | connection_ready_to_send_ = true; |
| 346 | } |
| 347 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 348 | IceMode ice_mode_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 349 | std::unique_ptr<Port> port_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 350 | |
| 351 | int complete_count_; |
| 352 | Connection* conn_; |
| 353 | SocketAddress remote_address_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 354 | std::unique_ptr<StunMessage> remote_request_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 355 | std::string remote_frag_; |
| 356 | bool nominated_; |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 357 | bool connection_ready_to_send_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 358 | }; |
| 359 | |
| 360 | class PortTest : public testing::Test, public sigslot::has_slots<> { |
| 361 | public: |
| 362 | PortTest() |
| 363 | : main_(rtc::Thread::Current()), |
| 364 | pss_(new rtc::PhysicalSocketServer), |
| 365 | ss_(new rtc::VirtualSocketServer(pss_.get())), |
| 366 | ss_scope_(ss_.get()), |
| 367 | network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32), |
| 368 | socket_factory_(rtc::Thread::Current()), |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 369 | nat_factory1_(ss_.get(), kNatAddr1, SocketAddress()), |
| 370 | nat_factory2_(ss_.get(), kNatAddr2, SocketAddress()), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 371 | nat_socket_factory1_(&nat_factory1_), |
| 372 | nat_socket_factory2_(&nat_factory2_), |
| 373 | stun_server_(TestStunServer::Create(main_, kStunAddr)), |
| 374 | turn_server_(main_, kTurnUdpIntAddr, kTurnUdpExtAddr), |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 375 | relay_server_(main_, |
| 376 | kRelayUdpIntAddr, |
| 377 | kRelayUdpExtAddr, |
| 378 | kRelayTcpIntAddr, |
| 379 | kRelayTcpExtAddr, |
| 380 | kRelaySslTcpIntAddr, |
| 381 | kRelaySslTcpExtAddr), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 382 | username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), |
| 383 | password_(rtc::CreateRandomString(ICE_PWD_LENGTH)), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 384 | role_conflict_(false), |
| 385 | destroyed_(false) { |
| 386 | network_.AddIP(rtc::IPAddress(INADDR_ANY)); |
| 387 | } |
| 388 | |
| 389 | protected: |
| 390 | void TestLocalToLocal() { |
| 391 | Port* port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 392 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 393 | Port* port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 394 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 395 | TestConnectivity("udp", port1, "udp", port2, true, true, true, true); |
| 396 | } |
| 397 | void TestLocalToStun(NATType ntype) { |
| 398 | Port* port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 399 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 400 | nat_server2_.reset(CreateNatServer(kNatAddr2, ntype)); |
| 401 | Port* port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 402 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 403 | TestConnectivity("udp", port1, StunName(ntype), port2, |
| 404 | ntype == NAT_OPEN_CONE, true, |
| 405 | ntype != NAT_SYMMETRIC, true); |
| 406 | } |
| 407 | void TestLocalToRelay(RelayType rtype, ProtocolType proto) { |
| 408 | Port* port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 409 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 410 | Port* port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_UDP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 411 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 412 | TestConnectivity("udp", port1, RelayName(rtype, proto), port2, |
| 413 | rtype == RELAY_GTURN, true, true, true); |
| 414 | } |
| 415 | void TestStunToLocal(NATType ntype) { |
| 416 | nat_server1_.reset(CreateNatServer(kNatAddr1, ntype)); |
| 417 | Port* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 418 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 419 | Port* port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 420 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 421 | TestConnectivity(StunName(ntype), port1, "udp", port2, |
| 422 | true, ntype != NAT_SYMMETRIC, true, true); |
| 423 | } |
| 424 | void TestStunToStun(NATType ntype1, NATType ntype2) { |
| 425 | nat_server1_.reset(CreateNatServer(kNatAddr1, ntype1)); |
| 426 | Port* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 427 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 428 | nat_server2_.reset(CreateNatServer(kNatAddr2, ntype2)); |
| 429 | Port* port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 430 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 431 | TestConnectivity(StunName(ntype1), port1, StunName(ntype2), port2, |
| 432 | ntype2 == NAT_OPEN_CONE, |
| 433 | ntype1 != NAT_SYMMETRIC, ntype2 != NAT_SYMMETRIC, |
| 434 | ntype1 + ntype2 < (NAT_PORT_RESTRICTED + NAT_SYMMETRIC)); |
| 435 | } |
| 436 | void TestStunToRelay(NATType ntype, RelayType rtype, ProtocolType proto) { |
| 437 | nat_server1_.reset(CreateNatServer(kNatAddr1, ntype)); |
| 438 | Port* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 439 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 440 | Port* port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_UDP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 441 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 442 | TestConnectivity(StunName(ntype), port1, RelayName(rtype, proto), port2, |
| 443 | rtype == RELAY_GTURN, ntype != NAT_SYMMETRIC, true, true); |
| 444 | } |
| 445 | void TestTcpToTcp() { |
| 446 | Port* port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 447 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 448 | Port* port2 = CreateTcpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 449 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 450 | TestConnectivity("tcp", port1, "tcp", port2, true, false, true, true); |
| 451 | } |
| 452 | void TestTcpToRelay(RelayType rtype, ProtocolType proto) { |
| 453 | Port* port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 454 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 455 | Port* port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_TCP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 456 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 457 | TestConnectivity("tcp", port1, RelayName(rtype, proto), port2, |
| 458 | rtype == RELAY_GTURN, false, true, true); |
| 459 | } |
| 460 | void TestSslTcpToRelay(RelayType rtype, ProtocolType proto) { |
| 461 | Port* port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 462 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 463 | Port* port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_SSLTCP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 464 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 465 | TestConnectivity("ssltcp", port1, RelayName(rtype, proto), port2, |
| 466 | rtype == RELAY_GTURN, false, true, true); |
| 467 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 468 | // helpers for above functions |
| 469 | UDPPort* CreateUdpPort(const SocketAddress& addr) { |
| 470 | return CreateUdpPort(addr, &socket_factory_); |
| 471 | } |
| 472 | UDPPort* CreateUdpPort(const SocketAddress& addr, |
| 473 | PacketSocketFactory* socket_factory) { |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 474 | return UDPPort::Create(main_, socket_factory, &network_, addr.ipaddr(), 0, |
| 475 | 0, username_, password_, std::string(), true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 476 | } |
| 477 | TCPPort* CreateTcpPort(const SocketAddress& addr) { |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 478 | return CreateTcpPort(addr, &socket_factory_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 479 | } |
| 480 | TCPPort* CreateTcpPort(const SocketAddress& addr, |
| 481 | PacketSocketFactory* socket_factory) { |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 482 | return TCPPort::Create(main_, socket_factory, &network_, |
| 483 | addr.ipaddr(), 0, 0, username_, password_, |
| 484 | true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 485 | } |
| 486 | StunPort* CreateStunPort(const SocketAddress& addr, |
| 487 | rtc::PacketSocketFactory* factory) { |
| 488 | ServerAddresses stun_servers; |
| 489 | stun_servers.insert(kStunAddr); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 490 | return StunPort::Create(main_, factory, &network_, |
| 491 | addr.ipaddr(), 0, 0, |
| 492 | username_, password_, stun_servers, |
| 493 | std::string()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 494 | } |
| 495 | Port* CreateRelayPort(const SocketAddress& addr, RelayType rtype, |
| 496 | ProtocolType int_proto, ProtocolType ext_proto) { |
| 497 | if (rtype == RELAY_TURN) { |
| 498 | return CreateTurnPort(addr, &socket_factory_, int_proto, ext_proto); |
| 499 | } else { |
| 500 | return CreateGturnPort(addr, int_proto, ext_proto); |
| 501 | } |
| 502 | } |
| 503 | TurnPort* CreateTurnPort(const SocketAddress& addr, |
| 504 | PacketSocketFactory* socket_factory, |
| 505 | ProtocolType int_proto, ProtocolType ext_proto) { |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 506 | SocketAddress server_addr = |
| 507 | int_proto == PROTO_TCP ? kTurnTcpIntAddr : kTurnUdpIntAddr; |
| 508 | return CreateTurnPort(addr, socket_factory, int_proto, ext_proto, |
| 509 | server_addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 510 | } |
| 511 | TurnPort* CreateTurnPort(const SocketAddress& addr, |
| 512 | PacketSocketFactory* socket_factory, |
| 513 | ProtocolType int_proto, ProtocolType ext_proto, |
| 514 | const rtc::SocketAddress& server_addr) { |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 515 | return TurnPort::Create(main_, socket_factory, &network_, addr.ipaddr(), 0, |
| 516 | 0, username_, password_, |
| 517 | ProtocolAddress(server_addr, int_proto), |
| 518 | kRelayCredentials, 0, std::string()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 519 | } |
| 520 | RelayPort* CreateGturnPort(const SocketAddress& addr, |
| 521 | ProtocolType int_proto, ProtocolType ext_proto) { |
| 522 | RelayPort* port = CreateGturnPort(addr); |
| 523 | SocketAddress addrs[] = |
| 524 | { kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr }; |
| 525 | port->AddServerAddress(ProtocolAddress(addrs[int_proto], int_proto)); |
| 526 | return port; |
| 527 | } |
| 528 | RelayPort* CreateGturnPort(const SocketAddress& addr) { |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 529 | // TODO(pthatcher): Remove GTURN. |
zhihuang | 6d0d4bf | 2016-05-24 10:13:32 -0700 | [diff] [blame] | 530 | // Generate a username with length of 16 for Gturn only. |
| 531 | std::string username = rtc::CreateRandomString(kGturnUserNameLength); |
| 532 | return RelayPort::Create(main_, &socket_factory_, &network_, addr.ipaddr(), |
| 533 | 0, 0, username, password_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 534 | // TODO: Add an external address for ext_proto, so that the |
| 535 | // other side can connect to this port using a non-UDP protocol. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 536 | } |
| 537 | rtc::NATServer* CreateNatServer(const SocketAddress& addr, |
| 538 | rtc::NATType type) { |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 539 | return new rtc::NATServer(type, ss_.get(), addr, addr, ss_.get(), addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 540 | } |
| 541 | static const char* StunName(NATType type) { |
| 542 | switch (type) { |
| 543 | case NAT_OPEN_CONE: return "stun(open cone)"; |
| 544 | case NAT_ADDR_RESTRICTED: return "stun(addr restricted)"; |
| 545 | case NAT_PORT_RESTRICTED: return "stun(port restricted)"; |
| 546 | case NAT_SYMMETRIC: return "stun(symmetric)"; |
| 547 | default: return "stun(?)"; |
| 548 | } |
| 549 | } |
| 550 | static const char* RelayName(RelayType type, ProtocolType proto) { |
| 551 | if (type == RELAY_TURN) { |
| 552 | switch (proto) { |
| 553 | case PROTO_UDP: return "turn(udp)"; |
| 554 | case PROTO_TCP: return "turn(tcp)"; |
| 555 | case PROTO_SSLTCP: return "turn(ssltcp)"; |
| 556 | default: return "turn(?)"; |
| 557 | } |
| 558 | } else { |
| 559 | switch (proto) { |
| 560 | case PROTO_UDP: return "gturn(udp)"; |
| 561 | case PROTO_TCP: return "gturn(tcp)"; |
| 562 | case PROTO_SSLTCP: return "gturn(ssltcp)"; |
| 563 | default: return "gturn(?)"; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 568 | void SetNetworkType(rtc::AdapterType adapter_type) { |
| 569 | network_.set_type(adapter_type); |
| 570 | } |
| 571 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 572 | void TestCrossFamilyPorts(int type); |
| 573 | |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 574 | void ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2); |
| 575 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 576 | // This does all the work and then deletes |port1| and |port2|. |
| 577 | void TestConnectivity(const char* name1, Port* port1, |
| 578 | const char* name2, Port* port2, |
| 579 | bool accept, bool same_addr1, |
| 580 | bool same_addr2, bool possible); |
| 581 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 582 | // This connects the provided channels which have already started. |ch1| |
| 583 | // should have its Connection created (either through CreateConnection() or |
| 584 | // TCP reconnecting mechanism before entering this function. |
| 585 | void ConnectStartedChannels(TestChannel* ch1, TestChannel* ch2) { |
| 586 | ASSERT_TRUE(ch1->conn()); |
| 587 | EXPECT_TRUE_WAIT(ch1->conn()->connected(), kTimeout); // for TCP connect |
| 588 | ch1->Ping(); |
| 589 | WAIT(!ch2->remote_address().IsNil(), kTimeout); |
| 590 | |
| 591 | // Send a ping from dst to src. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 592 | ch2->AcceptConnection(GetCandidate(ch1->port())); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 593 | ch2->Ping(); |
| 594 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2->conn()->write_state(), |
| 595 | kTimeout); |
| 596 | } |
| 597 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 598 | // This connects and disconnects the provided channels in the same sequence as |
| 599 | // TestConnectivity with all options set to |true|. It does not delete either |
| 600 | // channel. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 601 | void StartConnectAndStopChannels(TestChannel* ch1, TestChannel* ch2) { |
| 602 | // Acquire addresses. |
| 603 | ch1->Start(); |
| 604 | ch2->Start(); |
| 605 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 606 | ch1->CreateConnection(GetCandidate(ch2->port())); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 607 | ConnectStartedChannels(ch1, ch2); |
| 608 | |
| 609 | // Destroy the connections. |
| 610 | ch1->Stop(); |
| 611 | ch2->Stop(); |
| 612 | } |
| 613 | |
| 614 | // This disconnects both end's Connection and make sure ch2 ready for new |
| 615 | // connection. |
| 616 | void DisconnectTcpTestChannels(TestChannel* ch1, TestChannel* ch2) { |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 617 | TCPConnection* tcp_conn1 = static_cast<TCPConnection*>(ch1->conn()); |
| 618 | TCPConnection* tcp_conn2 = static_cast<TCPConnection*>(ch2->conn()); |
| 619 | ASSERT_TRUE( |
| 620 | ss_->CloseTcpConnections(tcp_conn1->socket()->GetLocalAddress(), |
| 621 | tcp_conn2->socket()->GetLocalAddress())); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 622 | |
| 623 | // Wait for both OnClose are delivered. |
| 624 | EXPECT_TRUE_WAIT(!ch1->conn()->connected(), kTimeout); |
| 625 | EXPECT_TRUE_WAIT(!ch2->conn()->connected(), kTimeout); |
| 626 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 627 | // Ensure redundant SignalClose events on TcpConnection won't break tcp |
| 628 | // reconnection. Chromium will fire SignalClose for all outstanding IPC |
| 629 | // packets during reconnection. |
| 630 | tcp_conn1->socket()->SignalClose(tcp_conn1->socket(), 0); |
| 631 | tcp_conn2->socket()->SignalClose(tcp_conn2->socket(), 0); |
| 632 | |
| 633 | // Speed up destroying ch2's connection such that the test is ready to |
| 634 | // accept a new connection from ch1 before ch1's connection destroys itself. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 635 | ch2->conn()->Destroy(); |
| 636 | EXPECT_TRUE_WAIT(ch2->conn() == NULL, kTimeout); |
| 637 | } |
| 638 | |
| 639 | void TestTcpReconnect(bool ping_after_disconnected, |
| 640 | bool send_after_disconnected) { |
| 641 | Port* port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 642 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 643 | Port* port2 = CreateTcpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 644 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 645 | |
| 646 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 647 | port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 648 | |
| 649 | // Set up channels and ensure both ports will be deleted. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 650 | TestChannel ch1(port1); |
| 651 | TestChannel ch2(port2); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 652 | EXPECT_EQ(0, ch1.complete_count()); |
| 653 | EXPECT_EQ(0, ch2.complete_count()); |
| 654 | |
| 655 | ch1.Start(); |
| 656 | ch2.Start(); |
| 657 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kTimeout); |
| 658 | ASSERT_EQ_WAIT(1, ch2.complete_count(), kTimeout); |
| 659 | |
| 660 | // Initial connecting the channel, create connection on channel1. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 661 | ch1.CreateConnection(GetCandidate(port2)); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 662 | ConnectStartedChannels(&ch1, &ch2); |
| 663 | |
| 664 | // Shorten the timeout period. |
| 665 | const int kTcpReconnectTimeout = kTimeout; |
| 666 | static_cast<TCPConnection*>(ch1.conn()) |
| 667 | ->set_reconnection_timeout(kTcpReconnectTimeout); |
| 668 | static_cast<TCPConnection*>(ch2.conn()) |
| 669 | ->set_reconnection_timeout(kTcpReconnectTimeout); |
| 670 | |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 671 | EXPECT_FALSE(ch1.connection_ready_to_send()); |
| 672 | EXPECT_FALSE(ch2.connection_ready_to_send()); |
| 673 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 674 | // Once connected, disconnect them. |
| 675 | DisconnectTcpTestChannels(&ch1, &ch2); |
| 676 | |
| 677 | if (send_after_disconnected || ping_after_disconnected) { |
| 678 | if (send_after_disconnected) { |
| 679 | // First SendData after disconnect should fail but will trigger |
| 680 | // reconnect. |
| 681 | EXPECT_EQ(-1, ch1.SendData(data, static_cast<int>(strlen(data)))); |
| 682 | } |
| 683 | |
| 684 | if (ping_after_disconnected) { |
| 685 | // Ping should trigger reconnect. |
| 686 | ch1.Ping(); |
| 687 | } |
| 688 | |
| 689 | // Wait for channel's outgoing TCPConnection connected. |
| 690 | EXPECT_TRUE_WAIT(ch1.conn()->connected(), kTimeout); |
| 691 | |
| 692 | // Verify that we could still connect channels. |
| 693 | ConnectStartedChannels(&ch1, &ch2); |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 694 | EXPECT_TRUE_WAIT(ch1.connection_ready_to_send(), |
| 695 | kTcpReconnectTimeout); |
| 696 | // Channel2 is the passive one so a new connection is created during |
| 697 | // reconnect. This new connection should never have issued EWOULDBLOCK |
| 698 | // hence the connection_ready_to_send() should be false. |
| 699 | EXPECT_FALSE(ch2.connection_ready_to_send()); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 700 | } else { |
| 701 | EXPECT_EQ(ch1.conn()->write_state(), Connection::STATE_WRITABLE); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 702 | // Since the reconnection never happens, the connections should have been |
| 703 | // destroyed after the timeout. |
| 704 | EXPECT_TRUE_WAIT(!ch1.conn(), kTcpReconnectTimeout + kTimeout); |
| 705 | EXPECT_TRUE(!ch2.conn()); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | // Tear down and ensure that goes smoothly. |
| 709 | ch1.Stop(); |
| 710 | ch2.Stop(); |
| 711 | EXPECT_TRUE_WAIT(ch1.conn() == NULL, kTimeout); |
| 712 | EXPECT_TRUE_WAIT(ch2.conn() == NULL, kTimeout); |
| 713 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 714 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 715 | IceMessage* CreateStunMessage(int type) { |
| 716 | IceMessage* msg = new IceMessage(); |
| 717 | msg->SetType(type); |
| 718 | msg->SetTransactionID("TESTTESTTEST"); |
| 719 | return msg; |
| 720 | } |
| 721 | IceMessage* CreateStunMessageWithUsername(int type, |
| 722 | const std::string& username) { |
| 723 | IceMessage* msg = CreateStunMessage(type); |
| 724 | msg->AddAttribute( |
| 725 | new StunByteStringAttribute(STUN_ATTR_USERNAME, username)); |
| 726 | return msg; |
| 727 | } |
| 728 | TestPort* CreateTestPort(const rtc::SocketAddress& addr, |
| 729 | const std::string& username, |
| 730 | const std::string& password) { |
| 731 | TestPort* port = new TestPort(main_, "test", &socket_factory_, &network_, |
| 732 | addr.ipaddr(), 0, 0, username, password); |
| 733 | port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); |
| 734 | return port; |
| 735 | } |
| 736 | TestPort* CreateTestPort(const rtc::SocketAddress& addr, |
| 737 | const std::string& username, |
| 738 | const std::string& password, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 739 | cricket::IceRole role, |
| 740 | int tiebreaker) { |
| 741 | TestPort* port = CreateTestPort(addr, username, password); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 742 | port->SetIceRole(role); |
| 743 | port->SetIceTiebreaker(tiebreaker); |
| 744 | return port; |
| 745 | } |
| 746 | |
| 747 | void OnRoleConflict(PortInterface* port) { |
| 748 | role_conflict_ = true; |
| 749 | } |
| 750 | bool role_conflict() const { return role_conflict_; } |
| 751 | |
| 752 | void ConnectToSignalDestroyed(PortInterface* port) { |
| 753 | port->SignalDestroyed.connect(this, &PortTest::OnDestroyed); |
| 754 | } |
| 755 | |
| 756 | void OnDestroyed(PortInterface* port) { |
| 757 | destroyed_ = true; |
| 758 | } |
| 759 | bool destroyed() const { return destroyed_; } |
| 760 | |
| 761 | rtc::BasicPacketSocketFactory* nat_socket_factory1() { |
| 762 | return &nat_socket_factory1_; |
| 763 | } |
| 764 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 765 | rtc::VirtualSocketServer* vss() { return ss_.get(); } |
| 766 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 767 | private: |
| 768 | rtc::Thread* main_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 769 | std::unique_ptr<rtc::PhysicalSocketServer> pss_; |
| 770 | std::unique_ptr<rtc::VirtualSocketServer> ss_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 771 | rtc::SocketServerScope ss_scope_; |
| 772 | rtc::Network network_; |
| 773 | rtc::BasicPacketSocketFactory socket_factory_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 774 | std::unique_ptr<rtc::NATServer> nat_server1_; |
| 775 | std::unique_ptr<rtc::NATServer> nat_server2_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 776 | rtc::NATSocketFactory nat_factory1_; |
| 777 | rtc::NATSocketFactory nat_factory2_; |
| 778 | rtc::BasicPacketSocketFactory nat_socket_factory1_; |
| 779 | rtc::BasicPacketSocketFactory nat_socket_factory2_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 780 | std::unique_ptr<TestStunServer> stun_server_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 781 | TestTurnServer turn_server_; |
| 782 | TestRelayServer relay_server_; |
| 783 | std::string username_; |
| 784 | std::string password_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 785 | bool role_conflict_; |
| 786 | bool destroyed_; |
| 787 | }; |
| 788 | |
| 789 | void PortTest::TestConnectivity(const char* name1, Port* port1, |
| 790 | const char* name2, Port* port2, |
| 791 | bool accept, bool same_addr1, |
| 792 | bool same_addr2, bool possible) { |
| 793 | LOG(LS_INFO) << "Test: " << name1 << " to " << name2 << ": "; |
| 794 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 795 | port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 796 | |
| 797 | // Set up channels and ensure both ports will be deleted. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 798 | TestChannel ch1(port1); |
| 799 | TestChannel ch2(port2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 800 | EXPECT_EQ(0, ch1.complete_count()); |
| 801 | EXPECT_EQ(0, ch2.complete_count()); |
| 802 | |
| 803 | // Acquire addresses. |
| 804 | ch1.Start(); |
| 805 | ch2.Start(); |
| 806 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kTimeout); |
| 807 | ASSERT_EQ_WAIT(1, ch2.complete_count(), kTimeout); |
| 808 | |
| 809 | // Send a ping from src to dst. This may or may not make it. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 810 | ch1.CreateConnection(GetCandidate(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 811 | ASSERT_TRUE(ch1.conn() != NULL); |
| 812 | EXPECT_TRUE_WAIT(ch1.conn()->connected(), kTimeout); // for TCP connect |
| 813 | ch1.Ping(); |
| 814 | WAIT(!ch2.remote_address().IsNil(), kTimeout); |
| 815 | |
| 816 | if (accept) { |
| 817 | // We are able to send a ping from src to dst. This is the case when |
| 818 | // sending to UDP ports and cone NATs. |
| 819 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 820 | EXPECT_EQ(ch2.remote_fragment(), port1->username_fragment()); |
| 821 | |
| 822 | // Ensure the ping came from the same address used for src. |
| 823 | // This is the case unless the source NAT was symmetric. |
| 824 | if (same_addr1) EXPECT_EQ(ch2.remote_address(), GetAddress(port1)); |
| 825 | EXPECT_TRUE(same_addr2); |
| 826 | |
| 827 | // Send a ping from dst to src. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 828 | ch2.AcceptConnection(GetCandidate(port1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 829 | ASSERT_TRUE(ch2.conn() != NULL); |
| 830 | ch2.Ping(); |
| 831 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2.conn()->write_state(), |
| 832 | kTimeout); |
| 833 | } else { |
| 834 | // We can't send a ping from src to dst, so flip it around. This will happen |
| 835 | // when the destination NAT is addr/port restricted or symmetric. |
| 836 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 837 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 838 | |
| 839 | // Send a ping from dst to src. Again, this may or may not make it. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 840 | ch2.CreateConnection(GetCandidate(port1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 841 | ASSERT_TRUE(ch2.conn() != NULL); |
| 842 | ch2.Ping(); |
| 843 | WAIT(ch2.conn()->write_state() == Connection::STATE_WRITABLE, kTimeout); |
| 844 | |
| 845 | if (same_addr1 && same_addr2) { |
| 846 | // The new ping got back to the source. |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 847 | EXPECT_TRUE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 848 | EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state()); |
| 849 | |
| 850 | // First connection may not be writable if the first ping did not get |
| 851 | // through. So we will have to do another. |
| 852 | if (ch1.conn()->write_state() == Connection::STATE_WRITE_INIT) { |
| 853 | ch1.Ping(); |
| 854 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(), |
| 855 | kTimeout); |
| 856 | } |
| 857 | } else if (!same_addr1 && possible) { |
| 858 | // The new ping went to the candidate address, but that address was bad. |
| 859 | // This will happen when the source NAT is symmetric. |
| 860 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 861 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 862 | |
| 863 | // However, since we have now sent a ping to the source IP, we should be |
| 864 | // able to get a ping from it. This gives us the real source address. |
| 865 | ch1.Ping(); |
| 866 | EXPECT_TRUE_WAIT(!ch2.remote_address().IsNil(), kTimeout); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 867 | EXPECT_FALSE(ch2.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 868 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 869 | |
| 870 | // Pick up the actual address and establish the connection. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 871 | ch2.AcceptConnection(GetCandidate(port1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 872 | ASSERT_TRUE(ch2.conn() != NULL); |
| 873 | ch2.Ping(); |
| 874 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2.conn()->write_state(), |
| 875 | kTimeout); |
| 876 | } else if (!same_addr2 && possible) { |
| 877 | // The new ping came in, but from an unexpected address. This will happen |
| 878 | // when the destination NAT is symmetric. |
| 879 | EXPECT_FALSE(ch1.remote_address().IsNil()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 880 | EXPECT_FALSE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 881 | |
| 882 | // Update our address and complete the connection. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 883 | ch1.AcceptConnection(GetCandidate(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 884 | ch1.Ping(); |
| 885 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(), |
| 886 | kTimeout); |
| 887 | } else { // (!possible) |
| 888 | // There should be s no way for the pings to reach each other. Check it. |
| 889 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 890 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 891 | ch1.Ping(); |
| 892 | WAIT(!ch2.remote_address().IsNil(), kTimeout); |
| 893 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 894 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | // Everything should be good, unless we know the situation is impossible. |
| 899 | ASSERT_TRUE(ch1.conn() != NULL); |
| 900 | ASSERT_TRUE(ch2.conn() != NULL); |
| 901 | if (possible) { |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 902 | EXPECT_TRUE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 903 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 904 | EXPECT_TRUE(ch2.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 905 | EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state()); |
| 906 | } else { |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 907 | EXPECT_FALSE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 908 | EXPECT_NE(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 909 | EXPECT_FALSE(ch2.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 910 | EXPECT_NE(Connection::STATE_WRITABLE, ch2.conn()->write_state()); |
| 911 | } |
| 912 | |
| 913 | // Tear down and ensure that goes smoothly. |
| 914 | ch1.Stop(); |
| 915 | ch2.Stop(); |
| 916 | EXPECT_TRUE_WAIT(ch1.conn() == NULL, kTimeout); |
| 917 | EXPECT_TRUE_WAIT(ch2.conn() == NULL, kTimeout); |
| 918 | } |
| 919 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 920 | class FakePacketSocketFactory : public rtc::PacketSocketFactory { |
| 921 | public: |
| 922 | FakePacketSocketFactory() |
| 923 | : next_udp_socket_(NULL), |
| 924 | next_server_tcp_socket_(NULL), |
| 925 | next_client_tcp_socket_(NULL) { |
| 926 | } |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 927 | ~FakePacketSocketFactory() override { } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 928 | |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 929 | AsyncPacketSocket* CreateUdpSocket(const SocketAddress& address, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 930 | uint16_t min_port, |
| 931 | uint16_t max_port) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 932 | EXPECT_TRUE(next_udp_socket_ != NULL); |
| 933 | AsyncPacketSocket* result = next_udp_socket_; |
| 934 | next_udp_socket_ = NULL; |
| 935 | return result; |
| 936 | } |
| 937 | |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 938 | AsyncPacketSocket* CreateServerTcpSocket(const SocketAddress& local_address, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 939 | uint16_t min_port, |
| 940 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 941 | int opts) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 942 | EXPECT_TRUE(next_server_tcp_socket_ != NULL); |
| 943 | AsyncPacketSocket* result = next_server_tcp_socket_; |
| 944 | next_server_tcp_socket_ = NULL; |
| 945 | return result; |
| 946 | } |
| 947 | |
| 948 | // TODO: |proxy_info| and |user_agent| should be set |
| 949 | // per-factory and not when socket is created. |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 950 | AsyncPacketSocket* CreateClientTcpSocket(const SocketAddress& local_address, |
| 951 | const SocketAddress& remote_address, |
| 952 | const rtc::ProxyInfo& proxy_info, |
| 953 | const std::string& user_agent, |
| 954 | int opts) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 955 | EXPECT_TRUE(next_client_tcp_socket_ != NULL); |
| 956 | AsyncPacketSocket* result = next_client_tcp_socket_; |
| 957 | next_client_tcp_socket_ = NULL; |
| 958 | return result; |
| 959 | } |
| 960 | |
| 961 | void set_next_udp_socket(AsyncPacketSocket* next_udp_socket) { |
| 962 | next_udp_socket_ = next_udp_socket; |
| 963 | } |
| 964 | void set_next_server_tcp_socket(AsyncPacketSocket* next_server_tcp_socket) { |
| 965 | next_server_tcp_socket_ = next_server_tcp_socket; |
| 966 | } |
| 967 | void set_next_client_tcp_socket(AsyncPacketSocket* next_client_tcp_socket) { |
| 968 | next_client_tcp_socket_ = next_client_tcp_socket; |
| 969 | } |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 970 | rtc::AsyncResolverInterface* CreateAsyncResolver() override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 971 | return NULL; |
| 972 | } |
| 973 | |
| 974 | private: |
| 975 | AsyncPacketSocket* next_udp_socket_; |
| 976 | AsyncPacketSocket* next_server_tcp_socket_; |
| 977 | AsyncPacketSocket* next_client_tcp_socket_; |
| 978 | }; |
| 979 | |
| 980 | class FakeAsyncPacketSocket : public AsyncPacketSocket { |
| 981 | public: |
| 982 | // Returns current local address. Address may be set to NULL if the |
| 983 | // socket is not bound yet (GetState() returns STATE_BINDING). |
| 984 | virtual SocketAddress GetLocalAddress() const { |
| 985 | return SocketAddress(); |
| 986 | } |
| 987 | |
| 988 | // Returns remote address. Returns zeroes if this is not a client TCP socket. |
| 989 | virtual SocketAddress GetRemoteAddress() const { |
| 990 | return SocketAddress(); |
| 991 | } |
| 992 | |
| 993 | // Send a packet. |
| 994 | virtual int Send(const void *pv, size_t cb, |
| 995 | const rtc::PacketOptions& options) { |
| 996 | return static_cast<int>(cb); |
| 997 | } |
| 998 | virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr, |
| 999 | const rtc::PacketOptions& options) { |
| 1000 | return static_cast<int>(cb); |
| 1001 | } |
| 1002 | virtual int Close() { |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | virtual State GetState() const { return state_; } |
| 1007 | virtual int GetOption(Socket::Option opt, int* value) { return 0; } |
| 1008 | virtual int SetOption(Socket::Option opt, int value) { return 0; } |
| 1009 | virtual int GetError() const { return 0; } |
| 1010 | virtual void SetError(int error) { } |
| 1011 | |
| 1012 | void set_state(State state) { state_ = state; } |
| 1013 | |
| 1014 | private: |
| 1015 | State state_; |
| 1016 | }; |
| 1017 | |
| 1018 | // Local -> XXXX |
| 1019 | TEST_F(PortTest, TestLocalToLocal) { |
| 1020 | TestLocalToLocal(); |
| 1021 | } |
| 1022 | |
| 1023 | TEST_F(PortTest, TestLocalToConeNat) { |
| 1024 | TestLocalToStun(NAT_OPEN_CONE); |
| 1025 | } |
| 1026 | |
| 1027 | TEST_F(PortTest, TestLocalToARNat) { |
| 1028 | TestLocalToStun(NAT_ADDR_RESTRICTED); |
| 1029 | } |
| 1030 | |
| 1031 | TEST_F(PortTest, TestLocalToPRNat) { |
| 1032 | TestLocalToStun(NAT_PORT_RESTRICTED); |
| 1033 | } |
| 1034 | |
| 1035 | TEST_F(PortTest, TestLocalToSymNat) { |
| 1036 | TestLocalToStun(NAT_SYMMETRIC); |
| 1037 | } |
| 1038 | |
| 1039 | // Flaky: https://code.google.com/p/webrtc/issues/detail?id=3316. |
| 1040 | TEST_F(PortTest, DISABLED_TestLocalToTurn) { |
| 1041 | TestLocalToRelay(RELAY_TURN, PROTO_UDP); |
| 1042 | } |
| 1043 | |
| 1044 | TEST_F(PortTest, TestLocalToGturn) { |
| 1045 | TestLocalToRelay(RELAY_GTURN, PROTO_UDP); |
| 1046 | } |
| 1047 | |
| 1048 | TEST_F(PortTest, TestLocalToTcpGturn) { |
| 1049 | TestLocalToRelay(RELAY_GTURN, PROTO_TCP); |
| 1050 | } |
| 1051 | |
| 1052 | TEST_F(PortTest, TestLocalToSslTcpGturn) { |
| 1053 | TestLocalToRelay(RELAY_GTURN, PROTO_SSLTCP); |
| 1054 | } |
| 1055 | |
| 1056 | // Cone NAT -> XXXX |
| 1057 | TEST_F(PortTest, TestConeNatToLocal) { |
| 1058 | TestStunToLocal(NAT_OPEN_CONE); |
| 1059 | } |
| 1060 | |
| 1061 | TEST_F(PortTest, TestConeNatToConeNat) { |
| 1062 | TestStunToStun(NAT_OPEN_CONE, NAT_OPEN_CONE); |
| 1063 | } |
| 1064 | |
| 1065 | TEST_F(PortTest, TestConeNatToARNat) { |
| 1066 | TestStunToStun(NAT_OPEN_CONE, NAT_ADDR_RESTRICTED); |
| 1067 | } |
| 1068 | |
| 1069 | TEST_F(PortTest, TestConeNatToPRNat) { |
| 1070 | TestStunToStun(NAT_OPEN_CONE, NAT_PORT_RESTRICTED); |
| 1071 | } |
| 1072 | |
| 1073 | TEST_F(PortTest, TestConeNatToSymNat) { |
| 1074 | TestStunToStun(NAT_OPEN_CONE, NAT_SYMMETRIC); |
| 1075 | } |
| 1076 | |
| 1077 | TEST_F(PortTest, TestConeNatToTurn) { |
| 1078 | TestStunToRelay(NAT_OPEN_CONE, RELAY_TURN, PROTO_UDP); |
| 1079 | } |
| 1080 | |
| 1081 | TEST_F(PortTest, TestConeNatToGturn) { |
| 1082 | TestStunToRelay(NAT_OPEN_CONE, RELAY_GTURN, PROTO_UDP); |
| 1083 | } |
| 1084 | |
| 1085 | TEST_F(PortTest, TestConeNatToTcpGturn) { |
| 1086 | TestStunToRelay(NAT_OPEN_CONE, RELAY_GTURN, PROTO_TCP); |
| 1087 | } |
| 1088 | |
| 1089 | // Address-restricted NAT -> XXXX |
| 1090 | TEST_F(PortTest, TestARNatToLocal) { |
| 1091 | TestStunToLocal(NAT_ADDR_RESTRICTED); |
| 1092 | } |
| 1093 | |
| 1094 | TEST_F(PortTest, TestARNatToConeNat) { |
| 1095 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_OPEN_CONE); |
| 1096 | } |
| 1097 | |
| 1098 | TEST_F(PortTest, TestARNatToARNat) { |
| 1099 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_ADDR_RESTRICTED); |
| 1100 | } |
| 1101 | |
| 1102 | TEST_F(PortTest, TestARNatToPRNat) { |
| 1103 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_PORT_RESTRICTED); |
| 1104 | } |
| 1105 | |
| 1106 | TEST_F(PortTest, TestARNatToSymNat) { |
| 1107 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_SYMMETRIC); |
| 1108 | } |
| 1109 | |
| 1110 | TEST_F(PortTest, TestARNatToTurn) { |
| 1111 | TestStunToRelay(NAT_ADDR_RESTRICTED, RELAY_TURN, PROTO_UDP); |
| 1112 | } |
| 1113 | |
| 1114 | TEST_F(PortTest, TestARNatToGturn) { |
| 1115 | TestStunToRelay(NAT_ADDR_RESTRICTED, RELAY_GTURN, PROTO_UDP); |
| 1116 | } |
| 1117 | |
| 1118 | TEST_F(PortTest, TestARNATNatToTcpGturn) { |
| 1119 | TestStunToRelay(NAT_ADDR_RESTRICTED, RELAY_GTURN, PROTO_TCP); |
| 1120 | } |
| 1121 | |
| 1122 | // Port-restricted NAT -> XXXX |
| 1123 | TEST_F(PortTest, TestPRNatToLocal) { |
| 1124 | TestStunToLocal(NAT_PORT_RESTRICTED); |
| 1125 | } |
| 1126 | |
| 1127 | TEST_F(PortTest, TestPRNatToConeNat) { |
| 1128 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_OPEN_CONE); |
| 1129 | } |
| 1130 | |
| 1131 | TEST_F(PortTest, TestPRNatToARNat) { |
| 1132 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_ADDR_RESTRICTED); |
| 1133 | } |
| 1134 | |
| 1135 | TEST_F(PortTest, TestPRNatToPRNat) { |
| 1136 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_PORT_RESTRICTED); |
| 1137 | } |
| 1138 | |
| 1139 | TEST_F(PortTest, TestPRNatToSymNat) { |
| 1140 | // Will "fail" |
| 1141 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_SYMMETRIC); |
| 1142 | } |
| 1143 | |
| 1144 | TEST_F(PortTest, TestPRNatToTurn) { |
| 1145 | TestStunToRelay(NAT_PORT_RESTRICTED, RELAY_TURN, PROTO_UDP); |
| 1146 | } |
| 1147 | |
| 1148 | TEST_F(PortTest, TestPRNatToGturn) { |
| 1149 | TestStunToRelay(NAT_PORT_RESTRICTED, RELAY_GTURN, PROTO_UDP); |
| 1150 | } |
| 1151 | |
| 1152 | TEST_F(PortTest, TestPRNatToTcpGturn) { |
| 1153 | TestStunToRelay(NAT_PORT_RESTRICTED, RELAY_GTURN, PROTO_TCP); |
| 1154 | } |
| 1155 | |
| 1156 | // Symmetric NAT -> XXXX |
| 1157 | TEST_F(PortTest, TestSymNatToLocal) { |
| 1158 | TestStunToLocal(NAT_SYMMETRIC); |
| 1159 | } |
| 1160 | |
| 1161 | TEST_F(PortTest, TestSymNatToConeNat) { |
| 1162 | TestStunToStun(NAT_SYMMETRIC, NAT_OPEN_CONE); |
| 1163 | } |
| 1164 | |
| 1165 | TEST_F(PortTest, TestSymNatToARNat) { |
| 1166 | TestStunToStun(NAT_SYMMETRIC, NAT_ADDR_RESTRICTED); |
| 1167 | } |
| 1168 | |
| 1169 | TEST_F(PortTest, TestSymNatToPRNat) { |
| 1170 | // Will "fail" |
| 1171 | TestStunToStun(NAT_SYMMETRIC, NAT_PORT_RESTRICTED); |
| 1172 | } |
| 1173 | |
| 1174 | TEST_F(PortTest, TestSymNatToSymNat) { |
| 1175 | // Will "fail" |
| 1176 | TestStunToStun(NAT_SYMMETRIC, NAT_SYMMETRIC); |
| 1177 | } |
| 1178 | |
| 1179 | TEST_F(PortTest, TestSymNatToTurn) { |
| 1180 | TestStunToRelay(NAT_SYMMETRIC, RELAY_TURN, PROTO_UDP); |
| 1181 | } |
| 1182 | |
| 1183 | TEST_F(PortTest, TestSymNatToGturn) { |
| 1184 | TestStunToRelay(NAT_SYMMETRIC, RELAY_GTURN, PROTO_UDP); |
| 1185 | } |
| 1186 | |
| 1187 | TEST_F(PortTest, TestSymNatToTcpGturn) { |
| 1188 | TestStunToRelay(NAT_SYMMETRIC, RELAY_GTURN, PROTO_TCP); |
| 1189 | } |
| 1190 | |
| 1191 | // Outbound TCP -> XXXX |
| 1192 | TEST_F(PortTest, TestTcpToTcp) { |
| 1193 | TestTcpToTcp(); |
| 1194 | } |
| 1195 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 1196 | TEST_F(PortTest, TestTcpReconnectOnSendPacket) { |
| 1197 | TestTcpReconnect(false /* ping */, true /* send */); |
| 1198 | } |
| 1199 | |
| 1200 | TEST_F(PortTest, TestTcpReconnectOnPing) { |
| 1201 | TestTcpReconnect(true /* ping */, false /* send */); |
| 1202 | } |
| 1203 | |
| 1204 | TEST_F(PortTest, TestTcpReconnectTimeout) { |
| 1205 | TestTcpReconnect(false /* ping */, false /* send */); |
| 1206 | } |
| 1207 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1208 | // Test when TcpConnection never connects, the OnClose() will be called to |
| 1209 | // destroy the connection. |
| 1210 | TEST_F(PortTest, TestTcpNeverConnect) { |
| 1211 | Port* port1 = CreateTcpPort(kLocalAddr1); |
| 1212 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1213 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 1214 | |
| 1215 | // Set up a channel and ensure the port will be deleted. |
| 1216 | TestChannel ch1(port1); |
| 1217 | EXPECT_EQ(0, ch1.complete_count()); |
| 1218 | |
| 1219 | ch1.Start(); |
| 1220 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kTimeout); |
| 1221 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1222 | std::unique_ptr<rtc::AsyncSocket> server( |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1223 | vss()->CreateAsyncSocket(kLocalAddr2.family(), SOCK_STREAM)); |
| 1224 | // Bind but not listen. |
| 1225 | EXPECT_EQ(0, server->Bind(kLocalAddr2)); |
| 1226 | |
| 1227 | Candidate c = GetCandidate(port1); |
| 1228 | c.set_address(server->GetLocalAddress()); |
| 1229 | |
| 1230 | ch1.CreateConnection(c); |
| 1231 | EXPECT_TRUE(ch1.conn()); |
| 1232 | EXPECT_TRUE_WAIT(!ch1.conn(), kTimeout); // for TCP connect |
| 1233 | } |
| 1234 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1235 | /* TODO: Enable these once testrelayserver can accept external TCP. |
| 1236 | TEST_F(PortTest, TestTcpToTcpRelay) { |
| 1237 | TestTcpToRelay(PROTO_TCP); |
| 1238 | } |
| 1239 | |
| 1240 | TEST_F(PortTest, TestTcpToSslTcpRelay) { |
| 1241 | TestTcpToRelay(PROTO_SSLTCP); |
| 1242 | } |
| 1243 | */ |
| 1244 | |
| 1245 | // Outbound SSLTCP -> XXXX |
| 1246 | /* TODO: Enable these once testrelayserver can accept external SSL. |
| 1247 | TEST_F(PortTest, TestSslTcpToTcpRelay) { |
| 1248 | TestSslTcpToRelay(PROTO_TCP); |
| 1249 | } |
| 1250 | |
| 1251 | TEST_F(PortTest, TestSslTcpToSslTcpRelay) { |
| 1252 | TestSslTcpToRelay(PROTO_SSLTCP); |
| 1253 | } |
| 1254 | */ |
| 1255 | |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1256 | // Test that a connection will be dead and deleted if |
| 1257 | // i) it has never received anything for MIN_CONNECTION_LIFETIME milliseconds |
| 1258 | // since it was created, or |
| 1259 | // ii) it has not received anything for DEAD_CONNECTION_RECEIVE_TIMEOUT |
| 1260 | // milliseconds since last receiving. |
| 1261 | TEST_F(PortTest, TestConnectionDead) { |
| 1262 | UDPPort* port1 = CreateUdpPort(kLocalAddr1); |
| 1263 | UDPPort* port2 = CreateUdpPort(kLocalAddr2); |
| 1264 | TestChannel ch1(port1); |
| 1265 | TestChannel ch2(port2); |
| 1266 | // Acquire address. |
| 1267 | ch1.Start(); |
| 1268 | ch2.Start(); |
| 1269 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kTimeout); |
| 1270 | ASSERT_EQ_WAIT(1, ch2.complete_count(), kTimeout); |
| 1271 | |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1272 | // Test case that the connection has never received anything. |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1273 | int64_t before_created = rtc::TimeMillis(); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1274 | ch1.CreateConnection(GetCandidate(port2)); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1275 | int64_t after_created = rtc::TimeMillis(); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1276 | Connection* conn = ch1.conn(); |
| 1277 | ASSERT(conn != nullptr); |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1278 | // It is not dead if it is after MIN_CONNECTION_LIFETIME but not pruned. |
| 1279 | conn->UpdateState(after_created + MIN_CONNECTION_LIFETIME + 1); |
| 1280 | rtc::Thread::Current()->ProcessMessages(0); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1281 | EXPECT_TRUE(ch1.conn() != nullptr); |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1282 | // It is not dead if it is before MIN_CONNECTION_LIFETIME and pruned. |
| 1283 | conn->UpdateState(before_created + MIN_CONNECTION_LIFETIME - 1); |
| 1284 | conn->Prune(); |
| 1285 | rtc::Thread::Current()->ProcessMessages(0); |
| 1286 | EXPECT_TRUE(ch1.conn() != nullptr); |
| 1287 | // It will be dead after MIN_CONNECTION_LIFETIME and pruned. |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1288 | conn->UpdateState(after_created + MIN_CONNECTION_LIFETIME + 1); |
| 1289 | EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kTimeout); |
| 1290 | |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1291 | // Test case that the connection has received something. |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1292 | // Create a connection again and receive a ping. |
| 1293 | ch1.CreateConnection(GetCandidate(port2)); |
| 1294 | conn = ch1.conn(); |
| 1295 | ASSERT(conn != nullptr); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1296 | int64_t before_last_receiving = rtc::TimeMillis(); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1297 | conn->ReceivedPing(); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1298 | int64_t after_last_receiving = rtc::TimeMillis(); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1299 | // The connection will be dead after DEAD_CONNECTION_RECEIVE_TIMEOUT |
| 1300 | conn->UpdateState( |
| 1301 | before_last_receiving + DEAD_CONNECTION_RECEIVE_TIMEOUT - 1); |
| 1302 | rtc::Thread::Current()->ProcessMessages(100); |
| 1303 | EXPECT_TRUE(ch1.conn() != nullptr); |
| 1304 | conn->UpdateState(after_last_receiving + DEAD_CONNECTION_RECEIVE_TIMEOUT + 1); |
| 1305 | EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kTimeout); |
| 1306 | } |
| 1307 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1308 | // This test case verifies standard ICE features in STUN messages. Currently it |
| 1309 | // verifies Message Integrity attribute in STUN messages and username in STUN |
| 1310 | // binding request will have colon (":") between remote and local username. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1311 | TEST_F(PortTest, TestLocalToLocalStandard) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1312 | UDPPort* port1 = CreateUdpPort(kLocalAddr1); |
| 1313 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1314 | port1->SetIceTiebreaker(kTiebreaker1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1315 | UDPPort* port2 = CreateUdpPort(kLocalAddr2); |
| 1316 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1317 | port2->SetIceTiebreaker(kTiebreaker2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1318 | // Same parameters as TestLocalToLocal above. |
| 1319 | TestConnectivity("udp", port1, "udp", port2, true, true, true, true); |
| 1320 | } |
| 1321 | |
| 1322 | // This test is trying to validate a successful and failure scenario in a |
| 1323 | // loopback test when protocol is RFC5245. For success IceTiebreaker, username |
| 1324 | // should remain equal to the request generated by the port and role of port |
| 1325 | // must be in controlling. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1326 | TEST_F(PortTest, TestLoopbackCal) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1327 | std::unique_ptr<TestPort> lport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1328 | CreateTestPort(kLocalAddr1, "lfrag", "lpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1329 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1330 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1331 | lport->PrepareAddress(); |
| 1332 | ASSERT_FALSE(lport->Candidates().empty()); |
| 1333 | Connection* conn = lport->CreateConnection(lport->Candidates()[0], |
| 1334 | Port::ORIGIN_MESSAGE); |
| 1335 | conn->Ping(0); |
| 1336 | |
| 1337 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
| 1338 | IceMessage* msg = lport->last_stun_msg(); |
| 1339 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1340 | conn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
| 1341 | lport->last_stun_buf()->size(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1342 | rtc::PacketTime()); |
| 1343 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
| 1344 | msg = lport->last_stun_msg(); |
| 1345 | EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); |
| 1346 | |
| 1347 | // If the tiebreaker value is different from port, we expect a error |
| 1348 | // response. |
| 1349 | lport->Reset(); |
| 1350 | lport->AddCandidateAddress(kLocalAddr2); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1351 | // Creating a different connection as |conn| is receiving. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1352 | Connection* conn1 = lport->CreateConnection(lport->Candidates()[1], |
| 1353 | Port::ORIGIN_MESSAGE); |
| 1354 | conn1->Ping(0); |
| 1355 | |
| 1356 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
| 1357 | msg = lport->last_stun_msg(); |
| 1358 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1359 | std::unique_ptr<IceMessage> modified_req( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1360 | CreateStunMessage(STUN_BINDING_REQUEST)); |
| 1361 | const StunByteStringAttribute* username_attr = msg->GetByteString( |
| 1362 | STUN_ATTR_USERNAME); |
| 1363 | modified_req->AddAttribute(new StunByteStringAttribute( |
| 1364 | STUN_ATTR_USERNAME, username_attr->GetString())); |
| 1365 | // To make sure we receive error response, adding tiebreaker less than |
| 1366 | // what's present in request. |
| 1367 | modified_req->AddAttribute(new StunUInt64Attribute( |
| 1368 | STUN_ATTR_ICE_CONTROLLING, kTiebreaker1 - 1)); |
| 1369 | modified_req->AddMessageIntegrity("lpass"); |
| 1370 | modified_req->AddFingerprint(); |
| 1371 | |
| 1372 | lport->Reset(); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1373 | std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1374 | WriteStunMessage(modified_req.get(), buf.get()); |
| 1375 | conn1->OnReadPacket(buf->Data(), buf->Length(), rtc::PacketTime()); |
| 1376 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
| 1377 | msg = lport->last_stun_msg(); |
| 1378 | EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type()); |
| 1379 | } |
| 1380 | |
| 1381 | // This test verifies role conflict signal is received when there is |
| 1382 | // conflict in the role. In this case both ports are in controlling and |
| 1383 | // |rport| has higher tiebreaker value than |lport|. Since |lport| has lower |
| 1384 | // value of tiebreaker, when it receives ping request from |rport| it will |
| 1385 | // send role conflict signal. |
| 1386 | TEST_F(PortTest, TestIceRoleConflict) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1387 | std::unique_ptr<TestPort> lport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1388 | CreateTestPort(kLocalAddr1, "lfrag", "lpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1389 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1390 | lport->SetIceTiebreaker(kTiebreaker1); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1391 | std::unique_ptr<TestPort> rport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1392 | CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1393 | rport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1394 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1395 | |
| 1396 | lport->PrepareAddress(); |
| 1397 | rport->PrepareAddress(); |
| 1398 | ASSERT_FALSE(lport->Candidates().empty()); |
| 1399 | ASSERT_FALSE(rport->Candidates().empty()); |
| 1400 | Connection* lconn = lport->CreateConnection(rport->Candidates()[0], |
| 1401 | Port::ORIGIN_MESSAGE); |
| 1402 | Connection* rconn = rport->CreateConnection(lport->Candidates()[0], |
| 1403 | Port::ORIGIN_MESSAGE); |
| 1404 | rconn->Ping(0); |
| 1405 | |
| 1406 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, 1000); |
| 1407 | IceMessage* msg = rport->last_stun_msg(); |
| 1408 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1409 | // Send rport binding request to lport. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1410 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
| 1411 | rport->last_stun_buf()->size(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1412 | rtc::PacketTime()); |
| 1413 | |
| 1414 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
| 1415 | EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type()); |
| 1416 | EXPECT_TRUE(role_conflict()); |
| 1417 | } |
| 1418 | |
| 1419 | TEST_F(PortTest, TestTcpNoDelay) { |
| 1420 | TCPPort* port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1421 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1422 | int option_value = -1; |
| 1423 | int success = port1->GetOption(rtc::Socket::OPT_NODELAY, |
| 1424 | &option_value); |
| 1425 | ASSERT_EQ(0, success); // GetOption() should complete successfully w/ 0 |
| 1426 | ASSERT_EQ(1, option_value); |
| 1427 | delete port1; |
| 1428 | } |
| 1429 | |
| 1430 | TEST_F(PortTest, TestDelayedBindingUdp) { |
| 1431 | FakeAsyncPacketSocket *socket = new FakeAsyncPacketSocket(); |
| 1432 | FakePacketSocketFactory socket_factory; |
| 1433 | |
| 1434 | socket_factory.set_next_udp_socket(socket); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1435 | std::unique_ptr<UDPPort> port(CreateUdpPort(kLocalAddr1, &socket_factory)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1436 | |
| 1437 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1438 | port->PrepareAddress(); |
| 1439 | |
| 1440 | EXPECT_EQ(0U, port->Candidates().size()); |
| 1441 | socket->SignalAddressReady(socket, kLocalAddr2); |
| 1442 | |
| 1443 | EXPECT_EQ(1U, port->Candidates().size()); |
| 1444 | } |
| 1445 | |
| 1446 | TEST_F(PortTest, TestDelayedBindingTcp) { |
| 1447 | FakeAsyncPacketSocket *socket = new FakeAsyncPacketSocket(); |
| 1448 | FakePacketSocketFactory socket_factory; |
| 1449 | |
| 1450 | socket_factory.set_next_server_tcp_socket(socket); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1451 | std::unique_ptr<TCPPort> port(CreateTcpPort(kLocalAddr1, &socket_factory)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1452 | |
| 1453 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1454 | port->PrepareAddress(); |
| 1455 | |
| 1456 | EXPECT_EQ(0U, port->Candidates().size()); |
| 1457 | socket->SignalAddressReady(socket, kLocalAddr2); |
| 1458 | |
| 1459 | EXPECT_EQ(1U, port->Candidates().size()); |
| 1460 | } |
| 1461 | |
| 1462 | void PortTest::TestCrossFamilyPorts(int type) { |
| 1463 | FakePacketSocketFactory factory; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1464 | std::unique_ptr<Port> ports[4]; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1465 | SocketAddress addresses[4] = {SocketAddress("192.168.1.3", 0), |
| 1466 | SocketAddress("192.168.1.4", 0), |
| 1467 | SocketAddress("2001:db8::1", 0), |
| 1468 | SocketAddress("2001:db8::2", 0)}; |
| 1469 | for (int i = 0; i < 4; i++) { |
| 1470 | FakeAsyncPacketSocket *socket = new FakeAsyncPacketSocket(); |
| 1471 | if (type == SOCK_DGRAM) { |
| 1472 | factory.set_next_udp_socket(socket); |
| 1473 | ports[i].reset(CreateUdpPort(addresses[i], &factory)); |
| 1474 | } else if (type == SOCK_STREAM) { |
| 1475 | factory.set_next_server_tcp_socket(socket); |
| 1476 | ports[i].reset(CreateTcpPort(addresses[i], &factory)); |
| 1477 | } |
| 1478 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1479 | socket->SignalAddressReady(socket, addresses[i]); |
| 1480 | ports[i]->PrepareAddress(); |
| 1481 | } |
| 1482 | |
| 1483 | // IPv4 Port, connects to IPv6 candidate and then to IPv4 candidate. |
| 1484 | if (type == SOCK_STREAM) { |
| 1485 | FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket(); |
| 1486 | factory.set_next_client_tcp_socket(clientsocket); |
| 1487 | } |
| 1488 | Connection* c = ports[0]->CreateConnection(GetCandidate(ports[2].get()), |
| 1489 | Port::ORIGIN_MESSAGE); |
| 1490 | EXPECT_TRUE(NULL == c); |
| 1491 | EXPECT_EQ(0U, ports[0]->connections().size()); |
| 1492 | c = ports[0]->CreateConnection(GetCandidate(ports[1].get()), |
| 1493 | Port::ORIGIN_MESSAGE); |
| 1494 | EXPECT_FALSE(NULL == c); |
| 1495 | EXPECT_EQ(1U, ports[0]->connections().size()); |
| 1496 | |
| 1497 | // IPv6 Port, connects to IPv4 candidate and to IPv6 candidate. |
| 1498 | if (type == SOCK_STREAM) { |
| 1499 | FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket(); |
| 1500 | factory.set_next_client_tcp_socket(clientsocket); |
| 1501 | } |
| 1502 | c = ports[2]->CreateConnection(GetCandidate(ports[0].get()), |
| 1503 | Port::ORIGIN_MESSAGE); |
| 1504 | EXPECT_TRUE(NULL == c); |
| 1505 | EXPECT_EQ(0U, ports[2]->connections().size()); |
| 1506 | c = ports[2]->CreateConnection(GetCandidate(ports[3].get()), |
| 1507 | Port::ORIGIN_MESSAGE); |
| 1508 | EXPECT_FALSE(NULL == c); |
| 1509 | EXPECT_EQ(1U, ports[2]->connections().size()); |
| 1510 | } |
| 1511 | |
| 1512 | TEST_F(PortTest, TestSkipCrossFamilyTcp) { |
| 1513 | TestCrossFamilyPorts(SOCK_STREAM); |
| 1514 | } |
| 1515 | |
| 1516 | TEST_F(PortTest, TestSkipCrossFamilyUdp) { |
| 1517 | TestCrossFamilyPorts(SOCK_DGRAM); |
| 1518 | } |
| 1519 | |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1520 | void PortTest::ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2) { |
| 1521 | Connection* c = p1->CreateConnection(GetCandidate(p2), |
| 1522 | Port::ORIGIN_MESSAGE); |
| 1523 | if (can_connect) { |
| 1524 | EXPECT_FALSE(NULL == c); |
| 1525 | EXPECT_EQ(1U, p1->connections().size()); |
| 1526 | } else { |
| 1527 | EXPECT_TRUE(NULL == c); |
| 1528 | EXPECT_EQ(0U, p1->connections().size()); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | TEST_F(PortTest, TestUdpV6CrossTypePorts) { |
| 1533 | FakePacketSocketFactory factory; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1534 | std::unique_ptr<Port> ports[4]; |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1535 | SocketAddress addresses[4] = {SocketAddress("2001:db8::1", 0), |
| 1536 | SocketAddress("fe80::1", 0), |
| 1537 | SocketAddress("fe80::2", 0), |
| 1538 | SocketAddress("::1", 0)}; |
| 1539 | for (int i = 0; i < 4; i++) { |
| 1540 | FakeAsyncPacketSocket *socket = new FakeAsyncPacketSocket(); |
| 1541 | factory.set_next_udp_socket(socket); |
| 1542 | ports[i].reset(CreateUdpPort(addresses[i], &factory)); |
| 1543 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1544 | socket->SignalAddressReady(socket, addresses[i]); |
| 1545 | ports[i]->PrepareAddress(); |
| 1546 | } |
| 1547 | |
| 1548 | Port* standard = ports[0].get(); |
| 1549 | Port* link_local1 = ports[1].get(); |
| 1550 | Port* link_local2 = ports[2].get(); |
| 1551 | Port* localhost = ports[3].get(); |
| 1552 | |
| 1553 | ExpectPortsCanConnect(false, link_local1, standard); |
| 1554 | ExpectPortsCanConnect(false, standard, link_local1); |
| 1555 | ExpectPortsCanConnect(false, link_local1, localhost); |
| 1556 | ExpectPortsCanConnect(false, localhost, link_local1); |
| 1557 | |
| 1558 | ExpectPortsCanConnect(true, link_local1, link_local2); |
| 1559 | ExpectPortsCanConnect(true, localhost, standard); |
| 1560 | ExpectPortsCanConnect(true, standard, localhost); |
| 1561 | } |
| 1562 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1563 | // This test verifies DSCP value set through SetOption interface can be |
| 1564 | // get through DefaultDscpValue. |
| 1565 | TEST_F(PortTest, TestDefaultDscpValue) { |
| 1566 | int dscp; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1567 | std::unique_ptr<UDPPort> udpport(CreateUdpPort(kLocalAddr1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1568 | EXPECT_EQ(0, udpport->SetOption(rtc::Socket::OPT_DSCP, |
| 1569 | rtc::DSCP_CS6)); |
| 1570 | EXPECT_EQ(0, udpport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1571 | std::unique_ptr<TCPPort> tcpport(CreateTcpPort(kLocalAddr1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1572 | EXPECT_EQ(0, tcpport->SetOption(rtc::Socket::OPT_DSCP, |
| 1573 | rtc::DSCP_AF31)); |
| 1574 | EXPECT_EQ(0, tcpport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1575 | EXPECT_EQ(rtc::DSCP_AF31, dscp); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1576 | std::unique_ptr<StunPort> stunport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1577 | CreateStunPort(kLocalAddr1, nat_socket_factory1())); |
| 1578 | EXPECT_EQ(0, stunport->SetOption(rtc::Socket::OPT_DSCP, |
| 1579 | rtc::DSCP_AF41)); |
| 1580 | EXPECT_EQ(0, stunport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1581 | EXPECT_EQ(rtc::DSCP_AF41, dscp); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1582 | std::unique_ptr<TurnPort> turnport1( |
| 1583 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1584 | // Socket is created in PrepareAddress. |
| 1585 | turnport1->PrepareAddress(); |
| 1586 | EXPECT_EQ(0, turnport1->SetOption(rtc::Socket::OPT_DSCP, |
| 1587 | rtc::DSCP_CS7)); |
| 1588 | EXPECT_EQ(0, turnport1->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1589 | EXPECT_EQ(rtc::DSCP_CS7, dscp); |
| 1590 | // This will verify correct value returned without the socket. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1591 | std::unique_ptr<TurnPort> turnport2( |
| 1592 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1593 | EXPECT_EQ(0, turnport2->SetOption(rtc::Socket::OPT_DSCP, |
| 1594 | rtc::DSCP_CS6)); |
| 1595 | EXPECT_EQ(0, turnport2->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1596 | EXPECT_EQ(rtc::DSCP_CS6, dscp); |
| 1597 | } |
| 1598 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1599 | // Test sending STUN messages. |
| 1600 | TEST_F(PortTest, TestSendStunMessage) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1601 | std::unique_ptr<TestPort> lport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1602 | CreateTestPort(kLocalAddr1, "lfrag", "lpass")); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1603 | std::unique_ptr<TestPort> rport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1604 | CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1605 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1606 | lport->SetIceTiebreaker(kTiebreaker1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1607 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1608 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1609 | |
| 1610 | // Send a fake ping from lport to rport. |
| 1611 | lport->PrepareAddress(); |
| 1612 | rport->PrepareAddress(); |
| 1613 | ASSERT_FALSE(rport->Candidates().empty()); |
| 1614 | Connection* lconn = lport->CreateConnection( |
| 1615 | rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1616 | Connection* rconn = rport->CreateConnection( |
| 1617 | lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1618 | lconn->Ping(0); |
| 1619 | |
| 1620 | // Check that it's a proper BINDING-REQUEST. |
| 1621 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
| 1622 | IceMessage* msg = lport->last_stun_msg(); |
| 1623 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1624 | EXPECT_FALSE(msg->IsLegacy()); |
| 1625 | const StunByteStringAttribute* username_attr = |
| 1626 | msg->GetByteString(STUN_ATTR_USERNAME); |
| 1627 | ASSERT_TRUE(username_attr != NULL); |
| 1628 | const StunUInt32Attribute* priority_attr = msg->GetUInt32(STUN_ATTR_PRIORITY); |
| 1629 | ASSERT_TRUE(priority_attr != NULL); |
| 1630 | EXPECT_EQ(kDefaultPrflxPriority, priority_attr->value()); |
| 1631 | EXPECT_EQ("rfrag:lfrag", username_attr->GetString()); |
| 1632 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
| 1633 | EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1634 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1635 | "rpass")); |
| 1636 | const StunUInt64Attribute* ice_controlling_attr = |
| 1637 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); |
| 1638 | ASSERT_TRUE(ice_controlling_attr != NULL); |
| 1639 | EXPECT_EQ(lport->IceTiebreaker(), ice_controlling_attr->value()); |
| 1640 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL); |
| 1641 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); |
| 1642 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1643 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1644 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1645 | |
| 1646 | // Request should not include ping count. |
| 1647 | ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL); |
| 1648 | |
| 1649 | // Save a copy of the BINDING-REQUEST for use below. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1650 | std::unique_ptr<IceMessage> request(CopyStunMessage(msg)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1651 | |
| 1652 | // Respond with a BINDING-RESPONSE. |
| 1653 | rport->SendBindingResponse(request.get(), lport->Candidates()[0].address()); |
| 1654 | msg = rport->last_stun_msg(); |
| 1655 | ASSERT_TRUE(msg != NULL); |
| 1656 | EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); |
| 1657 | |
| 1658 | |
| 1659 | EXPECT_FALSE(msg->IsLegacy()); |
| 1660 | const StunAddressAttribute* addr_attr = msg->GetAddress( |
| 1661 | STUN_ATTR_XOR_MAPPED_ADDRESS); |
| 1662 | ASSERT_TRUE(addr_attr != NULL); |
| 1663 | EXPECT_EQ(lport->Candidates()[0].address(), addr_attr->GetAddress()); |
| 1664 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
| 1665 | EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1666 | rport->last_stun_buf()->data<char>(), rport->last_stun_buf()->size(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1667 | "rpass")); |
| 1668 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1669 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1670 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1671 | // No USERNAME or PRIORITY in ICE responses. |
| 1672 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); |
| 1673 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); |
| 1674 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MAPPED_ADDRESS) == NULL); |
| 1675 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLING) == NULL); |
| 1676 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL); |
| 1677 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 1678 | |
| 1679 | // Response should not include ping count. |
| 1680 | ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL); |
| 1681 | |
| 1682 | // Respond with a BINDING-ERROR-RESPONSE. This wouldn't happen in real life, |
| 1683 | // but we can do it here. |
| 1684 | rport->SendBindingErrorResponse(request.get(), |
| 1685 | lport->Candidates()[0].address(), |
| 1686 | STUN_ERROR_SERVER_ERROR, |
| 1687 | STUN_ERROR_REASON_SERVER_ERROR); |
| 1688 | msg = rport->last_stun_msg(); |
| 1689 | ASSERT_TRUE(msg != NULL); |
| 1690 | EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type()); |
| 1691 | EXPECT_FALSE(msg->IsLegacy()); |
| 1692 | const StunErrorCodeAttribute* error_attr = msg->GetErrorCode(); |
| 1693 | ASSERT_TRUE(error_attr != NULL); |
| 1694 | EXPECT_EQ(STUN_ERROR_SERVER_ERROR, error_attr->code()); |
| 1695 | EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), error_attr->reason()); |
| 1696 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
| 1697 | EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1698 | rport->last_stun_buf()->data<char>(), rport->last_stun_buf()->size(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1699 | "rpass")); |
| 1700 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1701 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1702 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1703 | // No USERNAME with ICE. |
| 1704 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); |
| 1705 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); |
| 1706 | |
| 1707 | // Testing STUN binding requests from rport --> lport, having ICE_CONTROLLED |
| 1708 | // and (incremented) RETRANSMIT_COUNT attributes. |
| 1709 | rport->Reset(); |
| 1710 | rport->set_send_retransmit_count_attribute(true); |
| 1711 | rconn->Ping(0); |
| 1712 | rconn->Ping(0); |
| 1713 | rconn->Ping(0); |
| 1714 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, 1000); |
| 1715 | msg = rport->last_stun_msg(); |
| 1716 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1717 | const StunUInt64Attribute* ice_controlled_attr = |
| 1718 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED); |
| 1719 | ASSERT_TRUE(ice_controlled_attr != NULL); |
| 1720 | EXPECT_EQ(rport->IceTiebreaker(), ice_controlled_attr->value()); |
| 1721 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 1722 | |
| 1723 | // Request should include ping count. |
| 1724 | const StunUInt32Attribute* retransmit_attr = |
| 1725 | msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
| 1726 | ASSERT_TRUE(retransmit_attr != NULL); |
| 1727 | EXPECT_EQ(2U, retransmit_attr->value()); |
| 1728 | |
| 1729 | // Respond with a BINDING-RESPONSE. |
| 1730 | request.reset(CopyStunMessage(msg)); |
| 1731 | lport->SendBindingResponse(request.get(), rport->Candidates()[0].address()); |
| 1732 | msg = lport->last_stun_msg(); |
| 1733 | |
| 1734 | // Response should include same ping count. |
| 1735 | retransmit_attr = msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
| 1736 | ASSERT_TRUE(retransmit_attr != NULL); |
| 1737 | EXPECT_EQ(2U, retransmit_attr->value()); |
| 1738 | } |
| 1739 | |
| 1740 | TEST_F(PortTest, TestUseCandidateAttribute) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1741 | std::unique_ptr<TestPort> lport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1742 | CreateTestPort(kLocalAddr1, "lfrag", "lpass")); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1743 | std::unique_ptr<TestPort> rport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1744 | CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1745 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1746 | lport->SetIceTiebreaker(kTiebreaker1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1747 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1748 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1749 | |
| 1750 | // Send a fake ping from lport to rport. |
| 1751 | lport->PrepareAddress(); |
| 1752 | rport->PrepareAddress(); |
| 1753 | ASSERT_FALSE(rport->Candidates().empty()); |
| 1754 | Connection* lconn = lport->CreateConnection( |
| 1755 | rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1756 | lconn->Ping(0); |
| 1757 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
| 1758 | IceMessage* msg = lport->last_stun_msg(); |
| 1759 | const StunUInt64Attribute* ice_controlling_attr = |
| 1760 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); |
| 1761 | ASSERT_TRUE(ice_controlling_attr != NULL); |
| 1762 | const StunByteStringAttribute* use_candidate_attr = msg->GetByteString( |
| 1763 | STUN_ATTR_USE_CANDIDATE); |
| 1764 | ASSERT_TRUE(use_candidate_attr != NULL); |
| 1765 | } |
| 1766 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1767 | // Tests that when the network type changes, the network cost of the port will |
| 1768 | // change, the network cost of the local candidates will change. Also tests that |
| 1769 | // the remote network costs are updated with the stun binding requests. |
| 1770 | TEST_F(PortTest, TestNetworkCostChange) { |
| 1771 | std::unique_ptr<TestPort> lport( |
| 1772 | CreateTestPort(kLocalAddr1, "lfrag", "lpass")); |
| 1773 | std::unique_ptr<TestPort> rport( |
| 1774 | CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
| 1775 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1776 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1777 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1778 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1779 | lport->PrepareAddress(); |
| 1780 | rport->PrepareAddress(); |
| 1781 | |
| 1782 | // Default local port cost is rtc::kNetworkCostUnknown. |
| 1783 | EXPECT_EQ(rtc::kNetworkCostUnknown, lport->network_cost()); |
| 1784 | ASSERT_TRUE(!lport->Candidates().empty()); |
| 1785 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1786 | EXPECT_EQ(rtc::kNetworkCostUnknown, candidate.network_cost()); |
| 1787 | } |
| 1788 | |
| 1789 | // Change the network type to wifi. |
| 1790 | SetNetworkType(rtc::ADAPTER_TYPE_WIFI); |
| 1791 | EXPECT_EQ(rtc::kNetworkCostLow, lport->network_cost()); |
| 1792 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1793 | EXPECT_EQ(rtc::kNetworkCostLow, candidate.network_cost()); |
| 1794 | } |
| 1795 | |
| 1796 | // Add a connection and then change the network type. |
| 1797 | Connection* lconn = |
| 1798 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1799 | // Change the network type to cellular. |
| 1800 | SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
| 1801 | EXPECT_EQ(rtc::kNetworkCostHigh, lport->network_cost()); |
| 1802 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1803 | EXPECT_EQ(rtc::kNetworkCostHigh, candidate.network_cost()); |
| 1804 | } |
| 1805 | |
| 1806 | SetNetworkType(rtc::ADAPTER_TYPE_WIFI); |
| 1807 | Connection* rconn = |
| 1808 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1809 | SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
| 1810 | lconn->Ping(0); |
| 1811 | // The rconn's remote candidate cost is rtc::kNetworkCostLow, but the ping |
| 1812 | // contains an attribute of network cost of rtc::kNetworkCostHigh. Once the |
| 1813 | // message is handled in rconn, The rconn's remote candidate will have cost |
| 1814 | // rtc::kNetworkCostHigh; |
| 1815 | EXPECT_EQ(rtc::kNetworkCostLow, rconn->remote_candidate().network_cost()); |
| 1816 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
| 1817 | IceMessage* msg = lport->last_stun_msg(); |
| 1818 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1819 | // Pass the binding request to rport. |
| 1820 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
| 1821 | lport->last_stun_buf()->size(), rtc::PacketTime()); |
| 1822 | // Wait until rport sends the response and then check the remote network cost. |
| 1823 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, 1000); |
| 1824 | EXPECT_EQ(rtc::kNetworkCostHigh, rconn->remote_candidate().network_cost()); |
| 1825 | } |
| 1826 | |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1827 | TEST_F(PortTest, TestNetworkInfoAttribute) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1828 | std::unique_ptr<TestPort> lport( |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1829 | CreateTestPort(kLocalAddr1, "lfrag", "lpass")); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1830 | std::unique_ptr<TestPort> rport( |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1831 | CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
| 1832 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1833 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1834 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1835 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1836 | |
| 1837 | uint16_t lnetwork_id = 9; |
| 1838 | lport->Network()->set_id(lnetwork_id); |
| 1839 | // Send a fake ping from lport to rport. |
| 1840 | lport->PrepareAddress(); |
| 1841 | rport->PrepareAddress(); |
| 1842 | Connection* lconn = |
| 1843 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1844 | lconn->Ping(0); |
| 1845 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
| 1846 | IceMessage* msg = lport->last_stun_msg(); |
| 1847 | const StunUInt32Attribute* network_info_attr = |
| 1848 | msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
| 1849 | ASSERT_TRUE(network_info_attr != NULL); |
| 1850 | uint32_t network_info = network_info_attr->value(); |
| 1851 | EXPECT_EQ(lnetwork_id, network_info >> 16); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1852 | // Default network has unknown type and cost kNetworkCostUnknown. |
| 1853 | EXPECT_EQ(rtc::kNetworkCostUnknown, network_info & 0xFFFF); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1854 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1855 | // Set the network type to be cellular so its cost will be kNetworkCostHigh. |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1856 | // Send a fake ping from rport to lport. |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1857 | SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1858 | uint16_t rnetwork_id = 8; |
| 1859 | rport->Network()->set_id(rnetwork_id); |
| 1860 | Connection* rconn = |
| 1861 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1862 | rconn->Ping(0); |
| 1863 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, 1000); |
| 1864 | msg = rport->last_stun_msg(); |
| 1865 | network_info_attr = msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
| 1866 | ASSERT_TRUE(network_info_attr != NULL); |
| 1867 | network_info = network_info_attr->value(); |
| 1868 | EXPECT_EQ(rnetwork_id, network_info >> 16); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1869 | EXPECT_EQ(rtc::kNetworkCostHigh, network_info & 0xFFFF); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1872 | // Test handling STUN messages. |
| 1873 | TEST_F(PortTest, TestHandleStunMessage) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1874 | // Our port will act as the "remote" port. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1875 | std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1876 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1877 | std::unique_ptr<IceMessage> in_msg, out_msg; |
| 1878 | std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1879 | rtc::SocketAddress addr(kLocalAddr1); |
| 1880 | std::string username; |
| 1881 | |
| 1882 | // BINDING-REQUEST from local to remote with valid ICE username, |
| 1883 | // MESSAGE-INTEGRITY, and FINGERPRINT. |
| 1884 | in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, |
| 1885 | "rfrag:lfrag")); |
| 1886 | in_msg->AddMessageIntegrity("rpass"); |
| 1887 | in_msg->AddFingerprint(); |
| 1888 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1889 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 1890 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1891 | EXPECT_TRUE(out_msg.get() != NULL); |
| 1892 | EXPECT_EQ("lfrag", username); |
| 1893 | |
| 1894 | // BINDING-RESPONSE without username, with MESSAGE-INTEGRITY and FINGERPRINT. |
| 1895 | in_msg.reset(CreateStunMessage(STUN_BINDING_RESPONSE)); |
| 1896 | in_msg->AddAttribute( |
| 1897 | new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); |
| 1898 | in_msg->AddMessageIntegrity("rpass"); |
| 1899 | in_msg->AddFingerprint(); |
| 1900 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1901 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 1902 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1903 | EXPECT_TRUE(out_msg.get() != NULL); |
| 1904 | EXPECT_EQ("", username); |
| 1905 | |
| 1906 | // BINDING-ERROR-RESPONSE without username, with error, M-I, and FINGERPRINT. |
| 1907 | in_msg.reset(CreateStunMessage(STUN_BINDING_ERROR_RESPONSE)); |
| 1908 | in_msg->AddAttribute(new StunErrorCodeAttribute(STUN_ATTR_ERROR_CODE, |
| 1909 | STUN_ERROR_SERVER_ERROR, STUN_ERROR_REASON_SERVER_ERROR)); |
| 1910 | in_msg->AddFingerprint(); |
| 1911 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1912 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 1913 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1914 | EXPECT_TRUE(out_msg.get() != NULL); |
| 1915 | EXPECT_EQ("", username); |
| 1916 | ASSERT_TRUE(out_msg->GetErrorCode() != NULL); |
| 1917 | EXPECT_EQ(STUN_ERROR_SERVER_ERROR, out_msg->GetErrorCode()->code()); |
| 1918 | EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), |
| 1919 | out_msg->GetErrorCode()->reason()); |
| 1920 | } |
| 1921 | |
guoweis | d12140a | 2015-09-10 13:32:11 -0700 | [diff] [blame] | 1922 | // Tests handling of ICE binding requests with missing or incorrect usernames. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1923 | TEST_F(PortTest, TestHandleStunMessageBadUsername) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1924 | std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1925 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1926 | std::unique_ptr<IceMessage> in_msg, out_msg; |
| 1927 | std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1928 | rtc::SocketAddress addr(kLocalAddr1); |
| 1929 | std::string username; |
| 1930 | |
| 1931 | // BINDING-REQUEST with no username. |
| 1932 | in_msg.reset(CreateStunMessage(STUN_BINDING_REQUEST)); |
| 1933 | in_msg->AddMessageIntegrity("rpass"); |
| 1934 | in_msg->AddFingerprint(); |
| 1935 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1936 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 1937 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1938 | EXPECT_TRUE(out_msg.get() == NULL); |
| 1939 | EXPECT_EQ("", username); |
| 1940 | EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code()); |
| 1941 | |
| 1942 | // BINDING-REQUEST with empty username. |
| 1943 | in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "")); |
| 1944 | in_msg->AddMessageIntegrity("rpass"); |
| 1945 | in_msg->AddFingerprint(); |
| 1946 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1947 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 1948 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1949 | EXPECT_TRUE(out_msg.get() == NULL); |
| 1950 | EXPECT_EQ("", username); |
| 1951 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 1952 | |
| 1953 | // BINDING-REQUEST with too-short username. |
| 1954 | in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfra")); |
| 1955 | in_msg->AddMessageIntegrity("rpass"); |
| 1956 | in_msg->AddFingerprint(); |
| 1957 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1958 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 1959 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1960 | EXPECT_TRUE(out_msg.get() == NULL); |
| 1961 | EXPECT_EQ("", username); |
| 1962 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 1963 | |
| 1964 | // BINDING-REQUEST with reversed username. |
| 1965 | in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, |
| 1966 | "lfrag:rfrag")); |
| 1967 | in_msg->AddMessageIntegrity("rpass"); |
| 1968 | in_msg->AddFingerprint(); |
| 1969 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1970 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 1971 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1972 | EXPECT_TRUE(out_msg.get() == NULL); |
| 1973 | EXPECT_EQ("", username); |
| 1974 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 1975 | |
| 1976 | // BINDING-REQUEST with garbage username. |
| 1977 | in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, |
| 1978 | "abcd:efgh")); |
| 1979 | in_msg->AddMessageIntegrity("rpass"); |
| 1980 | in_msg->AddFingerprint(); |
| 1981 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1982 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 1983 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1984 | EXPECT_TRUE(out_msg.get() == NULL); |
| 1985 | EXPECT_EQ("", username); |
| 1986 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 1987 | } |
| 1988 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1989 | // Test handling STUN messages with missing or malformed M-I. |
| 1990 | TEST_F(PortTest, TestHandleStunMessageBadMessageIntegrity) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1991 | // Our port will act as the "remote" port. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1992 | std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1993 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1994 | std::unique_ptr<IceMessage> in_msg, out_msg; |
| 1995 | std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1996 | rtc::SocketAddress addr(kLocalAddr1); |
| 1997 | std::string username; |
| 1998 | |
| 1999 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2000 | // FINGERPRINT, but no MESSAGE-INTEGRITY. |
| 2001 | in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, |
| 2002 | "rfrag:lfrag")); |
| 2003 | in_msg->AddFingerprint(); |
| 2004 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2005 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2006 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2007 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2008 | EXPECT_EQ("", username); |
| 2009 | EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code()); |
| 2010 | |
| 2011 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2012 | // FINGERPRINT, but invalid MESSAGE-INTEGRITY. |
| 2013 | in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, |
| 2014 | "rfrag:lfrag")); |
| 2015 | in_msg->AddMessageIntegrity("invalid"); |
| 2016 | in_msg->AddFingerprint(); |
| 2017 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2018 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2019 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2020 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2021 | EXPECT_EQ("", username); |
| 2022 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2023 | |
| 2024 | // TODO: BINDING-RESPONSES and BINDING-ERROR-RESPONSES are checked |
| 2025 | // by the Connection, not the Port, since they require the remote username. |
| 2026 | // Change this test to pass in data via Connection::OnReadPacket instead. |
| 2027 | } |
| 2028 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2029 | // Test handling STUN messages with missing or malformed FINGERPRINT. |
| 2030 | TEST_F(PortTest, TestHandleStunMessageBadFingerprint) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2031 | // Our port will act as the "remote" port. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2032 | std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2033 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2034 | std::unique_ptr<IceMessage> in_msg, out_msg; |
| 2035 | std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2036 | rtc::SocketAddress addr(kLocalAddr1); |
| 2037 | std::string username; |
| 2038 | |
| 2039 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2040 | // MESSAGE-INTEGRITY, but no FINGERPRINT; GetStunMessage should fail. |
| 2041 | in_msg.reset(CreateStunMessageWithUsername(STUN_BINDING_REQUEST, |
| 2042 | "rfrag:lfrag")); |
| 2043 | in_msg->AddMessageIntegrity("rpass"); |
| 2044 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2045 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2046 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2047 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2048 | |
| 2049 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2050 | in_msg->AddFingerprint(); |
| 2051 | in_msg->SetTransactionID("TESTTESTBADD"); |
| 2052 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2053 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2054 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2055 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2056 | |
| 2057 | // Valid BINDING-RESPONSE, except no FINGERPRINT. |
| 2058 | in_msg.reset(CreateStunMessage(STUN_BINDING_RESPONSE)); |
| 2059 | in_msg->AddAttribute( |
| 2060 | new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); |
| 2061 | in_msg->AddMessageIntegrity("rpass"); |
| 2062 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2063 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2064 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2065 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2066 | |
| 2067 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2068 | in_msg->AddFingerprint(); |
| 2069 | in_msg->SetTransactionID("TESTTESTBADD"); |
| 2070 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2071 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2072 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2073 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2074 | |
| 2075 | // Valid BINDING-ERROR-RESPONSE, except no FINGERPRINT. |
| 2076 | in_msg.reset(CreateStunMessage(STUN_BINDING_ERROR_RESPONSE)); |
| 2077 | in_msg->AddAttribute(new StunErrorCodeAttribute(STUN_ATTR_ERROR_CODE, |
| 2078 | STUN_ERROR_SERVER_ERROR, STUN_ERROR_REASON_SERVER_ERROR)); |
| 2079 | in_msg->AddMessageIntegrity("rpass"); |
| 2080 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2081 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2082 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2083 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2084 | |
| 2085 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2086 | in_msg->AddFingerprint(); |
| 2087 | in_msg->SetTransactionID("TESTTESTBADD"); |
| 2088 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2089 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2090 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2091 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2092 | } |
| 2093 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2094 | // Test handling of STUN binding indication messages . STUN binding |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2095 | // indications are allowed only to the connection which is in read mode. |
| 2096 | TEST_F(PortTest, TestHandleStunBindingIndication) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2097 | std::unique_ptr<TestPort> lport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2098 | CreateTestPort(kLocalAddr2, "lfrag", "lpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2099 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2100 | lport->SetIceTiebreaker(kTiebreaker1); |
| 2101 | |
| 2102 | // Verifying encoding and decoding STUN indication message. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2103 | std::unique_ptr<IceMessage> in_msg, out_msg; |
| 2104 | std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2105 | rtc::SocketAddress addr(kLocalAddr1); |
| 2106 | std::string username; |
| 2107 | |
| 2108 | in_msg.reset(CreateStunMessage(STUN_BINDING_INDICATION)); |
| 2109 | in_msg->AddFingerprint(); |
| 2110 | WriteStunMessage(in_msg.get(), buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2111 | EXPECT_TRUE(lport->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2112 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2113 | EXPECT_TRUE(out_msg.get() != NULL); |
| 2114 | EXPECT_EQ(out_msg->type(), STUN_BINDING_INDICATION); |
| 2115 | EXPECT_EQ("", username); |
| 2116 | |
| 2117 | // Verify connection can handle STUN indication and updates |
| 2118 | // last_ping_received. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2119 | std::unique_ptr<TestPort> rport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2120 | CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2121 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2122 | rport->SetIceTiebreaker(kTiebreaker2); |
| 2123 | |
| 2124 | lport->PrepareAddress(); |
| 2125 | rport->PrepareAddress(); |
| 2126 | ASSERT_FALSE(lport->Candidates().empty()); |
| 2127 | ASSERT_FALSE(rport->Candidates().empty()); |
| 2128 | |
| 2129 | Connection* lconn = lport->CreateConnection(rport->Candidates()[0], |
| 2130 | Port::ORIGIN_MESSAGE); |
| 2131 | Connection* rconn = rport->CreateConnection(lport->Candidates()[0], |
| 2132 | Port::ORIGIN_MESSAGE); |
| 2133 | rconn->Ping(0); |
| 2134 | |
| 2135 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, 1000); |
| 2136 | IceMessage* msg = rport->last_stun_msg(); |
| 2137 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 2138 | // Send rport binding request to lport. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 2139 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
| 2140 | rport->last_stun_buf()->size(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2141 | rtc::PacketTime()); |
| 2142 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, 1000); |
| 2143 | EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type()); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2144 | int64_t last_ping_received1 = lconn->last_ping_received(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2145 | |
| 2146 | // Adding a delay of 100ms. |
| 2147 | rtc::Thread::Current()->ProcessMessages(100); |
| 2148 | // Pinging lconn using stun indication message. |
| 2149 | lconn->OnReadPacket(buf->Data(), buf->Length(), rtc::PacketTime()); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2150 | int64_t last_ping_received2 = lconn->last_ping_received(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2151 | EXPECT_GT(last_ping_received2, last_ping_received1); |
| 2152 | } |
| 2153 | |
| 2154 | TEST_F(PortTest, TestComputeCandidatePriority) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2155 | std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr1, "name", "pass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2156 | port->set_type_preference(90); |
| 2157 | port->set_component(177); |
| 2158 | port->AddCandidateAddress(SocketAddress("192.168.1.4", 1234)); |
| 2159 | port->AddCandidateAddress(SocketAddress("2001:db8::1234", 1234)); |
| 2160 | port->AddCandidateAddress(SocketAddress("fc12:3456::1234", 1234)); |
| 2161 | port->AddCandidateAddress(SocketAddress("::ffff:192.168.1.4", 1234)); |
| 2162 | port->AddCandidateAddress(SocketAddress("::192.168.1.4", 1234)); |
| 2163 | port->AddCandidateAddress(SocketAddress("2002::1234:5678", 1234)); |
| 2164 | port->AddCandidateAddress(SocketAddress("2001::1234:5678", 1234)); |
| 2165 | port->AddCandidateAddress(SocketAddress("fecf::1234:5678", 1234)); |
| 2166 | port->AddCandidateAddress(SocketAddress("3ffe::1234:5678", 1234)); |
| 2167 | // These should all be: |
| 2168 | // (90 << 24) | ([rfc3484 pref value] << 8) | (256 - 177) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2169 | uint32_t expected_priority_v4 = 1509957199U; |
| 2170 | uint32_t expected_priority_v6 = 1509959759U; |
| 2171 | uint32_t expected_priority_ula = 1509962319U; |
| 2172 | uint32_t expected_priority_v4mapped = expected_priority_v4; |
| 2173 | uint32_t expected_priority_v4compat = 1509949775U; |
| 2174 | uint32_t expected_priority_6to4 = 1509954639U; |
| 2175 | uint32_t expected_priority_teredo = 1509952079U; |
| 2176 | uint32_t expected_priority_sitelocal = 1509949775U; |
| 2177 | uint32_t expected_priority_6bone = 1509949775U; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2178 | ASSERT_EQ(expected_priority_v4, port->Candidates()[0].priority()); |
| 2179 | ASSERT_EQ(expected_priority_v6, port->Candidates()[1].priority()); |
| 2180 | ASSERT_EQ(expected_priority_ula, port->Candidates()[2].priority()); |
| 2181 | ASSERT_EQ(expected_priority_v4mapped, port->Candidates()[3].priority()); |
| 2182 | ASSERT_EQ(expected_priority_v4compat, port->Candidates()[4].priority()); |
| 2183 | ASSERT_EQ(expected_priority_6to4, port->Candidates()[5].priority()); |
| 2184 | ASSERT_EQ(expected_priority_teredo, port->Candidates()[6].priority()); |
| 2185 | ASSERT_EQ(expected_priority_sitelocal, port->Candidates()[7].priority()); |
| 2186 | ASSERT_EQ(expected_priority_6bone, port->Candidates()[8].priority()); |
| 2187 | } |
| 2188 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2189 | // In the case of shared socket, one port may be shared by local and stun. |
| 2190 | // Test that candidates with different types will have different foundation. |
| 2191 | TEST_F(PortTest, TestFoundation) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2192 | std::unique_ptr<TestPort> testport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2193 | CreateTestPort(kLocalAddr1, "name", "pass")); |
| 2194 | testport->AddCandidateAddress(kLocalAddr1, kLocalAddr1, |
| 2195 | LOCAL_PORT_TYPE, |
| 2196 | cricket::ICE_TYPE_PREFERENCE_HOST, false); |
| 2197 | testport->AddCandidateAddress(kLocalAddr2, kLocalAddr1, |
| 2198 | STUN_PORT_TYPE, |
| 2199 | cricket::ICE_TYPE_PREFERENCE_SRFLX, true); |
| 2200 | EXPECT_NE(testport->Candidates()[0].foundation(), |
| 2201 | testport->Candidates()[1].foundation()); |
| 2202 | } |
| 2203 | |
| 2204 | // This test verifies the foundation of different types of ICE candidates. |
| 2205 | TEST_F(PortTest, TestCandidateFoundation) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2206 | std::unique_ptr<rtc::NATServer> nat_server( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2207 | CreateNatServer(kNatAddr1, NAT_OPEN_CONE)); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2208 | std::unique_ptr<UDPPort> udpport1(CreateUdpPort(kLocalAddr1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2209 | udpport1->PrepareAddress(); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2210 | std::unique_ptr<UDPPort> udpport2(CreateUdpPort(kLocalAddr1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2211 | udpport2->PrepareAddress(); |
| 2212 | EXPECT_EQ(udpport1->Candidates()[0].foundation(), |
| 2213 | udpport2->Candidates()[0].foundation()); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2214 | std::unique_ptr<TCPPort> tcpport1(CreateTcpPort(kLocalAddr1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2215 | tcpport1->PrepareAddress(); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2216 | std::unique_ptr<TCPPort> tcpport2(CreateTcpPort(kLocalAddr1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2217 | tcpport2->PrepareAddress(); |
| 2218 | EXPECT_EQ(tcpport1->Candidates()[0].foundation(), |
| 2219 | tcpport2->Candidates()[0].foundation()); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2220 | std::unique_ptr<Port> stunport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2221 | CreateStunPort(kLocalAddr1, nat_socket_factory1())); |
| 2222 | stunport->PrepareAddress(); |
| 2223 | ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kTimeout); |
| 2224 | EXPECT_NE(tcpport1->Candidates()[0].foundation(), |
| 2225 | stunport->Candidates()[0].foundation()); |
| 2226 | EXPECT_NE(tcpport2->Candidates()[0].foundation(), |
| 2227 | stunport->Candidates()[0].foundation()); |
| 2228 | EXPECT_NE(udpport1->Candidates()[0].foundation(), |
| 2229 | stunport->Candidates()[0].foundation()); |
| 2230 | EXPECT_NE(udpport2->Candidates()[0].foundation(), |
| 2231 | stunport->Candidates()[0].foundation()); |
| 2232 | // Verify GTURN candidate foundation. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2233 | std::unique_ptr<RelayPort> relayport(CreateGturnPort(kLocalAddr1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2234 | relayport->AddServerAddress( |
| 2235 | cricket::ProtocolAddress(kRelayUdpIntAddr, cricket::PROTO_UDP)); |
| 2236 | relayport->PrepareAddress(); |
| 2237 | ASSERT_EQ_WAIT(1U, relayport->Candidates().size(), kTimeout); |
| 2238 | EXPECT_NE(udpport1->Candidates()[0].foundation(), |
| 2239 | relayport->Candidates()[0].foundation()); |
| 2240 | EXPECT_NE(udpport2->Candidates()[0].foundation(), |
| 2241 | relayport->Candidates()[0].foundation()); |
| 2242 | // Verifying TURN candidate foundation. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2243 | std::unique_ptr<Port> turnport1( |
| 2244 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2245 | turnport1->PrepareAddress(); |
| 2246 | ASSERT_EQ_WAIT(1U, turnport1->Candidates().size(), kTimeout); |
| 2247 | EXPECT_NE(udpport1->Candidates()[0].foundation(), |
| 2248 | turnport1->Candidates()[0].foundation()); |
| 2249 | EXPECT_NE(udpport2->Candidates()[0].foundation(), |
| 2250 | turnport1->Candidates()[0].foundation()); |
| 2251 | EXPECT_NE(stunport->Candidates()[0].foundation(), |
| 2252 | turnport1->Candidates()[0].foundation()); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2253 | std::unique_ptr<Port> turnport2( |
| 2254 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2255 | turnport2->PrepareAddress(); |
| 2256 | ASSERT_EQ_WAIT(1U, turnport2->Candidates().size(), kTimeout); |
| 2257 | EXPECT_EQ(turnport1->Candidates()[0].foundation(), |
| 2258 | turnport2->Candidates()[0].foundation()); |
| 2259 | |
| 2260 | // Running a second turn server, to get different base IP address. |
| 2261 | SocketAddress kTurnUdpIntAddr2("99.99.98.4", STUN_SERVER_PORT); |
| 2262 | SocketAddress kTurnUdpExtAddr2("99.99.98.5", 0); |
| 2263 | TestTurnServer turn_server2( |
| 2264 | rtc::Thread::Current(), kTurnUdpIntAddr2, kTurnUdpExtAddr2); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2265 | std::unique_ptr<Port> turnport3( |
| 2266 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP, |
| 2267 | kTurnUdpIntAddr2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2268 | turnport3->PrepareAddress(); |
| 2269 | ASSERT_EQ_WAIT(1U, turnport3->Candidates().size(), kTimeout); |
| 2270 | EXPECT_NE(turnport3->Candidates()[0].foundation(), |
| 2271 | turnport2->Candidates()[0].foundation()); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2272 | |
| 2273 | // Start a TCP turn server, and check that two turn candidates have |
| 2274 | // different foundations if their relay protocols are different. |
| 2275 | TestTurnServer turn_server3(rtc::Thread::Current(), kTurnTcpIntAddr, |
| 2276 | kTurnUdpExtAddr, PROTO_TCP); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2277 | std::unique_ptr<Port> turnport4( |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2278 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_TCP, PROTO_UDP)); |
| 2279 | turnport4->PrepareAddress(); |
| 2280 | ASSERT_EQ_WAIT(1U, turnport4->Candidates().size(), kTimeout); |
| 2281 | EXPECT_NE(turnport2->Candidates()[0].foundation(), |
| 2282 | turnport4->Candidates()[0].foundation()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | // This test verifies the related addresses of different types of |
| 2286 | // ICE candiates. |
| 2287 | TEST_F(PortTest, TestCandidateRelatedAddress) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2288 | std::unique_ptr<rtc::NATServer> nat_server( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2289 | CreateNatServer(kNatAddr1, NAT_OPEN_CONE)); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2290 | std::unique_ptr<UDPPort> udpport(CreateUdpPort(kLocalAddr1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2291 | udpport->PrepareAddress(); |
| 2292 | // For UDPPort, related address will be empty. |
| 2293 | EXPECT_TRUE(udpport->Candidates()[0].related_address().IsNil()); |
| 2294 | // Testing related address for stun candidates. |
| 2295 | // For stun candidate related address must be equal to the base |
| 2296 | // socket address. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2297 | std::unique_ptr<StunPort> stunport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2298 | CreateStunPort(kLocalAddr1, nat_socket_factory1())); |
| 2299 | stunport->PrepareAddress(); |
| 2300 | ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kTimeout); |
| 2301 | // Check STUN candidate address. |
| 2302 | EXPECT_EQ(stunport->Candidates()[0].address().ipaddr(), |
| 2303 | kNatAddr1.ipaddr()); |
| 2304 | // Check STUN candidate related address. |
| 2305 | EXPECT_EQ(stunport->Candidates()[0].related_address(), |
| 2306 | stunport->GetLocalAddress()); |
| 2307 | // Verifying the related address for the GTURN candidates. |
| 2308 | // NOTE: In case of GTURN related address will be equal to the mapped |
| 2309 | // address, but address(mapped) will not be XOR. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2310 | std::unique_ptr<RelayPort> relayport(CreateGturnPort(kLocalAddr1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2311 | relayport->AddServerAddress( |
| 2312 | cricket::ProtocolAddress(kRelayUdpIntAddr, cricket::PROTO_UDP)); |
| 2313 | relayport->PrepareAddress(); |
| 2314 | ASSERT_EQ_WAIT(1U, relayport->Candidates().size(), kTimeout); |
| 2315 | // For Gturn related address is set to "0.0.0.0:0" |
| 2316 | EXPECT_EQ(rtc::SocketAddress(), |
| 2317 | relayport->Candidates()[0].related_address()); |
| 2318 | // Verifying the related address for TURN candidate. |
| 2319 | // For TURN related address must be equal to the mapped address. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2320 | std::unique_ptr<Port> turnport( |
| 2321 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2322 | turnport->PrepareAddress(); |
| 2323 | ASSERT_EQ_WAIT(1U, turnport->Candidates().size(), kTimeout); |
| 2324 | EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), |
| 2325 | turnport->Candidates()[0].address().ipaddr()); |
| 2326 | EXPECT_EQ(kNatAddr1.ipaddr(), |
| 2327 | turnport->Candidates()[0].related_address().ipaddr()); |
| 2328 | } |
| 2329 | |
| 2330 | // Test priority value overflow handling when preference is set to 3. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2331 | TEST_F(PortTest, TestCandidatePriority) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2332 | cricket::Candidate cand1; |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2333 | cand1.set_priority(3); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2334 | cricket::Candidate cand2; |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2335 | cand2.set_priority(1); |
| 2336 | EXPECT_TRUE(cand1.priority() > cand2.priority()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | // Test the Connection priority is calculated correctly. |
| 2340 | TEST_F(PortTest, TestConnectionPriority) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2341 | std::unique_ptr<TestPort> lport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2342 | CreateTestPort(kLocalAddr1, "lfrag", "lpass")); |
| 2343 | lport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_HOST); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2344 | std::unique_ptr<TestPort> rport( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2345 | CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
| 2346 | rport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_RELAY); |
| 2347 | lport->set_component(123); |
| 2348 | lport->AddCandidateAddress(SocketAddress("192.168.1.4", 1234)); |
| 2349 | rport->set_component(23); |
| 2350 | rport->AddCandidateAddress(SocketAddress("10.1.1.100", 1234)); |
| 2351 | |
| 2352 | EXPECT_EQ(0x7E001E85U, lport->Candidates()[0].priority()); |
| 2353 | EXPECT_EQ(0x2001EE9U, rport->Candidates()[0].priority()); |
| 2354 | |
| 2355 | // RFC 5245 |
| 2356 | // pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0) |
| 2357 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2358 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2359 | Connection* lconn = lport->CreateConnection( |
| 2360 | rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2361 | #if defined(WEBRTC_WIN) |
| 2362 | EXPECT_EQ(0x2001EE9FC003D0BU, lconn->priority()); |
| 2363 | #else |
| 2364 | EXPECT_EQ(0x2001EE9FC003D0BLLU, lconn->priority()); |
| 2365 | #endif |
| 2366 | |
| 2367 | lport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2368 | rport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2369 | Connection* rconn = rport->CreateConnection( |
| 2370 | lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2371 | #if defined(WEBRTC_WIN) |
| 2372 | EXPECT_EQ(0x2001EE9FC003D0AU, rconn->priority()); |
| 2373 | #else |
| 2374 | EXPECT_EQ(0x2001EE9FC003D0ALLU, rconn->priority()); |
| 2375 | #endif |
| 2376 | } |
| 2377 | |
| 2378 | TEST_F(PortTest, TestWritableState) { |
| 2379 | UDPPort* port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2380 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2381 | UDPPort* port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2382 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2383 | |
| 2384 | // Set up channels. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2385 | TestChannel ch1(port1); |
| 2386 | TestChannel ch2(port2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2387 | |
| 2388 | // Acquire addresses. |
| 2389 | ch1.Start(); |
| 2390 | ch2.Start(); |
| 2391 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kTimeout); |
| 2392 | ASSERT_EQ_WAIT(1, ch2.complete_count(), kTimeout); |
| 2393 | |
| 2394 | // Send a ping from src to dst. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2395 | ch1.CreateConnection(GetCandidate(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2396 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2397 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 2398 | EXPECT_TRUE_WAIT(ch1.conn()->connected(), kTimeout); // for TCP connect |
| 2399 | ch1.Ping(); |
| 2400 | WAIT(!ch2.remote_address().IsNil(), kTimeout); |
| 2401 | |
| 2402 | // Data should be unsendable until the connection is accepted. |
| 2403 | char data[] = "abcd"; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 2404 | int data_size = arraysize(data); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2405 | rtc::PacketOptions options; |
| 2406 | EXPECT_EQ(SOCKET_ERROR, ch1.conn()->Send(data, data_size, options)); |
| 2407 | |
| 2408 | // Accept the connection to return the binding response, transition to |
| 2409 | // writable, and allow data to be sent. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2410 | ch2.AcceptConnection(GetCandidate(port1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2411 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(), |
| 2412 | kTimeout); |
| 2413 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
| 2414 | |
| 2415 | // Ask the connection to update state as if enough time has passed to lose |
| 2416 | // full writability and 5 pings went unresponded to. We'll accomplish the |
| 2417 | // latter by sending pings but not pumping messages. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2418 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2419 | ch1.Ping(i); |
| 2420 | } |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2421 | int unreliable_timeout_delay = CONNECTION_WRITE_CONNECT_TIMEOUT + 500; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2422 | ch1.conn()->UpdateState(unreliable_timeout_delay); |
| 2423 | EXPECT_EQ(Connection::STATE_WRITE_UNRELIABLE, ch1.conn()->write_state()); |
| 2424 | |
| 2425 | // Data should be able to be sent in this state. |
| 2426 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
| 2427 | |
| 2428 | // And now allow the other side to process the pings and send binding |
| 2429 | // responses. |
| 2430 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(), |
| 2431 | kTimeout); |
| 2432 | |
| 2433 | // Wait long enough for a full timeout (past however long we've already |
| 2434 | // waited). |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2435 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2436 | ch1.Ping(unreliable_timeout_delay + i); |
| 2437 | } |
| 2438 | ch1.conn()->UpdateState(unreliable_timeout_delay + CONNECTION_WRITE_TIMEOUT + |
| 2439 | 500u); |
| 2440 | EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state()); |
| 2441 | |
| 2442 | // Now that the connection has completely timed out, data send should fail. |
| 2443 | EXPECT_EQ(SOCKET_ERROR, ch1.conn()->Send(data, data_size, options)); |
| 2444 | |
| 2445 | ch1.Stop(); |
| 2446 | ch2.Stop(); |
| 2447 | } |
| 2448 | |
| 2449 | TEST_F(PortTest, TestTimeoutForNeverWritable) { |
| 2450 | UDPPort* port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2451 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2452 | UDPPort* port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2453 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2454 | |
| 2455 | // Set up channels. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2456 | TestChannel ch1(port1); |
| 2457 | TestChannel ch2(port2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2458 | |
| 2459 | // Acquire addresses. |
| 2460 | ch1.Start(); |
| 2461 | ch2.Start(); |
| 2462 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2463 | ch1.CreateConnection(GetCandidate(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2464 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2465 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 2466 | |
| 2467 | // Attempt to go directly to write timeout. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2468 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2469 | ch1.Ping(i); |
| 2470 | } |
| 2471 | ch1.conn()->UpdateState(CONNECTION_WRITE_TIMEOUT + 500u); |
| 2472 | EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state()); |
| 2473 | } |
| 2474 | |
| 2475 | // This test verifies the connection setup between ICEMODE_FULL |
| 2476 | // and ICEMODE_LITE. |
| 2477 | // In this test |ch1| behaves like FULL mode client and we have created |
| 2478 | // port which responds to the ping message just like LITE client. |
| 2479 | TEST_F(PortTest, TestIceLiteConnectivity) { |
| 2480 | TestPort* ice_full_port = CreateTestPort( |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2481 | kLocalAddr1, "lfrag", "lpass", |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2482 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
| 2483 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2484 | std::unique_ptr<TestPort> ice_lite_port( |
| 2485 | CreateTestPort(kLocalAddr2, "rfrag", "rpass", cricket::ICEROLE_CONTROLLED, |
| 2486 | kTiebreaker2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2487 | // Setup TestChannel. This behaves like FULL mode client. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2488 | TestChannel ch1(ice_full_port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2489 | ch1.SetIceMode(ICEMODE_FULL); |
| 2490 | |
| 2491 | // Start gathering candidates. |
| 2492 | ch1.Start(); |
| 2493 | ice_lite_port->PrepareAddress(); |
| 2494 | |
| 2495 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kTimeout); |
| 2496 | ASSERT_FALSE(ice_lite_port->Candidates().empty()); |
| 2497 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2498 | ch1.CreateConnection(GetCandidate(ice_lite_port.get())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2499 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2500 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 2501 | |
| 2502 | // Send ping from full mode client. |
| 2503 | // This ping must not have USE_CANDIDATE_ATTR. |
| 2504 | ch1.Ping(); |
| 2505 | |
| 2506 | // Verify stun ping is without USE_CANDIDATE_ATTR. Getting message directly |
| 2507 | // from port. |
| 2508 | ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != NULL, 1000); |
| 2509 | IceMessage* msg = ice_full_port->last_stun_msg(); |
| 2510 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 2511 | |
| 2512 | // Respond with a BINDING-RESPONSE from litemode client. |
| 2513 | // NOTE: Ideally we should't create connection at this stage from lite |
| 2514 | // port, as it should be done only after receiving ping with USE_CANDIDATE. |
| 2515 | // But we need a connection to send a response message. |
| 2516 | ice_lite_port->CreateConnection( |
| 2517 | ice_full_port->Candidates()[0], cricket::Port::ORIGIN_MESSAGE); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2518 | std::unique_ptr<IceMessage> request(CopyStunMessage(msg)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2519 | ice_lite_port->SendBindingResponse( |
| 2520 | request.get(), ice_full_port->Candidates()[0].address()); |
| 2521 | |
| 2522 | // Feeding the respone message from litemode to the full mode connection. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 2523 | ch1.conn()->OnReadPacket(ice_lite_port->last_stun_buf()->data<char>(), |
| 2524 | ice_lite_port->last_stun_buf()->size(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2525 | rtc::PacketTime()); |
| 2526 | // Verifying full mode connection becomes writable from the response. |
| 2527 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(), |
| 2528 | kTimeout); |
| 2529 | EXPECT_TRUE_WAIT(ch1.nominated(), kTimeout); |
| 2530 | |
| 2531 | // Clear existing stun messsages. Otherwise we will process old stun |
| 2532 | // message right after we send ping. |
| 2533 | ice_full_port->Reset(); |
| 2534 | // Send ping. This must have USE_CANDIDATE_ATTR. |
| 2535 | ch1.Ping(); |
| 2536 | ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != NULL, 1000); |
| 2537 | msg = ice_full_port->last_stun_msg(); |
| 2538 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); |
| 2539 | ch1.Stop(); |
| 2540 | } |
| 2541 | |
| 2542 | // This test case verifies that the CONTROLLING port does not time out. |
| 2543 | TEST_F(PortTest, TestControllingNoTimeout) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2544 | UDPPort* port1 = CreateUdpPort(kLocalAddr1); |
| 2545 | ConnectToSignalDestroyed(port1); |
| 2546 | port1->set_timeout_delay(10); // milliseconds |
| 2547 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2548 | port1->SetIceTiebreaker(kTiebreaker1); |
| 2549 | |
| 2550 | UDPPort* port2 = CreateUdpPort(kLocalAddr2); |
| 2551 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2552 | port2->SetIceTiebreaker(kTiebreaker2); |
| 2553 | |
| 2554 | // Set up channels and ensure both ports will be deleted. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2555 | TestChannel ch1(port1); |
| 2556 | TestChannel ch2(port2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2557 | |
| 2558 | // Simulate a connection that succeeds, and then is destroyed. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 2559 | StartConnectAndStopChannels(&ch1, &ch2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2560 | |
| 2561 | // After the connection is destroyed, the port should not be destroyed. |
| 2562 | rtc::Thread::Current()->ProcessMessages(kTimeout); |
| 2563 | EXPECT_FALSE(destroyed()); |
| 2564 | } |
| 2565 | |
| 2566 | // This test case verifies that the CONTROLLED port does time out, but only |
| 2567 | // after connectivity is lost. |
| 2568 | TEST_F(PortTest, TestControlledTimeout) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2569 | UDPPort* port1 = CreateUdpPort(kLocalAddr1); |
| 2570 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2571 | port1->SetIceTiebreaker(kTiebreaker1); |
| 2572 | |
| 2573 | UDPPort* port2 = CreateUdpPort(kLocalAddr2); |
| 2574 | ConnectToSignalDestroyed(port2); |
| 2575 | port2->set_timeout_delay(10); // milliseconds |
| 2576 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2577 | port2->SetIceTiebreaker(kTiebreaker2); |
| 2578 | |
| 2579 | // The connection must not be destroyed before a connection is attempted. |
| 2580 | EXPECT_FALSE(destroyed()); |
| 2581 | |
| 2582 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 2583 | port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 2584 | |
| 2585 | // Set up channels and ensure both ports will be deleted. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2586 | TestChannel ch1(port1); |
| 2587 | TestChannel ch2(port2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2588 | |
| 2589 | // Simulate a connection that succeeds, and then is destroyed. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 2590 | StartConnectAndStopChannels(&ch1, &ch2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2591 | |
| 2592 | // The controlled port should be destroyed after 10 milliseconds. |
| 2593 | EXPECT_TRUE_WAIT(destroyed(), kTimeout); |
| 2594 | } |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 2595 | |
| 2596 | // This test case verifies that if the role of a port changes from controlled |
| 2597 | // to controlling after all connections fail, the port will not be destroyed. |
| 2598 | TEST_F(PortTest, TestControlledToControllingNotDestroyed) { |
| 2599 | UDPPort* port1 = CreateUdpPort(kLocalAddr1); |
| 2600 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2601 | port1->SetIceTiebreaker(kTiebreaker1); |
| 2602 | |
| 2603 | UDPPort* port2 = CreateUdpPort(kLocalAddr2); |
| 2604 | ConnectToSignalDestroyed(port2); |
| 2605 | port2->set_timeout_delay(10); // milliseconds |
| 2606 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2607 | port2->SetIceTiebreaker(kTiebreaker2); |
| 2608 | |
| 2609 | // The connection must not be destroyed before a connection is attempted. |
| 2610 | EXPECT_FALSE(destroyed()); |
| 2611 | |
| 2612 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 2613 | port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 2614 | |
| 2615 | // Set up channels and ensure both ports will be deleted. |
| 2616 | TestChannel ch1(port1); |
| 2617 | TestChannel ch2(port2); |
| 2618 | |
| 2619 | // Simulate a connection that succeeds, and then is destroyed. |
| 2620 | StartConnectAndStopChannels(&ch1, &ch2); |
| 2621 | // Switch the role after all connections are destroyed. |
| 2622 | EXPECT_TRUE_WAIT(ch2.conn() == nullptr, kTimeout); |
| 2623 | port1->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2624 | port2->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2625 | |
| 2626 | // After the connection is destroyed, the port should not be destroyed. |
| 2627 | rtc::Thread::Current()->ProcessMessages(kTimeout); |
| 2628 | EXPECT_FALSE(destroyed()); |
| 2629 | } |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 2630 | |
| 2631 | TEST_F(PortTest, TestSupportsProtocol) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2632 | std::unique_ptr<Port> udp_port(CreateUdpPort(kLocalAddr1)); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 2633 | EXPECT_TRUE(udp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 2634 | EXPECT_FALSE(udp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 2635 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2636 | std::unique_ptr<Port> stun_port( |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 2637 | CreateStunPort(kLocalAddr1, nat_socket_factory1())); |
| 2638 | EXPECT_TRUE(stun_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 2639 | EXPECT_FALSE(stun_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 2640 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2641 | std::unique_ptr<Port> tcp_port(CreateTcpPort(kLocalAddr1)); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 2642 | EXPECT_TRUE(tcp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 2643 | EXPECT_TRUE(tcp_port->SupportsProtocol(SSLTCP_PROTOCOL_NAME)); |
| 2644 | EXPECT_FALSE(tcp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 2645 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2646 | std::unique_ptr<Port> turn_port( |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 2647 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP)); |
| 2648 | EXPECT_TRUE(turn_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 2649 | EXPECT_FALSE(turn_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 2650 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2651 | |
| 2652 | // Test that SetIceParameters updates the component, ufrag and password |
| 2653 | // on both the port itself and its candidates. |
| 2654 | TEST_F(PortTest, TestSetIceParameters) { |
| 2655 | std::unique_ptr<TestPort> port( |
| 2656 | CreateTestPort(kLocalAddr1, "ufrag1", "password1")); |
| 2657 | port->PrepareAddress(); |
| 2658 | EXPECT_EQ(1UL, port->Candidates().size()); |
| 2659 | port->SetIceParameters(1, "ufrag2", "password2"); |
| 2660 | EXPECT_EQ(1, port->component()); |
| 2661 | EXPECT_EQ("ufrag2", port->username_fragment()); |
| 2662 | EXPECT_EQ("password2", port->password()); |
| 2663 | const Candidate& candidate = port->Candidates()[0]; |
| 2664 | EXPECT_EQ(1, candidate.component()); |
| 2665 | EXPECT_EQ("ufrag2", candidate.username()); |
| 2666 | EXPECT_EQ("password2", candidate.password()); |
| 2667 | } |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame^] | 2668 | |
| 2669 | TEST_F(PortTest, TestAddConnectionWithSameAddress) { |
| 2670 | std::unique_ptr<TestPort> port( |
| 2671 | CreateTestPort(kLocalAddr1, "ufrag1", "password1")); |
| 2672 | port->PrepareAddress(); |
| 2673 | EXPECT_EQ(1u, port->Candidates().size()); |
| 2674 | rtc::SocketAddress address("1.1.1.1", 5000); |
| 2675 | cricket::Candidate candidate(1, "udp", address, 0, "", "", "relay", 0, ""); |
| 2676 | cricket::Connection* conn1 = |
| 2677 | port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); |
| 2678 | cricket::Connection* conn_in_use = port->GetConnection(address); |
| 2679 | EXPECT_EQ(conn1, conn_in_use); |
| 2680 | EXPECT_EQ(0u, conn_in_use->remote_candidate().generation()); |
| 2681 | |
| 2682 | // Creating with a candidate with the same address again will get us a |
| 2683 | // different connection with the new candidate. |
| 2684 | candidate.set_generation(2); |
| 2685 | cricket::Connection* conn2 = |
| 2686 | port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); |
| 2687 | EXPECT_NE(conn1, conn2); |
| 2688 | conn_in_use = port->GetConnection(address); |
| 2689 | EXPECT_EQ(conn2, conn_in_use); |
| 2690 | EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); |
| 2691 | |
| 2692 | // Make sure the new connection was not deleted. |
| 2693 | rtc::Thread::Current()->ProcessMessages(300); |
| 2694 | EXPECT_TRUE(port->GetConnection(address) != nullptr); |
| 2695 | } |