Skip to content

Commit

Permalink
Replaced nlunit-test with pw_unit_test in src/lib/core/ (project-chip…
Browse files Browse the repository at this point in the history
…#33062)

* Replaced nlunit-test with pw_unit_test in src/lib/core/

* Restyled by whitespace

* Restyled by clang-format

* Update openiotsdk test components

* Use ASSERT_EQ instead of VerifyOrDie

* Removed redundant nullptr checks

* Use EXPECT_STREQ

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
mbknust and restyled-commits authored Apr 22, 2024
1 parent a860932 commit 479d243
Show file tree
Hide file tree
Showing 11 changed files with 1,377 additions and 1,778 deletions.
5 changes: 1 addition & 4 deletions src/lib/core/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("//build_overrides/nlunit_test.gni")

import("${chip_root}/build/chip/chip_test_suite.gni")
import("${chip_root}/build/chip/fuzz_test.gni")

chip_test_suite_using_nltest("tests") {
chip_test_suite("tests") {
output_name = "libCoreTests"

test_sources = [
Expand All @@ -39,9 +38,7 @@ chip_test_suite_using_nltest("tests") {
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/core:vectortlv",
"${chip_root}/src/lib/support:test_utils",
"${chip_root}/src/lib/support:testing_nlunit",
"${chip_root}/src/platform",
"${nlunit_test_root}:nlunit-test",
]
}

Expand Down
153 changes: 52 additions & 101 deletions src/lib/core/tests/TestCATValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
* limitations under the License.
*/

#include <lib/support/UnitTestRegistration.h>
#include <nlunit-test.h>
#include <gtest/gtest.h>

#include <lib/core/CASEAuthTag.h>

using namespace chip;

void TestEqualityOperator(nlTestSuite * inSuite, void * inContext)
TEST(TestCATValues, TestEqualityOperator)
{
{
auto a = CATValues{ { 0x1111'0001, 0x2222'0002, 0x3333'0003 } };
Expand All @@ -37,7 +36,7 @@ void TestEqualityOperator(nlTestSuite * inSuite, void * inContext)
{
for (auto & inner : candidates)
{
NL_TEST_ASSERT(inSuite, inner == outer);
EXPECT_EQ(inner, outer);
}
}
}
Expand All @@ -49,7 +48,7 @@ void TestEqualityOperator(nlTestSuite * inSuite, void * inContext)
{
for (auto & inner : candidates)
{
NL_TEST_ASSERT(inSuite, inner == outer);
EXPECT_EQ(inner, outer);
}
}
}
Expand All @@ -65,13 +64,13 @@ void TestEqualityOperator(nlTestSuite * inSuite, void * inContext)
{
for (auto & inner : candidates)
{
NL_TEST_ASSERT(inSuite, inner == outer);
EXPECT_EQ(inner, outer);
}
}
}
}

void TestInequalityOperator(nlTestSuite * inSuite, void * inContext)
TEST(TestCATValues, TestInequalityOperator)
{
auto a = CATValues{ { 0x1111'0001 } };
auto b = CATValues{ { 0x1111'0001, 0x2222'0002 } };
Expand Down Expand Up @@ -103,12 +102,12 @@ void TestInequalityOperator(nlTestSuite * inSuite, void * inContext)
{
continue;
}
NL_TEST_ASSERT(inSuite, inner != outer);
EXPECT_NE(inner, outer);
}
}
}

void TestValidity(nlTestSuite * inSuite, void * inContext)
TEST(TestCATValues, TestValidity)
{
{
auto a = CATValues{ { 0x1111'0001, 0x2222'0002, 0x3333'0003 } };
Expand All @@ -119,7 +118,7 @@ void TestValidity(nlTestSuite * inSuite, void * inContext)
CATValues validCandidates[] = { a, b, c, d, e };
for (auto & candidate : validCandidates)
{
NL_TEST_ASSERT(inSuite, candidate.AreValid());
EXPECT_TRUE(candidate.AreValid());
}
}

Expand All @@ -130,117 +129,69 @@ void TestValidity(nlTestSuite * inSuite, void * inContext)
CATValues invalidCandidates[] = { versionZero1, versionZero2, collidingId };
for (auto & candidate : invalidCandidates)
{
NL_TEST_ASSERT(inSuite, !candidate.AreValid());
EXPECT_FALSE(candidate.AreValid());
}
}
}

void TestMembership(nlTestSuite * inSuite, void * inContext)
TEST(TestCATValues, TestMembership)
{
auto a = CATValues{ { 0x1111'0001 } };
auto b = CATValues{ { 0x1111'0001, 0x2222'0002 } };
auto c = CATValues{ { 0x1111'0001, 0x2222'0002, 0x3333'0003 } };

NL_TEST_ASSERT(inSuite, a.Contains(0x1111'0001));
NL_TEST_ASSERT(inSuite, a.GetNumTagsPresent() == 1);
NL_TEST_ASSERT(inSuite, !a.Contains(0x1111'0002));
NL_TEST_ASSERT(inSuite, !a.Contains(0x2222'0002));
NL_TEST_ASSERT(inSuite, a.ContainsIdentifier(0x1111));
NL_TEST_ASSERT(inSuite, !a.ContainsIdentifier(0x2222));
NL_TEST_ASSERT(inSuite, a.AreValid());

NL_TEST_ASSERT(inSuite, b.Contains(0x1111'0001));
NL_TEST_ASSERT(inSuite, b.Contains(0x2222'0002));
NL_TEST_ASSERT(inSuite, b.GetNumTagsPresent() == 2);
NL_TEST_ASSERT(inSuite, b.ContainsIdentifier(0x1111));
NL_TEST_ASSERT(inSuite, b.ContainsIdentifier(0x2222));
NL_TEST_ASSERT(inSuite, b.AreValid());

NL_TEST_ASSERT(inSuite, c.Contains(0x1111'0001));
NL_TEST_ASSERT(inSuite, c.Contains(0x2222'0002));
NL_TEST_ASSERT(inSuite, c.Contains(0x3333'0003));
NL_TEST_ASSERT(inSuite, c.GetNumTagsPresent() == 3);
NL_TEST_ASSERT(inSuite, c.ContainsIdentifier(0x1111));
NL_TEST_ASSERT(inSuite, c.ContainsIdentifier(0x2222));
NL_TEST_ASSERT(inSuite, c.ContainsIdentifier(0x3333));
NL_TEST_ASSERT(inSuite, c.AreValid());
EXPECT_TRUE(a.Contains(0x1111'0001));
EXPECT_EQ(a.GetNumTagsPresent(), 1u);
EXPECT_FALSE(a.Contains(0x1111'0002));
EXPECT_FALSE(a.Contains(0x2222'0002));
EXPECT_TRUE(a.ContainsIdentifier(0x1111));
EXPECT_FALSE(a.ContainsIdentifier(0x2222));
EXPECT_TRUE(a.AreValid());

EXPECT_TRUE(b.Contains(0x1111'0001));
EXPECT_TRUE(b.Contains(0x2222'0002));
EXPECT_EQ(b.GetNumTagsPresent(), 2u);
EXPECT_TRUE(b.ContainsIdentifier(0x1111));
EXPECT_TRUE(b.ContainsIdentifier(0x2222));
EXPECT_TRUE(b.AreValid());

EXPECT_TRUE(c.Contains(0x1111'0001));
EXPECT_TRUE(c.Contains(0x2222'0002));
EXPECT_TRUE(c.Contains(0x3333'0003));
EXPECT_EQ(c.GetNumTagsPresent(), 3u);
EXPECT_TRUE(c.ContainsIdentifier(0x1111));
EXPECT_TRUE(c.ContainsIdentifier(0x2222));
EXPECT_TRUE(c.ContainsIdentifier(0x3333));
EXPECT_TRUE(c.AreValid());
}

void TestSubjectMatching(nlTestSuite * inSuite, void * inContext)
TEST(TestCATValues, TestSubjectMatching)
{
// Check operational node IDs don't match
auto a = CATValues{ { 0x2222'0002 } };
NL_TEST_ASSERT(inSuite, !a.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0x0001'0002'0003'0004ull)));
NL_TEST_ASSERT(inSuite, !a.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0x0001'0002'2222'0002ull)));
EXPECT_FALSE(a.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0x0001'0002'0003'0004ull)));
EXPECT_FALSE(a.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0x0001'0002'2222'0002ull)));

auto b = CATValues{ { 0x1111'0001 } };
NL_TEST_ASSERT(inSuite, b.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'1111'0001ull)));
NL_TEST_ASSERT(inSuite, !b.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'1111'0002ull)));
EXPECT_TRUE(b.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'1111'0001ull)));
EXPECT_FALSE(b.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'1111'0002ull)));

auto c = CATValues{ { 0x1111'0001, 0x2222'0002 } };
NL_TEST_ASSERT(inSuite, c.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'2222'0001ull)));
NL_TEST_ASSERT(inSuite, c.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'2222'0002ull)));
NL_TEST_ASSERT(inSuite, !c.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'2222'0003ull)));
EXPECT_TRUE(c.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'2222'0001ull)));
EXPECT_TRUE(c.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'2222'0002ull)));
EXPECT_FALSE(c.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'2222'0003ull)));

auto d = CATValues{ { 0x1111'0001, 0x2222'0002, 0x3333'0003 } };
NL_TEST_ASSERT(inSuite, d.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0001ull)));
NL_TEST_ASSERT(inSuite, d.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0002ull)));
NL_TEST_ASSERT(inSuite, d.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0003ull)));
NL_TEST_ASSERT(inSuite, !d.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0004ull)));
NL_TEST_ASSERT(inSuite, !d.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'ffffull)));
EXPECT_TRUE(d.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0001ull)));
EXPECT_TRUE(d.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0002ull)));
EXPECT_TRUE(d.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0003ull)));
EXPECT_FALSE(d.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0004ull)));
EXPECT_FALSE(d.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'ffffull)));

auto e = CATValues{ { 0x1111'0001, 0x2222'0002, 0x3333'ffff } };
NL_TEST_ASSERT(inSuite, e.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0001ull)));
NL_TEST_ASSERT(inSuite, e.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0002ull)));
NL_TEST_ASSERT(inSuite, e.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0003ull)));
NL_TEST_ASSERT(inSuite, e.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0004ull)));
NL_TEST_ASSERT(inSuite, e.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'ffffull)));
EXPECT_TRUE(e.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0001ull)));
EXPECT_TRUE(e.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0002ull)));
EXPECT_TRUE(e.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0003ull)));
EXPECT_TRUE(e.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'0004ull)));
EXPECT_TRUE(e.CheckSubjectAgainstCATs(static_cast<chip::NodeId>(0xFFFF'FFFD'3333'ffffull)));
}
// Test Suite

/**
* Test Suite that lists all the test functions.
*/
// clang-format off
static const nlTest sTests[] =
{
NL_TEST_DEF("Equality operator", TestEqualityOperator),
NL_TEST_DEF("Inequality operator", TestInequalityOperator),
NL_TEST_DEF("Validity checks", TestValidity),
NL_TEST_DEF("Set operations", TestMembership),
NL_TEST_DEF("Subject matching for ACL", TestSubjectMatching),
NL_TEST_SENTINEL()
};
// clang-format on

int TestCATValues_Setup(void * inContext)
{
return SUCCESS;
}

/**
* Tear down the test suite.
*/
int TestCATValues_Teardown(void * inContext)
{
return SUCCESS;
}

int TestCATValues()
{
// clang-format off
nlTestSuite theSuite =
{
"CATValues",
&sTests[0],
TestCATValues_Setup,
TestCATValues_Teardown,
};
// clang-format on

nlTestRunner(&theSuite, nullptr);

return (nlTestRunnerStats(&theSuite));
}

CHIP_REGISTER_TEST_SUITE(TestCATValues)
Loading

0 comments on commit 479d243

Please sign in to comment.