1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker * Copyright (c) 2018, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker * All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker *
5*cfb92d14SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker *
16*cfb92d14SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker */
28*cfb92d14SAndroid Build Coastguard Worker
29*cfb92d14SAndroid Build Coastguard Worker #include "test_platform.h"
30*cfb92d14SAndroid Build Coastguard Worker
31*cfb92d14SAndroid Build Coastguard Worker #include <openthread/config.h>
32*cfb92d14SAndroid Build Coastguard Worker
33*cfb92d14SAndroid Build Coastguard Worker #include "test_util.h"
34*cfb92d14SAndroid Build Coastguard Worker #include "common/code_utils.hpp"
35*cfb92d14SAndroid Build Coastguard Worker #include "instance/instance.hpp"
36*cfb92d14SAndroid Build Coastguard Worker #include "thread/child.hpp"
37*cfb92d14SAndroid Build Coastguard Worker
38*cfb92d14SAndroid Build Coastguard Worker namespace ot {
39*cfb92d14SAndroid Build Coastguard Worker
40*cfb92d14SAndroid Build Coastguard Worker static Instance *sInstance;
41*cfb92d14SAndroid Build Coastguard Worker
42*cfb92d14SAndroid Build Coastguard Worker enum
43*cfb92d14SAndroid Build Coastguard Worker {
44*cfb92d14SAndroid Build Coastguard Worker kMaxChildIp6Addresses = OPENTHREAD_CONFIG_MLE_IP_ADDRS_PER_CHILD,
45*cfb92d14SAndroid Build Coastguard Worker };
46*cfb92d14SAndroid Build Coastguard Worker
VerifyChildIp6Addresses(const Child & aChild,uint8_t aAddressListLength,const Ip6::Address aAddressList[])47*cfb92d14SAndroid Build Coastguard Worker void VerifyChildIp6Addresses(const Child &aChild, uint8_t aAddressListLength, const Ip6::Address aAddressList[])
48*cfb92d14SAndroid Build Coastguard Worker {
49*cfb92d14SAndroid Build Coastguard Worker Ip6::Address::TypeFilter filters[] = {Ip6::Address::kTypeUnicast, Ip6::Address::kTypeMulticast};
50*cfb92d14SAndroid Build Coastguard Worker bool addressObserved[kMaxChildIp6Addresses];
51*cfb92d14SAndroid Build Coastguard Worker bool hasMeshLocal = false;
52*cfb92d14SAndroid Build Coastguard Worker
53*cfb92d14SAndroid Build Coastguard Worker for (uint8_t index = 0; index < aAddressListLength; index++)
54*cfb92d14SAndroid Build Coastguard Worker {
55*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aChild.HasIp6Address(aAddressList[index]));
56*cfb92d14SAndroid Build Coastguard Worker }
57*cfb92d14SAndroid Build Coastguard Worker
58*cfb92d14SAndroid Build Coastguard Worker memset(addressObserved, 0, sizeof(addressObserved));
59*cfb92d14SAndroid Build Coastguard Worker
60*cfb92d14SAndroid Build Coastguard Worker for (const Ip6::Address &address : aChild.GetIp6Addresses())
61*cfb92d14SAndroid Build Coastguard Worker {
62*cfb92d14SAndroid Build Coastguard Worker bool addressIsInList = false;
63*cfb92d14SAndroid Build Coastguard Worker
64*cfb92d14SAndroid Build Coastguard Worker for (uint8_t index = 0; index < aAddressListLength; index++)
65*cfb92d14SAndroid Build Coastguard Worker {
66*cfb92d14SAndroid Build Coastguard Worker if (address == aAddressList[index])
67*cfb92d14SAndroid Build Coastguard Worker {
68*cfb92d14SAndroid Build Coastguard Worker addressIsInList = true;
69*cfb92d14SAndroid Build Coastguard Worker addressObserved[index] = true;
70*cfb92d14SAndroid Build Coastguard Worker break;
71*cfb92d14SAndroid Build Coastguard Worker }
72*cfb92d14SAndroid Build Coastguard Worker }
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(addressIsInList, "Child::IterateIp6Addresses() returned an address not in the expected list");
75*cfb92d14SAndroid Build Coastguard Worker }
76*cfb92d14SAndroid Build Coastguard Worker
77*cfb92d14SAndroid Build Coastguard Worker for (uint8_t index = 0; index < aAddressListLength; index++)
78*cfb92d14SAndroid Build Coastguard Worker {
79*cfb92d14SAndroid Build Coastguard Worker Ip6::Address address;
80*cfb92d14SAndroid Build Coastguard Worker
81*cfb92d14SAndroid Build Coastguard Worker if (sInstance->Get<Mle::MleRouter>().IsMeshLocalAddress(aAddressList[index]))
82*cfb92d14SAndroid Build Coastguard Worker {
83*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(aChild.GetMeshLocalIp6Address(address));
84*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(address == aAddressList[index], "GetMeshLocalIp6Address() did not return expected address");
85*cfb92d14SAndroid Build Coastguard Worker hasMeshLocal = true;
86*cfb92d14SAndroid Build Coastguard Worker }
87*cfb92d14SAndroid Build Coastguard Worker else
88*cfb92d14SAndroid Build Coastguard Worker {
89*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(addressObserved[index], "Child::IterateIp6Addresses() missed an entry from the expected list");
90*cfb92d14SAndroid Build Coastguard Worker }
91*cfb92d14SAndroid Build Coastguard Worker }
92*cfb92d14SAndroid Build Coastguard Worker
93*cfb92d14SAndroid Build Coastguard Worker if (!hasMeshLocal)
94*cfb92d14SAndroid Build Coastguard Worker {
95*cfb92d14SAndroid Build Coastguard Worker Ip6::Address address;
96*cfb92d14SAndroid Build Coastguard Worker
97*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aChild.GetMeshLocalIp6Address(address) == kErrorNotFound,
98*cfb92d14SAndroid Build Coastguard Worker "Child::GetMeshLocalIp6Address() returned an address not in the expected list");
99*cfb92d14SAndroid Build Coastguard Worker }
100*cfb92d14SAndroid Build Coastguard Worker }
101*cfb92d14SAndroid Build Coastguard Worker
TestChildIp6Address(void)102*cfb92d14SAndroid Build Coastguard Worker void TestChildIp6Address(void)
103*cfb92d14SAndroid Build Coastguard Worker {
104*cfb92d14SAndroid Build Coastguard Worker Child child;
105*cfb92d14SAndroid Build Coastguard Worker Ip6::Address addresses[kMaxChildIp6Addresses];
106*cfb92d14SAndroid Build Coastguard Worker uint8_t numAddresses;
107*cfb92d14SAndroid Build Coastguard Worker const char *ip6Addresses[] = {
108*cfb92d14SAndroid Build Coastguard Worker "fd00:1234::1234",
109*cfb92d14SAndroid Build Coastguard Worker "ff6b:e251:52fb:0:12e6:b94c:1c28:c56a",
110*cfb92d14SAndroid Build Coastguard Worker "fd00:1234::204c:3d7c:98f6:9a1b",
111*cfb92d14SAndroid Build Coastguard Worker };
112*cfb92d14SAndroid Build Coastguard Worker
113*cfb92d14SAndroid Build Coastguard Worker const uint8_t meshLocalIidArray[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
114*cfb92d14SAndroid Build Coastguard Worker Ip6::InterfaceIdentifier meshLocalIid;
115*cfb92d14SAndroid Build Coastguard Worker
116*cfb92d14SAndroid Build Coastguard Worker meshLocalIid.SetBytes(meshLocalIidArray);
117*cfb92d14SAndroid Build Coastguard Worker
118*cfb92d14SAndroid Build Coastguard Worker sInstance = testInitInstance();
119*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(sInstance != nullptr);
120*cfb92d14SAndroid Build Coastguard Worker
121*cfb92d14SAndroid Build Coastguard Worker child.Init(*sInstance);
122*cfb92d14SAndroid Build Coastguard Worker
123*cfb92d14SAndroid Build Coastguard Worker //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124*cfb92d14SAndroid Build Coastguard Worker
125*cfb92d14SAndroid Build Coastguard Worker printf("\nConverting IPv6 addresses from string");
126*cfb92d14SAndroid Build Coastguard Worker
127*cfb92d14SAndroid Build Coastguard Worker numAddresses = 0;
128*cfb92d14SAndroid Build Coastguard Worker
129*cfb92d14SAndroid Build Coastguard Worker // First addresses uses the mesh local prefix (mesh-local address).
130*cfb92d14SAndroid Build Coastguard Worker addresses[numAddresses] = sInstance->Get<Mle::MleRouter>().GetMeshLocalEid();
131*cfb92d14SAndroid Build Coastguard Worker addresses[numAddresses].SetIid(meshLocalIid);
132*cfb92d14SAndroid Build Coastguard Worker
133*cfb92d14SAndroid Build Coastguard Worker numAddresses++;
134*cfb92d14SAndroid Build Coastguard Worker
135*cfb92d14SAndroid Build Coastguard Worker for (const char *ip6Address : ip6Addresses)
136*cfb92d14SAndroid Build Coastguard Worker {
137*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(numAddresses < kMaxChildIp6Addresses, "Too many IPv6 addresses in the unit test");
138*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(addresses[numAddresses++].FromString(ip6Address));
139*cfb92d14SAndroid Build Coastguard Worker }
140*cfb92d14SAndroid Build Coastguard Worker
141*cfb92d14SAndroid Build Coastguard Worker printf(" -- PASS\n");
142*cfb92d14SAndroid Build Coastguard Worker
143*cfb92d14SAndroid Build Coastguard Worker //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
144*cfb92d14SAndroid Build Coastguard Worker printf("Child state after init");
145*cfb92d14SAndroid Build Coastguard Worker child.Clear();
146*cfb92d14SAndroid Build Coastguard Worker VerifyChildIp6Addresses(child, 0, nullptr);
147*cfb92d14SAndroid Build Coastguard Worker printf(" -- PASS\n");
148*cfb92d14SAndroid Build Coastguard Worker
149*cfb92d14SAndroid Build Coastguard Worker //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
150*cfb92d14SAndroid Build Coastguard Worker printf("Adding a single IPv6 address");
151*cfb92d14SAndroid Build Coastguard Worker
152*cfb92d14SAndroid Build Coastguard Worker for (uint8_t index = 0; index < numAddresses; index++)
153*cfb92d14SAndroid Build Coastguard Worker {
154*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(child.AddIp6Address(addresses[index]));
155*cfb92d14SAndroid Build Coastguard Worker VerifyChildIp6Addresses(child, 1, &addresses[index]);
156*cfb92d14SAndroid Build Coastguard Worker
157*cfb92d14SAndroid Build Coastguard Worker child.ClearIp6Addresses();
158*cfb92d14SAndroid Build Coastguard Worker VerifyChildIp6Addresses(child, 0, nullptr);
159*cfb92d14SAndroid Build Coastguard Worker }
160*cfb92d14SAndroid Build Coastguard Worker
161*cfb92d14SAndroid Build Coastguard Worker printf(" -- PASS\n");
162*cfb92d14SAndroid Build Coastguard Worker
163*cfb92d14SAndroid Build Coastguard Worker //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
164*cfb92d14SAndroid Build Coastguard Worker printf("Adding multiple IPv6 addresses");
165*cfb92d14SAndroid Build Coastguard Worker
166*cfb92d14SAndroid Build Coastguard Worker for (uint8_t index = 0; index < numAddresses; index++)
167*cfb92d14SAndroid Build Coastguard Worker {
168*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(child.AddIp6Address(addresses[index]));
169*cfb92d14SAndroid Build Coastguard Worker VerifyChildIp6Addresses(child, index + 1, addresses);
170*cfb92d14SAndroid Build Coastguard Worker }
171*cfb92d14SAndroid Build Coastguard Worker
172*cfb92d14SAndroid Build Coastguard Worker printf(" -- PASS\n");
173*cfb92d14SAndroid Build Coastguard Worker
174*cfb92d14SAndroid Build Coastguard Worker //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
175*cfb92d14SAndroid Build Coastguard Worker printf("Checking for failure when adding an address already in list");
176*cfb92d14SAndroid Build Coastguard Worker
177*cfb92d14SAndroid Build Coastguard Worker for (uint8_t index = 0; index < numAddresses; index++)
178*cfb92d14SAndroid Build Coastguard Worker {
179*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(child.AddIp6Address(addresses[index]) == kErrorAlready,
180*cfb92d14SAndroid Build Coastguard Worker "AddIp6Address() did not fail when adding same address");
181*cfb92d14SAndroid Build Coastguard Worker VerifyChildIp6Addresses(child, numAddresses, addresses);
182*cfb92d14SAndroid Build Coastguard Worker }
183*cfb92d14SAndroid Build Coastguard Worker
184*cfb92d14SAndroid Build Coastguard Worker printf(" -- PASS\n");
185*cfb92d14SAndroid Build Coastguard Worker
186*cfb92d14SAndroid Build Coastguard Worker //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
187*cfb92d14SAndroid Build Coastguard Worker printf("Removing addresses from list starting from front of the list");
188*cfb92d14SAndroid Build Coastguard Worker
189*cfb92d14SAndroid Build Coastguard Worker for (uint8_t index = 0; index < numAddresses; index++)
190*cfb92d14SAndroid Build Coastguard Worker {
191*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(child.RemoveIp6Address(addresses[index]));
192*cfb92d14SAndroid Build Coastguard Worker VerifyChildIp6Addresses(child, numAddresses - 1 - index, &addresses[index + 1]);
193*cfb92d14SAndroid Build Coastguard Worker
194*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(child.RemoveIp6Address(addresses[index]) == kErrorNotFound,
195*cfb92d14SAndroid Build Coastguard Worker "RemoveIp6Address() did not fail when removing an address not on the list");
196*cfb92d14SAndroid Build Coastguard Worker }
197*cfb92d14SAndroid Build Coastguard Worker
198*cfb92d14SAndroid Build Coastguard Worker VerifyChildIp6Addresses(child, 0, nullptr);
199*cfb92d14SAndroid Build Coastguard Worker printf(" -- PASS\n");
200*cfb92d14SAndroid Build Coastguard Worker
201*cfb92d14SAndroid Build Coastguard Worker //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
202*cfb92d14SAndroid Build Coastguard Worker printf("Removing addresses from list starting from back of the list");
203*cfb92d14SAndroid Build Coastguard Worker
204*cfb92d14SAndroid Build Coastguard Worker for (uint8_t index = 0; index < numAddresses; index++)
205*cfb92d14SAndroid Build Coastguard Worker {
206*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(child.AddIp6Address(addresses[index]));
207*cfb92d14SAndroid Build Coastguard Worker }
208*cfb92d14SAndroid Build Coastguard Worker
209*cfb92d14SAndroid Build Coastguard Worker for (uint8_t index = numAddresses - 1; index > 0; index--)
210*cfb92d14SAndroid Build Coastguard Worker {
211*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(child.RemoveIp6Address(addresses[index]));
212*cfb92d14SAndroid Build Coastguard Worker VerifyChildIp6Addresses(child, index, &addresses[0]);
213*cfb92d14SAndroid Build Coastguard Worker
214*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(child.RemoveIp6Address(addresses[index]) == kErrorNotFound,
215*cfb92d14SAndroid Build Coastguard Worker "RemoveIp6Address() did not fail when removing an address not on the list");
216*cfb92d14SAndroid Build Coastguard Worker }
217*cfb92d14SAndroid Build Coastguard Worker
218*cfb92d14SAndroid Build Coastguard Worker printf(" -- PASS\n");
219*cfb92d14SAndroid Build Coastguard Worker
220*cfb92d14SAndroid Build Coastguard Worker //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
221*cfb92d14SAndroid Build Coastguard Worker printf("Removing address entries from middle of the list");
222*cfb92d14SAndroid Build Coastguard Worker
223*cfb92d14SAndroid Build Coastguard Worker for (uint8_t indexToRemove = 1; indexToRemove < numAddresses - 1; indexToRemove++)
224*cfb92d14SAndroid Build Coastguard Worker {
225*cfb92d14SAndroid Build Coastguard Worker child.ClearIp6Addresses();
226*cfb92d14SAndroid Build Coastguard Worker
227*cfb92d14SAndroid Build Coastguard Worker for (uint8_t index = 0; index < numAddresses; index++)
228*cfb92d14SAndroid Build Coastguard Worker {
229*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(child.AddIp6Address(addresses[index]));
230*cfb92d14SAndroid Build Coastguard Worker }
231*cfb92d14SAndroid Build Coastguard Worker
232*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(child.RemoveIp6Address(addresses[indexToRemove]));
233*cfb92d14SAndroid Build Coastguard Worker
234*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(child.RemoveIp6Address(addresses[indexToRemove]) == kErrorNotFound,
235*cfb92d14SAndroid Build Coastguard Worker "RemoveIp6Address() did not fail when removing an address not on the list");
236*cfb92d14SAndroid Build Coastguard Worker
237*cfb92d14SAndroid Build Coastguard Worker {
238*cfb92d14SAndroid Build Coastguard Worker Ip6::Address updatedAddressList[kMaxChildIp6Addresses];
239*cfb92d14SAndroid Build Coastguard Worker uint8_t updatedListIndex = 0;
240*cfb92d14SAndroid Build Coastguard Worker
241*cfb92d14SAndroid Build Coastguard Worker for (uint8_t index = 0; index < numAddresses; index++)
242*cfb92d14SAndroid Build Coastguard Worker {
243*cfb92d14SAndroid Build Coastguard Worker if (index != indexToRemove)
244*cfb92d14SAndroid Build Coastguard Worker {
245*cfb92d14SAndroid Build Coastguard Worker updatedAddressList[updatedListIndex++] = addresses[index];
246*cfb92d14SAndroid Build Coastguard Worker }
247*cfb92d14SAndroid Build Coastguard Worker }
248*cfb92d14SAndroid Build Coastguard Worker
249*cfb92d14SAndroid Build Coastguard Worker VerifyChildIp6Addresses(child, updatedListIndex, updatedAddressList);
250*cfb92d14SAndroid Build Coastguard Worker }
251*cfb92d14SAndroid Build Coastguard Worker }
252*cfb92d14SAndroid Build Coastguard Worker
253*cfb92d14SAndroid Build Coastguard Worker printf(" -- PASS\n");
254*cfb92d14SAndroid Build Coastguard Worker
255*cfb92d14SAndroid Build Coastguard Worker testFreeInstance(sInstance);
256*cfb92d14SAndroid Build Coastguard Worker }
257*cfb92d14SAndroid Build Coastguard Worker
258*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
259*cfb92d14SAndroid Build Coastguard Worker
main(void)260*cfb92d14SAndroid Build Coastguard Worker int main(void)
261*cfb92d14SAndroid Build Coastguard Worker {
262*cfb92d14SAndroid Build Coastguard Worker ot::TestChildIp6Address();
263*cfb92d14SAndroid Build Coastguard Worker printf("\nAll tests passed.\n");
264*cfb92d14SAndroid Build Coastguard Worker return 0;
265*cfb92d14SAndroid Build Coastguard Worker }
266