2020-11-16 21:33:31 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-16 21:33:31 +03:00
|
|
|
*/
|
|
|
|
|
2021-04-25 08:53:23 +03:00
|
|
|
#include <LibTest/TestCase.h>
|
2020-11-16 21:33:31 +03:00
|
|
|
|
|
|
|
#include <AK/MACAddress.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
TEST_CASE(should_default_construct)
|
|
|
|
{
|
2020-11-17 03:10:02 +03:00
|
|
|
constexpr MACAddress sut {};
|
|
|
|
static_assert(sut.is_zero());
|
2020-11-16 21:33:31 +03:00
|
|
|
EXPECT(sut.is_zero());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_braces_construct)
|
|
|
|
{
|
2020-11-17 03:10:02 +03:00
|
|
|
constexpr MACAddress sut { 1, 2, 3, 4, 5, 6 };
|
|
|
|
static_assert(!sut.is_zero());
|
2020-11-16 21:33:31 +03:00
|
|
|
EXPECT(!sut.is_zero());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_construct_from_6_octets)
|
|
|
|
{
|
2020-11-17 03:10:02 +03:00
|
|
|
constexpr MACAddress sut(1, 2, 3, 4, 5, 6);
|
|
|
|
static_assert(!sut.is_zero());
|
2020-11-16 21:33:31 +03:00
|
|
|
EXPECT(!sut.is_zero());
|
|
|
|
}
|
|
|
|
|
2020-11-17 03:41:46 +03:00
|
|
|
TEST_CASE(should_provide_read_access_to_octet_by_index)
|
2020-11-16 21:33:31 +03:00
|
|
|
{
|
2020-11-17 03:10:02 +03:00
|
|
|
constexpr auto is_all_expected = [](auto& sut) {
|
|
|
|
for (auto i = 0u; i < sizeof(MACAddress); ++i) {
|
|
|
|
if (sut[i] != i + 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr MACAddress sut(1, 2, 3, 4, 5, 6);
|
|
|
|
|
|
|
|
static_assert(is_all_expected(sut));
|
|
|
|
|
2020-11-16 21:33:31 +03:00
|
|
|
for (auto i = 0u; i < sizeof(MACAddress); ++i) {
|
|
|
|
EXPECT_EQ(i + 1, sut[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 03:41:46 +03:00
|
|
|
TEST_CASE(should_provide_write_access_to_octet_by_index)
|
|
|
|
{
|
|
|
|
constexpr auto sut = [] {
|
|
|
|
MACAddress m {};
|
|
|
|
for (auto i = 0u; i < sizeof(MACAddress); ++i) {
|
|
|
|
m[i] = i + 1;
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}();
|
|
|
|
|
|
|
|
constexpr MACAddress expected(1, 2, 3, 4, 5, 6);
|
|
|
|
|
|
|
|
static_assert(expected == sut);
|
|
|
|
}
|
|
|
|
|
2020-11-16 21:33:31 +03:00
|
|
|
TEST_CASE(should_equality_compare)
|
|
|
|
{
|
2020-11-17 03:10:02 +03:00
|
|
|
constexpr MACAddress a(1, 2, 3, 4, 5, 6);
|
|
|
|
constexpr MACAddress b(1, 2, 3, 42, 5, 6);
|
|
|
|
|
|
|
|
static_assert(a == a);
|
|
|
|
static_assert(a != b);
|
|
|
|
|
2020-11-16 21:33:31 +03:00
|
|
|
EXPECT(a == a);
|
|
|
|
EXPECT(a != b);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_string_format)
|
|
|
|
{
|
|
|
|
MACAddress sut(1, 2, 3, 4, 5, 6);
|
2022-12-06 04:12:49 +03:00
|
|
|
EXPECT_EQ("01:02:03:04:05:06", sut.to_deprecated_string());
|
2020-11-16 21:33:31 +03:00
|
|
|
}
|
2021-07-25 03:15:14 +03:00
|
|
|
|
|
|
|
TEST_CASE(should_make_mac_address_from_string_numbers)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
auto const sut = MACAddress::from_string("01:02:03:04:05:06"sv);
|
2021-07-25 03:15:14 +03:00
|
|
|
|
|
|
|
EXPECT(sut.has_value());
|
|
|
|
EXPECT_EQ(1, sut.value()[0]);
|
|
|
|
EXPECT_EQ(2, sut.value()[1]);
|
|
|
|
EXPECT_EQ(3, sut.value()[2]);
|
|
|
|
EXPECT_EQ(4, sut.value()[3]);
|
|
|
|
EXPECT_EQ(5, sut.value()[4]);
|
|
|
|
EXPECT_EQ(6, sut.value()[5]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_make_mac_address_from_string_letters)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
auto const sut = MACAddress::from_string("de:ad:be:ee:ee:ef"sv);
|
2021-07-25 03:15:14 +03:00
|
|
|
|
|
|
|
EXPECT(sut.has_value());
|
|
|
|
EXPECT_EQ(u8 { 0xDE }, sut.value()[0]);
|
|
|
|
EXPECT_EQ(u8 { 0xAD }, sut.value()[1]);
|
|
|
|
EXPECT_EQ(u8 { 0xBE }, sut.value()[2]);
|
|
|
|
EXPECT_EQ(u8 { 0xEE }, sut.value()[3]);
|
|
|
|
EXPECT_EQ(u8 { 0xEE }, sut.value()[4]);
|
|
|
|
EXPECT_EQ(u8 { 0xEF }, sut.value()[5]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_make_empty_optional_from_bad_string)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
auto const sut = MACAddress::from_string("bad string"sv);
|
2021-07-25 03:15:14 +03:00
|
|
|
|
|
|
|
EXPECT(!sut.has_value());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_make_empty_optional_from_out_of_range_values)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
auto const sut = MACAddress::from_string("de:ad:be:ee:ee:fz"sv);
|
2021-07-25 03:15:14 +03:00
|
|
|
|
|
|
|
EXPECT(!sut.has_value());
|
|
|
|
}
|