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