Skip to content

Commit 9ba918d

Browse files
cover error cases
1 parent 4a05f88 commit 9ba918d

2 files changed

Lines changed: 53 additions & 21 deletions

File tree

src/util/network/sock_t.hpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,21 @@ namespace util {
6363
* @return true if the addresses are equal, false otherwise.
6464
*/
6565
friend bool operator==(const sock_t& a, const sock_t& b) {
66-
if (a.sock.sa_family != b.sock.sa_family) {
67-
return false;
66+
if ((a.sock.sa_family != AF_INET && a.sock.sa_family != AF_INET6)
67+
|| (b.sock.sa_family != AF_INET && b.sock.sa_family != AF_INET6)) {
68+
throw std::system_error(EAFNOSUPPORT, std::system_category(), "Unsupported address family");
6869
}
6970

70-
switch (a.sock.sa_family) {
71-
case AF_INET:
72-
return a.ipv4.sin_port == b.ipv4.sin_port && a.ipv4.sin_addr.s_addr == b.ipv4.sin_addr.s_addr;
73-
case AF_INET6:
74-
return a.ipv6.sin6_port == b.ipv6.sin6_port
75-
&& std::memcmp(&a.ipv6.sin6_addr, &b.ipv6.sin6_addr, sizeof(in6_addr)) == 0;
76-
default:
77-
throw std::system_error(EAFNOSUPPORT, std::system_category(), "Unsupported address family");
71+
if (a.sock.sa_family != b.sock.sa_family) {
72+
return false;
7873
}
74+
return a.sock.sa_family == AF_INET
75+
? a.ipv4.sin_port == b.ipv4.sin_port && a.ipv4.sin_addr.s_addr == b.ipv4.sin_addr.s_addr
76+
: a.ipv6.sin6_port == b.ipv6.sin6_port
77+
&& std::memcmp(&a.ipv6.sin6_addr, &b.ipv6.sin6_addr, sizeof(in6_addr)) == 0;
7978
}
8079

80+
8181
/**
8282
* Inequality comparison operator for sock_t.
8383
*
@@ -99,21 +99,21 @@ namespace util {
9999
* @return true if a is less than b, false otherwise
100100
*/
101101
friend bool operator<(const sock_t& a, const sock_t& b) {
102+
if ((a.sock.sa_family != AF_INET && a.sock.sa_family != AF_INET6)
103+
|| (b.sock.sa_family != AF_INET && b.sock.sa_family != AF_INET6)) {
104+
throw std::system_error(EAFNOSUPPORT, std::system_category(), "Unsupported address family");
105+
}
106+
102107
if (a.sock.sa_family != b.sock.sa_family) {
103108
return a.sock.sa_family < b.sock.sa_family;
104109
}
105110

106-
switch (a.sock.sa_family) {
107-
case AF_INET:
108-
return std::forward_as_tuple(a.ipv4.sin_addr.s_addr, ntohs(a.ipv4.sin_port))
109-
< std::forward_as_tuple(b.ipv4.sin_addr.s_addr, ntohs(b.ipv4.sin_port));
110-
case AF_INET6: {
111-
int cmp = std::memcmp(&a.ipv6.sin6_addr, &b.ipv6.sin6_addr, sizeof(in6_addr));
112-
return cmp < 0 || (cmp == 0 && ntohs(a.ipv6.sin6_port) < ntohs(b.ipv6.sin6_port));
113-
}
114-
default:
115-
throw std::system_error(EAFNOSUPPORT, std::system_category(), "Unsupported address family");
116-
}
111+
return a.sock.sa_family == AF_INET
112+
? std::forward_as_tuple(a.ipv4.sin_addr.s_addr, ntohs(a.ipv4.sin_port))
113+
< std::forward_as_tuple(b.ipv4.sin_addr.s_addr, ntohs(b.ipv4.sin_port))
114+
: std::memcmp(&a.ipv6.sin6_addr, &b.ipv6.sin6_addr, sizeof(in6_addr)) < 0
115+
|| (std::memcmp(&a.ipv6.sin6_addr, &b.ipv6.sin6_addr, sizeof(in6_addr)) == 0
116+
&& ntohs(a.ipv6.sin6_port) < ntohs(b.ipv6.sin6_port));
117117
}
118118

119119
/**

tests/tests/util/network/sock_t.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,38 @@ SCENARIO("Address resolution handles errors", "[sock_t]") {
376376
THEN("attempting to resolve it should throw") {
377377
REQUIRE_THROWS_AS(addr.address(), std::system_error);
378378
}
379+
380+
THEN("equality comparison should throw") {
381+
sock_t other{};
382+
other.sock.sa_family = AF_UNSPEC;
383+
REQUIRE_THROWS_AS(addr == other, std::system_error);
384+
REQUIRE_THROWS_AS(addr != other, std::system_error);
385+
}
386+
387+
THEN("less than comparison should throw") {
388+
sock_t other{};
389+
other.sock.sa_family = AF_UNSPEC;
390+
REQUIRE_THROWS_AS(addr < other, std::system_error);
391+
}
392+
}
393+
394+
GIVEN("A socket with an unsupported address family compared with a valid socket") {
395+
sock_t invalid_addr{};
396+
invalid_addr.sock.sa_family = AF_UNSPEC;
397+
398+
sock_t valid_addr{};
399+
valid_addr.sock.sa_family = AF_INET;
400+
valid_addr.ipv4.sin_addr.s_addr = htonl(0xC0A80101); // 192.168.1.1
401+
valid_addr.ipv4.sin_port = htons(12345);
402+
403+
THEN("equality comparison should throw") {
404+
REQUIRE_THROWS_AS(invalid_addr == valid_addr, std::system_error);
405+
REQUIRE_THROWS_AS(invalid_addr != valid_addr, std::system_error);
406+
}
407+
408+
THEN("less than comparison should throw") {
409+
REQUIRE_THROWS_AS(invalid_addr < valid_addr, std::system_error);
410+
}
379411
}
380412
}
381413

0 commit comments

Comments
 (0)