Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -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 "platform/api/udp_socket.h" |
| 6 | |
| 7 | #include "gtest/gtest.h" |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame^] | 8 | #include "platform/api/time.h" |
| 9 | #include "platform/test/fake_clock.h" |
| 10 | #include "platform/test/fake_task_runner.h" |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 11 | #include "platform/test/mock_udp_socket.h" |
| 12 | |
| 13 | namespace openscreen { |
| 14 | namespace platform { |
| 15 | |
| 16 | // Under some conditions, calling a callback can result in an exception |
| 17 | // "terminate called after throwing an instance of 'std::bad_function_call'" |
| 18 | // which will then crash the running code. This test ensures that deleting a |
| 19 | // new, unmodified UDP Socket object doesn't hit this edge case. |
| 20 | TEST(UdpSocketTest, TestDeletionWithoutCallbackSet) { |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame^] | 21 | FakeClock clock(Clock::now()); |
| 22 | FakeTaskRunner task_runner(&clock); |
| 23 | MockUdpSocket::MockClient client; |
| 24 | UdpSocket* socket = |
| 25 | new MockUdpSocket(&task_runner, &client, UdpSocket::Version::kV4); |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 26 | delete socket; |
| 27 | } |
| 28 | |
| 29 | TEST(UdpSocketTest, TestCallbackCalledOnDeletion) { |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame^] | 30 | FakeClock clock(Clock::now()); |
| 31 | FakeTaskRunner task_runner(&clock); |
| 32 | MockUdpSocket::MockClient client; |
| 33 | UdpSocket* socket = |
| 34 | new MockUdpSocket(&task_runner, &client, UdpSocket::Version::kV4); |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 35 | int call_count = 0; |
| 36 | std::function<void(UdpSocket*)> callback = [&call_count](UdpSocket* socket) { |
| 37 | call_count++; |
| 38 | }; |
| 39 | socket->SetDeletionCallback(callback); |
| 40 | |
| 41 | EXPECT_EQ(call_count, 0); |
| 42 | delete socket; |
| 43 | |
| 44 | EXPECT_EQ(call_count, 1); |
| 45 | } |
| 46 | |
Yuri Wiitala | de01e53 | 2019-08-06 18:12:02 -0700 | [diff] [blame] | 47 | // Tests that a UdpSocket that does not specify any address or port will |
| 48 | // successfully Bind(), and that the operating system will return the |
| 49 | // auto-assigned socket name (i.e., the local endpoint's port will not be zero). |
| 50 | TEST(UdpSocketTest, ResolvesLocalEndpoint_IPv4) { |
| 51 | const uint8_t kIpV4AddrAny[4] = {}; |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame^] | 52 | FakeClock clock(Clock::now()); |
| 53 | FakeTaskRunner task_runner(&clock); |
| 54 | MockUdpSocket::MockClient client; |
| 55 | ErrorOr<UdpSocketUniquePtr> create_result = UdpSocket::Create( |
| 56 | &task_runner, &client, IPEndpoint{IPAddress(kIpV4AddrAny), 0}); |
Yuri Wiitala | de01e53 | 2019-08-06 18:12:02 -0700 | [diff] [blame] | 57 | ASSERT_TRUE(create_result) << create_result.error(); |
| 58 | const auto socket = create_result.MoveValue(); |
| 59 | const Error bind_result = socket->Bind(); |
| 60 | ASSERT_TRUE(bind_result.ok()) << bind_result; |
| 61 | const IPEndpoint local_endpoint = socket->GetLocalEndpoint(); |
| 62 | EXPECT_NE(local_endpoint.port, 0) << local_endpoint; |
| 63 | } |
| 64 | |
| 65 | // Tests that a UdpSocket that does not specify any address or port will |
| 66 | // successfully Bind(), and that the operating system will return the |
| 67 | // auto-assigned socket name (i.e., the local endpoint's port will not be zero). |
| 68 | TEST(UdpSocketTest, ResolvesLocalEndpoint_IPv6) { |
| 69 | const uint8_t kIpV6AddrAny[16] = {}; |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame^] | 70 | FakeClock clock(Clock::now()); |
| 71 | FakeTaskRunner task_runner(&clock); |
| 72 | MockUdpSocket::MockClient client; |
| 73 | ErrorOr<UdpSocketUniquePtr> create_result = UdpSocket::Create( |
| 74 | &task_runner, &client, IPEndpoint{IPAddress(kIpV6AddrAny), 0}); |
Yuri Wiitala | de01e53 | 2019-08-06 18:12:02 -0700 | [diff] [blame] | 75 | ASSERT_TRUE(create_result) << create_result.error(); |
| 76 | const auto socket = create_result.MoveValue(); |
| 77 | const Error bind_result = socket->Bind(); |
| 78 | ASSERT_TRUE(bind_result.ok()) << bind_result; |
| 79 | const IPEndpoint local_endpoint = socket->GetLocalEndpoint(); |
| 80 | EXPECT_NE(local_endpoint.port, 0) << local_endpoint; |
| 81 | } |
| 82 | |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 83 | } // namespace platform |
| 84 | } // namespace openscreen |