Avi Drissman | db497b3 | 2022-09-15 19:47:28 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 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 "ppapi/tests/test_net_address.h" |
| 6 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 10 | #include <cstring> |
| 11 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 12 | #include "ppapi/cpp/net_address.h" |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 13 | #include "ppapi/tests/test_utils.h" |
| 14 | #include "ppapi/tests/testing_instance.h" |
| 15 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 16 | using pp::NetAddress; |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 17 | |
| 18 | REGISTER_TEST_CASE(NetAddress); |
| 19 | |
| 20 | namespace { |
| 21 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 22 | bool EqualIPv4Address(const PP_NetAddress_IPv4& addr1, |
| 23 | const PP_NetAddress_IPv4& addr2) { |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 24 | return addr1.port == addr2.port && |
| 25 | !memcmp(addr1.addr, addr2.addr, sizeof(addr1.addr)); |
| 26 | } |
| 27 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 28 | bool EqualIPv6Address(const PP_NetAddress_IPv6& addr1, |
| 29 | const PP_NetAddress_IPv6& addr2) { |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 30 | return addr1.port == addr2.port && |
| 31 | !memcmp(addr1.addr, addr2.addr, sizeof(addr1.addr)); |
| 32 | } |
| 33 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 34 | NetAddress CreateFromHostOrderIPv6Address( |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 35 | const pp::InstanceHandle& instance, |
| 36 | const uint16_t host_order_addr[8], |
| 37 | uint16_t host_order_port) { |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 38 | PP_NetAddress_IPv6 ipv6_addr; |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 39 | ipv6_addr.port = ConvertToNetEndian16(host_order_port); |
| 40 | for (size_t i = 0; i < 8; ++i) { |
| 41 | uint16_t net_order_addr = ConvertToNetEndian16(host_order_addr[i]); |
| 42 | memcpy(&ipv6_addr.addr[2 * i], &net_order_addr, 2); |
| 43 | } |
| 44 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 45 | return NetAddress(instance, ipv6_addr); |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 50 | TestNetAddress::TestNetAddress(TestingInstance* instance) : TestCase(instance) { |
| 51 | } |
| 52 | |
| 53 | bool TestNetAddress::Init() { |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 54 | return NetAddress::IsAvailable(); |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void TestNetAddress::RunTests(const std::string& filter) { |
| 58 | RUN_TEST(IPv4Address, filter); |
| 59 | RUN_TEST(IPv6Address, filter); |
| 60 | RUN_TEST(DescribeAsString, filter); |
| 61 | } |
| 62 | |
| 63 | std::string TestNetAddress::TestIPv4Address() { |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 64 | PP_NetAddress_IPv4 ipv4_addr = { ConvertToNetEndian16(80), { 127, 0, 0, 1 } }; |
| 65 | NetAddress net_addr(instance_, ipv4_addr); |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 66 | ASSERT_NE(0, net_addr.pp_resource()); |
| 67 | |
| 68 | ASSERT_EQ(PP_NETADDRESS_FAMILY_IPV4, net_addr.GetFamily()); |
| 69 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 70 | PP_NetAddress_IPv4 out_ipv4_addr; |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 71 | ASSERT_TRUE(net_addr.DescribeAsIPv4Address(&out_ipv4_addr)); |
| 72 | ASSERT_TRUE(EqualIPv4Address(ipv4_addr, out_ipv4_addr)); |
| 73 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 74 | PP_NetAddress_IPv6 out_ipv6_addr; |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 75 | ASSERT_FALSE(net_addr.DescribeAsIPv6Address(&out_ipv6_addr)); |
| 76 | |
| 77 | PASS(); |
| 78 | } |
| 79 | |
| 80 | std::string TestNetAddress::TestIPv6Address() { |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 81 | PP_NetAddress_IPv6 ipv6_addr = { |
[email protected] | 534f61f | 2013-06-11 19:30:37 | [diff] [blame] | 82 | ConvertToNetEndian16(1024), |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 83 | { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } |
| 84 | }; |
| 85 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 86 | NetAddress net_addr(instance_, ipv6_addr); |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 87 | ASSERT_NE(0, net_addr.pp_resource()); |
| 88 | |
| 89 | ASSERT_EQ(PP_NETADDRESS_FAMILY_IPV6, net_addr.GetFamily()); |
| 90 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 91 | PP_NetAddress_IPv6 out_ipv6_addr; |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 92 | ASSERT_TRUE(net_addr.DescribeAsIPv6Address(&out_ipv6_addr)); |
| 93 | ASSERT_TRUE(EqualIPv6Address(ipv6_addr, out_ipv6_addr)); |
| 94 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 95 | PP_NetAddress_IPv4 out_ipv4_addr; |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 96 | ASSERT_FALSE(net_addr.DescribeAsIPv4Address(&out_ipv4_addr)); |
| 97 | |
| 98 | PASS(); |
| 99 | } |
| 100 | |
| 101 | std::string TestNetAddress::TestDescribeAsString() { |
| 102 | { |
| 103 | // Test describing IPv4 addresses. |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 104 | PP_NetAddress_IPv4 ipv4_addr1 = { ConvertToNetEndian16(1234), |
| 105 | { 127, 0, 0, 1 } }; |
| 106 | NetAddress addr1(instance_, ipv4_addr1); |
[email protected] | 000648e9 | 2013-06-18 02:38:32 | [diff] [blame] | 107 | ASSERT_EQ("127.0.0.1", addr1.DescribeAsString(false).AsString()); |
| 108 | ASSERT_EQ("127.0.0.1:1234", addr1.DescribeAsString(true).AsString()); |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 109 | |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 110 | PP_NetAddress_IPv4 ipv4_addr2 = { ConvertToNetEndian16(80), |
| 111 | { 192, 168, 0, 2 } }; |
| 112 | NetAddress addr2(instance_, ipv4_addr2); |
[email protected] | 000648e9 | 2013-06-18 02:38:32 | [diff] [blame] | 113 | ASSERT_EQ("192.168.0.2", addr2.DescribeAsString(false).AsString()); |
| 114 | ASSERT_EQ("192.168.0.2:80", addr2.DescribeAsString(true).AsString()); |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 115 | } |
| 116 | { |
| 117 | // Test describing IPv6 addresses. |
| 118 | static const struct { |
| 119 | uint16_t host_order_addr[8]; |
| 120 | uint16_t host_order_port; |
| 121 | const char* expected_without_port; |
| 122 | const char* expected_with_port; |
| 123 | } ipv6_test_cases[] = { |
| 124 | { // Generic test case (unique longest run of zeros to collapse). |
| 125 | { 0x12, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }, 12, |
| 126 | "12:abcd:0:1::cdef", "[12:abcd:0:1::cdef]:12" |
| 127 | }, |
| 128 | { // Ignore the first (non-longest) run of zeros. |
| 129 | { 0, 0, 0, 0x0123, 0, 0, 0, 0 }, 123, |
| 130 | "0:0:0:123::", "[0:0:0:123::]:123" |
| 131 | }, |
| 132 | { // Collapse the first (equally-longest) run of zeros. |
| 133 | { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef }, 123, |
| 134 | "1234:abcd::ff:0:0:cdef", "[1234:abcd::ff:0:0:cdef]:123" |
| 135 | }, |
| 136 | { // Don't collapse "runs" of zeros of length 1. |
| 137 | { 0, 0xa, 1, 2, 3, 0, 5, 0 }, 123, |
| 138 | "0:a:1:2:3:0:5:0", "[0:a:1:2:3:0:5:0]:123" |
| 139 | }, |
| 140 | { // Collapse a run of zeros at the beginning. |
| 141 | { 0, 0, 0, 2, 3, 0, 0, 0 }, 123, |
| 142 | "::2:3:0:0:0", "[::2:3:0:0:0]:123" |
| 143 | }, |
| 144 | { // Collapse a run of zeros at the end. |
| 145 | { 0, 0xa, 1, 2, 3, 0, 0, 0 }, 123, |
| 146 | "0:a:1:2:3::", "[0:a:1:2:3::]:123" |
| 147 | }, |
| 148 | { // IPv4 192.168.1.2 embedded in IPv6 in the deprecated way. |
| 149 | { 0, 0, 0, 0, 0, 0, 0xc0a8, 0x102 }, 123, |
| 150 | "::192.168.1.2", "[::192.168.1.2]:123" |
| 151 | }, |
| 152 | { // IPv4 192.168.1.2 embedded in IPv6. |
| 153 | { 0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x102 }, 123, |
| 154 | "::ffff:192.168.1.2", "[::ffff:192.168.1.2]:123" |
| 155 | }, |
| 156 | { // *Not* IPv4 embedded in IPv6. |
| 157 | { 0, 0, 0, 0, 0, 0x1234, 0xc0a8, 0x102 }, 123, |
| 158 | "::1234:c0a8:102", "[::1234:c0a8:102]:123" |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | for (size_t i = 0; |
| 163 | i < sizeof(ipv6_test_cases) / sizeof(ipv6_test_cases[0]); |
| 164 | ++i) { |
[email protected] | ee51755 | 2013-06-22 20:07:50 | [diff] [blame] | 165 | NetAddress addr = CreateFromHostOrderIPv6Address( |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 166 | instance_, ipv6_test_cases[i].host_order_addr, |
| 167 | ipv6_test_cases[i].host_order_port); |
| 168 | ASSERT_EQ(ipv6_test_cases[i].expected_without_port, |
[email protected] | 000648e9 | 2013-06-18 02:38:32 | [diff] [blame] | 169 | addr.DescribeAsString(false).AsString()); |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 170 | ASSERT_EQ(ipv6_test_cases[i].expected_with_port, |
[email protected] | 000648e9 | 2013-06-18 02:38:32 | [diff] [blame] | 171 | addr.DescribeAsString(true).AsString()); |
[email protected] | 2414d92 | 2013-06-10 09:19:21 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | PASS(); |
| 176 | } |