| // Copyright 2017 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| // These tests ensure the zircon libc can talk to netstack. |
| // No network connection is required, only a running netstack binary. |
| |
| #include <netdb.h> |
| |
| #include "gtest/gtest.h" |
| |
| TEST(GetAddrInfoTest, Localhost) { |
| struct addrinfo hints; |
| memset(&hints, 0, sizeof(struct addrinfo)); |
| hints.ai_family = AF_UNSPEC; |
| hints.ai_socktype = SOCK_STREAM; |
| |
| struct addrinfo* result; |
| ASSERT_EQ(getaddrinfo("localhost", NULL, &hints, &result), 0); |
| |
| int i = 0; |
| for (struct addrinfo* ai = result; ai != NULL; ai = ai->ai_next) { |
| i++; |
| |
| EXPECT_EQ(ai->ai_socktype, hints.ai_socktype); |
| const struct sockaddr* sa = ai->ai_addr; |
| |
| switch (ai->ai_family) { |
| case AF_INET: { |
| EXPECT_EQ(ai->ai_addrlen, (socklen_t)16); |
| |
| unsigned char expected_addr[4] = {0x7f, 0x00, 0x00, 0x01}; |
| |
| const struct sockaddr_in* sin = (struct sockaddr_in*)sa; |
| EXPECT_EQ(sin->sin_addr.s_addr, |
| *reinterpret_cast<uint32_t*>(expected_addr)); |
| |
| break; |
| } |
| case AF_INET6: { |
| EXPECT_EQ(ai->ai_addrlen, (socklen_t)28); |
| |
| const char expected_addr[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 0x00, 0x00, 0x00, 0x01}; |
| |
| auto sin6 = reinterpret_cast<const struct sockaddr_in6*>(sa); |
| EXPECT_STREQ((const char*)sin6->sin6_addr.s6_addr, expected_addr); |
| |
| break; |
| } |
| } |
| } |
| EXPECT_EQ(i, 2); |
| freeaddrinfo(result); |
| } |