blob: c0bc001c9676f560844e34b384ce40604c3db3ac [file] [log] [blame]
Avi Drissmandb497b32022-09-15 19:47:281// Copyright 2013 The Chromium Authors
[email protected]2414d922013-06-10 09:19:212// 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
avie029c4132015-12-23 06:45:227#include <stddef.h>
8#include <stdint.h>
9
[email protected]2414d922013-06-10 09:19:2110#include <cstring>
11
[email protected]ee517552013-06-22 20:07:5012#include "ppapi/cpp/net_address.h"
[email protected]2414d922013-06-10 09:19:2113#include "ppapi/tests/test_utils.h"
14#include "ppapi/tests/testing_instance.h"
15
[email protected]ee517552013-06-22 20:07:5016using pp::NetAddress;
[email protected]2414d922013-06-10 09:19:2117
18REGISTER_TEST_CASE(NetAddress);
19
20namespace {
21
[email protected]ee517552013-06-22 20:07:5022bool EqualIPv4Address(const PP_NetAddress_IPv4& addr1,
23 const PP_NetAddress_IPv4& addr2) {
[email protected]2414d922013-06-10 09:19:2124 return addr1.port == addr2.port &&
25 !memcmp(addr1.addr, addr2.addr, sizeof(addr1.addr));
26}
27
[email protected]ee517552013-06-22 20:07:5028bool EqualIPv6Address(const PP_NetAddress_IPv6& addr1,
29 const PP_NetAddress_IPv6& addr2) {
[email protected]2414d922013-06-10 09:19:2130 return addr1.port == addr2.port &&
31 !memcmp(addr1.addr, addr2.addr, sizeof(addr1.addr));
32}
33
[email protected]ee517552013-06-22 20:07:5034NetAddress CreateFromHostOrderIPv6Address(
[email protected]2414d922013-06-10 09:19:2135 const pp::InstanceHandle& instance,
36 const uint16_t host_order_addr[8],
37 uint16_t host_order_port) {
[email protected]ee517552013-06-22 20:07:5038 PP_NetAddress_IPv6 ipv6_addr;
[email protected]2414d922013-06-10 09:19:2139 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]ee517552013-06-22 20:07:5045 return NetAddress(instance, ipv6_addr);
[email protected]2414d922013-06-10 09:19:2146}
47
48} // namespace
49
50TestNetAddress::TestNetAddress(TestingInstance* instance) : TestCase(instance) {
51}
52
53bool TestNetAddress::Init() {
[email protected]ee517552013-06-22 20:07:5054 return NetAddress::IsAvailable();
[email protected]2414d922013-06-10 09:19:2155}
56
57void TestNetAddress::RunTests(const std::string& filter) {
58 RUN_TEST(IPv4Address, filter);
59 RUN_TEST(IPv6Address, filter);
60 RUN_TEST(DescribeAsString, filter);
61}
62
63std::string TestNetAddress::TestIPv4Address() {
[email protected]ee517552013-06-22 20:07:5064 PP_NetAddress_IPv4 ipv4_addr = { ConvertToNetEndian16(80), { 127, 0, 0, 1 } };
65 NetAddress net_addr(instance_, ipv4_addr);
[email protected]2414d922013-06-10 09:19:2166 ASSERT_NE(0, net_addr.pp_resource());
67
68 ASSERT_EQ(PP_NETADDRESS_FAMILY_IPV4, net_addr.GetFamily());
69
[email protected]ee517552013-06-22 20:07:5070 PP_NetAddress_IPv4 out_ipv4_addr;
[email protected]2414d922013-06-10 09:19:2171 ASSERT_TRUE(net_addr.DescribeAsIPv4Address(&out_ipv4_addr));
72 ASSERT_TRUE(EqualIPv4Address(ipv4_addr, out_ipv4_addr));
73
[email protected]ee517552013-06-22 20:07:5074 PP_NetAddress_IPv6 out_ipv6_addr;
[email protected]2414d922013-06-10 09:19:2175 ASSERT_FALSE(net_addr.DescribeAsIPv6Address(&out_ipv6_addr));
76
77 PASS();
78}
79
80std::string TestNetAddress::TestIPv6Address() {
[email protected]ee517552013-06-22 20:07:5081 PP_NetAddress_IPv6 ipv6_addr = {
[email protected]534f61f2013-06-11 19:30:3782 ConvertToNetEndian16(1024),
[email protected]2414d922013-06-10 09:19:2183 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
84 };
85
[email protected]ee517552013-06-22 20:07:5086 NetAddress net_addr(instance_, ipv6_addr);
[email protected]2414d922013-06-10 09:19:2187 ASSERT_NE(0, net_addr.pp_resource());
88
89 ASSERT_EQ(PP_NETADDRESS_FAMILY_IPV6, net_addr.GetFamily());
90
[email protected]ee517552013-06-22 20:07:5091 PP_NetAddress_IPv6 out_ipv6_addr;
[email protected]2414d922013-06-10 09:19:2192 ASSERT_TRUE(net_addr.DescribeAsIPv6Address(&out_ipv6_addr));
93 ASSERT_TRUE(EqualIPv6Address(ipv6_addr, out_ipv6_addr));
94
[email protected]ee517552013-06-22 20:07:5095 PP_NetAddress_IPv4 out_ipv4_addr;
[email protected]2414d922013-06-10 09:19:2196 ASSERT_FALSE(net_addr.DescribeAsIPv4Address(&out_ipv4_addr));
97
98 PASS();
99}
100
101std::string TestNetAddress::TestDescribeAsString() {
102 {
103 // Test describing IPv4 addresses.
[email protected]ee517552013-06-22 20:07:50104 PP_NetAddress_IPv4 ipv4_addr1 = { ConvertToNetEndian16(1234),
105 { 127, 0, 0, 1 } };
106 NetAddress addr1(instance_, ipv4_addr1);
[email protected]000648e92013-06-18 02:38:32107 ASSERT_EQ("127.0.0.1", addr1.DescribeAsString(false).AsString());
108 ASSERT_EQ("127.0.0.1:1234", addr1.DescribeAsString(true).AsString());
[email protected]2414d922013-06-10 09:19:21109
[email protected]ee517552013-06-22 20:07:50110 PP_NetAddress_IPv4 ipv4_addr2 = { ConvertToNetEndian16(80),
111 { 192, 168, 0, 2 } };
112 NetAddress addr2(instance_, ipv4_addr2);
[email protected]000648e92013-06-18 02:38:32113 ASSERT_EQ("192.168.0.2", addr2.DescribeAsString(false).AsString());
114 ASSERT_EQ("192.168.0.2:80", addr2.DescribeAsString(true).AsString());
[email protected]2414d922013-06-10 09:19:21115 }
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]ee517552013-06-22 20:07:50165 NetAddress addr = CreateFromHostOrderIPv6Address(
[email protected]2414d922013-06-10 09:19:21166 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]000648e92013-06-18 02:38:32169 addr.DescribeAsString(false).AsString());
[email protected]2414d922013-06-10 09:19:21170 ASSERT_EQ(ipv6_test_cases[i].expected_with_port,
[email protected]000648e92013-06-18 02:38:32171 addr.DescribeAsString(true).AsString());
[email protected]2414d922013-06-10 09:19:21172 }
173 }
174
175 PASS();
176}