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