MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_Address.cpp File Reference
#include <cstdint>
#include <cstring>
#include <array>
#include <variant>
#include <vector>
#include <endian.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <catch2/catch_test_macros.hpp>
#include <netinet/in.h>
#include "Address.hpp"
#include "Exception.hpp"
#include "FabricAddress.hpp"
#include "Provider.hpp"
Include dependency graph for test_Address.cpp:

Go to the source code of this file.

Functions

 TEST_CASE ("ofi: FabricAddress base64 decode", "[ofi][FabricAddress]")
 TEST_CASE ("ofi: FabricAddress default construction", "[ofi][FabricAddress]")
 TEST_CASE ("ofi: FabricAddress::decode FI_ADDR_EFA", "[ofi][FabricAddress]")
 TEST_CASE ("ofi: FabricAddress::decode FI_SOCKADDR_IB", "[ofi][FabricAddress]")
 TEST_CASE ("ofi: FabricAddress::decode FI_SOCKADDR_IN without a port", "[ofi][FabricAddress]")
 TEST_CASE ("ofi: FabricAddress::decode FI_SOCKADDR_IN", "[ofi][FabricAddress]")
 TEST_CASE ("ofi: FabricAddress::decode FI_SOCKADDR_IN6", "[ofi][FabricAddress]")
 TEST_CASE ("ofi: FabricAddress::decode returns an empty address for undecodable input", "[ofi][FabricAddress]")
 TEST_CASE ("ofi: FabricAddress::host returns the address portion of each type", "[ofi][FabricAddress]")
 TEST_CASE ("ofi: FabricAddress::parse checks the TCP port", "[ofi][FabricAddress]")

Function Documentation

◆ TEST_CASE() [1/10]

TEST_CASE ( "ofi: FabricAddress base64 decode" ,
"" [ofi][FabricAddress] )

Definition at line 81 of file test_Address.cpp.

81 : FabricAddress base64 decode", "[ofi][FabricAddress]")
82{
83 auto const addr = RawFabricAddress::fromBase64("AQIDBAU=", FabricAddressFormat::Unspec); // base64 for {1,2,3,4,5}
84 auto* addrInner = static_cast<uint8_t const*>(addr.raw());
85
86 REQUIRE(addr.size() == 5);
87
88 // Simulate a FabricAddress with some data
89 std::vector<uint8_t> expected = {1, 2, 3, 4, 5};
90 for (size_t i = 0; i < addr.size(); i++)
91 {
92 REQUIRE(addrInner[i] == expected[i]);
93 }
94
95 // Encode again and validate we get the same string
96 std::string b64 = addr.toBase64();
97 REQUIRE(b64 == "AQIDBAU=");
98}
Owning, type-safe representation of a libfabric address.

◆ TEST_CASE() [2/10]

TEST_CASE ( "ofi: FabricAddress default construction" ,
"" [ofi][FabricAddress] )

Definition at line 72 of file test_Address.cpp.

72 : FabricAddress default construction", "[ofi][FabricAddress]")
73{
74 auto const empty = RawFabricAddress{};
75 REQUIRE(empty.size() == 0);
76 REQUIRE(empty.raw() == nullptr);
77 REQUIRE(empty.raw() == static_cast<void*>(nullptr));
78 REQUIRE(empty.toBase64().empty());
79}

◆ TEST_CASE() [3/10]

TEST_CASE ( "ofi: FabricAddress::decode FI_ADDR_EFA" ,
"" [ofi][FabricAddress] )

Definition at line 147 of file test_Address.cpp.

147 : FabricAddress::decode FI_ADDR_EFA", "[ofi][FabricAddress]")
148{
149 auto const native = makeEfa("fe80::1", 12, 34);
150 auto const addr = FabricAddress::decode(FabricAddressFormat::Efa, native.data(), native.size());
151
152 REQUIRE(std::holds_alternative<EfaAddress>(addr.native()));
153 auto const& efa = std::get<EfaAddress>(addr.native());
154 REQUIRE(efa.qpn == 12);
155 REQUIRE(efa.qkey == 34);
156
157 REQUIRE(addr.toString() == "fi_addr_efa://[fe80::1]:12:34");
158}
static FabricAddress decode(FabricAddressFormat addrFormat, void const *addr, std::size_t len)
Decode a raw libfabric address buffer of the given addr_format.

◆ TEST_CASE() [4/10]

TEST_CASE ( "ofi: FabricAddress::decode FI_SOCKADDR_IB" ,
"" [ofi][FabricAddress] )

Definition at line 160 of file test_Address.cpp.

160 : FabricAddress::decode FI_SOCKADDR_IB", "[ofi][FabricAddress]")
161{
162 // P_Key, port space and scope id are hexadecimal; the lower 16 bits of the service id carry
163 // the port.
164 auto const native = makeSockaddrIb("fe80::1", /*pkey=*/0x1234, /*portSpace=*/0x10b, /*scopeId=*/0xff, /*port=*/0x1f90);
165 auto const addr = FabricAddress::decode(FabricAddressFormat::SockaddrIb, &native, sizeof(native));
166
167 REQUIRE(std::holds_alternative<OfiSockaddrIb>(addr.native()));
168 auto const& sib = std::get<OfiSockaddrIb>(addr.native());
169 REQUIRE(sib.sib_family == AF_IB);
170 REQUIRE(ntohs(sib.sib_pkey) == 0x1234);
171 REQUIRE(be64toh(sib.sib_scope_id) == 0xff);
172
173 // The service id packs the port space in bits 16..31 and the port in bits 0..15.
174 REQUIRE(((be64toh(sib.sib_sid) >> 16) & 0xfff) == 0x10b);
175 REQUIRE((be64toh(sib.sib_sid) & 0xffff) == 0x1f90);
176
177 // ofi_straddr does not emit the port, so the rendering drops it (but keeps the port space).
178 REQUIRE(addr.toString() == "fi_sockaddr_ib://[fe80::1]:0x1234:0x10b:0xff");
179}

◆ TEST_CASE() [5/10]

TEST_CASE ( "ofi: FabricAddress::decode FI_SOCKADDR_IN without a port" ,
"" [ofi][FabricAddress] )

Definition at line 120 of file test_Address.cpp.

120 : FabricAddress::decode FI_SOCKADDR_IN without a port", "[ofi][FabricAddress]")
121{
122 auto const native = makeSockaddrIn("10.130.40.13", 0);
123 auto const addr = FabricAddress::decode(FabricAddressFormat::SockaddrIn, &native, sizeof(native));
124
125 REQUIRE(std::holds_alternative<::sockaddr_in>(addr.native()));
126 auto const& sin = std::get<::sockaddr_in>(addr.native());
127 REQUIRE(sin.sin_family == AF_INET);
128 REQUIRE(ntohs(sin.sin_port) == 0);
129
130 // An absent port is rendered as :0 by ofi_straddr.
131 REQUIRE(addr.toString() == "fi_sockaddr_in://10.130.40.13:0");
132}

◆ TEST_CASE() [6/10]

TEST_CASE ( "ofi: FabricAddress::decode FI_SOCKADDR_IN" ,
"" [ofi][FabricAddress] )

Definition at line 100 of file test_Address.cpp.

100 : FabricAddress::decode FI_SOCKADDR_IN", "[ofi][FabricAddress]")
101{
102 auto const native = makeSockaddrIn("10.130.40.13", 9000);
103 auto const addr = FabricAddress::decode(FabricAddressFormat::SockaddrIn, &native, sizeof(native));
104
105 REQUIRE_FALSE(addr.empty());
106 REQUIRE(std::holds_alternative<::sockaddr_in>(addr.native()));
107
108 auto const& sin = std::get<::sockaddr_in>(addr.native());
109 REQUIRE(sin.sin_family == AF_INET);
110 REQUIRE(ntohs(sin.sin_port) == 9000);
111
112 char ip[INET_ADDRSTRLEN] = {};
113 REQUIRE(::inet_ntop(AF_INET, &sin.sin_addr, ip, sizeof(ip)) != nullptr);
114 REQUIRE(std::string{ip} == "10.130.40.13");
115
116 // Renders to the canonical ofi_straddr string.
117 REQUIRE(addr.toString() == "fi_sockaddr_in://10.130.40.13:9000");
118}

◆ TEST_CASE() [7/10]

TEST_CASE ( "ofi: FabricAddress::decode FI_SOCKADDR_IN6" ,
"" [ofi][FabricAddress] )

Definition at line 134 of file test_Address.cpp.

134 : FabricAddress::decode FI_SOCKADDR_IN6", "[ofi][FabricAddress]")
135{
136 auto const native = makeSockaddrIn6("2001:db8::1", 443);
137 auto const addr = FabricAddress::decode(FabricAddressFormat::SockaddrIn6, &native, sizeof(native));
138
139 REQUIRE(std::holds_alternative<::sockaddr_in6>(addr.native()));
140 auto const& sin6 = std::get<::sockaddr_in6>(addr.native());
141 REQUIRE(sin6.sin6_family == AF_INET6);
142 REQUIRE(ntohs(sin6.sin6_port) == 443);
143
144 REQUIRE(addr.toString() == "fi_sockaddr_in6://[2001:db8::1]:443");
145}

◆ TEST_CASE() [8/10]

TEST_CASE ( "ofi: FabricAddress::decode returns an empty address for undecodable input" ,
"" [ofi][FabricAddress] )

Definition at line 199 of file test_Address.cpp.

199 : FabricAddress::decode returns an empty address for undecodable input", "[ofi][FabricAddress]")
200{
201 auto const sin = makeSockaddrIn("10.0.0.1", 9000);
202
203 // A null address buffer.
204 REQUIRE(FabricAddress::decode(FabricAddressFormat::SockaddrIn, nullptr, sizeof(sin)).empty());
205
206 // A buffer that is too small for the declared format.
207 REQUIRE(FabricAddress::decode(FabricAddressFormat::SockaddrIn, &sin, sizeof(sin) - 1).empty());
208
209 auto const efa = makeEfa("fe80::1", 12, 34);
210 REQUIRE(FabricAddress::decode(FabricAddressFormat::SockaddrIn6, efa.data(), efa.size() - 1).empty());
211
212 // An unsupported / unrecognised address format.
213 REQUIRE(FabricAddress::decode(FabricAddressFormat::Unspec, &sin, sizeof(sin)).empty());
214
215 // FI_SOCKADDR dispatches on the embedded family; an unknown family does not decode.
216 auto unknown = ::sockaddr{};
217 unknown.sa_family = AF_UNSPEC;
218 REQUIRE(FabricAddress::decode(FabricAddressFormat::Sockaddr, &unknown, sizeof(unknown)).empty());
219}
static FabricInfo empty() noexcept
Create and empty FabricInfo.

◆ TEST_CASE() [9/10]

TEST_CASE ( "ofi: FabricAddress::host returns the address portion of each type" ,
"" [ofi][FabricAddress] )

Definition at line 181 of file test_Address.cpp.

181 : FabricAddress::host returns the address portion of each type", "[ofi][FabricAddress]")
182{
183 auto const sin = makeSockaddrIn("10.130.40.13", 9000);
184 REQUIRE(FabricAddress::decode(FabricAddressFormat::SockaddrIn, &sin, sizeof(sin)).node() == "10.130.40.13");
185
186 auto const sin6 = makeSockaddrIn6("2001:db8::1", 443);
187 REQUIRE(FabricAddress::decode(FabricAddressFormat::SockaddrIn6, &sin6, sizeof(sin6)).node() == "2001:db8::1");
188
189 auto const efa = makeEfa("fe80::1", 12, 34);
190 REQUIRE(FabricAddress::decode(FabricAddressFormat::Efa, efa.data(), efa.size()).node() == "fe80::1");
191
192 auto const sib = makeSockaddrIb("fe80::1", 0x1234, 0x10b, 0xff, 0x1f90);
193 REQUIRE(FabricAddress::decode(FabricAddressFormat::SockaddrIb, &sib, sizeof(sib)).node() == "fe80::1");
194
195 // An empty / undecoded address has no host.
196 REQUIRE_FALSE(FabricAddress{}.node().has_value());
197}

◆ TEST_CASE() [10/10]

TEST_CASE ( "ofi: FabricAddress::parse checks the TCP port" ,
"" [ofi][FabricAddress] )

Definition at line 221 of file test_Address.cpp.

221 : FabricAddress::parse checks the TCP port", "[ofi][FabricAddress]")
222{
223 auto const addr = FabricAddress::parse(Provider::TCP, "10.130.40.13", "9000");
224 REQUIRE(std::holds_alternative<::sockaddr_in>(addr.native()));
225 REQUIRE(std::holds_alternative<::sockaddr_in>(FabricAddress::parse(Provider::TCP, "10.130.40.13", "65535").native()));
226
227 // std::stoi accepted these, and the int was then narrowed to a 16-bit port.
228 for (auto const* service : {"65536", "-1", "9000x", " 9000", "+9000"})
229 {
230 CAPTURE(service);
231 try
232 {
233 (void)FabricAddress::parse(Provider::TCP, "10.130.40.13", service);
234 FAIL("parse accepted an invalid port");
235 }
236 catch (Exception const& e)
237 {
238 REQUIRE(e.status() == MXL_ERR_INVALID_ARG);
239 }
240 }
241}
static FabricAddress parse(Provider provider, std::optional< std::string > node, std::optional< std::string > service)
Parse a node/service pair into a typed address for the given provider.