1 /*
2 * Copyright (c) 2024, 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 <stdio.h>
30
31 #include <openthread/border_routing.h>
32 #include <openthread/platform/infra_if.h>
33
34 #include "test_platform.h"
35 #include "test_util.h"
36 #include "common/code_utils.hpp"
37 #include "lib/spinel/spinel_buffer.hpp"
38 #include "lib/spinel/spinel_encoder.hpp"
39 #include "ncp/ncp_base.hpp"
40
41 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE && OPENTHREAD_CONFIG_NCP_INFRA_IF_ENABLE
42
43 namespace ot {
44
45 constexpr uint16_t kMaxSpinelBufferSize = 2048;
46
GenerateSpinelInfraIfStateFrame(uint32_t akInfraIfIndex,bool aIsRunning,const otIp6Address * aAddrs,uint8_t aAddrCount,uint8_t * aBuf,uint16_t & aLen)47 static otError GenerateSpinelInfraIfStateFrame(uint32_t akInfraIfIndex,
48 bool aIsRunning,
49 const otIp6Address *aAddrs,
50 uint8_t aAddrCount,
51 uint8_t *aBuf,
52 uint16_t &aLen)
53 {
54 otError error = OT_ERROR_NONE;
55 uint8_t buf[kMaxSpinelBufferSize];
56 Spinel::Buffer ncpBuffer(buf, kMaxSpinelBufferSize);
57 Spinel::Encoder encoder(ncpBuffer);
58
59 uint8_t header = SPINEL_HEADER_FLAG | 0 /* Iid */ | 1 /* Tid */;
60 SuccessOrExit(error = encoder.BeginFrame(header, SPINEL_CMD_PROP_VALUE_SET, SPINEL_PROP_INFRA_IF_STATE));
61 SuccessOrExit(error = encoder.WriteUint32(akInfraIfIndex));
62 SuccessOrExit(error = encoder.WriteBool(true));
63 for (uint8_t i = 0; i < aAddrCount; i++)
64 {
65 SuccessOrExit(error = encoder.WriteIp6Address(aAddrs[i]));
66 }
67 SuccessOrExit(error = encoder.EndFrame());
68
69 SuccessOrExit(ncpBuffer.OutFrameBegin());
70 aLen = ncpBuffer.OutFrameGetLength();
71 VerifyOrExit(ncpBuffer.OutFrameRead(aLen, aBuf) == aLen, error = OT_ERROR_FAILED);
72
73 exit:
74 return error;
75 }
76
TestNcpInfraIfSetUp(void)77 void TestNcpInfraIfSetUp(void)
78 {
79 Instance *instance = static_cast<Instance *>(testInitInstance());
80 Ncp::NcpBase ncpBase(instance);
81
82 uint8_t recvBuf[kMaxSpinelBufferSize];
83 uint16_t recvLen;
84 constexpr uint32_t kInfraIfIndex = 1;
85
86 const otIp6Address infraIfAddresses[] = {
87 {0xfd, 0x35, 0x7a, 0x7d, 0x0f, 0x16, 0xe7, 0xe3, 0xc9, 0x79, 0x59, 0x29, 0xc8, 0xc2, 0xa3, 0x7b},
88 };
89
90 VerifyOrQuit(otBorderRoutingGetState(instance) == OT_BORDER_ROUTING_STATE_UNINITIALIZED);
91
92 SuccessOrQuit(GenerateSpinelInfraIfStateFrame(kInfraIfIndex, true /* IsRunning */, infraIfAddresses,
93 sizeof(infraIfAddresses) / sizeof(infraIfAddresses[0]), recvBuf,
94 recvLen));
95 ncpBase.HandleReceive(recvBuf, recvLen);
96 VerifyOrQuit(otBorderRoutingGetState(instance) == OT_BORDER_ROUTING_STATE_STOPPED);
97 VerifyOrQuit(otPlatInfraIfHasAddress(kInfraIfIndex, &infraIfAddresses[0]));
98 VerifyOrQuit(!otPlatInfraIfHasAddress(kInfraIfIndex + 100, &infraIfAddresses[0]));
99
100 SuccessOrQuit(
101 GenerateSpinelInfraIfStateFrame(kInfraIfIndex, true /* IsRunning */, infraIfAddresses, 0, recvBuf, recvLen));
102 ncpBase.HandleReceive(recvBuf, recvLen);
103 VerifyOrQuit(otBorderRoutingGetState(instance) == OT_BORDER_ROUTING_STATE_STOPPED);
104 VerifyOrQuit(!otPlatInfraIfHasAddress(kInfraIfIndex, &infraIfAddresses[0]));
105
106 printf("Test Ncp Infra If SetUp passed.\n");
107 }
108
TestNcpInfraIfUpdate(void)109 void TestNcpInfraIfUpdate(void)
110 {
111 Instance *instance = static_cast<Instance *>(testInitInstance());
112 Ncp::NcpBase ncpBase(instance);
113
114 uint8_t recvBuf[kMaxSpinelBufferSize];
115 uint16_t recvLen;
116 constexpr uint32_t kInfraIfIndex1 = 1;
117 constexpr uint32_t kInfraIfIndex2 = 2;
118
119 const otIp6Address infraIfAddresses[] = {
120 {0xfd, 0x35, 0x7a, 0x7d, 0x0f, 0x16, 0xe7, 0xe3, 0xc9, 0x79, 0x59, 0x29, 0xc8, 0xc2, 0xa3, 0x7b},
121 {0xfd, 0x35, 0x7a, 0x7d, 0x0f, 0x16, 0xe7, 0xe3, 0x7b, 0xa3, 0xc2, 0xc8, 0x29, 0x59, 0x79, 0xc9},
122 };
123
124 SuccessOrQuit(
125 GenerateSpinelInfraIfStateFrame(kInfraIfIndex1, true /* IsRunning */, infraIfAddresses, 1, recvBuf, recvLen));
126 ncpBase.HandleReceive(recvBuf, recvLen);
127 VerifyOrQuit(otPlatInfraIfHasAddress(kInfraIfIndex1, &infraIfAddresses[0]));
128 VerifyOrQuit(!otPlatInfraIfHasAddress(kInfraIfIndex1, &infraIfAddresses[1]));
129
130 SuccessOrQuit(
131 GenerateSpinelInfraIfStateFrame(kInfraIfIndex1, true /* IsRunning */, infraIfAddresses, 2, recvBuf, recvLen));
132 ncpBase.HandleReceive(recvBuf, recvLen);
133 VerifyOrQuit(otPlatInfraIfHasAddress(kInfraIfIndex1, &infraIfAddresses[0]));
134 VerifyOrQuit(otPlatInfraIfHasAddress(kInfraIfIndex1, &infraIfAddresses[1]));
135
136 SuccessOrQuit(
137 GenerateSpinelInfraIfStateFrame(kInfraIfIndex2, true /* IsRunning */, infraIfAddresses, 2, recvBuf, recvLen));
138 ncpBase.HandleReceive(recvBuf, recvLen);
139 VerifyOrQuit(!otPlatInfraIfHasAddress(kInfraIfIndex1, &infraIfAddresses[0]));
140 VerifyOrQuit(!otPlatInfraIfHasAddress(kInfraIfIndex1, &infraIfAddresses[1]));
141 VerifyOrQuit(otPlatInfraIfHasAddress(kInfraIfIndex2, &infraIfAddresses[0]));
142 VerifyOrQuit(otPlatInfraIfHasAddress(kInfraIfIndex2, &infraIfAddresses[1]));
143 }
144
145 } // namespace ot
146
147 #endif // OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE && OPENTHREAD_CONFIG_NCP_INFRA_IF_ENABLE
148
main(void)149 int main(void)
150 {
151 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE && OPENTHREAD_CONFIG_NCP_INFRA_IF_ENABLE
152 ot::TestNcpInfraIfSetUp();
153 ot::TestNcpInfraIfUpdate();
154 #endif
155 printf("All tests passed\n");
156 return 0;
157 }
158