William A. Kennington III | be3fb71 | 2023-12-17 19:14:56 -0800 | [diff] [blame^] | 1 | #include <stdplus/net/addr/sock.hpp> |
| 2 | |
| 3 | #include <gtest/gtest.h> |
| 4 | |
| 5 | namespace stdplus |
| 6 | { |
| 7 | |
| 8 | TEST(Sock4Addr, Basic) |
| 9 | { |
| 10 | constexpr Sock4Addr addr1{.addr = In4Addr{255}, .port = 3959}; |
| 11 | Sock4Addr addr2{.addr = In4Addr{255}, .port = 2000}; |
| 12 | Sock4Addr addr3{.addr = In4Addr{0}, .port = 3959}; |
| 13 | |
| 14 | EXPECT_EQ(addr1, addr1); |
| 15 | EXPECT_NE(addr1, addr2); |
| 16 | EXPECT_NE(addr3, addr1); |
| 17 | |
| 18 | auto addr = addr1.sockaddr(); |
| 19 | EXPECT_EQ(AF_INET, addr.sin_family); |
| 20 | EXPECT_EQ(In4Addr{255}, addr.sin_addr); |
| 21 | EXPECT_EQ(3959, stdplus::ntoh(addr.sin_port)); |
| 22 | EXPECT_EQ(sizeof(sockaddr_in), addr1.sockaddrLen()); |
| 23 | } |
| 24 | |
| 25 | TEST(Sock6Addr, Basic) |
| 26 | { |
| 27 | constexpr Sock6Addr addr1{.addr = In6Addr{255}, .port = 3959, .scope = 0}; |
| 28 | Sock6Addr addr2{.addr = In6Addr{255}, .port = 2000, .scope = 0}; |
| 29 | Sock6Addr addr3{.addr = In6Addr{0}, .port = 3959, .scope = 0}; |
| 30 | Sock6Addr addr4{.addr = In6Addr{255}, .port = 3959, .scope = 1}; |
| 31 | |
| 32 | EXPECT_EQ(addr1, addr1); |
| 33 | EXPECT_NE(addr1, addr2); |
| 34 | EXPECT_NE(addr3, addr1); |
| 35 | EXPECT_NE(addr4, addr1); |
| 36 | |
| 37 | auto addr = addr1.sockaddr(); |
| 38 | EXPECT_EQ(AF_INET6, addr.sin6_family); |
| 39 | EXPECT_EQ(In6Addr{255}, addr.sin6_addr); |
| 40 | EXPECT_EQ(3959, stdplus::ntoh(addr.sin6_port)); |
| 41 | EXPECT_EQ(sizeof(sockaddr_in6), addr1.sockaddrLen()); |
| 42 | } |
| 43 | |
| 44 | TEST(SockAnyAddr, Basic) |
| 45 | { |
| 46 | constexpr SockAnyAddr addr1(std::in_place_type<Sock4Addr>, In4Addr{255}, |
| 47 | 3959); |
| 48 | EXPECT_EQ(addr1, addr1); |
| 49 | auto buf = addr1.buf(); |
| 50 | EXPECT_EQ(buf.len, sizeof(sockaddr_in)); |
| 51 | EXPECT_EQ(addr1.sockaddrLen(), sizeof(sockaddr_in)); |
| 52 | } |
| 53 | |
| 54 | } // namespace stdplus |