-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
src/protocols/secure_channel/tests/TestSessionIDAllocator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* | ||
* 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 <protocols/secure_channel/SessionIDAllocator.h> | ||
#include <support/CHIPMem.h> | ||
#include <support/UnitTestRegistration.h> | ||
|
||
#include <nlunit-test.h> | ||
|
||
using namespace chip; | ||
|
||
void TestStatusReport_Allocate(nlTestSuite * inSuite, void * inContext) | ||
{ | ||
SessionIDAllocator allocator; | ||
|
||
NL_TEST_ASSERT(inSuite, allocator.Peek() == 0); | ||
|
||
uint16_t id; | ||
|
||
for (uint16_t i = 0; i < 16; i++) | ||
{ | ||
CHIP_ERROR err = allocator.Allocate(id); | ||
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); | ||
NL_TEST_ASSERT(inSuite, id == i); | ||
NL_TEST_ASSERT(inSuite, allocator.Peek() == i + 1); | ||
} | ||
} | ||
|
||
void TestStatusReport_Free(nlTestSuite * inSuite, void * inContext) | ||
{ | ||
SessionIDAllocator allocator; | ||
|
||
NL_TEST_ASSERT(inSuite, allocator.Peek() == 0); | ||
|
||
uint16_t id; | ||
|
||
for (uint16_t i = 0; i < 16; i++) | ||
{ | ||
CHIP_ERROR err = allocator.Allocate(id); | ||
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); | ||
NL_TEST_ASSERT(inSuite, id == i); | ||
NL_TEST_ASSERT(inSuite, allocator.Peek() == i + 1); | ||
} | ||
|
||
// Free an intermediate ID | ||
allocator.Free(10); | ||
NL_TEST_ASSERT(inSuite, allocator.Peek() == 16); | ||
|
||
// Free the last allocated ID | ||
allocator.Free(15); | ||
NL_TEST_ASSERT(inSuite, allocator.Peek() == 15); | ||
|
||
// Free some random unallocated ID | ||
allocator.Free(100); | ||
NL_TEST_ASSERT(inSuite, allocator.Peek() == 15); | ||
} | ||
|
||
void TestStatusReport_Reserve(nlTestSuite * inSuite, void * inContext) | ||
{ | ||
SessionIDAllocator allocator; | ||
|
||
uint16_t id; | ||
|
||
for (uint16_t i = 0; i < 16; i++) | ||
{ | ||
CHIP_ERROR err = allocator.Allocate(id); | ||
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); | ||
NL_TEST_ASSERT(inSuite, id == i); | ||
NL_TEST_ASSERT(inSuite, allocator.Peek() == i + 1); | ||
} | ||
|
||
allocator.Reserve(100); | ||
NL_TEST_ASSERT(inSuite, allocator.Peek() == 101); | ||
} | ||
|
||
// Test Suite | ||
|
||
/** | ||
* Test Suite that lists all the test functions. | ||
*/ | ||
// clang-format off | ||
static const nlTest sTests[] = | ||
{ | ||
NL_TEST_DEF("SessionIDAllocator_Allocate", TestStatusReport_Allocate), | ||
NL_TEST_DEF("SessionIDAllocator_Free", TestStatusReport_Free), | ||
NL_TEST_DEF("SessionIDAllocator_Reserve", TestStatusReport_Reserve), | ||
|
||
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-SessionIDAllocator", | ||
&sTests[0], | ||
TestSetup, | ||
TestTeardown, | ||
}; | ||
// clang-format on | ||
|
||
/** | ||
* Main | ||
*/ | ||
int TestStatusReport() | ||
{ | ||
// Run test suit against one context | ||
nlTestRunner(&sSuite, nullptr); | ||
|
||
return (nlTestRunnerStats(&sSuite)); | ||
} | ||
|
||
CHIP_REGISTER_TEST_SUITE(TestStatusReport) |