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 "openthread-core-config.h"
30 
31 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_DUA_NDPROXYING_ENABLE
32 
33 #include "test_platform.h"
34 
35 #include <openthread/config.h>
36 #include <openthread/ip6.h>
37 
38 #include "test_util.h"
39 #include "backbone_router/ndproxy_table.hpp"
40 #include "common/code_utils.hpp"
41 #include "instance/instance.hpp"
42 
43 namespace ot {
44 
45 static Instance *sInstance;
46 
47 using namespace ot::BackboneRouter;
48 
generateRandomIid(uint16_t aIndex)49 Ip6::InterfaceIdentifier generateRandomIid(uint16_t aIndex)
50 {
51     Ip6::InterfaceIdentifier iid;
52 
53     Random::NonCrypto::Fill(iid);
54     iid.mFields.m16[3] = aIndex;
55 
56     return iid;
57 }
58 
TestNdProxyTable(void)59 void TestNdProxyTable(void)
60 {
61     Error error;
62 
63     sInstance = testInitInstance();
64     VerifyOrQuit(sInstance != nullptr);
65 
66     BackboneRouter::NdProxyTable &table = sInstance->Get<BackboneRouter::NdProxyTable>();
67 
68     Ip6::InterfaceIdentifier existedAddressIid    = generateRandomIid(0);
69     Ip6::InterfaceIdentifier existedMeshLocalIid  = generateRandomIid(0);
70     Ip6::InterfaceIdentifier notExistAddressIid   = generateRandomIid(OPENTHREAD_CONFIG_NDPROXY_TABLE_ENTRY_NUM);
71     Ip6::InterfaceIdentifier notExistMeshLocalIid = generateRandomIid(OPENTHREAD_CONFIG_NDPROXY_TABLE_ENTRY_NUM);
72 
73     // Reregister address IID when there are enough room should succeed.
74     SuccessOrQuit(table.Register(existedAddressIid, existedMeshLocalIid, 0, nullptr));
75     VerifyOrQuit(table.IsRegistered(existedAddressIid));
76     VerifyOrQuit(!table.IsRegistered(notExistAddressIid));
77 
78     for (uint16_t i = 1; i < OPENTHREAD_CONFIG_NDPROXY_TABLE_ENTRY_NUM; i++)
79     {
80         Ip6::InterfaceIdentifier addressIid   = generateRandomIid(i);
81         Ip6::InterfaceIdentifier meshLocalIid = generateRandomIid(i);
82 
83         // Reregister address IID when there are enough room should succeed.
84         SuccessOrQuit(table.Register(addressIid, meshLocalIid, i, nullptr));
85 
86         VerifyOrQuit(table.IsRegistered(addressIid));
87 
88         // Reregister the same address IID should always succeed.
89         SuccessOrQuit(table.Register(addressIid, meshLocalIid, i, nullptr));
90 
91         // Register the same address IID with a different ML-IID should fail.
92         VerifyOrQuit(table.Register(addressIid, notExistMeshLocalIid, i, nullptr) == kErrorDuplicated);
93 
94         VerifyOrQuit(table.IsRegistered(addressIid));
95     }
96 
97     // Now the table is full, registering another IID should fail.
98     VerifyOrQuit(table.Register(notExistAddressIid, notExistMeshLocalIid, OPENTHREAD_CONFIG_NDPROXY_TABLE_ENTRY_NUM,
99                                 nullptr) == kErrorNoBufs);
100     VerifyOrQuit(!table.IsRegistered(notExistAddressIid));
101 }
102 
103 } // namespace ot
104 
main(void)105 int main(void)
106 {
107     ot::TestNdProxyTable();
108 
109     printf("\nAll tests passed.\n");
110     return 0;
111 }
112 
113 #else
main(void)114 int main(void) { return 0; }
115 #endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_DUA_NDPROXYING_ENABLE
116