1 /*
2 * Copyright (c) 2022, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <openthread/config.h>
30
31 #include "test_platform.h"
32 #include "test_util.hpp"
33
34 #include "common/frame_builder.hpp"
35
36 namespace ot {
37
TestFrameBuilder(void)38 void TestFrameBuilder(void)
39 {
40 const uint8_t kData1[] = {0x01, 0x02, 0x03, 0x04, 0x05};
41 const uint8_t kData2[] = {0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa};
42
43 static constexpr uint16_t kMaxBufferSize = sizeof(kData1) * 2 + sizeof(kData2);
44
45 Instance * instance;
46 Message * message;
47 uint16_t offset;
48 uint8_t buffer[kMaxBufferSize];
49 uint8_t zeroBuffer[kMaxBufferSize];
50 FrameBuilder frameBuilder;
51
52 printf("TestFrameBuilder\n");
53
54 instance = static_cast<Instance *>(testInitInstance());
55 VerifyOrQuit(instance != nullptr);
56
57 message = instance->Get<MessagePool>().Allocate(Message::kTypeIp6);
58 VerifyOrQuit(message != nullptr);
59
60 SuccessOrQuit(message->Append(kData1));
61 SuccessOrQuit(message->Append(kData2));
62
63 memset(buffer, 0, sizeof(buffer));
64 memset(zeroBuffer, 0, sizeof(zeroBuffer));
65
66 frameBuilder.Init(buffer, sizeof(buffer));
67
68 VerifyOrQuit(frameBuilder.GetBytes() == buffer);
69 VerifyOrQuit(frameBuilder.GetLength() == 0);
70 VerifyOrQuit(frameBuilder.GetMaxLength() == sizeof(buffer));
71 VerifyOrQuit(memcmp(buffer, zeroBuffer, sizeof(buffer)) == 0);
72 VerifyOrQuit(frameBuilder.CanAppend(sizeof(buffer)));
73 VerifyOrQuit(!frameBuilder.CanAppend(sizeof(buffer) + 1));
74
75 SuccessOrQuit(frameBuilder.Append(kData1));
76 VerifyOrQuit(frameBuilder.GetLength() == sizeof(kData1));
77 VerifyOrQuit(frameBuilder.GetBytes() == buffer);
78 VerifyOrQuit(memcmp(buffer, kData1, sizeof(kData1)) == 0);
79 VerifyOrQuit(memcmp(buffer + sizeof(kData1), zeroBuffer, sizeof(buffer) - sizeof(kData1)) == 0);
80
81 SuccessOrQuit(frameBuilder.AppendUint8(0x01));
82 SuccessOrQuit(frameBuilder.AppendBigEndianUint16(0x0203));
83 SuccessOrQuit(frameBuilder.AppendLittleEndianUint16(0x0504));
84 VerifyOrQuit(frameBuilder.GetLength() == sizeof(kData1) * 2);
85 VerifyOrQuit(frameBuilder.GetBytes() == buffer);
86 VerifyOrQuit(memcmp(buffer, kData1, sizeof(kData1)) == 0);
87 VerifyOrQuit(memcmp(buffer + sizeof(kData1), kData1, sizeof(kData1)) == 0);
88
89 SuccessOrQuit(frameBuilder.AppendBigEndianUint32(0x01020304));
90 SuccessOrQuit(frameBuilder.AppendUint8(0x05));
91 VerifyOrQuit(frameBuilder.GetLength() == sizeof(kData1) * 3);
92 VerifyOrQuit(frameBuilder.GetBytes() == buffer);
93 VerifyOrQuit(memcmp(buffer, kData1, sizeof(kData1)) == 0);
94 VerifyOrQuit(memcmp(buffer + sizeof(kData1), kData1, sizeof(kData1)) == 0);
95 VerifyOrQuit(memcmp(buffer + 2 * sizeof(kData1), kData1, sizeof(kData1)) == 0);
96
97 frameBuilder.Init(buffer, sizeof(buffer));
98 VerifyOrQuit(frameBuilder.GetBytes() == buffer);
99 VerifyOrQuit(frameBuilder.GetLength() == 0);
100 VerifyOrQuit(frameBuilder.GetMaxLength() == sizeof(buffer));
101
102 offset = sizeof(kData1);
103 SuccessOrQuit(frameBuilder.AppendBytesFromMessage(*message, offset, sizeof(kData2)));
104 VerifyOrQuit(frameBuilder.GetLength() == sizeof(kData2));
105 VerifyOrQuit(frameBuilder.GetBytes() == buffer);
106 VerifyOrQuit(memcmp(buffer, kData2, sizeof(kData2)) == 0);
107
108 frameBuilder.Init(buffer, sizeof(buffer));
109 VerifyOrQuit(frameBuilder.GetBytes() == buffer);
110 VerifyOrQuit(frameBuilder.GetLength() == 0);
111 VerifyOrQuit(frameBuilder.GetMaxLength() == sizeof(buffer));
112
113 SuccessOrQuit(frameBuilder.AppendLittleEndianUint32(0x04030201));
114 SuccessOrQuit(frameBuilder.AppendUint8(0x05));
115 VerifyOrQuit(frameBuilder.GetLength() == sizeof(kData1));
116 VerifyOrQuit(frameBuilder.GetBytes() == buffer);
117 VerifyOrQuit(memcmp(buffer, kData1, sizeof(kData1)) == 0);
118
119 SuccessOrQuit(frameBuilder.AppendBytes(zeroBuffer, sizeof(kData2)));
120 SuccessOrQuit(frameBuilder.Append(kData1));
121 VerifyOrQuit(frameBuilder.GetLength() == sizeof(buffer));
122 VerifyOrQuit(frameBuilder.GetBytes() == buffer);
123 VerifyOrQuit(memcmp(buffer, kData1, sizeof(kData1)) == 0);
124 VerifyOrQuit(memcmp(buffer + sizeof(kData1), zeroBuffer, sizeof(kData2)) == 0);
125 VerifyOrQuit(memcmp(buffer + sizeof(kData1) + sizeof(kData2), kData1, sizeof(kData1)) == 0);
126
127 VerifyOrQuit(!frameBuilder.CanAppend(1));
128 VerifyOrQuit(frameBuilder.AppendUint8(0x00) == kErrorNoBufs);
129
130 offset = sizeof(kData1);
131 frameBuilder.Write(offset, kData2);
132 VerifyOrQuit(frameBuilder.GetLength() == sizeof(buffer));
133 VerifyOrQuit(frameBuilder.GetBytes() == buffer);
134 VerifyOrQuit(memcmp(buffer, kData1, sizeof(kData1)) == 0);
135 VerifyOrQuit(memcmp(buffer + sizeof(kData1), kData2, sizeof(kData2)) == 0);
136 VerifyOrQuit(memcmp(buffer + sizeof(kData1) + sizeof(kData2), kData1, sizeof(kData1)) == 0);
137
138 message->Free();
139 testFreeInstance(instance);
140 }
141
142 } // namespace ot
143
main(void)144 int main(void)
145 {
146 ot::TestFrameBuilder();
147
148 printf("All tests passed\n");
149 return 0;
150 }
151