~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/p2p/base/port_unittest.cc

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * libjingle
 
3
 * Copyright 2004 Google Inc.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions are met:
 
7
 *
 
8
 *  1. Redistributions of source code must retain the above copyright notice,
 
9
 *     this list of conditions and the following disclaimer.
 
10
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 
11
 *     this list of conditions and the following disclaimer in the documentation
 
12
 *     and/or other materials provided with the distribution.
 
13
 *  3. The name of the author may not be used to endorse or promote products
 
14
 *     derived from this software without specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 
17
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
18
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 
19
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
20
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
21
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
22
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
23
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
24
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
25
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
26
 */
 
27
 
 
28
#include "talk/base/basicpacketsocketfactory.h"
 
29
#include "talk/base/gunit.h"
 
30
#include "talk/base/helpers.h"
 
31
#include "talk/base/host.h"
 
32
#include "talk/base/logging.h"
 
33
#include "talk/base/natserver.h"
 
34
#include "talk/base/natsocketfactory.h"
 
35
#include "talk/base/physicalsocketserver.h"
 
36
#include "talk/base/scoped_ptr.h"
 
37
#include "talk/base/socketaddress.h"
 
38
#include "talk/base/stringutils.h"
 
39
#include "talk/base/thread.h"
 
40
#include "talk/base/virtualsocketserver.h"
 
41
#include "talk/p2p/base/relayport.h"
 
42
#include "talk/p2p/base/stunport.h"
 
43
#include "talk/p2p/base/tcpport.h"
 
44
#include "talk/p2p/base/udpport.h"
 
45
#include "talk/p2p/base/teststunserver.h"
 
46
#include "talk/p2p/base/testrelayserver.h"
 
47
 
 
48
using talk_base::AsyncPacketSocket;
 
49
using talk_base::NATType;
 
50
using talk_base::NAT_OPEN_CONE;
 
51
using talk_base::NAT_ADDR_RESTRICTED;
 
52
using talk_base::NAT_PORT_RESTRICTED;
 
53
using talk_base::NAT_SYMMETRIC;
 
54
using talk_base::PacketSocketFactory;
 
55
using talk_base::scoped_ptr;
 
56
using talk_base::Socket;
 
57
using talk_base::SocketAddress;
 
58
using namespace cricket;
 
59
 
 
60
static const int kTimeout = 1000;
 
61
static const SocketAddress kLocalAddr1 = SocketAddress("192.168.1.2", 0);
 
62
static const SocketAddress kLocalAddr2 = SocketAddress("192.168.1.3", 0);
 
63
static const SocketAddress kNatAddr1 = SocketAddress("77.77.77.77",
 
64
                                                    talk_base::NAT_SERVER_PORT);
 
65
static const SocketAddress kNatAddr2 = SocketAddress("88.88.88.88",
 
66
                                                    talk_base::NAT_SERVER_PORT);
 
67
static const SocketAddress kStunAddr = SocketAddress("99.99.99.1",
 
68
                                                     STUN_SERVER_PORT);
 
69
static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
 
70
static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
 
71
static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
 
72
static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
 
73
static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
 
74
static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
 
75
 
 
76
static Candidate GetCandidate(Port* port) {
 
77
  assert(port->candidates().size() == 1);
 
78
  return port->candidates()[0];
 
79
}
 
80
 
 
81
static SocketAddress GetAddress(Port* port) {
 
82
  return GetCandidate(port).address();
 
83
}
 
84
 
 
85
class TestChannel : public sigslot::has_slots<> {
 
86
 public:
 
87
  TestChannel(Port* p1, Port* p2)
 
88
      : src_(p1), dst_(p2), address_count_(0), conn_(NULL),
 
89
        remote_request_(NULL) {
 
90
    src_->SignalAddressReady.connect(this, &TestChannel::OnAddressReady);
 
91
    src_->SignalUnknownAddress.connect(this, &TestChannel::OnUnknownAddress);
 
92
  }
 
93
 
 
94
  int address_count() { return address_count_; }
 
95
  Connection* conn() { return conn_; }
 
96
  const SocketAddress& remote_address() { return remote_address_; }
 
97
  const std::string remote_fragment() { return remote_frag_; }
 
98
 
 
99
  void Start() {
 
100
    src_->PrepareAddress();
 
101
  }
 
102
  void CreateConnection() {
 
103
    conn_ = src_->CreateConnection(GetCandidate(dst_), Port::ORIGIN_MESSAGE);
 
104
  }
 
105
  void AcceptConnection() {
 
106
    ASSERT_TRUE(remote_request_ != NULL);
 
107
    Candidate c = GetCandidate(dst_);
 
108
    c.set_address(remote_address_);
 
109
    conn_ = src_->CreateConnection(c, Port::ORIGIN_MESSAGE);
 
110
    src_->SendBindingResponse(remote_request_, remote_address_);
 
111
    delete remote_request_;
 
112
  }
 
113
  void Ping() {
 
114
    conn_->Ping(0);
 
115
  }
 
116
  void Stop() {
 
117
    conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed);
 
118
    conn_->Destroy();
 
119
  }
 
120
 
 
121
  void OnAddressReady(Port* port) {
 
122
    address_count_++;
 
123
  }
 
124
 
 
125
  void OnUnknownAddress(Port* port, const SocketAddress& addr,
 
126
                        StunMessage* msg, const std::string& rf,
 
127
                        bool /*port_muxed*/) {
 
128
    ASSERT_EQ(src_.get(), port);
 
129
    if (!remote_address_.IsAny()) {
 
130
      ASSERT_EQ(remote_address_, addr);
 
131
      delete remote_request_;
 
132
    }
 
133
    remote_address_ = addr;
 
134
    remote_request_ = msg;
 
135
    remote_frag_ = rf;
 
136
  }
 
137
 
 
138
  void OnDestroyed(Connection* conn) {
 
139
    ASSERT_EQ(conn_, conn);
 
140
    conn_ = NULL;
 
141
  }
 
142
 
 
143
 private:
 
144
  talk_base::Thread* thread_;
 
145
  talk_base::scoped_ptr<Port> src_;
 
146
  Port* dst_;
 
147
 
 
148
  int address_count_;
 
149
  Connection* conn_;
 
150
  SocketAddress remote_address_;
 
151
  StunMessage* remote_request_;
 
152
  std::string remote_frag_;
 
153
};
 
154
 
 
155
class PortTest : public testing::Test {
 
156
 public:
 
157
  PortTest()
 
158
      : main_(talk_base::Thread::Current()),
 
159
        pss_(new talk_base::PhysicalSocketServer),
 
160
        ss_(new talk_base::VirtualSocketServer(pss_.get())),
 
161
        ss_scope_(ss_.get()),
 
162
        network_("unittest", "unittest", talk_base::IPAddress(INADDR_ANY), 32),
 
163
        socket_factory_(talk_base::Thread::Current()),
 
164
        nat_factory1_(ss_.get(), kNatAddr1),
 
165
        nat_factory2_(ss_.get(), kNatAddr2),
 
166
        nat_socket_factory1_(&nat_factory1_),
 
167
        nat_socket_factory2_(&nat_factory2_),
 
168
        stun_server_(main_, kStunAddr),
 
169
        relay_server_(main_, kRelayUdpIntAddr, kRelayUdpExtAddr,
 
170
                      kRelayTcpIntAddr, kRelayTcpExtAddr,
 
171
                      kRelaySslTcpIntAddr, kRelaySslTcpExtAddr) {
 
172
    network_.AddIP(talk_base::IPAddress(INADDR_ANY));
 
173
  }
 
174
 
 
175
 protected:
 
176
  static void SetUpTestCase() {
 
177
    // Ensure the RNG is inited.
 
178
    talk_base::InitRandom(NULL, 0);
 
179
  }
 
180
 
 
181
  void TestLocalToLocal() {
 
182
    UDPPort* port1 = CreateUdpPort(kLocalAddr1);
 
183
    UDPPort* port2 = CreateUdpPort(kLocalAddr2);
 
184
    TestConnectivity("udp", port1, "udp", port2, true, true, true, true);
 
185
  }
 
186
  void TestLocalToStun(NATType type) {
 
187
    UDPPort* port1 = CreateUdpPort(kLocalAddr1);
 
188
    nat_server2_.reset(CreateNatServer(kNatAddr2, type));
 
189
    StunPort* port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_);
 
190
    TestConnectivity("udp", port1, StunName(type), port2,
 
191
                     type == NAT_OPEN_CONE, true, type != NAT_SYMMETRIC, true);
 
192
  }
 
193
  void TestLocalToRelay(ProtocolType proto) {
 
194
    UDPPort* port1 = CreateUdpPort(kLocalAddr1);
 
195
    RelayPort* port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_UDP);
 
196
    TestConnectivity("udp", port1, RelayName(proto), port2,
 
197
                     true, true, true, true);
 
198
  }
 
199
  void TestStunToLocal(NATType type) {
 
200
    nat_server1_.reset(CreateNatServer(kNatAddr1, type));
 
201
    StunPort* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
 
202
    UDPPort* port2 = CreateUdpPort(kLocalAddr2);
 
203
    TestConnectivity(StunName(type), port1, "udp", port2,
 
204
                     true, type != NAT_SYMMETRIC, true, true);
 
205
  }
 
206
  void TestStunToStun(NATType type1, NATType type2) {
 
207
    nat_server1_.reset(CreateNatServer(kNatAddr1, type1));
 
208
    StunPort* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
 
209
    nat_server2_.reset(CreateNatServer(kNatAddr2, type2));
 
210
    StunPort* port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_);
 
211
    TestConnectivity(StunName(type1), port1, StunName(type2), port2,
 
212
                     type2 == NAT_OPEN_CONE,
 
213
                     type1 != NAT_SYMMETRIC, type2 != NAT_SYMMETRIC,
 
214
                     type1 + type2 < (NAT_PORT_RESTRICTED + NAT_SYMMETRIC));
 
215
  }
 
216
  void TestStunToRelay(NATType type, ProtocolType proto) {
 
217
    nat_server1_.reset(CreateNatServer(kNatAddr1, type));
 
218
    StunPort* port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
 
219
    RelayPort* port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_UDP);
 
220
    TestConnectivity(StunName(type), port1, RelayName(proto), port2,
 
221
                     true, type != NAT_SYMMETRIC, true, true);
 
222
  }
 
223
  void TestTcpToTcp() {
 
224
    TCPPort* port1 = CreateTcpPort(kLocalAddr1);
 
225
    TCPPort* port2 = CreateTcpPort(kLocalAddr2);
 
226
    TestConnectivity("tcp", port1, "tcp", port2, true, false, true, true);
 
227
  }
 
228
  void TestTcpToRelay(ProtocolType proto) {
 
229
    TCPPort* port1 = CreateTcpPort(kLocalAddr1);
 
230
    RelayPort* port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_TCP);
 
231
    TestConnectivity("tcp", port1, RelayName(proto), port2,
 
232
                     true, false, true, true);
 
233
  }
 
234
  void TestSslTcpToRelay(ProtocolType proto) {
 
235
    TCPPort* port1 = CreateTcpPort(kLocalAddr1);
 
236
    RelayPort* port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_SSLTCP);
 
237
    TestConnectivity("ssltcp", port1, RelayName(proto), port2,
 
238
                     true, false, true, true);
 
239
  }
 
240
 
 
241
  // helpers for above functions
 
242
  UDPPort* CreateUdpPort(const SocketAddress& addr) {
 
243
    return CreateUdpPort(addr, &socket_factory_);
 
244
  }
 
245
  UDPPort* CreateUdpPort(const SocketAddress& addr,
 
246
                         PacketSocketFactory* socket_factory) {
 
247
    return UDPPort::Create(main_, socket_factory, &network_,
 
248
                           addr.ipaddr(), 0, 0);
 
249
  }
 
250
  TCPPort* CreateTcpPort(const SocketAddress& addr) {
 
251
    return CreateTcpPort(addr, &socket_factory_);
 
252
  }
 
253
  TCPPort* CreateTcpPort(const SocketAddress& addr,
 
254
                         PacketSocketFactory* socket_factory) {
 
255
    return TCPPort::Create(main_, socket_factory, &network_,
 
256
                           addr.ipaddr(), 0, 0, true);
 
257
  }
 
258
  StunPort* CreateStunPort(const SocketAddress& addr,
 
259
                           talk_base::PacketSocketFactory* factory) {
 
260
    return StunPort::Create(main_, factory, &network_,
 
261
                            addr.ipaddr(), 0, 0, kStunAddr);
 
262
  }
 
263
  RelayPort* CreateRelayPort(const SocketAddress& addr,
 
264
                             ProtocolType int_proto, ProtocolType ext_proto) {
 
265
    std::string user = talk_base::CreateRandomString(16);
 
266
    std::string pass = talk_base::CreateRandomString(16);
 
267
    RelayPort* port = RelayPort::Create(main_, &socket_factory_, &network_,
 
268
                                        addr.ipaddr(), 0, 0, user, pass, "");
 
269
    SocketAddress addrs[] =
 
270
        { kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr };
 
271
    port->AddServerAddress(ProtocolAddress(addrs[int_proto], int_proto));
 
272
    // TODO: Add an external address for ext_proto, so that the
 
273
    // other side can connect to this port using a non-UDP protocol.
 
274
    return port;
 
275
  }
 
276
  talk_base::NATServer* CreateNatServer(const SocketAddress& addr,
 
277
                                        talk_base::NATType type) {
 
278
    return new talk_base::NATServer(type, ss_.get(), addr, ss_.get(), addr);
 
279
  }
 
280
  static const char* StunName(NATType type) {
 
281
    switch (type) {
 
282
      case NAT_OPEN_CONE:       return "stun(open cone)";
 
283
      case NAT_ADDR_RESTRICTED: return "stun(addr restricted)";
 
284
      case NAT_PORT_RESTRICTED: return "stun(port restricted)";
 
285
      case NAT_SYMMETRIC:       return "stun(symmetric)";
 
286
      default:                  return "stun(?)";
 
287
    }
 
288
  }
 
289
  static const char* RelayName(ProtocolType type) {
 
290
    switch (type) {
 
291
      case PROTO_UDP:           return "relay(udp)";
 
292
      case PROTO_TCP:           return "relay(tcp)";
 
293
      case PROTO_SSLTCP:        return "relay(ssltcp)";
 
294
      default:                  return "relay(?)";
 
295
    }
 
296
  }
 
297
 
 
298
  // this does all the work
 
299
  void TestConnectivity(const char* name1, Port* port1,
 
300
                        const char* name2, Port* port2,
 
301
                        bool accept, bool same_addr1,
 
302
                        bool same_addr2, bool possible);
 
303
 
 
304
 private:
 
305
  talk_base::Thread* main_;
 
306
  talk_base::scoped_ptr<talk_base::PhysicalSocketServer> pss_;
 
307
  talk_base::scoped_ptr<talk_base::VirtualSocketServer> ss_;
 
308
  talk_base::SocketServerScope ss_scope_;
 
309
  talk_base::Network network_;
 
310
  talk_base::BasicPacketSocketFactory socket_factory_;
 
311
  talk_base::scoped_ptr<talk_base::NATServer> nat_server1_;
 
312
  talk_base::scoped_ptr<talk_base::NATServer> nat_server2_;
 
313
  talk_base::NATSocketFactory nat_factory1_;
 
314
  talk_base::NATSocketFactory nat_factory2_;
 
315
  talk_base::BasicPacketSocketFactory nat_socket_factory1_;
 
316
  talk_base::BasicPacketSocketFactory nat_socket_factory2_;
 
317
  TestStunServer stun_server_;
 
318
  TestRelayServer relay_server_;
 
319
};
 
320
 
 
321
void PortTest::TestConnectivity(const char* name1, Port* port1,
 
322
                                const char* name2, Port* port2,
 
323
                                bool accept, bool same_addr1,
 
324
                                bool same_addr2, bool possible) {
 
325
  LOG(LS_INFO) << "Test: " << name1 << " to " << name2 << ": ";
 
326
  port1->set_name("src");
 
327
  port2->set_name("dst");
 
328
 
 
329
  // Set up channels.
 
330
  TestChannel ch1(port1, port2);
 
331
  TestChannel ch2(port2, port1);
 
332
  EXPECT_EQ(0, ch1.address_count());
 
333
  EXPECT_EQ(0, ch2.address_count());
 
334
 
 
335
  // Acquire addresses.
 
336
  ch1.Start();
 
337
  ch2.Start();
 
338
  ASSERT_EQ_WAIT(1, ch1.address_count(), kTimeout);
 
339
  ASSERT_EQ_WAIT(1, ch2.address_count(), kTimeout);
 
340
 
 
341
  // Send a ping from src to dst. This may or may not make it.
 
342
  ch1.CreateConnection();
 
343
  ASSERT_TRUE(ch1.conn() != NULL);
 
344
  EXPECT_TRUE_WAIT(ch1.conn()->connected(), kTimeout);  // for TCP connect
 
345
  ch1.Ping();
 
346
  WAIT(!ch2.remote_address().IsAny(), kTimeout);
 
347
 
 
348
  if (accept) {
 
349
    // We are able to send a ping from src to dst. This is the case when
 
350
    // sending to UDP ports and cone NATs.
 
351
    EXPECT_TRUE(ch1.remote_address().IsAny());
 
352
    EXPECT_EQ(ch2.remote_fragment(), port1->username_fragment());
 
353
 
 
354
    // Ensure the ping came from the same address used for src.
 
355
    // This is the case unless the source NAT was symmetric.
 
356
    if (same_addr1) EXPECT_EQ(ch2.remote_address(), GetAddress(port1));
 
357
    EXPECT_TRUE(same_addr2);
 
358
 
 
359
    // Send a ping from dst to src.
 
360
    ch2.AcceptConnection();
 
361
    ASSERT_TRUE(ch2.conn() != NULL);
 
362
    ch2.Ping();
 
363
    EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2.conn()->write_state(),
 
364
                   kTimeout);
 
365
  } else {
 
366
    // We can't send a ping from src to dst, so flip it around. This will happen
 
367
    // when the destination NAT is addr/port restricted or symmetric.
 
368
    EXPECT_TRUE(ch1.remote_address().IsAny());
 
369
    EXPECT_TRUE(ch2.remote_address().IsAny());
 
370
 
 
371
    // Send a ping from dst to src. Again, this may or may not make it.
 
372
    ch2.CreateConnection();
 
373
    ASSERT_TRUE(ch2.conn() != NULL);
 
374
    ch2.Ping();
 
375
    WAIT(ch2.conn()->write_state() == Connection::STATE_WRITABLE, kTimeout);
 
376
 
 
377
    if (same_addr1 && same_addr2) {
 
378
      // The new ping got back to the source.
 
379
      EXPECT_EQ(Connection::STATE_READABLE, ch1.conn()->read_state());
 
380
      EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state());
 
381
 
 
382
      // First connection may not be writable if the first ping did not get
 
383
      // through.  So we will have to do another.
 
384
      if (ch1.conn()->write_state() == Connection::STATE_WRITE_CONNECT) {
 
385
        ch1.Ping();
 
386
        EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(),
 
387
                       kTimeout);
 
388
      }
 
389
    } else if (!same_addr1 && possible) {
 
390
      // The new ping went to the candidate address, but that address was bad.
 
391
      // This will happen when the source NAT is symmetric.
 
392
      EXPECT_TRUE(ch1.remote_address().IsAny());
 
393
      EXPECT_TRUE(ch2.remote_address().IsAny());
 
394
 
 
395
      // However, since we have now sent a ping to the source IP, we should be
 
396
      // able to get a ping from it. This gives us the real source address.
 
397
      ch1.Ping();
 
398
      EXPECT_TRUE_WAIT(!ch2.remote_address().IsAny(), kTimeout);
 
399
      EXPECT_EQ(Connection::STATE_READ_TIMEOUT, ch2.conn()->read_state());
 
400
      EXPECT_TRUE(ch1.remote_address().IsAny());
 
401
 
 
402
      // Pick up the actual address and establish the connection.
 
403
      ch2.AcceptConnection();
 
404
      ASSERT_TRUE(ch2.conn() != NULL);
 
405
      ch2.Ping();
 
406
      EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2.conn()->write_state(),
 
407
                     kTimeout);
 
408
    } else if (!same_addr2 && possible) {
 
409
      // The new ping came in, but from an unexpected address. This will happen
 
410
      // when the destination NAT is symmetric.
 
411
      EXPECT_FALSE(ch1.remote_address().IsAny());
 
412
      EXPECT_EQ(Connection::STATE_READ_TIMEOUT, ch1.conn()->read_state());
 
413
 
 
414
      // Update our address and complete the connection.
 
415
      ch1.AcceptConnection();
 
416
      ch1.Ping();
 
417
      EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(),
 
418
                     kTimeout);
 
419
    } else {  // (!possible)
 
420
      // There should be s no way for the pings to reach each other. Check it.
 
421
      EXPECT_TRUE(ch1.remote_address().IsAny());
 
422
      EXPECT_TRUE(ch2.remote_address().IsAny());
 
423
      ch1.Ping();
 
424
      WAIT(!ch2.remote_address().IsAny(), kTimeout);
 
425
      EXPECT_TRUE(ch1.remote_address().IsAny());
 
426
      EXPECT_TRUE(ch2.remote_address().IsAny());
 
427
    }
 
428
  }
 
429
 
 
430
  // Everything should be good, unless we know the situation is impossible.
 
431
  ASSERT_TRUE(ch1.conn() != NULL);
 
432
  ASSERT_TRUE(ch2.conn() != NULL);
 
433
  if (possible) {
 
434
    EXPECT_EQ(Connection::STATE_READABLE, ch1.conn()->read_state());
 
435
    EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state());
 
436
    EXPECT_EQ(Connection::STATE_READABLE, ch2.conn()->read_state());
 
437
    EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state());
 
438
  } else {
 
439
    EXPECT_NE(Connection::STATE_READABLE, ch1.conn()->read_state());
 
440
    EXPECT_NE(Connection::STATE_WRITABLE, ch1.conn()->write_state());
 
441
    EXPECT_NE(Connection::STATE_READABLE, ch2.conn()->read_state());
 
442
    EXPECT_NE(Connection::STATE_WRITABLE, ch2.conn()->write_state());
 
443
  }
 
444
 
 
445
  // Tear down and ensure that goes smoothly.
 
446
  ch1.Stop();
 
447
  ch2.Stop();
 
448
  EXPECT_TRUE_WAIT(ch1.conn() == NULL, kTimeout);
 
449
  EXPECT_TRUE_WAIT(ch2.conn() == NULL, kTimeout);
 
450
}
 
451
 
 
452
class FakePacketSocketFactory : public talk_base::PacketSocketFactory {
 
453
 public:
 
454
  FakePacketSocketFactory()
 
455
      : next_udp_socket_(NULL),
 
456
        next_server_tcp_socket_(NULL),
 
457
        next_client_tcp_socket_(NULL) {
 
458
  }
 
459
  virtual ~FakePacketSocketFactory() { }
 
460
 
 
461
  virtual AsyncPacketSocket* CreateUdpSocket(
 
462
      const SocketAddress& address, int min_port, int max_port) {
 
463
    EXPECT_TRUE(next_udp_socket_ != NULL);
 
464
    AsyncPacketSocket* result = next_udp_socket_;
 
465
    next_udp_socket_ = NULL;
 
466
    return result;
 
467
  }
 
468
 
 
469
  virtual AsyncPacketSocket* CreateServerTcpSocket(
 
470
      const SocketAddress& local_address, int min_port, int max_port,
 
471
      bool ssl) {
 
472
    EXPECT_TRUE(next_server_tcp_socket_ != NULL);
 
473
    AsyncPacketSocket* result = next_server_tcp_socket_;
 
474
    next_server_tcp_socket_ = NULL;
 
475
    return result;
 
476
  }
 
477
 
 
478
  // TODO: |proxy_info| and |user_agent| should be set
 
479
  // per-factory and not when socket is created.
 
480
  virtual AsyncPacketSocket* CreateClientTcpSocket(
 
481
      const SocketAddress& local_address, const SocketAddress& remote_address,
 
482
      const talk_base::ProxyInfo& proxy_info,
 
483
      const std::string& user_agent, bool ssl) {
 
484
    EXPECT_TRUE(next_client_tcp_socket_ != NULL);
 
485
    AsyncPacketSocket* result = next_client_tcp_socket_;
 
486
    next_client_tcp_socket_ = NULL;
 
487
    return result;
 
488
  }
 
489
 
 
490
  void set_next_udp_socket(AsyncPacketSocket* next_udp_socket) {
 
491
    next_udp_socket_ = next_udp_socket;
 
492
  }
 
493
  void set_next_server_tcp_socket(AsyncPacketSocket* next_server_tcp_socket) {
 
494
    next_server_tcp_socket_ = next_server_tcp_socket;
 
495
  }
 
496
  void set_next_client_tcp_socket(AsyncPacketSocket* next_client_tcp_socket) {
 
497
    next_client_tcp_socket_ = next_client_tcp_socket;
 
498
  }
 
499
 
 
500
 private:
 
501
  AsyncPacketSocket* next_udp_socket_;
 
502
  AsyncPacketSocket* next_server_tcp_socket_;
 
503
  AsyncPacketSocket* next_client_tcp_socket_;
 
504
};
 
505
 
 
506
class FakeAsyncPacketSocket : public AsyncPacketSocket {
 
507
 public:
 
508
  // Returns current local address. Address may be set to NULL if the
 
509
  // socket is not bound yet (GetState() returns STATE_BINDING).
 
510
  virtual SocketAddress GetLocalAddress() const {
 
511
    return SocketAddress();
 
512
  }
 
513
 
 
514
  // Returns remote address. Returns zeroes if this is not a client TCP socket.
 
515
  virtual SocketAddress GetRemoteAddress() const {
 
516
    return SocketAddress();
 
517
  }
 
518
 
 
519
  // Send a packet.
 
520
  virtual int Send(const void *pv, size_t cb) {
 
521
    return cb;
 
522
  }
 
523
  virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) {
 
524
    return cb;
 
525
  }
 
526
  virtual int Close() {
 
527
    return 0;
 
528
  }
 
529
 
 
530
  virtual State GetState() const { return state_; }
 
531
  virtual int GetOption(Socket::Option opt, int* value) { return 0; }
 
532
  virtual int SetOption(Socket::Option opt, int value) { return 0; }
 
533
  virtual int GetError() const { return 0; }
 
534
  virtual void SetError(int error) { }
 
535
 
 
536
  void set_state(State state) { state_ = state; }
 
537
 
 
538
 private:
 
539
  State state_;
 
540
};
 
541
 
 
542
// Local -> XXXX
 
543
TEST_F(PortTest, TestLocalToLocal) {
 
544
  TestLocalToLocal();
 
545
}
 
546
 
 
547
TEST_F(PortTest, TestLocalToConeNat) {
 
548
  TestLocalToStun(NAT_OPEN_CONE);
 
549
}
 
550
 
 
551
TEST_F(PortTest, TestLocalToARNat) {
 
552
  TestLocalToStun(NAT_ADDR_RESTRICTED);
 
553
}
 
554
 
 
555
TEST_F(PortTest, TestLocalToPRNat) {
 
556
  TestLocalToStun(NAT_PORT_RESTRICTED);
 
557
}
 
558
 
 
559
TEST_F(PortTest, TestLocalToSymNat) {
 
560
  TestLocalToStun(NAT_SYMMETRIC);
 
561
}
 
562
 
 
563
TEST_F(PortTest, TestLocalToRelay) {
 
564
  TestLocalToRelay(PROTO_UDP);
 
565
}
 
566
 
 
567
TEST_F(PortTest, TestLocalToTcpRelay) {
 
568
  TestLocalToRelay(PROTO_TCP);
 
569
}
 
570
 
 
571
TEST_F(PortTest, TestLocalToSslTcpRelay) {
 
572
  TestLocalToRelay(PROTO_SSLTCP);
 
573
}
 
574
 
 
575
// Cone NAT -> XXXX
 
576
TEST_F(PortTest, TestConeNatToLocal) {
 
577
  TestStunToLocal(NAT_OPEN_CONE);
 
578
}
 
579
 
 
580
TEST_F(PortTest, TestConeNatToConeNat) {
 
581
  TestStunToStun(NAT_OPEN_CONE, NAT_OPEN_CONE);
 
582
}
 
583
 
 
584
TEST_F(PortTest, TestConeNatToARNat) {
 
585
  TestStunToStun(NAT_OPEN_CONE, NAT_ADDR_RESTRICTED);
 
586
}
 
587
 
 
588
TEST_F(PortTest, TestConeNatToPRNat) {
 
589
  TestStunToStun(NAT_OPEN_CONE, NAT_PORT_RESTRICTED);
 
590
}
 
591
 
 
592
TEST_F(PortTest, TestConeNatToSymNat) {
 
593
  TestStunToStun(NAT_OPEN_CONE, NAT_SYMMETRIC);
 
594
}
 
595
 
 
596
TEST_F(PortTest, TestConeNatToRelay) {
 
597
  TestStunToRelay(NAT_OPEN_CONE, PROTO_UDP);
 
598
}
 
599
 
 
600
TEST_F(PortTest, TestConeNatToTcpRelay) {
 
601
  TestStunToRelay(NAT_OPEN_CONE, PROTO_TCP);
 
602
}
 
603
 
 
604
// Address-restricted NAT -> XXXX
 
605
TEST_F(PortTest, TestARNatToLocal) {
 
606
  TestStunToLocal(NAT_ADDR_RESTRICTED);
 
607
}
 
608
 
 
609
TEST_F(PortTest, TestARNatToConeNat) {
 
610
  TestStunToStun(NAT_ADDR_RESTRICTED, NAT_OPEN_CONE);
 
611
}
 
612
 
 
613
TEST_F(PortTest, TestARNatToARNat) {
 
614
  TestStunToStun(NAT_ADDR_RESTRICTED, NAT_ADDR_RESTRICTED);
 
615
}
 
616
 
 
617
TEST_F(PortTest, TestARNatToPRNat) {
 
618
  TestStunToStun(NAT_ADDR_RESTRICTED, NAT_PORT_RESTRICTED);
 
619
}
 
620
 
 
621
TEST_F(PortTest, TestARNatToSymNat) {
 
622
  TestStunToStun(NAT_ADDR_RESTRICTED, NAT_SYMMETRIC);
 
623
}
 
624
 
 
625
TEST_F(PortTest, TestARNatToRelay) {
 
626
  TestStunToRelay(NAT_ADDR_RESTRICTED, PROTO_UDP);
 
627
}
 
628
 
 
629
TEST_F(PortTest, TestARNATNatToTcpRelay) {
 
630
  TestStunToRelay(NAT_ADDR_RESTRICTED, PROTO_TCP);
 
631
}
 
632
 
 
633
// Port-restricted NAT -> XXXX
 
634
TEST_F(PortTest, TestPRNatToLocal) {
 
635
  TestStunToLocal(NAT_PORT_RESTRICTED);
 
636
}
 
637
 
 
638
TEST_F(PortTest, TestPRNatToConeNat) {
 
639
  TestStunToStun(NAT_PORT_RESTRICTED, NAT_OPEN_CONE);
 
640
}
 
641
 
 
642
TEST_F(PortTest, TestPRNatToARNat) {
 
643
  TestStunToStun(NAT_PORT_RESTRICTED, NAT_ADDR_RESTRICTED);
 
644
}
 
645
 
 
646
TEST_F(PortTest, TestPRNatToPRNat) {
 
647
  TestStunToStun(NAT_PORT_RESTRICTED, NAT_PORT_RESTRICTED);
 
648
}
 
649
 
 
650
TEST_F(PortTest, TestPRNatToSymNat) {
 
651
  // Will "fail"
 
652
  TestStunToStun(NAT_PORT_RESTRICTED, NAT_SYMMETRIC);
 
653
}
 
654
 
 
655
TEST_F(PortTest, TestPRNatToRelay) {
 
656
  TestStunToRelay(NAT_PORT_RESTRICTED, PROTO_UDP);
 
657
}
 
658
 
 
659
TEST_F(PortTest, TestPRNatToTcpRelay) {
 
660
  TestStunToRelay(NAT_PORT_RESTRICTED, PROTO_TCP);
 
661
}
 
662
 
 
663
// Symmetric NAT -> XXXX
 
664
TEST_F(PortTest, TestSymNatToLocal) {
 
665
  TestStunToLocal(NAT_SYMMETRIC);
 
666
}
 
667
 
 
668
TEST_F(PortTest, TestSymNatToConeNat) {
 
669
  TestStunToStun(NAT_SYMMETRIC, NAT_OPEN_CONE);
 
670
}
 
671
 
 
672
TEST_F(PortTest, TestSymNatToARNat) {
 
673
  TestStunToStun(NAT_SYMMETRIC, NAT_ADDR_RESTRICTED);
 
674
}
 
675
 
 
676
TEST_F(PortTest, TestSymNatToPRNat) {
 
677
  // Will "fail"
 
678
  TestStunToStun(NAT_SYMMETRIC, NAT_PORT_RESTRICTED);
 
679
}
 
680
 
 
681
TEST_F(PortTest, TestSymNatToSymNat) {
 
682
  // Will "fail"
 
683
  TestStunToStun(NAT_SYMMETRIC, NAT_SYMMETRIC);
 
684
}
 
685
 
 
686
TEST_F(PortTest, TestSymNatToRelay) {
 
687
  TestStunToRelay(NAT_SYMMETRIC, PROTO_UDP);
 
688
}
 
689
 
 
690
TEST_F(PortTest, TestSymNatToTcpRelay) {
 
691
  TestStunToRelay(NAT_SYMMETRIC, PROTO_TCP);
 
692
}
 
693
 
 
694
// Outbound TCP -> XXXX
 
695
TEST_F(PortTest, TestTcpToTcp) {
 
696
  TestTcpToTcp();
 
697
}
 
698
 
 
699
/* TODO: Enable these once testrelayserver can accept external TCP.
 
700
TEST_F(PortTest, TestTcpToTcpRelay) {
 
701
  TestTcpToRelay(PROTO_TCP);
 
702
}
 
703
 
 
704
TEST_F(PortTest, TestTcpToSslTcpRelay) {
 
705
  TestTcpToRelay(PROTO_SSLTCP);
 
706
}
 
707
*/
 
708
 
 
709
// Outbound SSLTCP -> XXXX
 
710
/* TODO: Enable these once testrelayserver can accept external SSL.
 
711
TEST_F(PortTest, TestSslTcpToTcpRelay) {
 
712
  TestSslTcpToRelay(PROTO_TCP);
 
713
}
 
714
 
 
715
TEST_F(PortTest, TestSslTcpToSslTcpRelay) {
 
716
  TestSslTcpToRelay(PROTO_SSLTCP);
 
717
}
 
718
*/
 
719
 
 
720
TEST_F(PortTest, TestTcpNoDelay) {
 
721
  TCPPort* port1 = CreateTcpPort(kLocalAddr1);
 
722
  int option_value = -1;
 
723
  int success = port1->GetOption(talk_base::Socket::OPT_NODELAY,
 
724
                                 &option_value);
 
725
  ASSERT_EQ(0, success);  // GetOption() should complete successfully w/ 0
 
726
  ASSERT_EQ(1, option_value);
 
727
  delete port1;
 
728
}
 
729
 
 
730
TEST_F(PortTest, TestDelayedBindingUdp) {
 
731
  FakeAsyncPacketSocket *socket = new FakeAsyncPacketSocket();
 
732
  FakePacketSocketFactory socket_factory;
 
733
 
 
734
  socket_factory.set_next_udp_socket(socket);
 
735
  scoped_ptr<UDPPort> port(
 
736
      CreateUdpPort(kLocalAddr1, &socket_factory));
 
737
 
 
738
  socket->set_state(AsyncPacketSocket::STATE_BINDING);
 
739
  port->PrepareAddress();
 
740
 
 
741
  EXPECT_EQ(0U, port->candidates().size());
 
742
  socket->SignalAddressReady(socket, kLocalAddr2);
 
743
 
 
744
  EXPECT_EQ(1U, port->candidates().size());
 
745
}
 
746
 
 
747
TEST_F(PortTest, TestDelayedBindingTcp) {
 
748
  FakeAsyncPacketSocket *socket = new FakeAsyncPacketSocket();
 
749
  FakePacketSocketFactory socket_factory;
 
750
 
 
751
  socket_factory.set_next_server_tcp_socket(socket);
 
752
  scoped_ptr<TCPPort> port(
 
753
      CreateTcpPort(kLocalAddr1, &socket_factory));
 
754
 
 
755
  socket->set_state(AsyncPacketSocket::STATE_BINDING);
 
756
  port->PrepareAddress();
 
757
 
 
758
  EXPECT_EQ(0U, port->candidates().size());
 
759
  socket->SignalAddressReady(socket, kLocalAddr2);
 
760
 
 
761
  EXPECT_EQ(1U, port->candidates().size());
 
762
}