Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include <errno.h> | 4 #include <errno.h> |
| 5 #include <fcntl.h> | 5 #include <fcntl.h> |
| 6 #include <pthread.h> | 6 #include <pthread.h> |
| 7 #include <sys/stat.h> | 7 #include <sys/stat.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 EXPECT_LT(ki_socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); | 259 EXPECT_LT(ki_socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); |
| 260 EXPECT_EQ(errno, EAFNOSUPPORT); | 260 EXPECT_EQ(errno, EAFNOSUPPORT); |
| 261 EXPECT_LT(ki_socketpair(AF_INET, SOCK_STREAM, 0, sv), 0); | 261 EXPECT_LT(ki_socketpair(AF_INET, SOCK_STREAM, 0, sv), 0); |
| 262 EXPECT_EQ(errno, EPROTONOSUPPORT); | 262 EXPECT_EQ(errno, EPROTONOSUPPORT); |
| 263 EXPECT_LT(ki_socketpair(AF_INET6, SOCK_STREAM, 0, sv), 0); | 263 EXPECT_LT(ki_socketpair(AF_INET6, SOCK_STREAM, 0, sv), 0); |
| 264 EXPECT_EQ(errno, EPROTONOSUPPORT); | 264 EXPECT_EQ(errno, EPROTONOSUPPORT); |
| 265 } | 265 } |
| 266 | 266 |
| 267 // These utility functions are only used for newlib (glibc provides its own | 267 // These utility functions are only used for newlib (glibc provides its own |
| 268 // implementations of these functions). | 268 // implementations of these functions). |
| 269 #if !defined(__GLIBC__) | 269 #if !defined(__GLIBC__) |
| 270 | |
| 271 TEST(SocketUtilityFunctions, Htonl) { | |
| 272 uint32_t host_long = 0x44332211; | |
|
Sam Clegg
2013/08/08 01:47:25
Doesn't this test depend on the byte order of the
binji
2013/08/08 16:07:21
Do we have a big-endian NaCl?
torinmr
2013/08/08 17:01:22
I believe this works on any host byte order: 0x443
Sam Clegg
2013/08/08 17:21:39
Ha! I stand corrected. Very good.
| |
| 273 uint32_t network_long = htonl(host_long); | |
| 274 uint8_t network_bytes[4]; | |
| 275 memcpy(network_bytes, &network_long, 4); | |
|
Sam Clegg
2013/08/08 01:47:25
Just use a cast maybe?
binji
2013/08/08 16:07:21
I disagree, that breaks strict aliasing. See above
torinmr
2013/08/08 17:01:22
I tried that first, but then the compiler complain
Sam Clegg
2013/08/08 17:21:39
Fair enough. Ben and the compiler and right I thi
| |
| 276 EXPECT_EQ(network_bytes[0], 0x44); | |
| 277 EXPECT_EQ(network_bytes[1], 0x33); | |
| 278 EXPECT_EQ(network_bytes[2], 0x22); | |
| 279 EXPECT_EQ(network_bytes[3], 0x11); | |
| 280 } | |
| 281 | |
| 282 TEST(SocketUtilityFunctions, Htons) { | |
| 283 uint16_t host_short = 0x2211; | |
| 284 uint16_t network_short = htons(host_short); | |
| 285 uint8_t network_bytes[2]; | |
| 286 memcpy(network_bytes, &network_short, 2); | |
| 287 EXPECT_EQ(network_bytes[0], 0x22); | |
| 288 EXPECT_EQ(network_bytes[1], 0x11); | |
| 289 } | |
| 270 | 290 |
| 271 static struct in_addr generate_ipv4_addr(int tuple1, int tuple2, | 291 static struct in_addr generate_ipv4_addr(int tuple1, int tuple2, |
| 272 int tuple3, int tuple4) { | 292 int tuple3, int tuple4) { |
| 273 unsigned char addr[4]; | 293 unsigned char addr[4]; |
| 274 addr[0] = static_cast<unsigned char>(tuple1); | 294 addr[0] = static_cast<unsigned char>(tuple1); |
| 275 addr[1] = static_cast<unsigned char>(tuple2); | 295 addr[1] = static_cast<unsigned char>(tuple2); |
| 276 addr[2] = static_cast<unsigned char>(tuple3); | 296 addr[2] = static_cast<unsigned char>(tuple3); |
| 277 addr[3] = static_cast<unsigned char>(tuple4); | 297 addr[3] = static_cast<unsigned char>(tuple4); |
| 278 struct in_addr* real_addr = reinterpret_cast<struct in_addr*>(addr); | 298 struct in_addr* real_addr = reinterpret_cast<struct in_addr*>(addr); |
| 279 return *real_addr; | 299 return *real_addr; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 365 | 385 |
| 366 EXPECT_TRUE(NULL == inet_ntop(AF_INET, &ipv4_addr, | 386 EXPECT_TRUE(NULL == inet_ntop(AF_INET, &ipv4_addr, |
| 367 addr_name, INET_ADDRSTRLEN - 1)); | 387 addr_name, INET_ADDRSTRLEN - 1)); |
| 368 EXPECT_EQ(errno, ENOSPC); | 388 EXPECT_EQ(errno, ENOSPC); |
| 369 | 389 |
| 370 EXPECT_TRUE(NULL == inet_ntop(AF_INET6, &ipv6_addr, | 390 EXPECT_TRUE(NULL == inet_ntop(AF_INET6, &ipv6_addr, |
| 371 addr_name, INET6_ADDRSTRLEN - 1)); | 391 addr_name, INET6_ADDRSTRLEN - 1)); |
| 372 EXPECT_EQ(errno, ENOSPC); | 392 EXPECT_EQ(errno, ENOSPC); |
| 373 } | 393 } |
| 374 | 394 |
| 395 TEST(SocketUtilityFunctions, Ntohs) { | |
| 396 uint8_t network_bytes[2] = { 0x22, 0x11 }; | |
| 397 uint16_t *network_short = reinterpret_cast<uint16_t*>(network_bytes); | |
| 398 uint16_t host_short = ntohs(*network_short); | |
| 399 EXPECT_EQ(host_short, 0x2211); | |
| 400 } | |
| 401 | |
| 402 TEST(SocketUtilityFunctions, Ntohl) { | |
| 403 uint8_t network_bytes[4] = { 0x44, 0x33, 0x22, 0x11 }; | |
| 404 uint32_t *network_long = reinterpret_cast<uint32_t*>(network_bytes); | |
| 405 uint32_t host_long = ntohl(*network_long); | |
| 406 EXPECT_EQ(host_long, 0x44332211); | |
| 407 } | |
| 408 | |
| 375 #endif // !defined(__GLIBC__) | 409 #endif // !defined(__GLIBC__) |
| 376 #endif // PROVIDES_SOCKETPAIR_API | 410 #endif // PROVIDES_SOCKETPAIR_API |
| OLD | NEW |