[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / lldb / unittests / Host / SocketAddressTest.cpp
blobdc5f55253f636e6aa7aa5c7f623ed68d0db33690
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 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/SocketAddress.h"
10 #include "TestingSupport/SubsystemRAII.h"
11 #include "lldb/Host/Socket.h"
12 #include "llvm/Testing/Support/Error.h"
14 #include "gtest/gtest.h"
16 using namespace lldb_private;
18 namespace {
19 class SocketAddressTest : public testing::Test {
20 public:
21 SubsystemRAII<Socket> subsystems;
23 } // namespace
25 TEST_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());
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());
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());
42 TEST_F(SocketAddressTest, GetAddressInfo) {
43 auto addr = SocketAddress::GetAddressInfo("127.0.0.1", 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());
50 #ifdef _WIN32
52 // we need to test our inet_ntop implementation for Windows XP
53 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
55 TEST_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};
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));
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));
83 #endif