1 /*
2  *  Copyright (c) 2021, 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/data.hpp"
35 
36 namespace ot {
37 
TestData(void)38 template <DataLengthType kDataLengthType> void TestData(void)
39 {
40     typedef Data<kDataLengthType> Data;
41 
42     const uint8_t kData[]     = {0x12, 0x03, 0x19, 0x77};
43     const uint8_t kDataCopy[] = {0x12, 0x03, 0x19, 0x77};
44 
45     Data     data;
46     Data     data2;
47     uint8_t  buffer[sizeof(kData) + 1];
48     uint8_t  u8  = 0x12;
49     uint16_t u16 = 0x3456;
50 
51     data.Clear();
52     data2.Clear();
53     VerifyOrQuit(data.GetLength() == 0);
54     VerifyOrQuit(data.GetBytes() == nullptr);
55     VerifyOrQuit(data == data2);
56     VerifyOrQuit(data.StartsWith(data));
57     VerifyOrQuit(data2.StartsWith(data));
58 
59     data.Init(kData, sizeof(kData));
60     VerifyOrQuit(data.GetLength() == sizeof(kData));
61     VerifyOrQuit(data.GetBytes() == &kData[0]);
62     VerifyOrQuit(data == data);
63     VerifyOrQuit(data.StartsWith(data));
64 
65     memset(buffer, 0, sizeof(buffer));
66     data.CopyBytesTo(buffer);
67     VerifyOrQuit(memcmp(buffer, kData, sizeof(kData)) == 0);
68     VerifyOrQuit(buffer[sizeof(kData)] == 0);
69     VerifyOrQuit(data.MatchesBytesIn(buffer));
70 
71     data2.InitFrom(kDataCopy);
72     VerifyOrQuit(data2.GetLength() == sizeof(kDataCopy));
73     VerifyOrQuit(data2.GetBytes() == &kDataCopy[0]);
74     VerifyOrQuit(data == data2);
75     VerifyOrQuit(data.StartsWith(data2));
76     VerifyOrQuit(data2.StartsWith(data));
77 
78     data2.SetLength(sizeof(kDataCopy) - 1);
79     VerifyOrQuit(data2.GetLength() == sizeof(kDataCopy) - 1);
80     VerifyOrQuit(data2.GetBytes() == &kDataCopy[0]);
81     VerifyOrQuit(data2.MatchesBytesIn(kDataCopy));
82     VerifyOrQuit(data != data2);
83     VerifyOrQuit(data.StartsWith(data2));
84     VerifyOrQuit(!data2.StartsWith(data));
85 
86     data2.InitFromRange(&kDataCopy[0], &kDataCopy[2]);
87     VerifyOrQuit(data2.GetLength() == 2);
88     VerifyOrQuit(data2.GetBytes() == &kDataCopy[0]);
89     VerifyOrQuit(data != data2);
90     VerifyOrQuit(data.StartsWith(data2));
91     VerifyOrQuit(!data2.StartsWith(data));
92 
93     data2 = data;
94     VerifyOrQuit(data2 == data);
95 
96     data.Clear();
97     VerifyOrQuit(data.GetLength() == 0);
98     VerifyOrQuit(data.GetBytes() == nullptr);
99     VerifyOrQuit(data != data2);
100     VerifyOrQuit(!data.StartsWith(data2));
101     VerifyOrQuit(data2.StartsWith(data));
102 
103     memset(buffer, 0xaa, sizeof(buffer));
104     data.CopyBytesTo(buffer);
105     VerifyOrQuit(buffer[0] == 0xaa);
106 
107     data.InitFrom(u8);
108     VerifyOrQuit(data.GetLength() == sizeof(u8));
109     VerifyOrQuit(data.GetBytes() == &u8);
110 
111     data.InitFrom(u16);
112     VerifyOrQuit(data.GetLength() == sizeof(u16));
113     VerifyOrQuit(data.GetBytes() == reinterpret_cast<uint8_t *>(&u16));
114 
115     printf("- TestData<%s> passed\n", kDataLengthType == kWithUint8Length ? "kWithUint8Length" : "kWithUint16Length");
116 }
117 
TestMutableData(void)118 template <DataLengthType kDataLengthType> void TestMutableData(void)
119 {
120     typedef Data<kDataLengthType>        Data;
121     typedef MutableData<kDataLengthType> MutableData;
122 
123     constexpr uint8_t kMaxSize = 20;
124 
125     const uint8_t kData[]  = {10, 20, 3, 15, 10, 00, 60, 16};
126     const uint8_t kData2[] = {0xab, 0xbc, 0xcd, 0xde, 0xef};
127 
128     MutableData mutableData;
129     Data        data;
130     uint8_t     buffer[kMaxSize];
131     uint8_t     u8;
132     uint16_t    u16;
133 
134     data.Init(kData, sizeof(kData));
135 
136     mutableData.Clear();
137     VerifyOrQuit(mutableData.GetLength() == 0);
138     VerifyOrQuit(mutableData.GetBytes() == nullptr);
139 
140     mutableData.Init(buffer, sizeof(buffer));
141     VerifyOrQuit(mutableData.GetLength() == sizeof(buffer));
142     VerifyOrQuit(mutableData.GetBytes() == &buffer[0]);
143 
144     SuccessOrQuit(mutableData.CopyBytesFrom(kData, sizeof(kData)));
145     VerifyOrQuit(mutableData.GetLength() == sizeof(kData));
146     VerifyOrQuit(mutableData.GetBytes() == &buffer[0]);
147     VerifyOrQuit(mutableData == data);
148 
149     SuccessOrQuit(mutableData.CopyBytesFrom(kData2, sizeof(kData2)));
150     VerifyOrQuit(mutableData.GetLength() == sizeof(kData2));
151     VerifyOrQuit(mutableData.GetBytes() == &buffer[0]);
152     VerifyOrQuit(memcmp(mutableData.GetBytes(), kData2, sizeof(kData2)) == 0);
153 
154     memset(buffer, 0, sizeof(buffer));
155     mutableData.InitFrom(buffer);
156     SuccessOrQuit(mutableData.CopyBytesFrom(kData, sizeof(kData)));
157     VerifyOrQuit(mutableData == data);
158 
159     memset(buffer, 0, sizeof(buffer));
160     SuccessOrQuit(mutableData.CopyBytesFrom(data));
161     VerifyOrQuit(mutableData == data);
162 
163     memset(buffer, 0, sizeof(buffer));
164     mutableData.InitFromRange(&buffer[0], &buffer[2]);
165     VerifyOrQuit(mutableData.GetLength() == 2);
166     VerifyOrQuit(mutableData.GetBytes() == &buffer[0]);
167 
168     VerifyOrQuit(mutableData.CopyBytesFrom(kData, sizeof(kData)) == kErrorNoBufs);
169     VerifyOrQuit(mutableData.GetLength() == 2);
170     VerifyOrQuit(mutableData.GetBytes() == &buffer[0]);
171     VerifyOrQuit(memcmp(mutableData.GetBytes(), kData, 2) == 0);
172 
173     VerifyOrQuit(mutableData.CopyBytesFrom(data) == kErrorNoBufs);
174     VerifyOrQuit(mutableData.GetLength() == 2);
175     VerifyOrQuit(mutableData.GetBytes() == &buffer[0]);
176     VerifyOrQuit(memcmp(mutableData.GetBytes(), kData, 2) == 0);
177 
178     memset(buffer, 0xff, sizeof(buffer));
179     mutableData.InitFrom(buffer);
180     VerifyOrQuit(mutableData.GetLength() == sizeof(buffer));
181     VerifyOrQuit(mutableData.GetBytes() == &buffer[0]);
182 
183     u8 = 0xaa;
184     mutableData.InitFrom(u8);
185     VerifyOrQuit(mutableData.GetLength() == sizeof(u8));
186     VerifyOrQuit(mutableData.GetBytes() == &u8);
187     mutableData.ClearBytes();
188     VerifyOrQuit(u8 == 0);
189 
190     u16 = 0x1234;
191     mutableData.InitFrom(u16);
192     VerifyOrQuit(mutableData.GetLength() == sizeof(u16));
193     VerifyOrQuit(mutableData.GetBytes() == reinterpret_cast<uint8_t *>(&u16));
194     mutableData.ClearBytes();
195     VerifyOrQuit(u16 == 0);
196 
197     printf("- TestMutableData<%s> passed\n",
198            kDataLengthType == kWithUint8Length ? "kWithUint8Length" : "kWithUint16Length");
199 }
200 
201 } // namespace ot
202 
main(void)203 int main(void)
204 {
205     ot::TestData<ot::kWithUint8Length>();
206     ot::TestData<ot::kWithUint16Length>();
207     ot::TestMutableData<ot::kWithUint8Length>();
208     ot::TestMutableData<ot::kWithUint16Length>();
209 
210     printf("All tests passed\n");
211     return 0;
212 }
213