forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestTLVPacketBufferBackingStore.cpp
323 lines (251 loc) · 11.1 KB
/
TestTLVPacketBufferBackingStore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Copyright (c) 2023 Project CHIP Authors
*
* 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 <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <gtest/gtest.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/ScopedBuffer.h>
#include <lib/support/Span.h>
#include <system/TLVPacketBufferBackingStore.h>
using ::chip::Platform::ScopedMemoryBuffer;
using ::chip::System::PacketBuffer;
using ::chip::System::PacketBufferHandle;
using ::chip::System::PacketBufferTLVReader;
using ::chip::System::PacketBufferTLVWriter;
using namespace ::chip;
class TestTLVPacketBufferBackingStore : public ::testing::Test
{
public:
static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); }
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
void WriteUntilRemainingLessThan(PacketBufferTLVWriter & writer, const uint32_t remainingSize)
{
uint32_t lengthRemaining = writer.GetRemainingFreeLength();
while (lengthRemaining >= remainingSize)
{
EXPECT_EQ(writer.Put(TLV::AnonymousTag(), static_cast<uint8_t>(7)), CHIP_NO_ERROR);
lengthRemaining = writer.GetRemainingFreeLength();
}
}
};
/**
* Test that we can do a basic encode to TLV followed by decode.
*/
TEST_F(TestTLVPacketBufferBackingStore, BasicEncodeDecode)
{
auto buffer = PacketBufferHandle::New(PacketBuffer::kMaxSizeWithoutReserve, 0);
PacketBufferTLVWriter writer;
writer.Init(std::move(buffer));
TLV::TLVType outerContainerType;
CHIP_ERROR error = writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Array, outerContainerType);
EXPECT_EQ(error, CHIP_NO_ERROR);
error = writer.Put(TLV::AnonymousTag(), static_cast<uint8_t>(7));
EXPECT_EQ(error, CHIP_NO_ERROR);
error = writer.Put(TLV::AnonymousTag(), static_cast<uint8_t>(8));
EXPECT_EQ(error, CHIP_NO_ERROR);
error = writer.Put(TLV::AnonymousTag(), static_cast<uint8_t>(9));
EXPECT_EQ(error, CHIP_NO_ERROR);
error = writer.EndContainer(outerContainerType);
EXPECT_EQ(error, CHIP_NO_ERROR);
error = writer.Finalize(&buffer);
EXPECT_EQ(error, CHIP_NO_ERROR);
// Array start/end is 2 bytes. Each entry is also 2 bytes: control +
// value. So 8 bytes total.
EXPECT_FALSE(buffer->HasChainedBuffer());
EXPECT_EQ(buffer->TotalLength(), 8);
EXPECT_EQ(buffer->DataLength(), 8);
PacketBufferTLVReader reader;
reader.Init(std::move(buffer));
error = reader.Next(TLV::kTLVType_Array, TLV::AnonymousTag());
EXPECT_EQ(error, CHIP_NO_ERROR);
error = reader.EnterContainer(outerContainerType);
EXPECT_EQ(error, CHIP_NO_ERROR);
error = reader.Next(TLV::kTLVType_UnsignedInteger, TLV::AnonymousTag());
EXPECT_EQ(error, CHIP_NO_ERROR);
uint8_t value;
error = reader.Get(value);
EXPECT_EQ(error, CHIP_NO_ERROR);
EXPECT_EQ(value, 7);
error = reader.Next(TLV::kTLVType_UnsignedInteger, TLV::AnonymousTag());
EXPECT_EQ(error, CHIP_NO_ERROR);
error = reader.Get(value);
EXPECT_EQ(error, CHIP_NO_ERROR);
EXPECT_EQ(value, 8);
error = reader.Next(TLV::kTLVType_UnsignedInteger, TLV::AnonymousTag());
EXPECT_EQ(error, CHIP_NO_ERROR);
error = reader.Get(value);
EXPECT_EQ(error, CHIP_NO_ERROR);
EXPECT_EQ(value, 9);
error = reader.Next();
EXPECT_EQ(error, CHIP_END_OF_TLV);
error = reader.ExitContainer(outerContainerType);
EXPECT_EQ(error, CHIP_NO_ERROR);
error = reader.Next();
EXPECT_EQ(error, CHIP_END_OF_TLV);
}
/**
* Test that we can do an encode that's going to split across multiple buffers correctly.
*/
TEST_F(TestTLVPacketBufferBackingStore, MultiBufferEncode)
{
// Start with a too-small buffer.
auto buffer = PacketBufferHandle::New(2, 0);
PacketBufferTLVWriter writer;
writer.Init(std::move(buffer), /* useChainedBuffers = */ true);
TLV::TLVType outerContainerType;
CHIP_ERROR error = writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Array, outerContainerType);
EXPECT_EQ(error, CHIP_NO_ERROR);
error = writer.Put(TLV::AnonymousTag(), static_cast<uint8_t>(7));
EXPECT_EQ(error, CHIP_NO_ERROR);
error = writer.Put(TLV::AnonymousTag(), static_cast<uint8_t>(8));
EXPECT_EQ(error, CHIP_NO_ERROR);
// Something to make sure we have 3 buffers.
uint8_t bytes[2000] = { 0 };
error = writer.Put(TLV::AnonymousTag(), ByteSpan(bytes));
EXPECT_EQ(error, CHIP_NO_ERROR);
error = writer.EndContainer(outerContainerType);
EXPECT_EQ(error, CHIP_NO_ERROR);
error = writer.Finalize(&buffer);
EXPECT_EQ(error, CHIP_NO_ERROR);
// Array start/end is 2 bytes. First two entries are 2 bytes each.
// Third entry is 1 control byte, 2 length bytes, 2000 bytes of data,
// for a total of 2009 bytes.
constexpr size_t totalSize = 2009;
EXPECT_TRUE(buffer->HasChainedBuffer());
EXPECT_EQ(buffer->TotalLength(), totalSize);
EXPECT_EQ(buffer->DataLength(), 2);
auto nextBuffer = buffer->Next();
EXPECT_TRUE(nextBuffer->HasChainedBuffer());
EXPECT_EQ(nextBuffer->TotalLength(), totalSize - 2);
EXPECT_EQ(nextBuffer->DataLength(), PacketBuffer::kMaxSizeWithoutReserve);
nextBuffer = nextBuffer->Next();
EXPECT_FALSE(nextBuffer->HasChainedBuffer());
EXPECT_EQ(nextBuffer->TotalLength(), nextBuffer->DataLength());
EXPECT_EQ(nextBuffer->DataLength(), totalSize - 2 - PacketBuffer::kMaxSizeWithoutReserve);
// PacketBufferTLVReader cannot handle non-contiguous buffers, and our
// buffers are too big to stick into a single packet buffer.
ScopedMemoryBuffer<uint8_t> buf;
EXPECT_TRUE(buf.Calloc(totalSize));
size_t offset = 0;
while (!buffer.IsNull())
{
memcpy(buf.Get() + offset, buffer->Start(), buffer->DataLength());
offset += buffer->DataLength();
buffer.Advance();
EXPECT_TRUE(offset < totalSize || (offset == totalSize && buffer.IsNull()));
}
TLV::TLVReader reader;
reader.Init(buf.Get(), totalSize);
error = reader.Next(TLV::kTLVType_Array, TLV::AnonymousTag());
EXPECT_EQ(error, CHIP_NO_ERROR);
error = reader.EnterContainer(outerContainerType);
EXPECT_EQ(error, CHIP_NO_ERROR);
error = reader.Next(TLV::kTLVType_UnsignedInteger, TLV::AnonymousTag());
EXPECT_EQ(error, CHIP_NO_ERROR);
uint8_t value;
error = reader.Get(value);
EXPECT_EQ(error, CHIP_NO_ERROR);
EXPECT_EQ(value, 7);
error = reader.Next(TLV::kTLVType_UnsignedInteger, TLV::AnonymousTag());
EXPECT_EQ(error, CHIP_NO_ERROR);
error = reader.Get(value);
EXPECT_EQ(error, CHIP_NO_ERROR);
EXPECT_EQ(value, 8);
error = reader.Next(TLV::kTLVType_ByteString, TLV::AnonymousTag());
EXPECT_EQ(error, CHIP_NO_ERROR);
ByteSpan byteValue;
error = reader.Get(byteValue);
EXPECT_EQ(error, CHIP_NO_ERROR);
EXPECT_EQ(byteValue.size(), sizeof(bytes));
error = reader.Next();
EXPECT_EQ(error, CHIP_END_OF_TLV);
error = reader.ExitContainer(outerContainerType);
EXPECT_EQ(error, CHIP_NO_ERROR);
error = reader.Next();
EXPECT_EQ(error, CHIP_END_OF_TLV);
}
TEST_F(TestTLVPacketBufferBackingStore, NonChainedBufferCanReserve)
{
// Start with a too-small buffer.
uint32_t smallSize = 5;
uint32_t smallerSizeToReserver = smallSize - 1;
auto buffer = PacketBufferHandle::New(smallSize, /* aReservedSize = */ 0);
PacketBufferTLVWriter writer;
writer.Init(std::move(buffer), /* useChainedBuffers = */ false);
CHIP_ERROR error = writer.ReserveBuffer(smallerSizeToReserver);
EXPECT_EQ(error, CHIP_NO_ERROR);
}
// This test previously was created to show that there was an overflow bug, now this test mainly
// just checks that you cannot reserve this type of TLVBackingStorage buffer.
TEST_F(TestTLVPacketBufferBackingStore, TestWriterReserveUnreserveDoesNotOverflow)
{
// Start with a too-small buffer.
uint32_t smallSize = 100;
uint32_t smallerSizeToReserver = smallSize - 1;
auto buffer = PacketBufferHandle::New(smallSize, 0);
PacketBufferTLVWriter writer;
writer.Init(std::move(buffer), /* useChainedBuffers = */ true);
CHIP_ERROR error = writer.ReserveBuffer(smallerSizeToReserver);
if (error == CHIP_NO_ERROR)
{
uint32_t lengthRemaining = writer.GetRemainingFreeLength();
EXPECT_EQ(lengthRemaining, 1U);
// Lets try to overflow by getting next buffer in the chain,
// unreserving then writing until the end of the current buffer.
error = writer.Put(TLV::AnonymousTag(), static_cast<uint8_t>(7));
EXPECT_EQ(error, CHIP_NO_ERROR);
lengthRemaining = writer.GetRemainingFreeLength();
EXPECT_GT(lengthRemaining, smallerSizeToReserver);
WriteUntilRemainingLessThan(writer, 2);
lengthRemaining = writer.GetRemainingFreeLength();
EXPECT_NE(lengthRemaining, 0U);
EXPECT_LT(lengthRemaining, smallerSizeToReserver);
error = writer.UnreserveBuffer(smallerSizeToReserver);
EXPECT_EQ(error, CHIP_NO_ERROR);
lengthRemaining = writer.GetRemainingFreeLength();
EXPECT_GT(lengthRemaining, smallerSizeToReserver);
// This is where we get overflow.
WriteUntilRemainingLessThan(writer, 2);
// If we get here then the overflow condition we were expecting did not happen. If that is the case,
// either we have fixed reservation for chained buffers, or expected failure didn't hit on this
// platform.
//
// If there is a fix please add reservation for chained buffers, please make sure you account for
// what happens if TLVWriter::WriteData fails to get a new buffer but we are not at max size, do
// you actually have space for what was supposed to be reserved.
FAIL();
}
// We no longer allow non-contigous buffers to be reserved.
EXPECT_EQ(error, CHIP_ERROR_INCORRECT_STATE);
}
TEST_F(TestTLVPacketBufferBackingStore, TestWriterReserve)
{
// Start with a too-small buffer.
uint32_t smallSize = 5;
uint32_t smallerSizeToReserver = smallSize - 1;
auto buffer = PacketBufferHandle::New(smallSize, 0);
PacketBufferTLVWriter writer;
writer.Init(std::move(buffer), /* useChainedBuffers = */ false);
CHIP_ERROR error = writer.ReserveBuffer(smallerSizeToReserver);
EXPECT_EQ(error, CHIP_NO_ERROR);
error = writer.Put(TLV::AnonymousTag(), static_cast<uint8_t>(7));
EXPECT_EQ(error, CHIP_ERROR_NO_MEMORY);
error = writer.UnreserveBuffer(smallerSizeToReserver);
EXPECT_EQ(error, CHIP_NO_ERROR);
error = writer.Put(TLV::AnonymousTag(), static_cast<uint8_t>(7));
EXPECT_EQ(error, CHIP_NO_ERROR);
}