Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/tests/test_net_address_private.h" | 5 #include "ppapi/tests/test_net_address_private.h" |
| 6 | 6 |
| 7 #include "ppapi/cpp/private/net_address_private.h" | 7 #include "ppapi/cpp/private/net_address_private.h" |
| 8 #include "ppapi/c/private/ppb_net_address_private.h" | 8 #include "ppapi/c/private/ppb_net_address_private.h" |
| 9 #include "ppapi/tests/testing_instance.h" | 9 #include "ppapi/tests/testing_instance.h" |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 #include <arpa/inet.h> | 27 #include <arpa/inet.h> |
| 28 #include <netdb.h> | 28 #include <netdb.h> |
| 29 #include <netinet/in.h> | 29 #include <netinet/in.h> |
| 30 #include <sys/socket.h> | 30 #include <sys/socket.h> |
| 31 #endif | 31 #endif |
| 32 | 32 |
| 33 using pp::NetAddressPrivate; | 33 using pp::NetAddressPrivate; |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 // |host| should be an IP address represented as text, e.g., "192.168.0.1". | |
| 37 PP_NetAddress_Private MakeIPv4NetAddress(const char* host, int port) { | 38 PP_NetAddress_Private MakeIPv4NetAddress(const char* host, int port) { |
| 38 PP_NetAddress_Private addr = PP_NetAddress_Private(); | 39 PP_NetAddress_Private addr = PP_NetAddress_Private(); |
| 39 addr.size = sizeof(sockaddr_in); | 40 addr.size = sizeof(sockaddr_in); |
| 40 sockaddr_in* a = reinterpret_cast<sockaddr_in*>(addr.data); | 41 sockaddr_in* a = reinterpret_cast<sockaddr_in*>(addr.data); |
| 41 a->sin_family = AF_INET; | 42 a->sin_family = AF_INET; |
| 42 a->sin_port = htons(port); | 43 a->sin_port = htons(port); |
| 43 a->sin_addr.s_addr = inet_addr(host); | 44 a->sin_addr.s_addr = inet_addr(host); |
| 44 return addr; | 45 return addr; |
| 45 } | 46 } |
| 46 | 47 |
| 47 // TODO(viettrungluu): Also add IPv6 tests. | 48 // |host| should be an array of eight 16-bit numbers. |
| 49 PP_NetAddress_Private MakeIPv6NetAddress(const uint16_t host[], uint16_t port, | |
| 50 uint32_t scope_id) { | |
| 51 PP_NetAddress_Private addr = PP_NetAddress_Private(); | |
| 52 addr.size = sizeof(sockaddr_in6); | |
| 53 sockaddr_in6* a = reinterpret_cast<sockaddr_in6*>(addr.data); | |
| 54 a->sin6_family = AF_INET6; | |
| 55 a->sin6_port = htons(port); | |
| 56 a->sin6_flowinfo = 0; | |
|
dmichael (off chromium)
2011/11/17 04:41:46
Would it maybe be safer to use a sockaddr_in6 on t
viettrungluu
2011/11/17 20:22:56
That's a good point, though in that case I should
| |
| 57 for (int i = 0; i < 8; i++) | |
| 58 a->sin6_addr.s6_addr16[i] = htons(host[i]); | |
| 59 a->sin6_scope_id = scope_id; | |
| 60 return addr; | |
| 61 } | |
| 48 | 62 |
| 49 } // namespace | 63 } // namespace |
| 50 | 64 |
| 51 REGISTER_TEST_CASE(NetAddressPrivate); | 65 REGISTER_TEST_CASE(NetAddressPrivate); |
| 52 | 66 |
| 53 TestNetAddressPrivate::TestNetAddressPrivate(TestingInstance* instance) | 67 TestNetAddressPrivate::TestNetAddressPrivate(TestingInstance* instance) |
| 54 : TestCase(instance) { | 68 : TestCase(instance) { |
| 55 } | 69 } |
| 56 | 70 |
| 57 bool TestNetAddressPrivate::Init() { | 71 bool TestNetAddressPrivate::Init() { |
| 58 return NetAddressPrivate::IsAvailable(); | 72 return NetAddressPrivate::IsAvailable(); |
| 59 } | 73 } |
| 60 | 74 |
| 61 void TestNetAddressPrivate::RunTest() { | 75 void TestNetAddressPrivate::RunTest() { |
| 62 RUN_TEST(AreEqual); | 76 RUN_TEST(AreEqual); |
| 63 RUN_TEST(AreHostsEqual); | 77 RUN_TEST(AreHostsEqual); |
| 64 RUN_TEST(Describe); | 78 RUN_TEST(Describe); |
| 65 RUN_TEST(ReplacePort); | 79 RUN_TEST(ReplacePort); |
| 66 RUN_TEST(GetAnyAddress); | 80 RUN_TEST(GetAnyAddress); |
| 81 | |
| 82 RUN_TEST(DescribeIPv6); | |
| 67 } | 83 } |
| 68 | 84 |
| 69 std::string TestNetAddressPrivate::TestAreEqual() { | 85 std::string TestNetAddressPrivate::TestAreEqual() { |
| 70 // No comparisons should ever be done with invalid addresses. | 86 // No comparisons should ever be done with invalid addresses. |
| 71 PP_NetAddress_Private invalid = PP_NetAddress_Private(); | 87 PP_NetAddress_Private invalid = PP_NetAddress_Private(); |
| 72 ASSERT_FALSE(NetAddressPrivate::AreEqual(invalid, invalid)); | 88 ASSERT_FALSE(NetAddressPrivate::AreEqual(invalid, invalid)); |
| 73 | 89 |
| 74 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80); | 90 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80); |
| 75 ASSERT_TRUE(NetAddressPrivate::AreEqual(localhost_80, localhost_80)); | 91 ASSERT_TRUE(NetAddressPrivate::AreEqual(localhost_80, localhost_80)); |
| 76 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, invalid)); | 92 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, invalid)); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 PP_NetAddress_Private result = PP_NetAddress_Private(); | 164 PP_NetAddress_Private result = PP_NetAddress_Private(); |
| 149 | 165 |
| 150 NetAddressPrivate::GetAnyAddress(false, &result); | 166 NetAddressPrivate::GetAnyAddress(false, &result); |
| 151 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result)); | 167 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result)); |
| 152 | 168 |
| 153 NetAddressPrivate::GetAnyAddress(true, &result); | 169 NetAddressPrivate::GetAnyAddress(true, &result); |
| 154 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result)); | 170 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result)); |
| 155 | 171 |
| 156 PASS(); | 172 PASS(); |
| 157 } | 173 } |
| 174 | |
| 175 // TODO(viettrungluu): More IPv6 tests needed. | |
| 176 | |
| 177 std::string TestNetAddressPrivate::TestDescribeIPv6() { | |
| 178 static const uint16_t a1[8] = { 0x1234, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }; | |
| 179 PP_NetAddress_Private addr1 = MakeIPv6NetAddress(a1, 1234, 789); | |
| 180 ASSERT_EQ("1234:abcd:0:1::cdef%789", | |
| 181 NetAddressPrivate::Describe(addr1, false)); | |
| 182 ASSERT_EQ("[1234:abcd:0:1::cdef%789]:1234", | |
| 183 NetAddressPrivate::Describe(addr1, true)); | |
| 184 | |
| 185 static const uint16_t a2[8] = { 0x12, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }; | |
| 186 PP_NetAddress_Private addr2 = MakeIPv6NetAddress(a2, 12, 0); | |
| 187 ASSERT_EQ("12:abcd:0:1::cdef", | |
| 188 NetAddressPrivate::Describe(addr2, false)); | |
| 189 ASSERT_EQ("[12:abcd:0:1::cdef]:12", | |
| 190 NetAddressPrivate::Describe(addr2, true)); | |
| 191 | |
| 192 static const uint16_t a3[8] = { 0, 0, 0, 0x0123, 0, 0, 0, 0 }; | |
| 193 PP_NetAddress_Private addr3 = MakeIPv6NetAddress(a3, 123, 0); | |
| 194 ASSERT_EQ("0:0:0:123::", | |
| 195 NetAddressPrivate::Describe(addr3, false)); | |
| 196 ASSERT_EQ("[0:0:0:123::]:123", | |
| 197 NetAddressPrivate::Describe(addr3, true)); | |
| 198 | |
| 199 static const uint16_t a4[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef }; | |
| 200 PP_NetAddress_Private addr4 = MakeIPv6NetAddress(a4, 123, 0); | |
| 201 ASSERT_EQ("1234:abcd::ff:0:0:cdef", | |
| 202 NetAddressPrivate::Describe(addr4, false)); | |
| 203 ASSERT_EQ("[1234:abcd::ff:0:0:cdef]:123", | |
| 204 NetAddressPrivate::Describe(addr4, true)); | |
| 205 | |
| 206 static const uint16_t a5[8] = { 0, 0xa, 1, 2, 3, 0, 5, 0 }; | |
| 207 PP_NetAddress_Private addr5 = MakeIPv6NetAddress(a5, 123, 0); | |
| 208 ASSERT_EQ("0:a:1:2:3:0:5:0", | |
| 209 NetAddressPrivate::Describe(addr5, false)); | |
| 210 ASSERT_EQ("[0:a:1:2:3:0:5:0]:123", | |
| 211 NetAddressPrivate::Describe(addr5, true)); | |
| 212 | |
| 213 static const uint16_t a6[8] = { 0, 0xa, 1, 2, 3, 0, 0, 0 }; | |
| 214 PP_NetAddress_Private addr6 = MakeIPv6NetAddress(a6, 123, 0); | |
| 215 ASSERT_EQ("0:a:1:2:3::", | |
| 216 NetAddressPrivate::Describe(addr6, false)); | |
| 217 ASSERT_EQ("[0:a:1:2:3::]:123", | |
| 218 NetAddressPrivate::Describe(addr6, true)); | |
| 219 | |
| 220 static const uint16_t a7[8] = { 0, 0, 0, 2, 3, 0, 0, 0 }; | |
| 221 PP_NetAddress_Private addr7 = MakeIPv6NetAddress(a7, 123, 0); | |
| 222 ASSERT_EQ("::2:3:0:0:0", | |
| 223 NetAddressPrivate::Describe(addr7, false)); | |
| 224 ASSERT_EQ("[::2:3:0:0:0]:123", | |
| 225 NetAddressPrivate::Describe(addr7, true)); | |
| 226 | |
| 227 PASS(); | |
| 228 } | |
| OLD | NEW |