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

source code of lldb/unittests/Host/SocketAddressTest.cpp