File: tcp_port_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (446 lines) | stat: -rw-r--r-- 18,552 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 *  Copyright 2016 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "p2p/base/tcp_port.h"

#include <cstdint>
#include <list>
#include <memory>
#include <string>
#include <vector>

#include "api/candidate.h"
#include "api/environment/environment.h"
#include "api/environment/environment_factory.h"
#include "api/test/rtc_error_matchers.h"
#include "api/units/time_delta.h"
#include "p2p/base/basic_packet_socket_factory.h"
#include "p2p/base/connection.h"
#include "p2p/base/p2p_constants.h"
#include "p2p/base/port.h"
#include "p2p/base/transport_description.h"
#include "rtc_base/async_packet_socket.h"
#include "rtc_base/checks.h"
#include "rtc_base/crypto_random.h"
#include "rtc_base/ip_address.h"
#include "rtc_base/network.h"
#include "rtc_base/network/sent_packet.h"
#include "rtc_base/socket.h"
#include "rtc_base/socket_address.h"
#include "rtc_base/third_party/sigslot/sigslot.h"
#include "rtc_base/thread.h"
#include "rtc_base/time_utils.h"
#include "rtc_base/virtual_socket_server.h"
#include "test/gmock.h"
#include "test/gtest.h"
#include "test/wait_until.h"

using ::testing::Eq;
using ::testing::IsTrue;
using ::webrtc::Connection;
using ::webrtc::CreateEnvironment;
using ::webrtc::Environment;
using ::webrtc::ICE_PWD_LENGTH;
using ::webrtc::ICE_UFRAG_LENGTH;
using ::webrtc::Port;
using ::webrtc::SocketAddress;
using ::webrtc::TCPPort;

static int kTimeout = 1000;
static const SocketAddress kLocalAddr("11.11.11.11", 0);
static const SocketAddress kLocalIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
                                          0);
static const SocketAddress kAlternateLocalAddr("1.2.3.4", 0);
static const SocketAddress kRemoteAddr("22.22.22.22", 0);
static const SocketAddress kRemoteIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c4",
                                           0);

constexpr uint64_t kTiebreakerDefault = 44444;

class ConnectionObserver : public sigslot::has_slots<> {
 public:
  explicit ConnectionObserver(Connection* conn) : conn_(conn) {
    conn->SignalDestroyed.connect(this, &ConnectionObserver::OnDestroyed);
  }

  ~ConnectionObserver() {
    if (!connection_destroyed_) {
      RTC_DCHECK(conn_);
      conn_->SignalDestroyed.disconnect(this);
    }
  }

  bool connection_destroyed() { return connection_destroyed_; }

 private:
  void OnDestroyed(Connection*) { connection_destroyed_ = true; }

  Connection* const conn_;
  bool connection_destroyed_ = false;
};

class TCPPortTest : public ::testing::Test, public sigslot::has_slots<> {
 public:
  TCPPortTest()
      : ss_(new webrtc::VirtualSocketServer()),
        main_(ss_.get()),
        socket_factory_(ss_.get()),
        username_(webrtc::CreateRandomString(webrtc::ICE_UFRAG_LENGTH)),
        password_(webrtc::CreateRandomString(webrtc::ICE_PWD_LENGTH)) {}

  webrtc::Network* MakeNetwork(const SocketAddress& addr) {
    networks_.emplace_back("unittest", "unittest", addr.ipaddr(), 32);
    networks_.back().AddIP(addr.ipaddr());
    return &networks_.back();
  }

  std::unique_ptr<TCPPort> CreateTCPPort(const SocketAddress& addr,
                                         bool allow_listen = true,
                                         int port_number = 0) {
    auto port = std::unique_ptr<TCPPort>(
        TCPPort::Create({.env = env_,
                         .network_thread = &main_,
                         .socket_factory = &socket_factory_,
                         .network = MakeNetwork(addr),
                         .ice_username_fragment = username_,
                         .ice_password = password_},
                        port_number, port_number, allow_listen));
    port->SetIceTiebreaker(kTiebreakerDefault);
    return port;
  }

  std::unique_ptr<TCPPort> CreateTCPPort(const webrtc::Network* network) {
    auto port = std::unique_ptr<TCPPort>(
        TCPPort::Create({.env = env_,
                         .network_thread = &main_,
                         .socket_factory = &socket_factory_,
                         .network = network,
                         .ice_username_fragment = username_,
                         .ice_password = password_},
                        0, 0, true));
    port->SetIceTiebreaker(kTiebreakerDefault);
    return port;
  }

 protected:
  const Environment env_ = CreateEnvironment();
  // When a "create port" helper method is called with an IP, we create a
  // Network with that IP and add it to this list. Using a list instead of a
  // vector so that when it grows, pointers aren't invalidated.
  std::list<webrtc::Network> networks_;
  std::unique_ptr<webrtc::VirtualSocketServer> ss_;
  webrtc::AutoSocketServerThread main_;
  webrtc::BasicPacketSocketFactory socket_factory_;
  std::string username_;
  std::string password_;
};

TEST_F(TCPPortTest, TestTCPPortWithLocalhostAddress) {
  SocketAddress local_address("127.0.0.1", 0);
  // After calling this, when TCPPort attempts to get a socket bound to
  // kLocalAddr, it will end up using localhost instead.
  ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(), local_address.ipaddr());
  auto local_port = CreateTCPPort(kLocalAddr);
  auto remote_port = CreateTCPPort(kRemoteAddr);
  local_port->PrepareAddress();
  remote_port->PrepareAddress();
  Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0],
                                                  Port::ORIGIN_MESSAGE);
  EXPECT_THAT(
      webrtc::WaitUntil([&] { return conn->connected(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());
  // Verify that the socket actually used localhost, otherwise this test isn't
  // doing what it meant to.
  ASSERT_EQ(local_address.ipaddr(),
            local_port->Candidates()[0].address().ipaddr());
}

// If the address the socket ends up bound to does not match any address of the
// TCPPort's Network, then the socket should be discarded and no candidates
// should be signaled. In the context of ICE, where one TCPPort is created for
// each Network, when this happens it's likely that the unexpected address is
// associated with some other Network, which another TCPPort is already
// covering.
TEST_F(TCPPortTest, TCPPortDiscardedIfBoundAddressDoesNotMatchNetwork) {
  // Sockets bound to kLocalAddr will actually end up with kAlternateLocalAddr.
  ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(),
                                  kAlternateLocalAddr.ipaddr());

  // Create ports (local_port is the one whose IP will end up reassigned).
  auto local_port = CreateTCPPort(kLocalAddr);
  auto remote_port = CreateTCPPort(kRemoteAddr);
  local_port->PrepareAddress();
  remote_port->PrepareAddress();

  // Tell port to create a connection; it should be destroyed when it's
  // realized that it's using an unexpected address.
  Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0],
                                                  Port::ORIGIN_MESSAGE);
  ConnectionObserver observer(conn);
  EXPECT_THAT(webrtc::WaitUntil(
                  [&] { return observer.connection_destroyed(); }, IsTrue(),
                  {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
              webrtc::IsRtcOk());
}

// A caveat for the above logic: if the socket ends up bound to one of the IPs
// associated with the Network, just not the "best" one, this is ok.
TEST_F(TCPPortTest, TCPPortNotDiscardedIfNotBoundToBestIP) {
  // Sockets bound to kLocalAddr will actually end up with kAlternateLocalAddr.
  ss_->SetAlternativeLocalAddress(kLocalAddr.ipaddr(),
                                  kAlternateLocalAddr.ipaddr());

  // Set up a network with kLocalAddr1 as the "best" IP, and kAlternateLocalAddr
  // as an alternate.
  webrtc::Network* network = MakeNetwork(kLocalAddr);
  network->AddIP(kAlternateLocalAddr.ipaddr());
  ASSERT_EQ(kLocalAddr.ipaddr(), network->GetBestIP());

  // Create ports (using our special 2-IP Network for local_port).
  auto local_port = CreateTCPPort(network);
  auto remote_port = CreateTCPPort(kRemoteAddr);
  local_port->PrepareAddress();
  remote_port->PrepareAddress();

  // Expect connection to succeed.
  Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0],
                                                  Port::ORIGIN_MESSAGE);
  EXPECT_THAT(
      webrtc::WaitUntil([&] { return conn->connected(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());

  // Verify that the socket actually used the alternate address, otherwise this
  // test isn't doing what it meant to.
  ASSERT_EQ(kAlternateLocalAddr.ipaddr(),
            local_port->Candidates()[0].address().ipaddr());
}

// Regression test for crbug.com/webrtc/8972, caused by buggy comparison
// between webrtc::IPAddress and webrtc::InterfaceAddress.
TEST_F(TCPPortTest, TCPPortNotDiscardedIfBoundToTemporaryIP) {
  networks_.emplace_back("unittest", "unittest", kLocalIPv6Addr.ipaddr(), 32);
  networks_.back().AddIP(webrtc::InterfaceAddress(
      kLocalIPv6Addr.ipaddr(), webrtc::IPV6_ADDRESS_FLAG_TEMPORARY));

  auto local_port = CreateTCPPort(&networks_.back());
  auto remote_port = CreateTCPPort(kRemoteIPv6Addr);
  local_port->PrepareAddress();
  remote_port->PrepareAddress();

  // Connection should succeed if the port isn't discarded.
  Connection* conn = local_port->CreateConnection(remote_port->Candidates()[0],
                                                  Port::ORIGIN_MESSAGE);
  ASSERT_NE(nullptr, conn);
  EXPECT_THAT(
      webrtc::WaitUntil([&] { return conn->connected(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());
}

class SentPacketCounter : public sigslot::has_slots<> {
 public:
  explicit SentPacketCounter(TCPPort* p) {
    p->SignalSentPacket.connect(this, &SentPacketCounter::OnSentPacket);
  }

  int sent_packets() const { return sent_packets_; }

 private:
  void OnSentPacket(const webrtc::SentPacketInfo&) { ++sent_packets_; }

  int sent_packets_ = 0;
};

// Test that SignalSentPacket is fired when a packet is successfully sent, for
// both TCP client and server sockets.
TEST_F(TCPPortTest, SignalSentPacket) {
  std::unique_ptr<TCPPort> client(CreateTCPPort(kLocalAddr));
  std::unique_ptr<TCPPort> server(CreateTCPPort(kRemoteAddr));
  client->SetIceRole(webrtc::ICEROLE_CONTROLLING);
  server->SetIceRole(webrtc::ICEROLE_CONTROLLED);
  client->PrepareAddress();
  server->PrepareAddress();

  Connection* client_conn =
      client->CreateConnection(server->Candidates()[0], Port::ORIGIN_MESSAGE);
  ASSERT_NE(nullptr, client_conn);
  ASSERT_THAT(
      webrtc::WaitUntil([&] { return client_conn->connected(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());

  // Need to get the port of the actual outgoing socket, not the server socket..
  webrtc::Candidate client_candidate = client->Candidates()[0];
  client_candidate.set_address(static_cast<webrtc::TCPConnection*>(client_conn)
                                   ->socket()
                                   ->GetLocalAddress());
  Connection* server_conn =
      server->CreateConnection(client_candidate, Port::ORIGIN_THIS_PORT);
  ASSERT_NE(nullptr, server_conn);
  ASSERT_THAT(
      webrtc::WaitUntil([&] { return server_conn->connected(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());

  client_conn->Ping(webrtc::TimeMillis());
  server_conn->Ping(webrtc::TimeMillis());
  ASSERT_THAT(
      webrtc::WaitUntil([&] { return client_conn->writable(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());
  ASSERT_THAT(
      webrtc::WaitUntil([&] { return server_conn->writable(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());

  SentPacketCounter client_counter(client.get());
  SentPacketCounter server_counter(server.get());
  static const char kData[] = "hello";
  for (int i = 0; i < 10; ++i) {
    client_conn->Send(&kData, sizeof(kData),
                      webrtc::AsyncSocketPacketOptions());
    server_conn->Send(&kData, sizeof(kData),
                      webrtc::AsyncSocketPacketOptions());
  }
  EXPECT_THAT(
      webrtc::WaitUntil([&] { return client_counter.sent_packets(); }, Eq(10),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());
  EXPECT_THAT(
      webrtc::WaitUntil([&] { return server_counter.sent_packets(); }, Eq(10),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());
}

// Test that SignalSentPacket is fired when a packet is successfully sent, even
// after a remote server has been restarted.
TEST_F(TCPPortTest, SignalSentPacketAfterReconnect) {
  std::unique_ptr<TCPPort> client(
      CreateTCPPort(kLocalAddr, /*allow_listen=*/false));
  constexpr int kServerPort = 123;
  std::unique_ptr<TCPPort> server(
      CreateTCPPort(kRemoteAddr, /*allow_listen=*/true, kServerPort));
  client->SetIceRole(webrtc::ICEROLE_CONTROLLING);
  server->SetIceRole(webrtc::ICEROLE_CONTROLLED);
  client->PrepareAddress();
  server->PrepareAddress();

  Connection* client_conn =
      client->CreateConnection(server->Candidates()[0], Port::ORIGIN_MESSAGE);
  ASSERT_NE(nullptr, client_conn);
  ASSERT_THAT(
      webrtc::WaitUntil([&] { return client_conn->connected(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());

  // Need to get the port of the actual outgoing socket.
  webrtc::Candidate client_candidate = client->Candidates()[0];
  client_candidate.set_address(static_cast<webrtc::TCPConnection*>(client_conn)
                                   ->socket()
                                   ->GetLocalAddress());
  client_candidate.set_tcptype("");
  Connection* server_conn =
      server->CreateConnection(client_candidate, Port::ORIGIN_THIS_PORT);
  ASSERT_THAT(
      webrtc::WaitUntil([&] { return server_conn->connected(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());
  EXPECT_FALSE(client_conn->writable());
  client_conn->Ping(webrtc::TimeMillis());
  ASSERT_THAT(
      webrtc::WaitUntil([&] { return client_conn->writable(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());

  SentPacketCounter client_counter(client.get());
  static const char kData[] = "hello";
  int result = client_conn->Send(&kData, sizeof(kData),
                                 webrtc::AsyncSocketPacketOptions());
  EXPECT_EQ(result, 6);

  // Deleting the server port should break the current connection.
  server = nullptr;
  server_conn = nullptr;
  ASSERT_THAT(
      webrtc::WaitUntil([&] { return !client_conn->connected(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());

  // Recreate the server port with the same port number.
  server = CreateTCPPort(kRemoteAddr, /*allow_listen=*/true, kServerPort);
  server->SetIceRole(webrtc::ICEROLE_CONTROLLED);
  server->PrepareAddress();

  // Sending a packet from the client will trigger a reconnect attempt but the
  // packet will be discarded.
  result = client_conn->Send(&kData, sizeof(kData),
                             webrtc::AsyncSocketPacketOptions());
  EXPECT_EQ(result, SOCKET_ERROR);
  ASSERT_THAT(
      webrtc::WaitUntil([&] { return client_conn->connected(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());
  // For unknown reasons, connection is still supposed to be writable....
  EXPECT_TRUE(client_conn->writable());
  for (int i = 0; i < 10; ++i) {
    // All sent packets still fail to send.
    EXPECT_EQ(client_conn->Send(&kData, sizeof(kData),
                                webrtc::AsyncSocketPacketOptions()),
              SOCKET_ERROR);
  }
  // And are not reported as sent.
  EXPECT_THAT(
      webrtc::WaitUntil([&] { return client_counter.sent_packets(); }, Eq(1),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());

  // Create the server connection again so server can reply to STUN pings.
  // Client outgoing socket port will have changed since the client create a new
  // socket when it reconnect.
  client_candidate = client->Candidates()[0];
  client_candidate.set_address(static_cast<webrtc::TCPConnection*>(client_conn)
                                   ->socket()
                                   ->GetLocalAddress());
  client_candidate.set_tcptype("");
  server_conn =
      server->CreateConnection(client_candidate, Port::ORIGIN_THIS_PORT);
  ASSERT_THAT(
      webrtc::WaitUntil([&] { return server_conn->connected(); }, IsTrue(),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());
  EXPECT_THAT(
      webrtc::WaitUntil([&] { return client_counter.sent_packets(); }, Eq(1),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());

  // Send Stun Binding request.
  client_conn->Ping(webrtc::TimeMillis());
  // The Stun Binding request is reported as sent.
  EXPECT_THAT(
      webrtc::WaitUntil([&] { return client_counter.sent_packets(); }, Eq(2),
                        {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
      webrtc::IsRtcOk());
  // Wait a bit for the Stun response to be received.
  webrtc::Thread::Current()->ProcessMessages(100);

  // After the Stun Ping response has been received, packets can be sent again
  // and SignalSentPacket should be invoked.
  for (int i = 0; i < 5; ++i) {
    EXPECT_EQ(client_conn->Send(&kData, sizeof(kData),
                                webrtc::AsyncSocketPacketOptions()),
              6);
  }
  EXPECT_THAT(webrtc::WaitUntil(
                  [&] { return client_counter.sent_packets(); }, Eq(2 + 5),
                  {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
              webrtc::IsRtcOk());
}