1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file. 4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/tests/test_net_address.h" 6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <cstring> 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 9eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ppapi/cpp/net_address.h" 10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/tests/test_utils.h" 11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/tests/testing_instance.h" 12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing pp::NetAddress; 14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)REGISTER_TEST_CASE(NetAddress); 16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace { 18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool EqualIPv4Address(const PP_NetAddress_IPv4& addr1, 20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const PP_NetAddress_IPv4& addr2) { 21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return addr1.port == addr2.port && 22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) !memcmp(addr1.addr, addr2.addr, sizeof(addr1.addr)); 23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool EqualIPv6Address(const PP_NetAddress_IPv6& addr1, 26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const PP_NetAddress_IPv6& addr2) { 27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return addr1.port == addr2.port && 28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) !memcmp(addr1.addr, addr2.addr, sizeof(addr1.addr)); 29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochNetAddress CreateFromHostOrderIPv6Address( 32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const pp::InstanceHandle& instance, 33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const uint16_t host_order_addr[8], 34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) uint16_t host_order_port) { 35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PP_NetAddress_IPv6 ipv6_addr; 36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ipv6_addr.port = ConvertToNetEndian16(host_order_port); 37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (size_t i = 0; i < 8; ++i) { 38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) uint16_t net_order_addr = ConvertToNetEndian16(host_order_addr[i]); 39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) memcpy(&ipv6_addr.addr[2 * i], &net_order_addr, 2); 40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return NetAddress(instance, ipv6_addr); 43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} // namespace 46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TestNetAddress::TestNetAddress(TestingInstance* instance) : TestCase(instance) { 48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool TestNetAddress::Init() { 51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return NetAddress::IsAvailable(); 52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void TestNetAddress::RunTests(const std::string& filter) { 55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RUN_TEST(IPv4Address, filter); 56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RUN_TEST(IPv6Address, filter); 57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RUN_TEST(DescribeAsString, filter); 58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::string TestNetAddress::TestIPv4Address() { 61eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PP_NetAddress_IPv4 ipv4_addr = { ConvertToNetEndian16(80), { 127, 0, 0, 1 } }; 62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch NetAddress net_addr(instance_, ipv4_addr); 63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_NE(0, net_addr.pp_resource()); 64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_EQ(PP_NETADDRESS_FAMILY_IPV4, net_addr.GetFamily()); 66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PP_NetAddress_IPv4 out_ipv4_addr; 68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_TRUE(net_addr.DescribeAsIPv4Address(&out_ipv4_addr)); 69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_TRUE(EqualIPv4Address(ipv4_addr, out_ipv4_addr)); 70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 71eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PP_NetAddress_IPv6 out_ipv6_addr; 72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_FALSE(net_addr.DescribeAsIPv6Address(&out_ipv6_addr)); 73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) PASS(); 75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::string TestNetAddress::TestIPv6Address() { 78eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PP_NetAddress_IPv6 ipv6_addr = { 797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ConvertToNetEndian16(1024), 80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } 81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }; 82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 83eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch NetAddress net_addr(instance_, ipv6_addr); 84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_NE(0, net_addr.pp_resource()); 85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_EQ(PP_NETADDRESS_FAMILY_IPV6, net_addr.GetFamily()); 87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 88eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PP_NetAddress_IPv6 out_ipv6_addr; 89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_TRUE(net_addr.DescribeAsIPv6Address(&out_ipv6_addr)); 90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_TRUE(EqualIPv6Address(ipv6_addr, out_ipv6_addr)); 91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 92eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PP_NetAddress_IPv4 out_ipv4_addr; 93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_FALSE(net_addr.DescribeAsIPv4Address(&out_ipv4_addr)); 94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) PASS(); 96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::string TestNetAddress::TestDescribeAsString() { 99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Test describing IPv4 addresses. 101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PP_NetAddress_IPv4 ipv4_addr1 = { ConvertToNetEndian16(1234), 102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch { 127, 0, 0, 1 } }; 103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch NetAddress addr1(instance_, ipv4_addr1); 1047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ASSERT_EQ("127.0.0.1", addr1.DescribeAsString(false).AsString()); 1057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ASSERT_EQ("127.0.0.1:1234", addr1.DescribeAsString(true).AsString()); 106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PP_NetAddress_IPv4 ipv4_addr2 = { ConvertToNetEndian16(80), 108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch { 192, 168, 0, 2 } }; 109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch NetAddress addr2(instance_, ipv4_addr2); 1107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ASSERT_EQ("192.168.0.2", addr2.DescribeAsString(false).AsString()); 1117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ASSERT_EQ("192.168.0.2:80", addr2.DescribeAsString(true).AsString()); 112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Test describing IPv6 addresses. 115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static const struct { 116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) uint16_t host_order_addr[8]; 117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) uint16_t host_order_port; 118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const char* expected_without_port; 119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const char* expected_with_port; 120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } ipv6_test_cases[] = { 121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { // Generic test case (unique longest run of zeros to collapse). 122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 0x12, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }, 12, 123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) "12:abcd:0:1::cdef", "[12:abcd:0:1::cdef]:12" 124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }, 125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { // Ignore the first (non-longest) run of zeros. 126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 0, 0, 0, 0x0123, 0, 0, 0, 0 }, 123, 127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) "0:0:0:123::", "[0:0:0:123::]:123" 128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }, 129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { // Collapse the first (equally-longest) run of zeros. 130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef }, 123, 131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) "1234:abcd::ff:0:0:cdef", "[1234:abcd::ff:0:0:cdef]:123" 132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }, 133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { // Don't collapse "runs" of zeros of length 1. 134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 0, 0xa, 1, 2, 3, 0, 5, 0 }, 123, 135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) "0:a:1:2:3:0:5:0", "[0:a:1:2:3:0:5:0]:123" 136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }, 137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { // Collapse a run of zeros at the beginning. 138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 0, 0, 0, 2, 3, 0, 0, 0 }, 123, 139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) "::2:3:0:0:0", "[::2:3:0:0:0]:123" 140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }, 141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { // Collapse a run of zeros at the end. 142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 0, 0xa, 1, 2, 3, 0, 0, 0 }, 123, 143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) "0:a:1:2:3::", "[0:a:1:2:3::]:123" 144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }, 145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { // IPv4 192.168.1.2 embedded in IPv6 in the deprecated way. 146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 0, 0, 0, 0, 0, 0, 0xc0a8, 0x102 }, 123, 147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) "::192.168.1.2", "[::192.168.1.2]:123" 148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }, 149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { // IPv4 192.168.1.2 embedded in IPv6. 150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x102 }, 123, 151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) "::ffff:192.168.1.2", "[::ffff:192.168.1.2]:123" 152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }, 153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { // *Not* IPv4 embedded in IPv6. 154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) { 0, 0, 0, 0, 0, 0x1234, 0xc0a8, 0x102 }, 123, 155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) "::1234:c0a8:102", "[::1234:c0a8:102]:123" 156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }; 158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (size_t i = 0; 160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) i < sizeof(ipv6_test_cases) / sizeof(ipv6_test_cases[0]); 161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ++i) { 162eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch NetAddress addr = CreateFromHostOrderIPv6Address( 163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) instance_, ipv6_test_cases[i].host_order_addr, 164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ipv6_test_cases[i].host_order_port); 165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_EQ(ipv6_test_cases[i].expected_without_port, 1667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) addr.DescribeAsString(false).AsString()); 167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_EQ(ipv6_test_cases[i].expected_with_port, 1687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) addr.DescribeAsString(true).AsString()); 169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) PASS(); 173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 174