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