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