ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / native_client_sdk / src / tests / nacl_io_test / socket_test.cc
blob03d96375685ebef522042067da42e2b07fa74c6d
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <arpa/inet.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <netinet/in.h>
9 #include <pthread.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/stat.h>
14 #include <map>
16 #include "gmock/gmock.h"
17 #include "gtest/gtest.h"
19 #include "nacl_io/kernel_intercept.h"
20 #include "nacl_io/kernel_proxy.h"
21 #include "nacl_io/ossocket.h"
22 #include "nacl_io/ostypes.h"
24 #ifdef PROVIDES_SOCKET_API
26 using namespace nacl_io;
27 using namespace sdk_util;
29 namespace {
30 class SocketTest : public ::testing::Test {
31 public:
32 SocketTest() {}
34 void SetUp() {
35 ASSERT_EQ(0, ki_push_state_for_testing());
36 ASSERT_EQ(0, ki_init(&kp_));
39 void TearDown() {
40 ki_uninit();
43 protected:
44 KernelProxy kp_;
47 } // namespace
49 TEST_F(SocketTest, Accept) {
50 struct sockaddr addr = {};
51 socklen_t len = 0;
53 // accept() should allow NULL args for addr and len
54 // https://code.google.com/p/chromium/issues/detail?id=442164
55 // EXPECT_LT(ki_accept(123, NULL, &len), 0);
56 // EXPECT_EQ(errno, EFAULT);
57 // EXPECT_LT(ki_accept(123, &addr, NULL), 0);
58 // EXPECT_EQ(errno, EFAULT);
59 // EXPECT_LT(ki_accept(123, NULL, NULL), 0);
60 // EXPECT_EQ(errno, EFAULT);
61 EXPECT_LT(ki_accept(-1, &addr, &len), 0);
62 EXPECT_EQ(errno, EBADF);
63 EXPECT_LT(ki_accept(0, &addr, &len), 0);
64 EXPECT_EQ(errno, ENOTSOCK);
67 TEST_F(SocketTest, Bind) {
68 const struct sockaddr const_addr = {};
69 socklen_t len = 0;
71 EXPECT_LT(ki_bind(123, NULL, len), 0);
72 EXPECT_EQ(errno, EFAULT);
73 EXPECT_LT(ki_bind(-1, &const_addr, len), 0);
74 EXPECT_EQ(errno, EBADF);
75 EXPECT_LT(ki_bind(0, &const_addr, len), 0);
76 EXPECT_EQ(errno, ENOTSOCK);
79 TEST_F(SocketTest, Connect) {
80 const struct sockaddr const_addr = {};
81 socklen_t len = 0;
83 EXPECT_LT(ki_connect(123, NULL, len), 0);
84 EXPECT_EQ(errno, EFAULT);
85 EXPECT_LT(ki_connect(-1, &const_addr, len), 0);
86 EXPECT_EQ(errno, EBADF);
87 EXPECT_LT(ki_connect(0, &const_addr, len), 0);
88 EXPECT_EQ(errno, ENOTSOCK);
91 TEST_F(SocketTest, Getpeername) {
92 struct sockaddr addr = {};
93 socklen_t len = 0;
95 EXPECT_LT(ki_getpeername(123, NULL, &len), 0);
96 EXPECT_EQ(errno, EFAULT);
97 EXPECT_LT(ki_getpeername(123, &addr, NULL), 0);
98 EXPECT_EQ(errno, EFAULT);
99 EXPECT_LT(ki_getpeername(123, NULL, NULL), 0);
100 EXPECT_EQ(errno, EFAULT);
101 EXPECT_LT(ki_getpeername(-1, &addr, &len), 0);
102 EXPECT_EQ(errno, EBADF);
103 EXPECT_LT(ki_getpeername(0, &addr, &len), 0);
104 EXPECT_EQ(errno, ENOTSOCK);
107 TEST_F(SocketTest, Getsockname) {
108 struct sockaddr addr = {};
109 socklen_t len = 0;
111 EXPECT_LT(ki_getsockname(123, NULL, &len), 0);
112 EXPECT_EQ(errno, EFAULT);
113 EXPECT_LT(ki_getsockname(123, &addr, NULL), 0);
114 EXPECT_EQ(errno, EFAULT);
115 EXPECT_LT(ki_getsockname(123, NULL, NULL), 0);
116 EXPECT_EQ(errno, EFAULT);
117 EXPECT_LT(ki_getsockname(-1, &addr, &len), 0);
118 EXPECT_EQ(errno, EBADF);
119 EXPECT_LT(ki_getsockname(0, &addr, &len), 0);
120 EXPECT_EQ(errno, ENOTSOCK);
123 TEST_F(SocketTest, Getsockopt) {
124 socklen_t len = 10;
125 char optval[len];
127 EXPECT_LT(ki_getsockopt(123, SOL_SOCKET, SO_ACCEPTCONN, optval, NULL), 0);
128 EXPECT_EQ(errno, EFAULT);
129 EXPECT_LT(ki_getsockopt(123, SOL_SOCKET, SO_ACCEPTCONN, NULL, &len), 0);
130 EXPECT_EQ(errno, EFAULT);
131 EXPECT_LT(ki_getsockopt(123, SOL_SOCKET, SO_ACCEPTCONN, NULL, NULL), 0);
132 EXPECT_EQ(errno, EFAULT);
133 EXPECT_LT(ki_getsockopt(-1, SOL_SOCKET, SO_ACCEPTCONN, optval, &len), 0);
134 EXPECT_EQ(errno, EBADF);
135 EXPECT_LT(ki_getsockopt(0, SOL_SOCKET, SO_ACCEPTCONN, optval, &len), 0);
136 EXPECT_EQ(errno, ENOTSOCK);
139 TEST_F(SocketTest, Listen) {
140 EXPECT_LT(ki_listen(-1, 123), 0);
141 EXPECT_EQ(errno, EBADF);
142 EXPECT_LT(ki_listen(0, 123), 0);
143 EXPECT_EQ(errno, ENOTSOCK);
146 TEST_F(SocketTest, Recv) {
147 size_t len = 10;
148 char buf[len];
150 EXPECT_LT(ki_recv(123, NULL, len, 0), 0);
151 EXPECT_EQ(errno, EFAULT);
152 EXPECT_LT(ki_recv(-1, buf, len, 0), 0);
153 EXPECT_EQ(errno, EBADF);
154 EXPECT_LT(ki_recv(0, buf, len, 0), 0);
155 EXPECT_EQ(errno, ENOTSOCK);
158 TEST_F(SocketTest, Recvfrom) {
159 size_t len = 10;
160 char buf[len];
161 struct sockaddr addr = {};
162 socklen_t addrlen = 4;
164 EXPECT_LT(ki_recvfrom(123, NULL, len, 0, &addr, &addrlen), 0);
165 EXPECT_EQ(errno, EFAULT);
166 EXPECT_LT(ki_recvfrom(123, buf, len, 0, &addr, NULL), 0);
167 EXPECT_EQ(errno, EFAULT);
168 EXPECT_LT(ki_recvfrom(-1, buf, len, 0, &addr, &addrlen), 0);
169 EXPECT_EQ(errno, EBADF);
170 EXPECT_LT(ki_recvfrom(0, buf, len, 0, &addr, &addrlen), 0);
171 EXPECT_EQ(errno, ENOTSOCK);
174 TEST_F(SocketTest, Recvmsg) {
175 struct msghdr msg = {};
177 EXPECT_LT(ki_recvmsg(123, NULL, 0), 0);
178 EXPECT_EQ(errno, EFAULT);
179 EXPECT_LT(ki_recvmsg(-1, &msg, 0), 0);
180 EXPECT_EQ(errno, EBADF);
181 EXPECT_LT(ki_recvmsg(0, &msg, 0), 0);
182 EXPECT_EQ(errno, ENOTSOCK);
185 TEST_F(SocketTest, Send) {
186 size_t len = 10;
187 char buf[len];
189 EXPECT_LT(ki_send(123, NULL, len, 0), 0);
190 EXPECT_EQ(errno, EFAULT);
191 EXPECT_LT(ki_send(-1, buf, len, 0), 0);
192 EXPECT_EQ(errno, EBADF);
193 EXPECT_LT(ki_send(0, buf, len, 0), 0);
194 EXPECT_EQ(errno, ENOTSOCK);
197 TEST_F(SocketTest, Sendto) {
198 size_t len = 10;
199 char buf[len];
200 struct sockaddr addr = {};
201 socklen_t addrlen = 4;
203 EXPECT_LT(ki_sendto(123, NULL, len, 0, &addr, addrlen), 0);
204 EXPECT_EQ(errno, EFAULT);
205 EXPECT_LT(ki_sendto(-1, buf, len, 0, &addr, addrlen), 0);
206 EXPECT_EQ(errno, EBADF);
207 EXPECT_LT(ki_sendto(0, buf, len, 0, &addr, addrlen), 0);
208 EXPECT_EQ(errno, ENOTSOCK);
211 TEST_F(SocketTest, Sendmsg) {
212 struct msghdr msg = {};
214 EXPECT_LT(ki_sendmsg(123, NULL, 0), 0);
215 EXPECT_EQ(errno, EFAULT);
216 EXPECT_LT(ki_sendmsg(-1, &msg, 0), 0);
217 EXPECT_EQ(errno, EBADF);
218 EXPECT_LT(ki_sendmsg(0, &msg, 0), 0);
219 EXPECT_EQ(errno, ENOTSOCK);
222 TEST_F(SocketTest, Setsockopt) {
223 socklen_t len = 10;
224 char optval[len];
226 // Passing a bad address as optval should generate EFAULT
227 EXPECT_EQ(-1, ki_setsockopt(123, SOL_SOCKET, SO_ACCEPTCONN, NULL, len));
228 EXPECT_EQ(errno, EFAULT);
230 // Passing a bad socket descriptor should generate EBADF
231 EXPECT_EQ(-1, ki_setsockopt(-1, SOL_SOCKET, SO_ACCEPTCONN, optval, len));
232 EXPECT_EQ(errno, EBADF);
234 // Passing an FD that is valid but not a socket should generate ENOTSOCK
235 EXPECT_EQ(-1, ki_setsockopt(0, SOL_SOCKET, SO_ACCEPTCONN, optval, len));
236 EXPECT_EQ(errno, ENOTSOCK);
239 TEST_F(SocketTest, Shutdown) {
240 EXPECT_LT(ki_shutdown(-1, SHUT_RDWR), 0);
241 EXPECT_EQ(errno, EBADF);
242 EXPECT_LT(ki_shutdown(0, SHUT_RDWR), 0);
243 EXPECT_EQ(errno, ENOTSOCK);
246 TEST_F(SocketTest, Socket) {
247 EXPECT_LT(ki_socket(AF_UNIX, SOCK_STREAM, 0), 0);
248 EXPECT_EQ(errno, EAFNOSUPPORT);
249 EXPECT_LT(ki_socket(AF_INET, SOCK_RAW, 0), 0);
250 EXPECT_EQ(errno, EPROTONOSUPPORT);
253 TEST_F(SocketTest, Socketpair) {
254 int sv[2];
255 EXPECT_LT(ki_socketpair(AF_INET, SOCK_STREAM, 0, NULL), 0);
256 EXPECT_EQ(errno, EFAULT);
257 EXPECT_LT(ki_socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
258 EXPECT_EQ(errno, EAFNOSUPPORT);
259 EXPECT_LT(ki_socketpair(AF_INET, SOCK_STREAM, 0, sv), 0);
260 EXPECT_EQ(errno, EPROTONOSUPPORT);
261 EXPECT_LT(ki_socketpair(AF_INET6, SOCK_STREAM, 0, sv), 0);
262 EXPECT_EQ(errno, EPROTONOSUPPORT);
265 TEST(SocketUtilityFunctions, Htonl) {
266 uint32_t host_long = 0x44332211;
267 uint32_t network_long = htonl(host_long);
268 uint8_t network_bytes[4];
269 memcpy(network_bytes, &network_long, 4);
270 EXPECT_EQ(network_bytes[0], 0x44);
271 EXPECT_EQ(network_bytes[1], 0x33);
272 EXPECT_EQ(network_bytes[2], 0x22);
273 EXPECT_EQ(network_bytes[3], 0x11);
276 TEST(SocketUtilityFunctions, Htons) {
277 uint16_t host_short = 0x2211;
278 uint16_t network_short = htons(host_short);
279 uint8_t network_bytes[2];
280 memcpy(network_bytes, &network_short, 2);
281 EXPECT_EQ(network_bytes[0], 0x22);
282 EXPECT_EQ(network_bytes[1], 0x11);
285 static struct in_addr generate_ipv4_addr(uint8_t* tuple) {
286 unsigned char addr[4];
287 addr[0] = static_cast<unsigned char>(tuple[0]);
288 addr[1] = static_cast<unsigned char>(tuple[1]);
289 addr[2] = static_cast<unsigned char>(tuple[2]);
290 addr[3] = static_cast<unsigned char>(tuple[3]);
291 struct in_addr real_addr;
292 memcpy(&real_addr, addr, 4);
293 return real_addr;
296 static struct in6_addr generate_ipv6_addr(uint16_t* tuple) {
297 unsigned char addr[16];
298 for (int i = 0; i < 8; i++) {
299 addr[2*i] = (tuple[i] >> 8) & 0xFF;
300 addr[2*i+1] = tuple[i] & 0xFF;
302 struct in6_addr real_addr;
303 memcpy(&real_addr, addr, 16);
304 return real_addr;
307 TEST(SocketUtilityFunctions, Inet_addr) {
308 // Fails for if string contains non-integers.
309 ASSERT_EQ(INADDR_NONE, inet_addr("foobar"));
311 // Fails if there are too many quads
312 ASSERT_EQ(INADDR_NONE, inet_addr("0.0.0.0.0"));
314 // Fails if a single element is > 255
315 ASSERT_EQ(INADDR_NONE, inet_addr("999.0.0.0"));
317 // Fails if a single element is negative.
318 ASSERT_EQ(INADDR_NONE, inet_addr("-55.0.0.0"));
320 // In tripple, notation third integer cannot be larger
321 // and 16bit unsigned int.
322 ASSERT_EQ(INADDR_NONE, inet_addr("1.2.66000"));
324 // Success cases.
325 // Normal dotted-quad address.
326 uint32_t expected_addr = ntohl(0x07060504);
327 ASSERT_EQ(expected_addr, inet_addr("7.6.5.4"));
328 expected_addr = ntohl(0xffffffff);
329 ASSERT_EQ(expected_addr, inet_addr("255.255.255.255"));
331 // Tripple case
332 expected_addr = ntohl(1 << 24 | 2 << 16 | 3);
333 ASSERT_EQ(expected_addr, inet_addr("1.2.3"));
334 expected_addr = ntohl(1 << 24 | 2 << 16 | 300);
335 ASSERT_EQ(expected_addr, inet_addr("1.2.300"));
337 // Double case
338 expected_addr = ntohl(1 << 24 | 20000);
339 ASSERT_EQ(expected_addr, inet_addr("1.20000"));
340 expected_addr = ntohl(1 << 24 | 2);
341 ASSERT_EQ(expected_addr, inet_addr("1.2"));
343 // Single case
344 expected_addr = ntohl(255);
345 ASSERT_EQ(expected_addr, inet_addr("255"));
346 expected_addr = ntohl(4000000000U);
347 ASSERT_EQ(expected_addr, inet_addr("4000000000"));
350 TEST(SocketUtilityFunctions, Inet_aton) {
351 struct in_addr addr;
353 // Failure cases
354 ASSERT_EQ(0, inet_aton("foobar", &addr));
355 ASSERT_EQ(0, inet_aton("0.0.0.0.0", &addr));
356 ASSERT_EQ(0, inet_aton("999.0.0.0", &addr));
358 // Success cases
359 uint32_t expected_addr = htonl(0xff020304);
360 ASSERT_NE(0, inet_aton("255.2.3.4", &addr));
361 ASSERT_EQ(expected_addr, addr.s_addr);
363 expected_addr = htonl(0x01000002);
364 ASSERT_NE(0, inet_aton("1.2", &addr));
365 ASSERT_EQ(expected_addr, addr.s_addr);
367 expected_addr = htonl(0x01020003);
368 ASSERT_NE(0, inet_aton("1.2.3", &addr));
369 ASSERT_EQ(expected_addr, addr.s_addr);
371 expected_addr = htonl(0x0000100);
372 ASSERT_NE(0, inet_aton("256", &addr));
373 ASSERT_EQ(expected_addr, addr.s_addr);
376 TEST(SocketUtilityFunctions, Inet_ntoa) {
377 struct {
378 unsigned char addr_tuple[4];
379 const char* output;
380 } tests[] = {
381 { { 0, 0, 0, 0 }, "0.0.0.0" },
382 { { 127, 0, 0, 1 }, "127.0.0.1" },
383 { { 255, 255, 255, 255 }, "255.255.255.255" },
386 for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
387 char* stringified_addr = inet_ntoa(generate_ipv4_addr(tests[i].addr_tuple));
388 ASSERT_TRUE(NULL != stringified_addr);
389 EXPECT_STREQ(tests[i].output, stringified_addr);
393 TEST(SocketUtilityFunctions, Inet_ntop_ipv4) {
394 struct {
395 unsigned char addr_tuple[4];
396 const char* output;
397 } tests[] = {
398 { { 0, 0, 0, 0 }, "0.0.0.0" },
399 { { 127, 0, 0, 1 }, "127.0.0.1" },
400 { { 255, 255, 255, 255 }, "255.255.255.255" },
403 for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
404 char stringified_addr[INET_ADDRSTRLEN];
405 struct in_addr real_addr = generate_ipv4_addr(tests[i].addr_tuple);
406 EXPECT_TRUE(NULL != inet_ntop(AF_INET, &real_addr,
407 stringified_addr, INET_ADDRSTRLEN));
408 EXPECT_STREQ(tests[i].output, stringified_addr);
412 TEST(SocketUtilityFunctions, Inet_ntop_ipv6) {
413 struct {
414 unsigned short addr_tuple[8];
415 const char* output;
416 } tests[] = {
417 { { 0, 0, 0, 0, 0, 0, 0, 0 }, "::" },
418 { { 1, 2, 3, 0, 0, 0, 0, 0 }, "1:2:3::" },
419 { { 0, 0, 0, 0, 0, 1, 2, 3 }, "::1:2:3" },
420 { { 0x1234, 0xa, 0x12, 0x0000, 0x5678, 0x9abc, 0xdef, 0xffff },
421 "1234:a:12:0:5678:9abc:def:ffff" },
422 { { 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff },
423 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" },
424 { { 0, 0, 0, 0, 0, 0xffff, 0x101, 0x101 }, "::ffff:1.1.1.1" },
425 { { 0, 0, 0, 0, 0, 0, 0x101, 0x101 }, "::1.1.1.1" },
428 for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
429 char stringified_addr[INET6_ADDRSTRLEN];
430 struct in6_addr real_addr = generate_ipv6_addr(tests[i].addr_tuple);
431 EXPECT_TRUE(NULL != inet_ntop(AF_INET6, &real_addr,
432 stringified_addr, INET6_ADDRSTRLEN));
433 EXPECT_STREQ(tests[i].output, stringified_addr);
437 TEST(SocketUtilityFunctions, Inet_ntop_failure) {
438 char addr_name[INET6_ADDRSTRLEN];
439 uint16_t addr6_tuple[8] = { 0xffff, 0xffff, 0xffff, 0xffff,
440 0xffff, 0xffff, 0xffff, 0xffff };
441 uint8_t addr_tuple[4] = { 255, 255, 255, 255 };
442 struct in6_addr ipv6_addr = generate_ipv6_addr(addr6_tuple);
443 struct in_addr ipv4_addr = generate_ipv4_addr(addr_tuple);
445 EXPECT_EQ(NULL, inet_ntop(AF_UNIX, &ipv6_addr,
446 addr_name, INET6_ADDRSTRLEN));
447 EXPECT_EQ(EAFNOSUPPORT, errno);
449 EXPECT_EQ(NULL, inet_ntop(AF_INET, &ipv4_addr,
450 addr_name, INET_ADDRSTRLEN - 1));
451 EXPECT_EQ(ENOSPC, errno);
453 EXPECT_EQ(NULL, inet_ntop(AF_INET6, &ipv6_addr,
454 addr_name, INET6_ADDRSTRLEN / 2));
455 EXPECT_EQ(ENOSPC, errno);
458 TEST(SocketUtilityFunctions, Inet_pton) {
459 struct {
460 int family;
461 const char* input;
462 const char* output; // NULL means output should match input
463 } tests[] = {
464 { AF_INET, "127.127.12.0", NULL },
465 { AF_INET, "0.0.0.0", NULL },
467 { AF_INET6, "0:0:0:0:0:0:0:0", "::" },
468 { AF_INET6, "1234:5678:9abc:def0:1234:5678:9abc:def0", NULL },
469 { AF_INET6, "1:2:3:4:5:6:7:8", NULL },
470 { AF_INET6, "a:b:c:d:e:f:1:2", NULL },
471 { AF_INET6, "A:B:C:D:E:F:1:2", "a:b:c:d:e:f:1:2" },
472 { AF_INET6, "::", "::" },
473 { AF_INET6, "::12", "::12" },
474 { AF_INET6, "::1:2:3", "::1:2:3" },
475 { AF_INET6, "12::", "12::" },
476 { AF_INET6, "1:2::", "1:2::" },
477 { AF_INET6, "12:0:0:0:0:0:0:0", "12::" },
478 { AF_INET6, "1:2:3::4:5", "1:2:3::4:5" },
479 { AF_INET6, "::ffff:1.1.1.1", "::ffff:1.1.1.1" },
480 { AF_INET6, "ffff::1.1.1.1", "ffff::101:101" },
481 { AF_INET6, "::1.1.1.1", "::1.1.1.1" },
484 for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
485 uint8_t addr[16];
486 ASSERT_TRUE(inet_pton(tests[i].family, tests[i].input, addr))
487 << "inet_pton failed for " << tests[i].input;
488 const char* expected = tests[i].output ? tests[i].output : tests[i].input;
489 char out_buffer[256];
490 ASSERT_EQ(out_buffer,
491 inet_ntop(tests[i].family, addr, out_buffer, sizeof(out_buffer)));
492 ASSERT_STREQ(expected, out_buffer);
496 TEST(SocketUtilityFunctions, Inet_pton_failure) {
497 // All these are examples of strings that do not map
498 // to IP address. inet_pton returns 0 on failure.
499 uint8_t addr[16];
500 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12.24312", &addr));
501 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12.24 ", &addr));
502 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12.0.1", &addr));
503 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12. 0", &addr));
504 EXPECT_EQ(0, inet_pton(AF_INET, " 127.127.12.0", &addr));
505 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12.0.", &addr));
506 EXPECT_EQ(0, inet_pton(AF_INET, ".127.127.12.0", &addr));
507 EXPECT_EQ(0, inet_pton(AF_INET, "127.127.12.0x0", &addr));
509 EXPECT_EQ(0, inet_pton(AF_INET6, ":::", &addr));
510 EXPECT_EQ(0, inet_pton(AF_INET6, "0:::0", &addr));
511 EXPECT_EQ(0, inet_pton(AF_INET6, "0::0:0::1", &addr));
512 EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0:0:0:1: 2", &addr));
513 EXPECT_EQ(0, inet_pton(AF_INET6, " 0:0:0:0:0:0:1:2", &addr));
514 EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0:0:0:1:2 ", &addr));
515 EXPECT_EQ(0, inet_pton(AF_INET6, ":0:0:0:0:0:0:1:2", &addr));
516 EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0:0:0:1:2:4", &addr));
517 EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0:0:0:1:0.0.0.0", &addr));
518 EXPECT_EQ(0, inet_pton(AF_INET6, "::0.0.0.0:1", &addr));
519 EXPECT_EQ(0, inet_pton(AF_INET6, "::0.0.0.0.0", &addr));
520 EXPECT_EQ(0, inet_pton(AF_INET6, "::1.2.3.4.5.6.7.8", &addr));
523 TEST(SocketUtilityFunctions, Ntohs) {
524 uint8_t network_bytes[2] = { 0x22, 0x11 };
525 uint16_t network_short;
526 memcpy(&network_short, network_bytes, 2);
527 uint16_t host_short = ntohs(network_short);
528 EXPECT_EQ(host_short, 0x2211);
531 TEST(SocketUtilityFunctions, Ntohl) {
532 uint8_t network_bytes[4] = { 0x44, 0x33, 0x22, 0x11 };
533 uint32_t network_long;
534 memcpy(&network_long, network_bytes, 4);
535 uint32_t host_long = ntohl(network_long);
536 EXPECT_EQ(host_long, 0x44332211);
539 #endif // PROVIDES_SOCKETPAIR_API