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" |
| 9 | #include "platform/test/mock_udp_socket.h" |
| 10 | |
| 11 | namespace openscreen { |
| 12 | namespace platform { |
| 13 | |
| 14 | // Tests that a UdpSocket that does not specify any address or port will |
| 15 | // successfully Bind(), and that the operating system will return the |
| 16 | // auto-assigned socket name (i.e., the local endpoint's port will not be zero). |
| 17 | TEST(SocketIntegrationTest, ResolvesLocalEndpoint_IPv4) { |
| 18 | const uint8_t kIpV4AddrAny[4] = {}; |
| 19 | FakeClock clock(Clock::now()); |
| 20 | FakeTaskRunner task_runner(&clock); |
| 21 | MockUdpSocket::MockClient client; |
| 22 | ErrorOr<UdpSocketUniquePtr> create_result = UdpSocket::Create( |
| 23 | &task_runner, &client, IPEndpoint{IPAddress(kIpV4AddrAny), 0}); |
| 24 | ASSERT_TRUE(create_result) << create_result.error(); |
| 25 | const auto socket = create_result.MoveValue(); |
| 26 | const Error bind_result = socket->Bind(); |
| 27 | ASSERT_TRUE(bind_result.ok()) << bind_result; |
| 28 | const IPEndpoint local_endpoint = socket->GetLocalEndpoint(); |
| 29 | EXPECT_NE(local_endpoint.port, 0) << local_endpoint; |
| 30 | } |
| 31 | |
| 32 | // Tests that a UdpSocket that does not specify any address or port will |
| 33 | // successfully Bind(), and that the operating system will return the |
| 34 | // auto-assigned socket name (i.e., the local endpoint's port will not be zero). |
| 35 | TEST(SocketIntegrationTest, ResolvesLocalEndpoint_IPv6) { |
| 36 | const uint8_t kIpV6AddrAny[16] = {}; |
| 37 | FakeClock clock(Clock::now()); |
| 38 | FakeTaskRunner task_runner(&clock); |
| 39 | MockUdpSocket::MockClient client; |
| 40 | ErrorOr<UdpSocketUniquePtr> create_result = UdpSocket::Create( |
| 41 | &task_runner, &client, IPEndpoint{IPAddress(kIpV6AddrAny), 0}); |
| 42 | ASSERT_TRUE(create_result) << create_result.error(); |
| 43 | const auto socket = create_result.MoveValue(); |
| 44 | const Error bind_result = socket->Bind(); |
| 45 | ASSERT_TRUE(bind_result.ok()) << bind_result; |
| 46 | const IPEndpoint local_endpoint = socket->GetLocalEndpoint(); |
| 47 | EXPECT_NE(local_endpoint.port, 0) << local_endpoint; |
| 48 | } |
| 49 | |
| 50 | } // namespace platform |
| 51 | } // namespace openscreen |