Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / tests / TestBufferReader.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  *    @file
21  *      This file implements a unit test suite for CHIP BufferReader
22  *
23  */
24
25 #include <support/BufferReader.h>
26 #include <support/UnitTestRegistration.h>
27 #include <type_traits>
28
29 #include <nlunit-test.h>
30
31 using namespace chip;
32 using namespace chip::Encoding::LittleEndian;
33
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 };
35
36 struct TestReader : public Reader
37 {
38     TestReader() : Reader(test_buffer, std::extent<decltype(test_buffer)>::value) {}
39 };
40
41 static void TestBufferReader_Basic(nlTestSuite * inSuite, void * inContext)
42 {
43     TestReader reader;
44     uint8_t first;
45     uint16_t second;
46     uint32_t third;
47     uint64_t fourth;
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));
59
60     uint32_t fourMore;
61     err = reader.Read32(&fourMore).StatusCode();
62     NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
63 }
64
65 static void TestBufferReader_Saturation(nlTestSuite * inSuite, void * inContext)
66 {
67     TestReader reader;
68     uint64_t temp;
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);
74
75     NL_TEST_ASSERT(inSuite, reader.HasAtLeast(2));
76     NL_TEST_ASSERT(inSuite, !reader.HasAtLeast(3));
77     uint32_t tooBig;
78     err = reader.Read32(&tooBig).StatusCode();
79     NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
80     NL_TEST_ASSERT(inSuite, !reader.HasAtLeast(1));
81
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.
84     uint8_t small;
85     err = reader.Read8(&small).StatusCode();
86     NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
87 }
88
89 static void TestBufferReader_Skip(nlTestSuite * inSuite, void * inContext)
90 {
91     TestReader reader;
92     uint8_t temp          = 0;
93     uint16_t firstSkipLen = 2;
94
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);
100
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);
106
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);
110 }
111
112 #define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn)
113 /**
114  *   Test Suite. It lists all the test functions.
115  */
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() };
118
119 int TestBufferReader(void)
120 {
121     nlTestSuite theSuite = { "CHIP BufferReader tests", &sTests[0], nullptr, nullptr };
122
123     // Run test suit againt one context.
124     nlTestRunner(&theSuite, nullptr);
125     return nlTestRunnerStats(&theSuite);
126 }
127
128 CHIP_REGISTER_TEST_SUITE(TestBufferReader)