1 /*
2  *  Copyright (c) 2020, 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 
36 namespace ot {
37 
TestSteeringData(void)38 void TestSteeringData(void)
39 {
40     MeshCoP::SteeringData                 steeringData;
41     MeshCoP::SteeringData::HashBitIndexes indexes;
42     Mac::ExtAddress                       joinerId1;
43     Mac::ExtAddress                       joinerId2;
44 
45     const uint8_t kAddress1[sizeof(Mac::ExtAddress)] = {0x10, 0x20, 0x03, 0x15, 0x10, 0x00, 0x60, 0x16};
46     const uint8_t kAddress2[sizeof(Mac::ExtAddress)] = {0xbe, 0xef, 0xca, 0xfe, 0xde, 0xad, 0xba, 0xbe};
47 
48     joinerId1.Set(kAddress1);
49     joinerId2.Set(kAddress2);
50 
51     MeshCoP::SteeringData::CalculateHashBitIndexes(joinerId2, indexes);
52 
53     steeringData.SetToPermitAllJoiners();
54 
55     DumpBuffer("After SetToPermitAllJoiners()", steeringData.GetData(), steeringData.GetLength());
56     VerifyOrQuit(steeringData.GetLength() == 1, "after SetToPermitAllJoiners()");
57     VerifyOrQuit(steeringData.PermitsAllJoiners(), "after SetToPermitAllJoiners()");
58     VerifyOrQuit(!steeringData.IsEmpty(), "after SetToPermitAllJoiners()");
59     VerifyOrQuit(steeringData.Contains(joinerId1), "after SetToPermitAllJoiners()");
60     VerifyOrQuit(steeringData.Contains(joinerId2), "after SetToPermitAllJoiners()");
61     VerifyOrQuit(steeringData.Contains(indexes), "after SetToPermitAllJoiners()");
62 
63     steeringData.Clear();
64 
65     DumpBuffer("After Clear()", steeringData.GetData(), steeringData.GetLength());
66     VerifyOrQuit(steeringData.GetLength() == 1, "after Clear()");
67     VerifyOrQuit(!steeringData.PermitsAllJoiners(), "after Clear()");
68     VerifyOrQuit(steeringData.IsEmpty(), "after Clear()");
69     VerifyOrQuit(!steeringData.Contains(joinerId1), "after Clear()");
70     VerifyOrQuit(!steeringData.Contains(joinerId2), "after Clear()");
71     VerifyOrQuit(!steeringData.Contains(indexes), "after Clear()");
72 
73     for (uint8_t len = 1; len <= MeshCoP::SteeringData::kMaxLength; len++)
74     {
75         printf("\n--------------------------------------------");
76 
77         steeringData.Init(len);
78 
79         VerifyOrQuit(steeringData.GetLength() == len, "after Init()");
80         VerifyOrQuit(steeringData.IsEmpty(), "IsEmpy() failed after Init()");
81         VerifyOrQuit(!steeringData.PermitsAllJoiners(), "after Init()");
82         VerifyOrQuit(!steeringData.Contains(joinerId1), "after Init()");
83         VerifyOrQuit(!steeringData.Contains(joinerId2), "after Init()");
84         VerifyOrQuit(!steeringData.Contains(indexes), "after Init()");
85 
86         steeringData.UpdateBloomFilter(joinerId1);
87         DumpBuffer("After UpdateBloomFilter(joinerId1)", steeringData.GetData(), steeringData.GetLength());
88         VerifyOrQuit(steeringData.GetLength() == len, "after UpdateBloomFilter()");
89         VerifyOrQuit(!steeringData.IsEmpty(), "IsEmpy() failed after UpdateBloomFilter()");
90         VerifyOrQuit(!steeringData.PermitsAllJoiners(), "after UpdateBloomFilter");
91         VerifyOrQuit(steeringData.Contains(joinerId1), "after UpdateBloomFilter");
92 
93         steeringData.UpdateBloomFilter(joinerId2);
94         DumpBuffer("After UpdateBloomFilter(joinerId2)", steeringData.GetData(), steeringData.GetLength());
95         VerifyOrQuit(steeringData.GetLength() == len, "after UpdateBloomFilter()");
96         VerifyOrQuit(!steeringData.IsEmpty(), "IsEmpy() failed after UpdateBloomFilter()");
97         VerifyOrQuit(!steeringData.PermitsAllJoiners(), "after UpdateBloomFilter");
98         VerifyOrQuit(steeringData.Contains(joinerId1), "after UpdateBloomFilter");
99         VerifyOrQuit(steeringData.Contains(joinerId2), "after UpdateBloomFilter");
100         VerifyOrQuit(steeringData.Contains(indexes), "after UpdateBloomFilter");
101     }
102 
103     steeringData.Init(0);
104 
105     VerifyOrQuit(steeringData.GetLength() == 0, "after Init()");
106     VerifyOrQuit(steeringData.IsEmpty(), "IsEmpy() failed after Init()");
107     VerifyOrQuit(!steeringData.PermitsAllJoiners(), "after Init()");
108     VerifyOrQuit(!steeringData.Contains(joinerId1), "after Init()");
109     VerifyOrQuit(!steeringData.Contains(joinerId2), "after Init()");
110     VerifyOrQuit(!steeringData.Contains(indexes), "after Init()");
111 }
112 
113 } // namespace ot
114 
main(void)115 int main(void)
116 {
117     ot::TestSteeringData();
118     printf("\nAll tests passed.\n");
119     return 0;
120 }
121