Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Unified Diff: native_client_sdk/src/libraries/nacl_io_test/socket_test.cc

Issue 22625004: Adds htonl, htons, ntohl, and ntohs to newlib (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gethostbyname
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: native_client_sdk/src/libraries/nacl_io_test/socket_test.cc
diff --git a/native_client_sdk/src/libraries/nacl_io_test/socket_test.cc b/native_client_sdk/src/libraries/nacl_io_test/socket_test.cc
index 893975100ce72a0c4db346c42edd6eb670b0cdab..b7dbda063e67a8154232412ef174d28a5110136d 100644
--- a/native_client_sdk/src/libraries/nacl_io_test/socket_test.cc
+++ b/native_client_sdk/src/libraries/nacl_io_test/socket_test.cc
@@ -266,7 +266,27 @@ TEST_F(SocketTest, Socketpair) {
// These utility functions are only used for newlib (glibc provides its own
// implementations of these functions).
-#if !defined(__GLIBC__)
+#if !defined(__GLIBC__)
+
+TEST(SocketUtilityFunctions, Htonl) {
+ 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.
+ uint32_t network_long = htonl(host_long);
+ uint8_t network_bytes[4];
+ 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
+ EXPECT_EQ(network_bytes[0], 0x44);
+ EXPECT_EQ(network_bytes[1], 0x33);
+ EXPECT_EQ(network_bytes[2], 0x22);
+ EXPECT_EQ(network_bytes[3], 0x11);
+}
+
+TEST(SocketUtilityFunctions, Htons) {
+ uint16_t host_short = 0x2211;
+ uint16_t network_short = htons(host_short);
+ uint8_t network_bytes[2];
+ memcpy(network_bytes, &network_short, 2);
+ EXPECT_EQ(network_bytes[0], 0x22);
+ EXPECT_EQ(network_bytes[1], 0x11);
+}
static struct in_addr generate_ipv4_addr(int tuple1, int tuple2,
int tuple3, int tuple4) {
@@ -372,5 +392,19 @@ TEST(SocketUtilityFunctions, Inet_ntop_failure) {
EXPECT_EQ(errno, ENOSPC);
}
+TEST(SocketUtilityFunctions, Ntohs) {
+ uint8_t network_bytes[2] = { 0x22, 0x11 };
+ uint16_t *network_short = reinterpret_cast<uint16_t*>(network_bytes);
+ uint16_t host_short = ntohs(*network_short);
+ EXPECT_EQ(host_short, 0x2211);
+}
+
+TEST(SocketUtilityFunctions, Ntohl) {
+ uint8_t network_bytes[4] = { 0x44, 0x33, 0x22, 0x11 };
+ uint32_t *network_long = reinterpret_cast<uint32_t*>(network_bytes);
+ uint32_t host_long = ntohl(*network_long);
+ EXPECT_EQ(host_long, 0x44332211);
+}
+
#endif // !defined(__GLIBC__)
#endif // PROVIDES_SOCKETPAIR_API

Powered by Google App Engine
This is Rietveld 408576698