Index: ppapi/tests/test_net_address_private.cc |
diff --git a/ppapi/tests/test_net_address_private.cc b/ppapi/tests/test_net_address_private.cc |
index c3c0cecb62f205ca75ab1d136ed59f5ed7526f5a..8e90bf2d9be54565cac38ba57bb79cc3afde8158 100644 |
--- a/ppapi/tests/test_net_address_private.cc |
+++ b/ppapi/tests/test_net_address_private.cc |
@@ -4,6 +4,8 @@ |
#include "ppapi/tests/test_net_address_private.h" |
+#include "base/basictypes.h" |
+#include "build/build_config.h" |
#include "ppapi/cpp/private/net_address_private.h" |
#include "ppapi/c/private/ppb_net_address_private.h" |
#include "ppapi/tests/testing_instance.h" |
@@ -11,29 +13,30 @@ |
// Other than |GetAnyAddress()|, there's no way to actually get |
// |PP_NetAddress_Private| structs from just this interface. We'll cheat and |
// synthesize some. |
-// TODO(viettrungluu): This is very fragile and implementation-dependent. :( |
-#if defined(_WIN32) |
-#define OS_WIN |
-#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \ |
- defined(__OpenBSD__) || defined(__sun) || defined(__native_client__) |
-#define OS_POSIX |
-#else |
-#error "Unsupported platform." |
-#endif |
-#if defined(OS_WIN) |
-#include <ws2tcpip.h> |
-#elif defined(OS_POSIX) |
+#if defined(OS_POSIX) |
#include <arpa/inet.h> |
#include <netdb.h> |
#include <netinet/in.h> |
#include <sys/socket.h> |
#endif |
+#if defined(OS_MACOSX) |
+// This is a bit evil, but it's standard operating procedure for |s6_addr|.... |
+#define s6_addr16 __u6_addr.__u6_addr16 |
+#endif |
+ |
+#if defined(OS_WIN) |
+#include <ws2tcpip.h> |
+ |
+#define s6_addr16 u.Word |
+#endif |
+ |
using pp::NetAddressPrivate; |
namespace { |
+// |host| should be an IP address represented as text, e.g., "192.168.0.1". |
PP_NetAddress_Private MakeIPv4NetAddress(const char* host, int port) { |
PP_NetAddress_Private addr = PP_NetAddress_Private(); |
addr.size = sizeof(sockaddr_in); |
@@ -44,7 +47,20 @@ PP_NetAddress_Private MakeIPv4NetAddress(const char* host, int port) { |
return addr; |
} |
-// TODO(viettrungluu): Also add IPv6 tests. |
+// |host| should be an array of eight 16-bit numbers. |
+PP_NetAddress_Private MakeIPv6NetAddress(const uint16_t host[], uint16_t port, |
+ uint32_t scope_id) { |
+ PP_NetAddress_Private addr = PP_NetAddress_Private(); |
+ addr.size = sizeof(sockaddr_in6); |
+ sockaddr_in6* a = reinterpret_cast<sockaddr_in6*>(addr.data); |
+ a->sin6_family = AF_INET6; |
+ a->sin6_port = htons(port); |
+ a->sin6_flowinfo = 0; |
+ for (int i = 0; i < 8; i++) |
+ a->sin6_addr.s6_addr16[i] = htons(host[i]); |
+ a->sin6_scope_id = scope_id; |
+ return addr; |
+} |
} // namespace |
@@ -64,6 +80,7 @@ void TestNetAddressPrivate::RunTests(const std::string& filter) { |
RUN_TEST(Describe, filter); |
RUN_TEST(ReplacePort, filter); |
RUN_TEST(GetAnyAddress, filter); |
+ RUN_TEST(DescribeIPv6, filter); |
} |
std::string TestNetAddressPrivate::TestAreEqual() { |
@@ -155,3 +172,76 @@ std::string TestNetAddressPrivate::TestGetAnyAddress() { |
PASS(); |
} |
+ |
+// TODO(viettrungluu): More IPv6 tests needed. |
+ |
+std::string TestNetAddressPrivate::TestDescribeIPv6() { |
+ static const struct { |
+ uint16_t address[8]; |
+ uint16_t port; |
+ uint32_t scope; |
+ const char* expected_without_port; |
+ const char* expected_with_port; |
+ } test_cases[] = { |
+ { // Generic test case (unique longest run of zeros to collapse). |
+ { 0x12, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }, 12, 0, |
+ "12:abcd:0:1::cdef", "[12:abcd:0:1::cdef]:12" |
+ }, |
+ { // Non-zero scope. |
+ { 0x1234, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }, 1234, 789, |
+ "1234:abcd:0:1::cdef%789", "[1234:abcd:0:1::cdef%789]:1234" |
+ }, |
+ { // Ignore the first (non-longest) run of zeros. |
+ { 0, 0, 0, 0x0123, 0, 0, 0, 0 }, 123, 0, |
+ "0:0:0:123::", "[0:0:0:123::]:123" |
+ }, |
+ { // Collapse the first (equally-longest) run of zeros. |
+ { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef }, 123, 0, |
+ "1234:abcd::ff:0:0:cdef", "[1234:abcd::ff:0:0:cdef]:123" |
+ }, |
+ { // Don't collapse "runs" of zeros of length 1. |
+ { 0, 0xa, 1, 2, 3, 0, 5, 0 }, 123, 0, |
+ "0:a:1:2:3:0:5:0", "[0:a:1:2:3:0:5:0]:123" |
+ }, |
+ { // Collapse a run of zeros at the beginning. |
+ { 0, 0, 0, 2, 3, 0, 0, 0 }, 123, 0, |
+ "::2:3:0:0:0", "[::2:3:0:0:0]:123" |
+ }, |
+ { // Collapse a run of zeros at the end. |
+ { 0, 0xa, 1, 2, 3, 0, 0, 0 }, 123, 0, |
+ "0:a:1:2:3::", "[0:a:1:2:3::]:123" |
+ }, |
+ { // IPv4 192.168.1.2 embedded in IPv6 in the deprecated way. |
+ { 0, 0, 0, 0, 0, 0, 0xc0a8, 0x102 }, 123, 0, |
+ "::192.168.1.2", "[::192.168.1.2]:123" |
+ }, |
+ { // ... with non-zero scope. |
+ { 0, 0, 0, 0, 0, 0, 0xc0a8, 0x102 }, 123, 789, |
+ "::192.168.1.2%789", "[::192.168.1.2%789]:123" |
+ }, |
+ { // IPv4 192.168.1.2 embedded in IPv6. |
+ { 0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x102 }, 123, 0, |
+ "::ffff:192.168.1.2", "[::ffff:192.168.1.2]:123" |
+ }, |
+ { // ... with non-zero scope. |
+ { 0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x102 }, 123, 789, |
+ "::ffff:192.168.1.2%789", "[::ffff:192.168.1.2%789]:123" |
+ }, |
+ { // *Not* IPv4 embedded in IPv6. |
+ { 0, 0, 0, 0, 0, 0x1234, 0xc0a8, 0x102 }, 123, 0, |
+ "::1234:c0a8:102", "[::1234:c0a8:102]:123" |
+ } |
+ }; |
+ |
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); i++) { |
+ PP_NetAddress_Private addr = MakeIPv6NetAddress(test_cases[i].address, |
+ test_cases[i].port, |
+ test_cases[i].scope); |
+ ASSERT_EQ(test_cases[i].expected_without_port, |
+ NetAddressPrivate::Describe(addr, false)); |
+ ASSERT_EQ(test_cases[i].expected_with_port, |
+ NetAddressPrivate::Describe(addr, true)); |
dmichael (off chromium)
2011/11/21 21:20:46
The downside with this approach is if one of these
viettrungluu
2011/11/21 21:33:48
I'll defer this, since what I'd really like is a m
|
+ } |
+ |
+ PASS(); |
+} |