Skip to content

Commit

Permalink
Updated unit tests in src/protocols/bdx/tests/ to use PW instead of NL.
Browse files Browse the repository at this point in the history
  • Loading branch information
feasel0 committed Apr 21, 2024
1 parent b4650b9 commit bafa47a
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 331 deletions.
6 changes: 2 additions & 4 deletions src/protocols/bdx/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

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

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

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

test_sources = [
Expand All @@ -30,9 +30,7 @@ chip_test_suite_using_nltest("tests") {
public_deps = [
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support",
"${chip_root}/src/lib/support:testing_nlunit",
"${chip_root}/src/protocols/bdx",
"${nlunit_test_root}:nlunit-test",
]

cflags = [ "-Wconversion" ]
Expand Down
117 changes: 34 additions & 83 deletions src/protocols/bdx/tests/TestBdxMessages.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include <protocols/bdx/BdxMessages.h>

#include <nlunit-test.h>
#include <gtest/gtest.h>

#include <lib/support/BufferWriter.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/UnitTestRegistration.h>

#include <limits>

Expand All @@ -17,29 +16,43 @@ using namespace chip::bdx;
* is identical to the origianl.
*/
template <class MsgType>
void TestHelperWrittenAndParsedMatch(nlTestSuite * inSuite, void * inContext, MsgType & testMsg)
void TestHelperWrittenAndParsedMatch(MsgType & testMsg)
{
CHIP_ERROR err = CHIP_NO_ERROR;

size_t msgSize = testMsg.MessageSize();
Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(msgSize));
NL_TEST_ASSERT(inSuite, !bbuf.IsNull());
EXPECT_FALSE(bbuf.IsNull());

testMsg.WriteToBuffer(bbuf);
NL_TEST_ASSERT(inSuite, bbuf.Fit());
EXPECT_TRUE(bbuf.Fit());

System::PacketBufferHandle msgBuf = bbuf.Finalize();
NL_TEST_ASSERT(inSuite, !msgBuf.IsNull());
EXPECT_FALSE(msgBuf.IsNull());
System::PacketBufferHandle rcvBuf = System::PacketBufferHandle::NewWithData(msgBuf->Start(), msgSize);
NL_TEST_ASSERT(inSuite, !rcvBuf.IsNull());
EXPECT_FALSE(rcvBuf.IsNull());

MsgType testMsgRcvd;
err = testMsgRcvd.Parse(std::move(rcvBuf));
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, testMsgRcvd == testMsg);
EXPECT_EQ(err, CHIP_NO_ERROR);
EXPECT_EQ(testMsgRcvd, testMsg);
}

void TestTransferInitMessage(nlTestSuite * inSuite, void * inContext)
struct TestBdxMessages : public ::testing::Test
{
static void SetUpTestSuite()
{
CHIP_ERROR error = chip::Platform::MemoryInit();
EXPECT_EQ(error, CHIP_NO_ERROR);
}

static void TearDownTestSuite()
{
chip::Platform::MemoryShutdown();
}
};

TEST_F(TestBdxMessages, TestTransferInitMessage)
{
TransferInit testMsg;

Expand All @@ -60,10 +73,10 @@ void TestTransferInitMessage(nlTestSuite * inSuite, void * inContext)
testMsg.MetadataLength = 5;
testMsg.Metadata = reinterpret_cast<uint8_t *>(fakeData);

TestHelperWrittenAndParsedMatch<TransferInit>(inSuite, inContext, testMsg);
TestHelperWrittenAndParsedMatch<TransferInit>(testMsg);
}

void TestSendAcceptMessage(nlTestSuite * inSuite, void * inContext)
TEST_F(TestBdxMessages, TestSendAcceptMessage)
{
SendAccept testMsg;

Expand All @@ -75,10 +88,10 @@ void TestSendAcceptMessage(nlTestSuite * inSuite, void * inContext)
testMsg.MetadataLength = 5;
testMsg.Metadata = reinterpret_cast<uint8_t *>(fakeData);

TestHelperWrittenAndParsedMatch<SendAccept>(inSuite, inContext, testMsg);
TestHelperWrittenAndParsedMatch<SendAccept>(testMsg);
}

void TestReceiveAcceptMessage(nlTestSuite * inSuite, void * inContext)
TEST_F(TestBdxMessages, TestReceiveAcceptMessage)
{
ReceiveAccept testMsg;

Expand All @@ -95,19 +108,19 @@ void TestReceiveAcceptMessage(nlTestSuite * inSuite, void * inContext)
testMsg.MetadataLength = 5;
testMsg.Metadata = reinterpret_cast<uint8_t *>(fakeData);

TestHelperWrittenAndParsedMatch<ReceiveAccept>(inSuite, inContext, testMsg);
TestHelperWrittenAndParsedMatch<ReceiveAccept>(testMsg);
}

void TestCounterMessage(nlTestSuite * inSuite, void * inContext)
TEST_F(TestBdxMessages, TestCounterMessage)
{
CounterMessage testMsg;

testMsg.BlockCounter = 4;

TestHelperWrittenAndParsedMatch<CounterMessage>(inSuite, inContext, testMsg);
TestHelperWrittenAndParsedMatch<CounterMessage>(testMsg);
}

void TestDataBlockMessage(nlTestSuite * inSuite, void * inContext)
TEST_F(TestBdxMessages, TestDataBlockMessage)
{
DataBlock testMsg;

Expand All @@ -116,77 +129,15 @@ void TestDataBlockMessage(nlTestSuite * inSuite, void * inContext)
testMsg.DataLength = 5;
testMsg.Data = reinterpret_cast<uint8_t *>(fakeData);

TestHelperWrittenAndParsedMatch<DataBlock>(inSuite, inContext, testMsg);
TestHelperWrittenAndParsedMatch<DataBlock>(testMsg);
}

void TestBlockQueryWithSkipMessage(nlTestSuite * inSuite, void * inContext)
TEST_F(TestBdxMessages, TestBlockQueryWithSkipMessage)
{
BlockQueryWithSkip testMsg;

testMsg.BlockCounter = 5;
testMsg.BytesToSkip = 16;

TestHelperWrittenAndParsedMatch<BlockQueryWithSkip>(inSuite, inContext, testMsg);
TestHelperWrittenAndParsedMatch<BlockQueryWithSkip>(testMsg);
}

// Test Suite

/**
* Test Suite that lists all the test functions.
*/
// clang-format off
static const nlTest sTests[] =
{
NL_TEST_DEF("TestTransferInitMessage", TestTransferInitMessage),
NL_TEST_DEF("TestSendAcceptMessage", TestSendAcceptMessage),
NL_TEST_DEF("TestReceiveAcceptMessage", TestReceiveAcceptMessage),
NL_TEST_DEF("TestCounterMessage", TestCounterMessage),
NL_TEST_DEF("TestDataBlockMessage", TestDataBlockMessage),
NL_TEST_DEF("TestBlockQueryWithSkipMessage", TestBlockQueryWithSkipMessage),

NL_TEST_SENTINEL()
};
// clang-format on

/**
* Set up the test suite.
*/
static int TestSetup(void * inContext)
{
CHIP_ERROR error = chip::Platform::MemoryInit();
if (error != CHIP_NO_ERROR)
return FAILURE;
return SUCCESS;
}

/**
* Tear down the test suite.
*/
static int TestTeardown(void * inContext)
{
chip::Platform::MemoryShutdown();
return SUCCESS;
}

// clang-format off
static nlTestSuite sSuite =
{
"Test-CHIP-BdxMessages",
&sTests[0],
TestSetup,
TestTeardown,
};
// clang-format on

/**
* Main
*/
int TestBdxMessages()
{
// Run test suit against one context
nlTestRunner(&sSuite, nullptr);

return (nlTestRunnerStats(&sSuite));
}

CHIP_REGISTER_TEST_SUITE(TestBdxMessages)
Loading

0 comments on commit bafa47a

Please sign in to comment.