Ryan Keane | 493029d | 2019-08-19 14:01:04 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | #include "platform/api/time.h" |
| 7 | #include "platform/test/fake_clock.h" |
| 8 | #include "platform/test/fake_task_runner.h" |
Ryan Keane | 7d4b5fc | 2019-08-30 12:38:39 -0700 | [diff] [blame] | 9 | #include "platform/test/fake_udp_socket.h" |
Ryan Keane | 493029d | 2019-08-19 14:01:04 -0700 | [diff] [blame] | 10 | |
| 11 | namespace openscreen { |
| 12 | namespace platform { |
| 13 | |
Ryan Keane | 83e6832 | 2019-08-23 14:54:38 -0700 | [diff] [blame] | 14 | using testing::_; |
| 15 | |
Ryan Keane | 493029d | 2019-08-19 14:01:04 -0700 | [diff] [blame] | 16 | // Tests that a UdpSocket that does not specify any address or port will |
| 17 | // successfully Bind(), and that the operating system will return the |
| 18 | // auto-assigned socket name (i.e., the local endpoint's port will not be zero). |
| 19 | TEST(SocketIntegrationTest, ResolvesLocalEndpoint_IPv4) { |
| 20 | const uint8_t kIpV4AddrAny[4] = {}; |
| 21 | FakeClock clock(Clock::now()); |
| 22 | FakeTaskRunner task_runner(&clock); |
Ryan Keane | 7d4b5fc | 2019-08-30 12:38:39 -0700 | [diff] [blame] | 23 | FakeUdpSocket::MockClient client; |
Ryan Keane | 493029d | 2019-08-19 14:01:04 -0700 | [diff] [blame] | 24 | ErrorOr<UdpSocketUniquePtr> create_result = UdpSocket::Create( |
| 25 | &task_runner, &client, IPEndpoint{IPAddress(kIpV4AddrAny), 0}); |
| 26 | ASSERT_TRUE(create_result) << create_result.error(); |
Jordan Bayles | ca5885e | 2019-09-06 14:09:10 -0700 | [diff] [blame] | 27 | const auto socket = std::move(create_result.value()); |
Ryan Keane | 83e6832 | 2019-08-23 14:54:38 -0700 | [diff] [blame] | 28 | EXPECT_CALL(client, OnError(_, _)).Times(0); |
| 29 | socket->Bind(); |
Ryan Keane | 493029d | 2019-08-19 14:01:04 -0700 | [diff] [blame] | 30 | const IPEndpoint local_endpoint = socket->GetLocalEndpoint(); |
| 31 | EXPECT_NE(local_endpoint.port, 0) << local_endpoint; |
| 32 | } |
| 33 | |
| 34 | // Tests that a UdpSocket that does not specify any address or port will |
| 35 | // successfully Bind(), and that the operating system will return the |
| 36 | // auto-assigned socket name (i.e., the local endpoint's port will not be zero). |
| 37 | TEST(SocketIntegrationTest, ResolvesLocalEndpoint_IPv6) { |
Yuri Wiitala | 75ea15d | 2019-12-03 16:01:48 -0800 | [diff] [blame^] | 38 | const uint16_t kIpV6AddrAny[8] = {}; |
Ryan Keane | 493029d | 2019-08-19 14:01:04 -0700 | [diff] [blame] | 39 | FakeClock clock(Clock::now()); |
| 40 | FakeTaskRunner task_runner(&clock); |
Ryan Keane | 7d4b5fc | 2019-08-30 12:38:39 -0700 | [diff] [blame] | 41 | FakeUdpSocket::MockClient client; |
Ryan Keane | 493029d | 2019-08-19 14:01:04 -0700 | [diff] [blame] | 42 | ErrorOr<UdpSocketUniquePtr> create_result = UdpSocket::Create( |
| 43 | &task_runner, &client, IPEndpoint{IPAddress(kIpV6AddrAny), 0}); |
| 44 | ASSERT_TRUE(create_result) << create_result.error(); |
Jordan Bayles | ca5885e | 2019-09-06 14:09:10 -0700 | [diff] [blame] | 45 | const auto socket = std::move(create_result.value()); |
Ryan Keane | 83e6832 | 2019-08-23 14:54:38 -0700 | [diff] [blame] | 46 | EXPECT_CALL(client, OnError(_, _)).Times(0); |
| 47 | socket->Bind(); |
Ryan Keane | 493029d | 2019-08-19 14:01:04 -0700 | [diff] [blame] | 48 | const IPEndpoint local_endpoint = socket->GetLocalEndpoint(); |
| 49 | EXPECT_NE(local_endpoint.port, 0) << local_endpoint; |
| 50 | } |
| 51 | |
| 52 | } // namespace platform |
| 53 | } // namespace openscreen |