3 * Copyright (c) 2020 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file implements a unit test suite for CHIP BufferReader
25 #include <support/BufferReader.h>
26 #include <support/UnitTestRegistration.h>
27 #include <type_traits>
29 #include <nlunit-test.h>
32 using namespace chip::Encoding::LittleEndian;
34 static const uint8_t test_buffer[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
36 struct TestReader : public Reader
38 TestReader() : Reader(test_buffer, std::extent<decltype(test_buffer)>::value) {}
41 static void TestBufferReader_Basic(nlTestSuite * inSuite, void * inContext)
48 CHIP_ERROR err = reader.Read8(&first).Read16(&second).Read32(&third).Read64(&fourth).StatusCode();
49 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
50 NL_TEST_ASSERT(inSuite, first == 0x01);
51 NL_TEST_ASSERT(inSuite, second == 0x0302);
52 NL_TEST_ASSERT(inSuite, third == 0x07060504);
53 NL_TEST_ASSERT(inSuite, fourth == 0x0f0e0d0c0b0a0908);
54 NL_TEST_ASSERT(inSuite, reader.OctetsRead() == 15);
55 NL_TEST_ASSERT(inSuite, reader.Remaining() == 3);
56 NL_TEST_ASSERT(inSuite, reader.HasAtLeast(2));
57 NL_TEST_ASSERT(inSuite, reader.HasAtLeast(3));
58 NL_TEST_ASSERT(inSuite, !reader.HasAtLeast(4));
61 err = reader.Read32(&fourMore).StatusCode();
62 NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
65 static void TestBufferReader_Saturation(nlTestSuite * inSuite, void * inContext)
69 // Read some bytes out so we can get to the end of the buffer.
70 CHIP_ERROR err = reader.Read64(&temp).StatusCode();
71 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
72 err = reader.Read64(&temp).StatusCode();
73 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
75 NL_TEST_ASSERT(inSuite, reader.HasAtLeast(2));
76 NL_TEST_ASSERT(inSuite, !reader.HasAtLeast(3));
78 err = reader.Read32(&tooBig).StatusCode();
79 NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
80 NL_TEST_ASSERT(inSuite, !reader.HasAtLeast(1));
82 // Check that even though we only really read out 16 bytes, we can't read
83 // out one more bytes, because our previous read failed.
85 err = reader.Read8(&small).StatusCode();
86 NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
89 static void TestBufferReader_Skip(nlTestSuite * inSuite, void * inContext)
93 uint16_t firstSkipLen = 2;
95 // Verify Skip() advances the start pointer the correct amount.
96 CHIP_ERROR err = reader.Skip(firstSkipLen).Read8(&temp).StatusCode();
97 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
98 NL_TEST_ASSERT(inSuite, temp == test_buffer[firstSkipLen]);
99 NL_TEST_ASSERT(inSuite, reader.OctetsRead() == firstSkipLen + 1);
101 // Verify Skip() called with a length larger than available buffer space jumps to the end.
102 err = reader.Skip(sizeof(test_buffer)).StatusCode();
103 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
104 NL_TEST_ASSERT(inSuite, reader.OctetsRead() == sizeof(test_buffer));
105 NL_TEST_ASSERT(inSuite, reader.Remaining() == 0);
107 // Verify no read allowed after jumping to the end.
108 err = reader.Read8(&temp).StatusCode();
109 NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
112 #define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn)
114 * Test Suite. It lists all the test functions.
116 static const nlTest sTests[] = { NL_TEST_DEF_FN(TestBufferReader_Basic), NL_TEST_DEF_FN(TestBufferReader_Saturation),
117 NL_TEST_DEF_FN(TestBufferReader_Skip), NL_TEST_SENTINEL() };
119 int TestBufferReader(void)
121 nlTestSuite theSuite = { "CHIP BufferReader tests", &sTests[0], nullptr, nullptr };
123 // Run test suit againt one context.
124 nlTestRunner(&theSuite, nullptr);
125 return nlTestRunnerStats(&theSuite);
128 CHIP_REGISTER_TEST_SUITE(TestBufferReader)