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

Add tests for CHIPMemString. #9619

Merged
merged 3 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/lib/support/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ chip_test_suite("tests") {
"TestCHIPArgParser.cpp",
"TestCHIPCounter.cpp",
"TestCHIPMem.cpp",
"TestCHIPMemString.cpp",
"TestDefer.cpp",
"TestErrorStr.cpp",
"TestFixedBufferAllocator.cpp",
Expand Down
166 changes: 166 additions & 0 deletions src/lib/support/tests/TestCHIPMemString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <lib/support/CHIPMemString.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/ScopedBuffer.h>
#include <lib/support/Span.h>
#include <lib/support/UnitTestRegistration.h>
#include <nlunit-test.h>

using namespace chip;
using namespace chip::Platform;

// =================================
// Unit tests
// =================================

namespace {
template <size_t kTestBufLen>
struct TestBuffers
{
char correctSizeBuf[kTestBufLen];
char tooSmallBuf[kTestBufLen - 1];
char wayTooSmallBuf[1];
char tooBigBuf[kTestBufLen + 10];
void Reset()
{
memset(correctSizeBuf, 1, sizeof(correctSizeBuf));
memset(tooSmallBuf, 1, sizeof(tooSmallBuf));
memset(wayTooSmallBuf, 1, sizeof(wayTooSmallBuf));
memset(tooBigBuf, 1, sizeof(tooBigBuf));
}
void CheckCorrectness(nlTestSuite * inSuite, const char * testStr)
{
// correctSizeBuf and tooBigBuf should have the complete string.
NL_TEST_ASSERT(inSuite, correctSizeBuf[kTestBufLen - 1] == '\0');
NL_TEST_ASSERT(inSuite, tooBigBuf[kTestBufLen - 1] == '\0');
NL_TEST_ASSERT(inSuite, strcmp(correctSizeBuf, testStr) == 0);
NL_TEST_ASSERT(inSuite, strcmp(tooBigBuf, testStr) == 0);
NL_TEST_ASSERT(inSuite, strlen(correctSizeBuf) == strlen(testStr));
NL_TEST_ASSERT(inSuite, strlen(tooBigBuf) == strlen(testStr));

// wayTooSmallBuf is tiny and thus will only have the null terminator
NL_TEST_ASSERT(inSuite, wayTooSmallBuf[0] == '\0');

// tooSmallBuf should still have a null terminator on the end
NL_TEST_ASSERT(inSuite, tooSmallBuf[kTestBufLen - 2] == '\0');
NL_TEST_ASSERT(inSuite, memcmp(tooSmallBuf, testStr, kTestBufLen - 2) == 0);
}
};

} // namespace

static void TestCopyString(nlTestSuite * inSuite, void * inContext)
{
constexpr char testWord[] = "testytest";
ByteSpan testWordSpan = ByteSpan(reinterpret_cast<const uint8_t *>(testWord), sizeof(testWord) - 1);
TestBuffers<sizeof(testWord)> testBuffers;
andy31415 marked this conversation as resolved.
Show resolved Hide resolved

// CopyString with explicit size
testBuffers.Reset();
CopyString(testBuffers.correctSizeBuf, sizeof(testBuffers.correctSizeBuf), testWord);
CopyString(testBuffers.tooSmallBuf, sizeof(testBuffers.tooSmallBuf), testWord);
CopyString(testBuffers.wayTooSmallBuf, sizeof(testBuffers.wayTooSmallBuf), testWord);
CopyString(testBuffers.tooBigBuf, sizeof(testBuffers.tooBigBuf), testWord);
testBuffers.CheckCorrectness(inSuite, testWord);

// CopyString with array size
testBuffers.Reset();
CopyString(testBuffers.correctSizeBuf, testWord);
CopyString(testBuffers.tooSmallBuf, testWord);
CopyString(testBuffers.wayTooSmallBuf, testWord);
CopyString(testBuffers.tooBigBuf, testWord);
testBuffers.CheckCorrectness(inSuite, testWord);

// CopyString with explicit size from ByteSpan
testBuffers.Reset();
CopyString(testBuffers.correctSizeBuf, sizeof(testBuffers.correctSizeBuf), testWordSpan);
CopyString(testBuffers.tooSmallBuf, sizeof(testBuffers.tooSmallBuf), testWordSpan);
CopyString(testBuffers.wayTooSmallBuf, sizeof(testBuffers.wayTooSmallBuf), testWordSpan);
CopyString(testBuffers.tooBigBuf, sizeof(testBuffers.tooBigBuf), testWordSpan);
testBuffers.CheckCorrectness(inSuite, testWord);

// CopyString with array size from ByteSpan
testBuffers.Reset();
CopyString(testBuffers.correctSizeBuf, testWordSpan);
CopyString(testBuffers.tooSmallBuf, testWordSpan);
CopyString(testBuffers.wayTooSmallBuf, testWordSpan);
CopyString(testBuffers.tooBigBuf, testWordSpan);
testBuffers.CheckCorrectness(inSuite, testWord);
}

static void TestMemoryAllocString(nlTestSuite * inSuite, void * inContext)
{
constexpr char testStr[] = "testytestString";
char * allocatedStr = MemoryAllocString(testStr, sizeof(testStr));
NL_TEST_ASSERT(inSuite, allocatedStr != nullptr);
if (allocatedStr == nullptr)
{
return;
}
NL_TEST_ASSERT(inSuite, strcmp(testStr, allocatedStr) == 0);
MemoryFree(allocatedStr);
}

static void TestScopedBuffer(nlTestSuite * inSuite, void * inContext)
{
// Scoped buffer has its own tests that check the memory properly. Here we are just testing that the string is copied in
// properly.
constexpr char testStr[] = "testytestString";
ScopedMemoryString scopedString = ScopedMemoryString(testStr, sizeof(testStr));
NL_TEST_ASSERT(inSuite, strcmp(scopedString.Get(), testStr) == 0);
}

static const nlTest sTests[] = { NL_TEST_DEF("Test CopyString", TestCopyString),
NL_TEST_DEF("Test MemoryAllocString", TestMemoryAllocString),
NL_TEST_DEF("Test ScopedBuffer", TestScopedBuffer), NL_TEST_SENTINEL() };

int TestMemString_Setup(void * inContext)
{
CHIP_ERROR error = MemoryInit();
if (error != CHIP_NO_ERROR)
return (FAILURE);
return (SUCCESS);
}

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

int TestMemString()
{
nlTestSuite theSuite = { "CHIP Memory Allocation tests", &sTests[0], TestMemString_Setup, TestMemString_Teardown };

// Run test suit againt one context.
nlTestRunner(&theSuite, nullptr);
return nlTestRunnerStats(&theSuite);
}

CHIP_REGISTER_TEST_SUITE(TestMemString)