blob: 3014507f6fe9bb3a16e6f05233417142a4d43347 [file] [log] [blame]
Ryan Keane493029d2019-08-19 14:01:04 -07001// 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 Keane7d4b5fc2019-08-30 12:38:39 -07009#include "platform/test/fake_udp_socket.h"
Ryan Keane493029d2019-08-19 14:01:04 -070010
11namespace openscreen {
12namespace platform {
13
Ryan Keane83e68322019-08-23 14:54:38 -070014using testing::_;
15
Ryan Keane493029d2019-08-19 14:01:04 -070016// 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).
19TEST(SocketIntegrationTest, ResolvesLocalEndpoint_IPv4) {
20 const uint8_t kIpV4AddrAny[4] = {};
21 FakeClock clock(Clock::now());
22 FakeTaskRunner task_runner(&clock);
Ryan Keane7d4b5fc2019-08-30 12:38:39 -070023 FakeUdpSocket::MockClient client;
Ryan Keane493029d2019-08-19 14:01:04 -070024 ErrorOr<UdpSocketUniquePtr> create_result = UdpSocket::Create(
25 &task_runner, &client, IPEndpoint{IPAddress(kIpV4AddrAny), 0});
26 ASSERT_TRUE(create_result) << create_result.error();
Jordan Baylesca5885e2019-09-06 14:09:10 -070027 const auto socket = std::move(create_result.value());
Ryan Keane83e68322019-08-23 14:54:38 -070028 EXPECT_CALL(client, OnError(_, _)).Times(0);
29 socket->Bind();
Ryan Keane493029d2019-08-19 14:01:04 -070030 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).
37TEST(SocketIntegrationTest, ResolvesLocalEndpoint_IPv6) {
Yuri Wiitala75ea15d2019-12-03 16:01:48 -080038 const uint16_t kIpV6AddrAny[8] = {};
Ryan Keane493029d2019-08-19 14:01:04 -070039 FakeClock clock(Clock::now());
40 FakeTaskRunner task_runner(&clock);
Ryan Keane7d4b5fc2019-08-30 12:38:39 -070041 FakeUdpSocket::MockClient client;
Ryan Keane493029d2019-08-19 14:01:04 -070042 ErrorOr<UdpSocketUniquePtr> create_result = UdpSocket::Create(
43 &task_runner, &client, IPEndpoint{IPAddress(kIpV6AddrAny), 0});
44 ASSERT_TRUE(create_result) << create_result.error();
Jordan Baylesca5885e2019-09-06 14:09:10 -070045 const auto socket = std::move(create_result.value());
Ryan Keane83e68322019-08-23 14:54:38 -070046 EXPECT_CALL(client, OnError(_, _)).Times(0);
47 socket->Bind();
Ryan Keane493029d2019-08-19 14:01:04 -070048 const IPEndpoint local_endpoint = socket->GetLocalEndpoint();
49 EXPECT_NE(local_endpoint.port, 0) << local_endpoint;
50}
51
52} // namespace platform
53} // namespace openscreen