From fd97f23cefab108d57eb60503e1ab3f7c1ff9529 Mon Sep 17 00:00:00 2001 From: Lenny Maiorani Date: Mon, 16 Nov 2020 11:33:31 -0700 Subject: [PATCH] MACAddress: Unit testing for basic functionality Problem: - There are no unit tests for `MACAddress` class. This makes it difficult to refactor and ensure the same behavior. - `m_data` private member variable is uninitialized leading to undefined behavior of `is_zero()`. Solution: - Add unit tests to cover basic functionality. - Initialize `m_data`. --- AK/MACAddress.h | 2 +- AK/Tests/TestMACAddress.cpp | 79 +++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 AK/Tests/TestMACAddress.cpp diff --git a/AK/MACAddress.h b/AK/MACAddress.h index dd0977ed8e..b2c59bc81f 100644 --- a/AK/MACAddress.h +++ b/AK/MACAddress.h @@ -71,7 +71,7 @@ public: } private: - u8 m_data[6]; + u8 m_data[6] {}; }; static_assert(sizeof(MACAddress) == 6); diff --git a/AK/Tests/TestMACAddress.cpp b/AK/Tests/TestMACAddress.cpp new file mode 100644 index 0000000000..fe62dd73c4 --- /dev/null +++ b/AK/Tests/TestMACAddress.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include +#include + +TEST_CASE(should_default_construct) +{ + MACAddress sut {}; + EXPECT(sut.is_zero()); +} + +TEST_CASE(should_braces_construct) +{ + MACAddress sut { 1, 2, 3, 4, 5, 6 }; + EXPECT(!sut.is_zero()); +} + +TEST_CASE(should_construct_from_c_array) +{ + u8 addr[6] = { 1, 2, 3, 4, 5, 6 }; + MACAddress sut(addr); + EXPECT(!sut.is_zero()); +} + +TEST_CASE(should_construct_from_6_octets) +{ + MACAddress sut(1, 2, 3, 4, 5, 6); + EXPECT(!sut.is_zero()); +} + +TEST_CASE(should_provide_access_to_octet_by_index) +{ + MACAddress sut(1, 2, 3, 4, 5, 6); + for (auto i = 0u; i < sizeof(MACAddress); ++i) { + EXPECT_EQ(i + 1, sut[i]); + } +} + +TEST_CASE(should_equality_compare) +{ + MACAddress a(1, 2, 3, 4, 5, 6); + MACAddress b(1, 2, 3, 42, 5, 6); + EXPECT(a == a); + EXPECT(a != b); +} + +TEST_CASE(should_string_format) +{ + MACAddress sut(1, 2, 3, 4, 5, 6); + EXPECT_EQ("01:02:03:04:05:06", sut.to_string()); +} + +TEST_MAIN(MACAddress)