blob: 372092e1a16f4445319dd846ea512c2b193d83d2 [file] [log] [blame]
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -08001#include <stdplus/net/addr/sock.hpp>
2
William A. Kennington IIIc03c91f2023-12-17 22:18:53 -08003#include <string_view>
4
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -08005#include <gtest/gtest.h>
6
William A. Kennington IIIc03c91f2023-12-17 22:18:53 -08007using std::literals::string_view_literals::operator""sv;
8
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -08009namespace stdplus
10{
11
12TEST(Sock4Addr, Basic)
13{
14 constexpr Sock4Addr addr1{.addr = In4Addr{255}, .port = 3959};
15 Sock4Addr addr2{.addr = In4Addr{255}, .port = 2000};
16 Sock4Addr addr3{.addr = In4Addr{0}, .port = 3959};
17
18 EXPECT_EQ(addr1, addr1);
19 EXPECT_NE(addr1, addr2);
20 EXPECT_NE(addr3, addr1);
21
22 auto addr = addr1.sockaddr();
23 EXPECT_EQ(AF_INET, addr.sin_family);
24 EXPECT_EQ(In4Addr{255}, addr.sin_addr);
25 EXPECT_EQ(3959, stdplus::ntoh(addr.sin_port));
26 EXPECT_EQ(sizeof(sockaddr_in), addr1.sockaddrLen());
27}
28
29TEST(Sock6Addr, Basic)
30{
31 constexpr Sock6Addr addr1{.addr = In6Addr{255}, .port = 3959, .scope = 0};
32 Sock6Addr addr2{.addr = In6Addr{255}, .port = 2000, .scope = 0};
33 Sock6Addr addr3{.addr = In6Addr{0}, .port = 3959, .scope = 0};
34 Sock6Addr addr4{.addr = In6Addr{255}, .port = 3959, .scope = 1};
35
36 EXPECT_EQ(addr1, addr1);
37 EXPECT_NE(addr1, addr2);
38 EXPECT_NE(addr3, addr1);
39 EXPECT_NE(addr4, addr1);
40
41 auto addr = addr1.sockaddr();
42 EXPECT_EQ(AF_INET6, addr.sin6_family);
43 EXPECT_EQ(In6Addr{255}, addr.sin6_addr);
44 EXPECT_EQ(3959, stdplus::ntoh(addr.sin6_port));
45 EXPECT_EQ(sizeof(sockaddr_in6), addr1.sockaddrLen());
46}
47
William A. Kennington IIIc03c91f2023-12-17 22:18:53 -080048TEST(SockUAddr, Basic)
49{
50 // Non-abstract Requires null-terminator
51 EXPECT_THROW(SockUAddr(std::string(SockUAddr::maxLen, 'a')),
52 std::invalid_argument);
53 EXPECT_NO_THROW(SockUAddr(std::string(SockUAddr::maxLen - 1, 'a')));
54
55 // Abstract can use the full space
56 EXPECT_THROW(SockUAddr(std::string(SockUAddr::maxLen + 1, '\0')),
57 std::invalid_argument);
58 EXPECT_NO_THROW(SockUAddr(std::string(SockUAddr::maxLen, '\0')));
59
60 EXPECT_THROW(SockUAddr("hi\0o"sv), std::invalid_argument);
61
62 constexpr SockUAddr addr1{"@hi"sv};
63 constexpr SockUAddr addr2{"\0hi"sv};
64 constexpr SockUAddr addr3{"/nope"sv};
65 constexpr SockUAddr addr4{""sv};
66
67 EXPECT_EQ(addr1, addr1);
68 EXPECT_EQ(addr1, addr2);
69 EXPECT_NE(addr3, addr1);
70 EXPECT_NE(addr4, addr1);
71 EXPECT_NE(addr4, addr3);
72
73 EXPECT_EQ(addr1.path(), "@hi"sv);
74 EXPECT_EQ(addr2.path(), "@hi"sv);
75 EXPECT_EQ(addr3.path(), "/nope"sv);
76 EXPECT_EQ(addr4.path(), ""sv);
77
78 auto addr = addr1.sockaddr();
79 EXPECT_EQ(AF_UNIX, addr.sun_family);
80 EXPECT_EQ("\0hi"sv, std::string_view(addr.sun_path, 3));
81 auto buf = addr2.buf();
82 auto ptr = reinterpret_cast<std::uint8_t*>(&buf);
83 EXPECT_TRUE(
84 std::equal(ptr, ptr + buf.len, reinterpret_cast<std::uint8_t*>(&addr)));
85 addr = addr3;
86 EXPECT_EQ("/nope\0"sv, std::string_view(addr.sun_path, 6));
87 EXPECT_EQ(addr1.sockaddrLen(), sizeof(addr.sun_family) + 3);
88 EXPECT_EQ(addr2.sockaddrLen(), sizeof(addr.sun_family) + 3);
89 EXPECT_EQ(addr3.sockaddrLen(), sizeof(addr.sun_family) + 6);
90 EXPECT_EQ(addr4.sockaddrLen(), sizeof(addr.sun_family));
91}
92
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -080093TEST(SockAnyAddr, Basic)
94{
95 constexpr SockAnyAddr addr1(std::in_place_type<Sock4Addr>, In4Addr{255},
96 3959);
William A. Kennington IIIc03c91f2023-12-17 22:18:53 -080097 constexpr SockAnyAddr addr2(std::in_place_type<SockUAddr>, "/hi"sv);
98 EXPECT_NE(addr1, addr2);
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -080099 EXPECT_EQ(addr1, addr1);
100 auto buf = addr1.buf();
101 EXPECT_EQ(buf.len, sizeof(sockaddr_in));
102 EXPECT_EQ(addr1.sockaddrLen(), sizeof(sockaddr_in));
William A. Kennington IIIc03c91f2023-12-17 22:18:53 -0800103 EXPECT_EQ(addr2.sockaddrLen(), sizeof(sa_family_t) + 4);
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -0800104}
105
106} // namespace stdplus