1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker * Copyright (c) 2019, 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 <stdarg.h>
30*cfb92d14SAndroid Build Coastguard Worker
31*cfb92d14SAndroid Build Coastguard Worker #include "test_platform.h"
32*cfb92d14SAndroid Build Coastguard Worker
33*cfb92d14SAndroid Build Coastguard Worker #include <openthread/config.h>
34*cfb92d14SAndroid Build Coastguard Worker
35*cfb92d14SAndroid Build Coastguard Worker #include "common/code_utils.hpp"
36*cfb92d14SAndroid Build Coastguard Worker #include "common/debug.hpp"
37*cfb92d14SAndroid Build Coastguard Worker #include "instance/instance.hpp"
38*cfb92d14SAndroid Build Coastguard Worker #include "net/netif.hpp"
39*cfb92d14SAndroid Build Coastguard Worker
40*cfb92d14SAndroid Build Coastguard Worker #include "test_util.h"
41*cfb92d14SAndroid Build Coastguard Worker
42*cfb92d14SAndroid Build Coastguard Worker namespace ot {
43*cfb92d14SAndroid Build Coastguard Worker
44*cfb92d14SAndroid Build Coastguard Worker class TestNetif : public Ip6::Netif
45*cfb92d14SAndroid Build Coastguard Worker {
46*cfb92d14SAndroid Build Coastguard Worker public:
TestNetif(Instance & aInstance)47*cfb92d14SAndroid Build Coastguard Worker explicit TestNetif(Instance &aInstance)
48*cfb92d14SAndroid Build Coastguard Worker : Ip6::Netif(aInstance)
49*cfb92d14SAndroid Build Coastguard Worker {
50*cfb92d14SAndroid Build Coastguard Worker }
51*cfb92d14SAndroid Build Coastguard Worker
52*cfb92d14SAndroid Build Coastguard Worker // Provide `protected` methods in `Netif` as `public` from `TestNetif`
53*cfb92d14SAndroid Build Coastguard Worker // so that we can verify their behavior in this test
SubscribeAllNodesMulticast(void)54*cfb92d14SAndroid Build Coastguard Worker void SubscribeAllNodesMulticast(void) { Ip6::Netif::SubscribeAllNodesMulticast(); }
UnsubscribeAllNodesMulticast(void)55*cfb92d14SAndroid Build Coastguard Worker void UnsubscribeAllNodesMulticast(void) { Ip6::Netif::UnsubscribeAllNodesMulticast(); }
56*cfb92d14SAndroid Build Coastguard Worker };
57*cfb92d14SAndroid Build Coastguard Worker
58*cfb92d14SAndroid Build Coastguard Worker // This function verifies the multicast addresses on Netif matches the list of given addresses.
VerifyMulticastAddressList(const Ip6::Netif & aNetif,Ip6::Address aAddresses[],uint8_t aLength)59*cfb92d14SAndroid Build Coastguard Worker void VerifyMulticastAddressList(const Ip6::Netif &aNetif, Ip6::Address aAddresses[], uint8_t aLength)
60*cfb92d14SAndroid Build Coastguard Worker {
61*cfb92d14SAndroid Build Coastguard Worker uint8_t count = 0;
62*cfb92d14SAndroid Build Coastguard Worker
63*cfb92d14SAndroid Build Coastguard Worker for (uint8_t i = 0; i < aLength; i++)
64*cfb92d14SAndroid Build Coastguard Worker {
65*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aNetif.IsMulticastSubscribed(aAddresses[i]));
66*cfb92d14SAndroid Build Coastguard Worker }
67*cfb92d14SAndroid Build Coastguard Worker
68*cfb92d14SAndroid Build Coastguard Worker for (const Ip6::Netif::MulticastAddress &addr : aNetif.GetMulticastAddresses())
69*cfb92d14SAndroid Build Coastguard Worker {
70*cfb92d14SAndroid Build Coastguard Worker bool didFind = false;
71*cfb92d14SAndroid Build Coastguard Worker
72*cfb92d14SAndroid Build Coastguard Worker for (uint8_t i = 0; i < aLength; i++)
73*cfb92d14SAndroid Build Coastguard Worker {
74*cfb92d14SAndroid Build Coastguard Worker if (addr.GetAddress() == aAddresses[i])
75*cfb92d14SAndroid Build Coastguard Worker {
76*cfb92d14SAndroid Build Coastguard Worker didFind = true;
77*cfb92d14SAndroid Build Coastguard Worker break;
78*cfb92d14SAndroid Build Coastguard Worker }
79*cfb92d14SAndroid Build Coastguard Worker }
80*cfb92d14SAndroid Build Coastguard Worker
81*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(didFind, "Netif multicast address is missing from expected address list");
82*cfb92d14SAndroid Build Coastguard Worker count++;
83*cfb92d14SAndroid Build Coastguard Worker }
84*cfb92d14SAndroid Build Coastguard Worker
85*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(count == aLength, "Expected address is missing from Netif address list");
86*cfb92d14SAndroid Build Coastguard Worker }
87*cfb92d14SAndroid Build Coastguard Worker
TestNetifMulticastAddresses(void)88*cfb92d14SAndroid Build Coastguard Worker void TestNetifMulticastAddresses(void)
89*cfb92d14SAndroid Build Coastguard Worker {
90*cfb92d14SAndroid Build Coastguard Worker const uint8_t kMaxAddresses = 8;
91*cfb92d14SAndroid Build Coastguard Worker
92*cfb92d14SAndroid Build Coastguard Worker Instance *instance = testInitInstance();
93*cfb92d14SAndroid Build Coastguard Worker TestNetif netif(*instance);
94*cfb92d14SAndroid Build Coastguard Worker Ip6::Address addresses[kMaxAddresses];
95*cfb92d14SAndroid Build Coastguard Worker
96*cfb92d14SAndroid Build Coastguard Worker Ip6::Address address;
97*cfb92d14SAndroid Build Coastguard Worker Ip6::Netif::MulticastAddress netifAddress;
98*cfb92d14SAndroid Build Coastguard Worker
99*cfb92d14SAndroid Build Coastguard Worker const char *kLinkLocalAllNodes = "ff02::01";
100*cfb92d14SAndroid Build Coastguard Worker const char *kRealmLocalAllNodes = "ff03::01";
101*cfb92d14SAndroid Build Coastguard Worker const char *kRealmLocalAllMpl = "ff03::fc";
102*cfb92d14SAndroid Build Coastguard Worker const char *kLinkLocalAllRouters = "ff02::02";
103*cfb92d14SAndroid Build Coastguard Worker const char *kRealmLocalAllRouters = "ff03::02";
104*cfb92d14SAndroid Build Coastguard Worker const char *kTestAddress1 = "ff02::114";
105*cfb92d14SAndroid Build Coastguard Worker const char *kTestAddress2 = "ff03::114";
106*cfb92d14SAndroid Build Coastguard Worker const char *kTestAddress3 = "ff04::114";
107*cfb92d14SAndroid Build Coastguard Worker
108*cfb92d14SAndroid Build Coastguard Worker IgnoreError(addresses[0].FromString(kLinkLocalAllRouters));
109*cfb92d14SAndroid Build Coastguard Worker IgnoreError(addresses[1].FromString(kRealmLocalAllRouters));
110*cfb92d14SAndroid Build Coastguard Worker IgnoreError(addresses[2].FromString(kLinkLocalAllNodes));
111*cfb92d14SAndroid Build Coastguard Worker IgnoreError(addresses[3].FromString(kRealmLocalAllNodes));
112*cfb92d14SAndroid Build Coastguard Worker IgnoreError(addresses[4].FromString(kRealmLocalAllMpl));
113*cfb92d14SAndroid Build Coastguard Worker IgnoreError(addresses[5].FromString(kTestAddress1));
114*cfb92d14SAndroid Build Coastguard Worker IgnoreError(addresses[6].FromString(kTestAddress2));
115*cfb92d14SAndroid Build Coastguard Worker IgnoreError(addresses[7].FromString(kTestAddress3));
116*cfb92d14SAndroid Build Coastguard Worker
117*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, addresses, 0);
118*cfb92d14SAndroid Build Coastguard Worker
119*cfb92d14SAndroid Build Coastguard Worker netif.SubscribeAllNodesMulticast();
120*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 3);
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard Worker netif.SubscribeAllNodesMulticast();
123*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 3);
124*cfb92d14SAndroid Build Coastguard Worker
125*cfb92d14SAndroid Build Coastguard Worker netif.SubscribeAllRoutersMulticast();
126*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[0], 5);
127*cfb92d14SAndroid Build Coastguard Worker
128*cfb92d14SAndroid Build Coastguard Worker netif.SubscribeAllRoutersMulticast();
129*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[0], 5);
130*cfb92d14SAndroid Build Coastguard Worker
131*cfb92d14SAndroid Build Coastguard Worker netif.UnsubscribeAllRoutersMulticast();
132*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 3);
133*cfb92d14SAndroid Build Coastguard Worker
134*cfb92d14SAndroid Build Coastguard Worker netif.UnsubscribeAllRoutersMulticast();
135*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 3);
136*cfb92d14SAndroid Build Coastguard Worker
137*cfb92d14SAndroid Build Coastguard Worker IgnoreError(netifAddress.GetAddress().FromString(kTestAddress1));
138*cfb92d14SAndroid Build Coastguard Worker netif.SubscribeMulticast(netifAddress);
139*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 4);
140*cfb92d14SAndroid Build Coastguard Worker
141*cfb92d14SAndroid Build Coastguard Worker netif.SubscribeMulticast(netifAddress);
142*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 4);
143*cfb92d14SAndroid Build Coastguard Worker
144*cfb92d14SAndroid Build Coastguard Worker netif.UnsubscribeAllNodesMulticast();
145*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[5], 1);
146*cfb92d14SAndroid Build Coastguard Worker
147*cfb92d14SAndroid Build Coastguard Worker netif.UnsubscribeAllNodesMulticast();
148*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[5], 1);
149*cfb92d14SAndroid Build Coastguard Worker
150*cfb92d14SAndroid Build Coastguard Worker IgnoreError(address.FromString(kTestAddress2));
151*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(netif.SubscribeExternalMulticast(address));
152*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[5], 2);
153*cfb92d14SAndroid Build Coastguard Worker
154*cfb92d14SAndroid Build Coastguard Worker netif.SubscribeAllNodesMulticast();
155*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 5);
156*cfb92d14SAndroid Build Coastguard Worker
157*cfb92d14SAndroid Build Coastguard Worker netif.SubscribeAllNodesMulticast();
158*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 5);
159*cfb92d14SAndroid Build Coastguard Worker
160*cfb92d14SAndroid Build Coastguard Worker netif.SubscribeAllRoutersMulticast();
161*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[0], 7);
162*cfb92d14SAndroid Build Coastguard Worker
163*cfb92d14SAndroid Build Coastguard Worker netif.SubscribeAllRoutersMulticast();
164*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[0], 7);
165*cfb92d14SAndroid Build Coastguard Worker
166*cfb92d14SAndroid Build Coastguard Worker IgnoreError(address.FromString(kTestAddress3));
167*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(netif.SubscribeExternalMulticast(address));
168*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[0], 8);
169*cfb92d14SAndroid Build Coastguard Worker
170*cfb92d14SAndroid Build Coastguard Worker IgnoreError(address.FromString(kTestAddress1)); // same as netifAddress (internal)
171*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(netif.UnsubscribeExternalMulticast(address) == kErrorRejected,
172*cfb92d14SAndroid Build Coastguard Worker "UnsubscribeExternalMulticast() did not fail when address was not external");
173*cfb92d14SAndroid Build Coastguard Worker
174*cfb92d14SAndroid Build Coastguard Worker IgnoreError(address.FromString(kRealmLocalAllMpl));
175*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(netif.UnsubscribeExternalMulticast(address) == kErrorRejected,
176*cfb92d14SAndroid Build Coastguard Worker "UnsubscribeExternalMulticast() did not fail when address was fixed address");
177*cfb92d14SAndroid Build Coastguard Worker
178*cfb92d14SAndroid Build Coastguard Worker netif.UnsubscribeAllRoutersMulticast();
179*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 6);
180*cfb92d14SAndroid Build Coastguard Worker
181*cfb92d14SAndroid Build Coastguard Worker netif.UnsubscribeAllRoutersMulticast();
182*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 6);
183*cfb92d14SAndroid Build Coastguard Worker
184*cfb92d14SAndroid Build Coastguard Worker netif.UnsubscribeAllExternalMulticastAddresses();
185*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 4);
186*cfb92d14SAndroid Build Coastguard Worker
187*cfb92d14SAndroid Build Coastguard Worker netif.UnsubscribeMulticast(netifAddress);
188*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 3);
189*cfb92d14SAndroid Build Coastguard Worker
190*cfb92d14SAndroid Build Coastguard Worker netif.UnsubscribeMulticast(netifAddress);
191*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, &addresses[2], 3);
192*cfb92d14SAndroid Build Coastguard Worker
193*cfb92d14SAndroid Build Coastguard Worker netif.UnsubscribeAllNodesMulticast();
194*cfb92d14SAndroid Build Coastguard Worker VerifyMulticastAddressList(netif, nullptr, 0);
195*cfb92d14SAndroid Build Coastguard Worker
196*cfb92d14SAndroid Build Coastguard Worker // The first five elements in `addresses[]` are the default/fixed addresses:
197*cfb92d14SAndroid Build Coastguard Worker // kLinkLocalAllRouters, kRealmLocalAllRouters, kLinkLocalAllNodes,
198*cfb92d14SAndroid Build Coastguard Worker // kRealmLocalAllNodes, and kRealmLocalAllMpl. Verify that we cannot add
199*cfb92d14SAndroid Build Coastguard Worker // any of them as an external multicast address.
200*cfb92d14SAndroid Build Coastguard Worker
201*cfb92d14SAndroid Build Coastguard Worker for (uint8_t i = 0; i < 5; i++)
202*cfb92d14SAndroid Build Coastguard Worker {
203*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(netif.SubscribeExternalMulticast(addresses[i]) == kErrorRejected,
204*cfb92d14SAndroid Build Coastguard Worker "SubscribeExternalMulticast() did not fail when address was a default/fixed address");
205*cfb92d14SAndroid Build Coastguard Worker }
206*cfb92d14SAndroid Build Coastguard Worker }
207*cfb92d14SAndroid Build Coastguard Worker
208*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
209*cfb92d14SAndroid Build Coastguard Worker
main(void)210*cfb92d14SAndroid Build Coastguard Worker int main(void)
211*cfb92d14SAndroid Build Coastguard Worker {
212*cfb92d14SAndroid Build Coastguard Worker ot::TestNetifMulticastAddresses();
213*cfb92d14SAndroid Build Coastguard Worker printf("All tests passed\n");
214*cfb92d14SAndroid Build Coastguard Worker return 0;
215*cfb92d14SAndroid Build Coastguard Worker }
216