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 BufBound
25 #include "TestSupport.h"
27 #include <support/BufBound.h>
28 #include <support/UnitTestRegistration.h>
30 #include <nlunit-test.h>
34 class BBTest : public BufBound
37 static const size_t kLen = 64;
38 static const uint8_t kGuard = 0xfe;
42 BBTest(size_t len) : BufBound(mBuf + 1, len), mLen(len) { memset(mBuf, kGuard, kLen); }
44 bool expect(const void * val, size_t needed, size_t available)
47 for (size_t i = mLen + 1; i < sizeof(mBuf); i++)
49 if (mBuf[i] != kGuard)
54 if (mBuf[0] != kGuard)
59 size_t written = 0xcafebabe;
60 bool fit = Fit(written);
61 if (written == 0xcafebabe)
63 printf("Fit(written) didn't set written\n");
66 if ((fit && (mLen < needed || written != needed)) || (!fit && (mLen >= needed || written != mLen)))
68 printf("Fit(written) is wrong: mLen == %zu, needed == %zu, written == %zu, Fit() == %s\n", mLen, needed, written,
69 fit ? "true" : "false");
73 // check everything else
74 if (memcmp(mBuf + 1, val, needed < mLen ? needed : mLen) != 0)
79 return Available() == available && Needed() == needed;
83 static void TestBufBound_Str(nlTestSuite * inSuite, void * inContext)
91 NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
98 NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
102 static void TestBufBound_Buf(nlTestSuite * inSuite, void * inContext)
109 NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
116 NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
120 static void TestBufBound_Put(nlTestSuite * inSuite, void * inContext)
126 bb.Put16('h' + 'i' * 256);
128 NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
133 bb.Put32(0x01020304);
135 NL_TEST_ASSERT(inSuite, bb.expect("\x04\x03\x02\x01", 4, 0));
141 bb.Put64(0x0102030405060708);
143 NL_TEST_ASSERT(inSuite, bb.expect("\x08\x07\x06\x05\x04\x03\x02\x01", 8, 0));
149 bb.Put(0x0102030405060708u, 3);
151 NL_TEST_ASSERT(inSuite, bb.expect("\x08\x07\x06", 3, 0));
155 static void TestBufBound_PutBE(nlTestSuite * inSuite, void * inContext)
161 bb.PutBE16('i' + 'h' * 256);
163 NL_TEST_ASSERT(inSuite, bb.expect("hi", 2, 0));
169 bb.PutBE32(0x01020304);
171 NL_TEST_ASSERT(inSuite, bb.expect("\x01\x02\x03\x04", 4, 0));
177 bb.PutBE64(0x0102030405060708);
179 NL_TEST_ASSERT(inSuite, bb.expect("\x01\x02\x03\x04\x05\x06\x07\x08", 8, 0));
185 bb.PutBE(0x0102030405060708u, 3);
187 NL_TEST_ASSERT(inSuite, bb.expect("\x06\x07\x08", 3, 0));
191 #define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn)
193 * Test Suite. It lists all the test functions.
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() };
198 int TestBufBound(void)
200 nlTestSuite theSuite = { "CHIP BufBound tests", &sTests[0], nullptr, nullptr };
202 // Run test suit againt one context.
203 nlTestRunner(&theSuite, nullptr);
204 return nlTestRunnerStats(&theSuite);
207 CHIP_REGISTER_TEST_SUITE(TestBufBound)