libcruft-util/test/ip.cpp

59 lines
1.4 KiB
C++
Raw Normal View History

#include <cruft/util/ip.hpp>
#include <cruft/util/debug.hpp>
#include <cruft/util/platform.hpp>
#include <cruft/util/tap.hpp>
#include <cruft/util/types.hpp>
#include <cstdlib>
2016-01-19 18:31:49 +11:00
//-----------------------------------------------------------------------------
static void
test_good (cruft::TAP::logger &tap)
2016-01-19 18:31:49 +11:00
{
static const struct {
const char *str;
ipv4::ip ip;
const char *msg;
} TESTS[] = {
{ "0.0.0.0", { 0, 0, 0, 0 }, "null" },
{ "255.255.255.255", { 255, 255, 255, 255 }, "full" },
{ "127.0.0.1", { 127, 0, 0, 1 }, "localhost" }
};
for (const auto &i: TESTS)
2021-04-14 10:23:33 +10:00
tap.expect_eq (ipv4::ip::parse (i.str), i.ip, "{:s}", i.msg);
2016-01-19 18:31:49 +11:00
}
//-----------------------------------------------------------------------------
static void
test_bad (cruft::TAP::logger &tap)
2016-01-19 18:31:49 +11:00
{
static const struct {
const char *str;
const char *msg;
} TESTS[] = {
{ "::1", "ipv6" },
{ "foo", "alpha" },
{ "", "empty" },
{ "256.0.0.1", "overflow" }
};
for (const auto &i: TESTS)
2021-04-14 10:23:33 +10:00
tap.expect_throw<ipv4::error> ([&] { ipv4::ip::parse (i.str); }, "{:s}", i.msg);
2016-01-19 18:31:49 +11:00
}
2016-01-19 18:31:49 +11:00
//-----------------------------------------------------------------------------
int
main (int, char **) {
cruft::TAP::logger tap;
2016-01-19 18:31:49 +11:00
test_good (tap);
test_bad (tap);
return tap.status ();
}