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/srp_server.h>
32
33 #include "test_platform.h"
34 #include "test_util.h"
35 #include "common/code_utils.hpp"
36 #include "lib/spinel/spinel_buffer.hpp"
37 #include "lib/spinel/spinel_encoder.hpp"
38 #include "ncp/ncp_base.hpp"
39
40 #if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
41
42 namespace ot {
43
44 constexpr uint16_t kMaxSpinelBufferSize = 2048;
45
GenerateSpinelSrpServerFrame(spinel_prop_key_t aProp,bool aEnable,uint8_t * aBuf,uint16_t & aLen)46 static otError GenerateSpinelSrpServerFrame(spinel_prop_key_t aProp, bool aEnable, uint8_t *aBuf, uint16_t &aLen)
47 {
48 otError error = OT_ERROR_NONE;
49 uint8_t buf[kMaxSpinelBufferSize];
50 Spinel::Buffer ncpBuffer(buf, kMaxSpinelBufferSize);
51 Spinel::Encoder encoder(ncpBuffer);
52
53 uint8_t header = SPINEL_HEADER_FLAG | 0 /* Iid */ | 1 /* Tid */;
54 SuccessOrExit(error = encoder.BeginFrame(header, SPINEL_CMD_PROP_VALUE_SET, aProp));
55 SuccessOrExit(error = encoder.WriteBool(aEnable));
56 SuccessOrExit(error = encoder.EndFrame());
57
58 SuccessOrExit(ncpBuffer.OutFrameBegin());
59 aLen = ncpBuffer.OutFrameGetLength();
60 VerifyOrExit(ncpBuffer.OutFrameRead(aLen, aBuf) == aLen, error = OT_ERROR_FAILED);
61
62 exit:
63 return error;
64 }
65
TestNcpSrpServerSetEnabled(void)66 void TestNcpSrpServerSetEnabled(void)
67 {
68 Instance *instance = static_cast<Instance *>(testInitInstance());
69 Ncp::NcpBase ncpBase(instance);
70
71 uint8_t recvBuf[kMaxSpinelBufferSize];
72 uint16_t recvLen;
73 otSrpServerState state;
74
75 state = otSrpServerGetState(instance);
76 VerifyOrQuit(state == OT_SRP_SERVER_STATE_DISABLED);
77
78 SuccessOrQuit(GenerateSpinelSrpServerFrame(SPINEL_PROP_SRP_SERVER_ENABLED, true, recvBuf, recvLen));
79 ncpBase.HandleReceive(recvBuf, recvLen);
80 state = otSrpServerGetState(instance);
81 VerifyOrQuit(state == OT_SRP_SERVER_STATE_STOPPED);
82
83 SuccessOrQuit(GenerateSpinelSrpServerFrame(SPINEL_PROP_SRP_SERVER_ENABLED, false, recvBuf, recvLen));
84 ncpBase.HandleReceive(recvBuf, recvLen);
85 state = otSrpServerGetState(instance);
86 VerifyOrQuit(state == OT_SRP_SERVER_STATE_DISABLED);
87
88 printf("TestNcpSrpServerSetEnabled passed.\n");
89 }
90
91 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
TestNcpSrpServerSetAutoEnableMode(void)92 void TestNcpSrpServerSetAutoEnableMode(void)
93 {
94 Instance *instance = static_cast<Instance *>(testInitInstance());
95 Ncp::NcpBase ncpBase(instance);
96
97 uint8_t recvBuf[kMaxSpinelBufferSize];
98 uint16_t recvLen;
99 bool isAutoEnableMode;
100
101 isAutoEnableMode = otSrpServerIsAutoEnableMode(instance);
102 VerifyOrQuit(isAutoEnableMode == false);
103
104 SuccessOrQuit(GenerateSpinelSrpServerFrame(SPINEL_PROP_SRP_SERVER_AUTO_ENABLE_MODE, true, recvBuf, recvLen));
105 ncpBase.HandleReceive(recvBuf, recvLen);
106 isAutoEnableMode = otSrpServerIsAutoEnableMode(instance);
107 VerifyOrQuit(isAutoEnableMode == true);
108
109 SuccessOrQuit(GenerateSpinelSrpServerFrame(SPINEL_PROP_SRP_SERVER_AUTO_ENABLE_MODE, false, recvBuf, recvLen));
110 ncpBase.HandleReceive(recvBuf, recvLen);
111 isAutoEnableMode = otSrpServerIsAutoEnableMode(instance);
112 VerifyOrQuit(isAutoEnableMode == false);
113
114 printf("TestNcpSrpServerSetAutoEnableMode passed.\n");
115 }
116 #endif
117
118 } // namespace ot
119
120 #endif // OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
121
main(void)122 int main(void)
123 {
124 #if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
125 ot::TestNcpSrpServerSetEnabled();
126 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
127 ot::TestNcpSrpServerSetAutoEnableMode();
128 #endif
129 #endif
130 printf("All tests passed\n");
131 return 0;
132 }
133