forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathip_address_tests.cpp
More file actions
89 lines (72 loc) · 3.02 KB
/
ip_address_tests.cpp
File metadata and controls
89 lines (72 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/net/ip_address.hpp>
#include "doctest/cppcoro_doctest.h"
#include <string_view>
TEST_SUITE_BEGIN("ip_address");
using cppcoro::net::ip_address;
using cppcoro::net::ipv4_address;
using cppcoro::net::ipv6_address;
TEST_CASE("default constructor")
{
ip_address x;
CHECK(x.is_ipv4());
CHECK(x.to_ipv4() == ipv4_address{});
}
TEST_CASE("to_string")
{
ip_address a = ipv6_address{ 0xAABBCCDD00112233, 0x0102030405060708 };
ip_address b = ipv4_address{ 192, 168, 0, 1 };
CHECK(a.to_string() == "aabb:ccdd:11:2233:102:304:506:708");
CHECK(b.to_string() == "192.168.0.1");
}
TEST_CASE("from_string")
{
CHECK(ip_address::from_string("") == std::nullopt);
CHECK(ip_address::from_string("foo") == std::nullopt);
CHECK(ip_address::from_string(" 192.168.0.1") == std::nullopt);
CHECK(ip_address::from_string("192.168.0.1asdf") == std::nullopt);
CHECK(ip_address::from_string("192.168.0.1") == ipv4_address(192, 168, 0, 1));
CHECK(ip_address::from_string("::192.168.0.1") == ipv6_address(0, 0, 0, 0, 0, 0, 0xc0a8, 0x1));
CHECK(ip_address::from_string("aabb:ccdd:11:2233:102:304:506:708") ==
ipv6_address{ 0xAABBCCDD00112233, 0x0102030405060708 });
}
TEST_CASE("round-trip and ordering")
{
// Round-trip: to_string(from_string(s)) yields canonical string, and parsing back preserves value
auto check_round_trip = [](std::string_view s) {
auto p = ip_address::from_string(s);
REQUIRE(p.has_value());
auto canon = p->to_string();
auto p2 = ip_address::from_string(canon);
REQUIRE(p2.has_value());
CHECK(*p == *p2);
// Canonical string should be stable
CHECK(p2->to_string() == canon);
};
// IPv4
check_round_trip("0.0.0.0");
check_round_trip("255.255.255.255");
// IPv6 (mixed case input should parse and normalise to lower-case hex without leading zeros)
check_round_trip("::");
check_round_trip("::1");
check_round_trip("FFFF:0000:0000:0000:0000:0000:0000:0001");
check_round_trip("::192.168.0.1");
// Ordering: IPv4 sorts less than IPv6
ip_address v4 = ipv4_address{127, 0, 0, 1};
ip_address v6 = ipv6_address{0, 0, 0, 0, 0, 0, 0, 1}; // ::1
CHECK(v4 < v6);
// Ordering within same family
CHECK(ipv4_address{1, 1, 1, 1} < ipv4_address{1, 1, 1, 2});
CHECK(ipv6_address{0, 0, 0, 0, 0, 0, 0, 1} < ipv6_address{0, 0, 0, 0, 0, 0, 0, 2});
// Reject trailing/leading whitespace
CHECK(ip_address::from_string("192.168.0.1 ") == std::nullopt);
CHECK(ip_address::from_string("\t::1") == std::nullopt);
// Reject malformed IPv6
CHECK(ip_address::from_string("::::") == std::nullopt);
CHECK(ip_address::from_string("gggg::1") == std::nullopt);
CHECK(ip_address::from_string("12345::1") == std::nullopt);
}
TEST_SUITE_END();