Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated unit tests in src/protocols/bdx/tests/ to use PW instead of NL. #33084

Merged
merged 11 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
114 changes: 31 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,40 @@ 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);
}

arkq marked this conversation as resolved.
Show resolved Hide resolved
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
};

TEST_F(TestBdxMessages, TestTransferInitMessage)
{
TransferInit testMsg;

Expand All @@ -60,10 +70,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 +85,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 +105,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 +126,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);
}

// 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;
TestHelperWrittenAndParsedMatch<BlockQueryWithSkip>(testMsg);
}

// 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
Loading