1 /*
2 * Copyright (c) 2020-21, 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 "test_platform.h"
30
31 #include <openthread/config.h>
32
33 #include "test_util.hpp"
34 #include "meshcop/meshcop.hpp"
35 #include "meshcop/timestamp.hpp"
36
37 namespace ot {
38
TestSteeringData(void)39 void TestSteeringData(void)
40 {
41 MeshCoP::SteeringData steeringData;
42 MeshCoP::SteeringData::HashBitIndexes indexes;
43 Mac::ExtAddress joinerId1;
44 Mac::ExtAddress joinerId2;
45
46 const uint8_t kAddress1[sizeof(Mac::ExtAddress)] = {0x10, 0x20, 0x03, 0x15, 0x10, 0x00, 0x60, 0x16};
47 const uint8_t kAddress2[sizeof(Mac::ExtAddress)] = {0xbe, 0xef, 0xca, 0xfe, 0xde, 0xad, 0xba, 0xbe};
48
49 joinerId1.Set(kAddress1);
50 joinerId2.Set(kAddress2);
51
52 MeshCoP::SteeringData::CalculateHashBitIndexes(joinerId2, indexes);
53
54 steeringData.SetToPermitAllJoiners();
55
56 DumpBuffer("After SetToPermitAllJoiners()", steeringData.GetData(), steeringData.GetLength());
57 VerifyOrQuit(steeringData.GetLength() == 1, "after SetToPermitAllJoiners()");
58 VerifyOrQuit(steeringData.PermitsAllJoiners(), "after SetToPermitAllJoiners()");
59 VerifyOrQuit(!steeringData.IsEmpty(), "after SetToPermitAllJoiners()");
60 VerifyOrQuit(steeringData.Contains(joinerId1), "after SetToPermitAllJoiners()");
61 VerifyOrQuit(steeringData.Contains(joinerId2), "after SetToPermitAllJoiners()");
62 VerifyOrQuit(steeringData.Contains(indexes), "after SetToPermitAllJoiners()");
63
64 steeringData.Clear();
65
66 DumpBuffer("After Clear()", steeringData.GetData(), steeringData.GetLength());
67 VerifyOrQuit(steeringData.GetLength() == 1, "after Clear()");
68 VerifyOrQuit(!steeringData.PermitsAllJoiners(), "after Clear()");
69 VerifyOrQuit(steeringData.IsEmpty(), "after Clear()");
70 VerifyOrQuit(!steeringData.Contains(joinerId1), "after Clear()");
71 VerifyOrQuit(!steeringData.Contains(joinerId2), "after Clear()");
72 VerifyOrQuit(!steeringData.Contains(indexes), "after Clear()");
73
74 for (uint8_t len = 1; len <= MeshCoP::SteeringData::kMaxLength; len++)
75 {
76 printf("\n--------------------------------------------");
77
78 steeringData.Init(len);
79
80 VerifyOrQuit(steeringData.GetLength() == len, "after Init()");
81 VerifyOrQuit(steeringData.IsEmpty(), "IsEmpy() failed after Init()");
82 VerifyOrQuit(!steeringData.PermitsAllJoiners(), "after Init()");
83 VerifyOrQuit(!steeringData.Contains(joinerId1), "after Init()");
84 VerifyOrQuit(!steeringData.Contains(joinerId2), "after Init()");
85 VerifyOrQuit(!steeringData.Contains(indexes), "after Init()");
86
87 steeringData.UpdateBloomFilter(joinerId1);
88 DumpBuffer("After UpdateBloomFilter(joinerId1)", steeringData.GetData(), steeringData.GetLength());
89 VerifyOrQuit(steeringData.GetLength() == len, "after UpdateBloomFilter()");
90 VerifyOrQuit(!steeringData.IsEmpty(), "IsEmpy() failed after UpdateBloomFilter()");
91 VerifyOrQuit(!steeringData.PermitsAllJoiners(), "after UpdateBloomFilter");
92 VerifyOrQuit(steeringData.Contains(joinerId1), "after UpdateBloomFilter");
93
94 steeringData.UpdateBloomFilter(joinerId2);
95 DumpBuffer("After UpdateBloomFilter(joinerId2)", steeringData.GetData(), steeringData.GetLength());
96 VerifyOrQuit(steeringData.GetLength() == len, "after UpdateBloomFilter()");
97 VerifyOrQuit(!steeringData.IsEmpty(), "IsEmpy() failed after UpdateBloomFilter()");
98 VerifyOrQuit(!steeringData.PermitsAllJoiners(), "after UpdateBloomFilter");
99 VerifyOrQuit(steeringData.Contains(joinerId1), "after UpdateBloomFilter");
100 VerifyOrQuit(steeringData.Contains(joinerId2), "after UpdateBloomFilter");
101 VerifyOrQuit(steeringData.Contains(indexes), "after UpdateBloomFilter");
102 }
103
104 steeringData.Init(0);
105
106 VerifyOrQuit(steeringData.GetLength() == 0, "after Init()");
107 VerifyOrQuit(steeringData.IsEmpty(), "IsEmpy() failed after Init()");
108 VerifyOrQuit(!steeringData.PermitsAllJoiners(), "after Init()");
109 VerifyOrQuit(!steeringData.Contains(joinerId1), "after Init()");
110 VerifyOrQuit(!steeringData.Contains(joinerId2), "after Init()");
111 VerifyOrQuit(!steeringData.Contains(indexes), "after Init()");
112
113 printf("TestSteeringData() passed\n");
114 }
115
TestTimestamp(void)116 void TestTimestamp(void)
117 {
118 MeshCoP::Timestamp t1;
119 MeshCoP::Timestamp t2;
120
121 t1.Clear();
122 t2.Clear();
123
124 VerifyOrQuit(t1.GetSeconds() == 0);
125 VerifyOrQuit(t1.GetTicks() == 0);
126 VerifyOrQuit(!t1.GetAuthoritative());
127
128 VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) == 0);
129 VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, nullptr) > 0);
130 VerifyOrQuit(MeshCoP::Timestamp::Compare(nullptr, &t2) < 0);
131 VerifyOrQuit(MeshCoP::Timestamp::Compare(nullptr, nullptr) == 0);
132
133 t1.SetTicks(10);
134 VerifyOrQuit(t1.GetTicks() == 10);
135 VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) > 0);
136 VerifyOrQuit(MeshCoP::Timestamp::Compare(&t2, &t1) < 0);
137
138 t2.SetTicks(10);
139 VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) == 0);
140
141 t1.SetAuthoritative(true);
142 VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) > 0);
143
144 t1.SetSeconds(1);
145 VerifyOrQuit(t1.GetSeconds() == 1);
146 VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) > 0);
147 VerifyOrQuit(MeshCoP::Timestamp::Compare(&t2, &t1) < 0);
148
149 t2.SetSeconds(1);
150 t2.SetAuthoritative(true);
151 VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) == 0);
152
153 printf("TestTimestamp() passed\n");
154 }
155
156 } // namespace ot
157
main(void)158 int main(void)
159 {
160 ot::TestSteeringData();
161 ot::TestTimestamp();
162 printf("\nAll tests passed.\n");
163 return 0;
164 }
165