1 //===-- SocketAddressTest.cpp -----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Host/SocketAddress.h"
10 #include "lldb/Host/Socket.h"
11 #include "llvm/Testing/Support/Error.h"
12 
13 #include "gtest/gtest.h"
14 
15 using namespace lldb_private;
16 
17 namespace {
18 class SocketAddressTest : public testing::Test {
19 public:
20   static void SetUpTestCase() {
21     ASSERT_THAT_ERROR(Socket::Initialize(), llvm::Succeeded());
22   }
23   static void TearDownTestCase() { Socket::Terminate(); }
24 };
25 } // namespace
26 
27 TEST_F(SocketAddressTest, Set) {
28   SocketAddress sa;
29   ASSERT_TRUE(sa.SetToLocalhost(AF_INET, 1138));
30   ASSERT_STREQ("127.0.0.1", sa.GetIPAddress().c_str());
31   ASSERT_EQ(1138, sa.GetPort());
32 
33   ASSERT_TRUE(sa.SetToAnyAddress(AF_INET, 0));
34   ASSERT_STREQ("0.0.0.0", sa.GetIPAddress().c_str());
35   ASSERT_EQ(0, sa.GetPort());
36 
37   ASSERT_TRUE(sa.SetToLocalhost(AF_INET6, 1139));
38   ASSERT_TRUE(sa.GetIPAddress() == "::1" ||
39               sa.GetIPAddress() == "0:0:0:0:0:0:0:1")
40       << "Address was: " << sa.GetIPAddress();
41   ASSERT_EQ(1139, sa.GetPort());
42 }
43 
44 TEST_F(SocketAddressTest, GetAddressInfo) {
45   auto addr = SocketAddress::GetAddressInfo("127.0.0.1", nullptr, AF_UNSPEC,
46                                             SOCK_STREAM, IPPROTO_TCP);
47   ASSERT_EQ(1u, addr.size());
48   EXPECT_EQ(AF_INET, addr[0].GetFamily());
49   EXPECT_EQ("127.0.0.1", addr[0].GetIPAddress());
50 }
51 
52 #ifdef _WIN32
53 
54 // we need to test our inet_ntop implementation for Windows XP
55 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
56 
57 TEST_F(SocketAddressTest, inet_ntop) {
58   const uint8_t address4[4] = {255, 0, 1, 100};
59   const uint8_t address6[16] = {0, 1, 2,  3,  4,  5,  6,   7,
60                                 8, 9, 10, 11, 12, 13, 255, 0};
61 
62   char buffer[INET6_ADDRSTRLEN];
63   memset(buffer, 'x', sizeof(buffer));
64   EXPECT_STREQ("1:203:405:607:809:a0b:c0d:ff00",
65                inet_ntop(AF_INET6, address6, buffer, sizeof(buffer)));
66   memset(buffer, 'x', sizeof(buffer));
67   EXPECT_STREQ("1:203:405:607:809:a0b:c0d:ff00",
68                inet_ntop(AF_INET6, address6, buffer, 31));
69   memset(buffer, 'x', sizeof(buffer));
70   EXPECT_STREQ(nullptr, inet_ntop(AF_INET6, address6, buffer, 0));
71   memset(buffer, 'x', sizeof(buffer));
72   EXPECT_STREQ(nullptr, inet_ntop(AF_INET6, address6, buffer, 30));
73 
74   memset(buffer, 'x', sizeof(buffer));
75   EXPECT_STREQ("255.0.1.100",
76                inet_ntop(AF_INET, address4, buffer, sizeof(buffer)));
77   memset(buffer, 'x', sizeof(buffer));
78   EXPECT_STREQ("255.0.1.100", inet_ntop(AF_INET, address4, buffer, 12));
79   memset(buffer, 'x', sizeof(buffer));
80   EXPECT_STREQ(nullptr, inet_ntop(AF_INET, address4, buffer, 0));
81   memset(buffer, 'x', sizeof(buffer));
82   EXPECT_STREQ(nullptr, inet_ntop(AF_INET, address4, buffer, 11));
83 }
84 
85 #endif
86