3889fd5c23423b18a9cfd2e6181a4bb3664c8003
[platform/upstream/connectedhomeip.git] / src / lib / support / tests / TestBufBound.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 BufBound
22  *
23  */
24
25 #include "TestSupport.h"
26
27 #include <support/BufBound.h>
28 #include <support/UnitTestRegistration.h>
29
30 #include <nlunit-test.h>
31
32 using namespace chip;
33
34 class BBTest : public BufBound
35 {
36 public:
37     static const size_t kLen    = 64;
38     static const uint8_t kGuard = 0xfe;
39     uint8_t mBuf[kLen];
40     size_t mLen;
41
42     BBTest(size_t len) : BufBound(mBuf + 1, len), mLen(len) { memset(mBuf, kGuard, kLen); }
43
44     bool expect(const void * val, size_t needed, size_t available)
45     {
46         // check guards
47         for (size_t i = mLen + 1; i < sizeof(mBuf); i++)
48         {
49             if (mBuf[i] != kGuard)
50             {
51                 return false;
52             }
53         }
54         if (mBuf[0] != kGuard)
55         {
56             return false;
57         }
58
59         size_t written = 0xcafebabe;
60         bool fit       = Fit(written);
61         if (written == 0xcafebabe)
62         {
63             printf("Fit(written) didn't set written\n");
64             return false;
65         }
66         if ((fit && (mLen < needed || written != needed)) || (!fit && (mLen >= needed || written != mLen)))
67         {
68             printf("Fit(written) is wrong: mLen == %zu, needed == %zu, written == %zu, Fit() == %s\n", mLen, needed, written,
69                    fit ? "true" : "false");
70             return false;
71         }
72
73         // check everything else
74         if (memcmp(mBuf + 1, val, needed < mLen ? needed : mLen) != 0)
75         {
76             return false;
77         }
78
79         return Available() == available && Needed() == needed;
80     }
81 };
82
83 static void TestBufBound_Str(nlTestSuite * inSuite, void * inContext)
84 {
85     {
86         (void) inContext;
87         BBTest bb(2);
88
89         bb.Put("hi");
90
91         NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
92     }
93     {
94         (void) inContext;
95         BBTest bb(1);
96         bb.Put("hi");
97
98         NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
99     }
100 }
101
102 static void TestBufBound_Buf(nlTestSuite * inSuite, void * inContext)
103 {
104     {
105         (void) inContext;
106         BBTest bb(2);
107         bb.Put("hi", 2);
108
109         NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
110     }
111     {
112         (void) inContext;
113         BBTest bb(1);
114         bb.Put("hi", 2);
115
116         NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
117     }
118 }
119
120 static void TestBufBound_Put(nlTestSuite * inSuite, void * inContext)
121 {
122     (void) inContext;
123     {
124         BBTest bb(2);
125
126         bb.Put16('h' + 'i' * 256);
127
128         NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
129     }
130     {
131         BBTest bb(4);
132
133         bb.Put32(0x01020304);
134
135         NL_TEST_ASSERT(inSuite, bb.expect("\x04\x03\x02\x01", 4, 0));
136     }
137
138     {
139         BBTest bb(8);
140
141         bb.Put64(0x0102030405060708);
142
143         NL_TEST_ASSERT(inSuite, bb.expect("\x08\x07\x06\x05\x04\x03\x02\x01", 8, 0));
144     }
145
146     {
147         BBTest bb(3);
148
149         bb.Put(0x0102030405060708u, 3);
150
151         NL_TEST_ASSERT(inSuite, bb.expect("\x08\x07\x06", 3, 0));
152     }
153 }
154
155 static void TestBufBound_PutBE(nlTestSuite * inSuite, void * inContext)
156 {
157     (void) inContext;
158     {
159         BBTest bb(2);
160
161         bb.PutBE16('i' + 'h' * 256);
162
163         NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
164     }
165
166     {
167         BBTest bb(4);
168
169         bb.PutBE32(0x01020304);
170
171         NL_TEST_ASSERT(inSuite, bb.expect("\x01\x02\x03\x04", 4, 0));
172     }
173
174     {
175         BBTest bb(8);
176
177         bb.PutBE64(0x0102030405060708);
178
179         NL_TEST_ASSERT(inSuite, bb.expect("\x01\x02\x03\x04\x05\x06\x07\x08", 8, 0));
180     }
181
182     {
183         BBTest bb(3);
184
185         bb.PutBE(0x0102030405060708u, 3);
186
187         NL_TEST_ASSERT(inSuite, bb.expect("\x06\x07\x08", 3, 0));
188     }
189 }
190
191 #define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn)
192 /**
193  *   Test Suite. It lists all the test functions.
194  */
195 static const nlTest sTests[] = { NL_TEST_DEF_FN(TestBufBound_Str), NL_TEST_DEF_FN(TestBufBound_Buf),
196                                  NL_TEST_DEF_FN(TestBufBound_Put), NL_TEST_DEF_FN(TestBufBound_PutBE), NL_TEST_SENTINEL() };
197
198 int TestBufBound(void)
199 {
200     nlTestSuite theSuite = { "CHIP BufBound tests", &sTests[0], nullptr, nullptr };
201
202     // Run test suit againt one context.
203     nlTestRunner(&theSuite, nullptr);
204     return nlTestRunnerStats(&theSuite);
205 }
206
207 CHIP_REGISTER_TEST_SUITE(TestBufBound)