Add a new CAPI and test for thread_br_get_ipv6_prefix
[platform/core/api/thread.git] / tests / unittest / thread-unittest-br.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <gtest/gtest.h>
18 #include <arpa/inet.h>
19
20 #include "thread.h"
21 #include "mocks/thread-mock-dummy.h"
22
23 class ThreadBRTest : public ::testing::Test
24 {
25 public:
26         enum class RouteInfo {
27                 kIpv6Prefix,
28         };
29
30         uint8_t ipv6Prefix[THREAD_IPV6_PREFIX_SIZE];
31         uint8_t ipv6PrefixLen;
32         uint16_t rloc16;
33         int8_t preference;
34         bool isStable;
35         bool isDeviceNextHop;
36
37         uint8_t onmeshIpv6Prefix[THREAD_IPV6_PREFIX_SIZE];
38         uint8_t onmeshIpv6PrefixLen;
39         bool preferred;
40         bool slaac;
41         bool dhcp;
42         bool configure;
43         bool defaultRoute;
44         bool onMesh;
45         bool stable;
46
47         thread_instance_h instance;
48         thread_route_info_h routeInfo;
49         thread_onmesh_prefix_info_h onmeshPrefix;
50
51 public:
52         static bool GetExternalRoutesCallback(int total,
53                 thread_route_info_h route_info, void *user_data)
54                 {
55                         RouteInfo routeInfo = static_cast<RouteInfo>(GPOINTER_TO_UINT(user_data));
56
57                         switch (routeInfo) {
58                         case RouteInfo::kIpv6Prefix:
59                                 {
60                                         uint8_t ipv6Prefix[THREAD_IPV6_ADDRESS_SIZE];
61                                         uint8_t ipv6PrefixLen;
62                                         EXPECT_EQ(THREAD_ERROR_NONE, thread_br_get_ipv6_prefix(route_info, ipv6Prefix, &ipv6PrefixLen));
63                                 }
64                                 break;
65                         default:
66                                 break;
67                         }
68
69                         return true;
70                 };
71
72 protected:
73         void SetUp() override
74         {
75                 thread_initialize();
76                 instance = nullptr;
77                 makeIpv6Prefix();
78                 ipv6PrefixLen = THREAD_IPV6_PREFIX_SIZE;
79                 rloc16 = 0xb801;
80                 preference = 0;
81                 isStable = TRUE;
82                 isDeviceNextHop = TRUE;
83                 makeOnmeshIpv6Prefix();
84                 onmeshIpv6PrefixLen = THREAD_IPV6_PREFIX_SIZE;
85                 preferred = true;
86                 slaac = true;
87                 dhcp = true;
88                 configure = true;
89                 defaultRoute = true;
90                 onMesh = true;
91                 stable = true;
92         }
93
94         void TearDown() override
95         {
96                 if (instance)
97                 {
98                         thread_br_disable(instance);
99                         thread_disable(instance);
100                 }
101                 thread_deinitialize();
102         }
103
104 private:
105         void makeIpv6Prefix()
106         {
107                 struct in6_addr result;
108                 inet_pton(AF_INET6, DUMMY_IPV6_ADDRESS, &result);
109                 memcpy(ipv6Prefix, result.s6_addr,
110                         THREAD_IPV6_PREFIX_SIZE * sizeof(uint8_t));
111         }
112
113         void makeOnmeshIpv6Prefix()
114         {
115                 struct in6_addr result;
116                 inet_pton(AF_INET6, DUMMY_IPV6_ADDRESS, &result);
117                 memcpy(ipv6Prefix, result.s6_addr,
118                         THREAD_IPV6_PREFIX_SIZE * sizeof(uint8_t));
119         }
120 };
121
122 TEST_F(ThreadBRTest, BREnableNotInitialized)
123 {
124         EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
125         EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED, thread_br_enable(instance));
126 }
127
128 TEST_F(ThreadBRTest, BREnableInvalidParameter)
129 {
130         EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER, thread_br_enable(instance));
131 }
132
133 TEST_F(ThreadBRTest, BREnableErrorNone)
134 {
135         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
136         EXPECT_EQ(THREAD_ERROR_NONE, thread_br_enable(instance));
137 }
138
139 TEST_F(ThreadBRTest, BRDisableNotInitialized)
140 {
141         EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
142         EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED, thread_br_disable(instance));
143 }
144
145 TEST_F(ThreadBRTest, BRDisableInvalidParameter)
146 {
147         EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER, thread_br_disable(instance));
148 }
149
150 TEST_F(ThreadBRTest, BRDisableErrorNone)
151 {
152         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
153         EXPECT_EQ(THREAD_ERROR_NONE, thread_br_enable(instance));
154         EXPECT_EQ(THREAD_ERROR_NONE, thread_br_disable(instance));
155 }
156
157 TEST_F(ThreadBRTest, BRGetExternalRoutesNotInitialized)
158 {
159         EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
160         EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED,
161                 thread_br_get_external_routes(instance, GetExternalRoutesCallback, nullptr));
162 }
163
164 TEST_F(ThreadBRTest, BRGetExternalRoutesInvalidParameter)
165 {
166         EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER,
167                 thread_br_get_external_routes(instance, GetExternalRoutesCallback, nullptr));
168 }
169
170 TEST_F(ThreadBRTest, BRGetExternalRoutesNotEnabled)
171 {
172         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
173         EXPECT_EQ(THREAD_ERROR_NOT_ENABLED,
174                 thread_br_get_external_routes(instance, GetExternalRoutesCallback, nullptr));
175 }
176
177 TEST_F(ThreadBRTest, BRGetExternalRoutesErrorNone)
178 {
179         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
180         EXPECT_EQ(THREAD_ERROR_NONE, thread_br_enable(instance));
181         EXPECT_EQ(THREAD_ERROR_NONE,
182                 thread_br_get_external_routes(instance, GetExternalRoutesCallback, nullptr));
183 }
184
185 TEST_F(ThreadBRTest, BRGetIpv6PrefixErrorNone)
186 {
187         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
188         EXPECT_EQ(THREAD_ERROR_NONE, thread_br_enable(instance));
189         EXPECT_EQ(THREAD_ERROR_NONE,
190                 thread_br_get_external_routes(instance, GetExternalRoutesCallback,
191                 GUINT_TO_POINTER(static_cast<guint>(RouteInfo::kIpv6Prefix))));
192 }
193
194 TEST_F(ThreadBRTest, BRAddExternalRouteNotInitialized)
195 {
196         EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
197         EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED,
198                 thread_br_add_external_route(instance, ipv6Prefix, ipv6PrefixLen,
199                 rloc16, preference, isStable, isDeviceNextHop, &routeInfo));
200 }
201
202 TEST_F(ThreadBRTest, BRAddExternalRouteInvalidParameter)
203 {
204         EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER,
205                 thread_br_add_external_route(instance, ipv6Prefix, ipv6PrefixLen,
206                 rloc16, preference, isStable, isDeviceNextHop, &routeInfo));
207 }
208
209 TEST_F(ThreadBRTest, BRAddExternalRouteNotEnabled)
210 {
211         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
212         EXPECT_EQ(THREAD_ERROR_NOT_ENABLED,
213                 thread_br_add_external_route(instance, ipv6Prefix, ipv6PrefixLen,
214                 rloc16, preference, isStable, isDeviceNextHop, &routeInfo));
215 }
216
217 TEST_F(ThreadBRTest, BRAddExternalRouteErrorNone)
218 {
219         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
220         EXPECT_EQ(THREAD_ERROR_NONE, thread_br_enable(instance));
221         EXPECT_EQ(THREAD_ERROR_NONE,
222                 thread_br_add_external_route(instance, ipv6Prefix, ipv6PrefixLen,
223                 rloc16, preference, isStable, isDeviceNextHop, &routeInfo));
224 }
225
226 TEST_F(ThreadBRTest, BRRemoveExternalRouteNotInitialized)
227 {
228         EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
229         EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED,
230                 thread_br_remove_external_route(instance, routeInfo));
231 }
232
233 TEST_F(ThreadBRTest, BRRemoveExternalRouteInvalidParameter)
234 {
235         EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER,
236                 thread_br_remove_external_route(instance, routeInfo));
237 }
238
239 TEST_F(ThreadBRTest, BRRemoveExternalRouteNotEnabled)
240 {
241         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
242         EXPECT_EQ(THREAD_ERROR_NOT_ENABLED,
243                 thread_br_remove_external_route(instance, routeInfo));
244 }
245
246 TEST_F(ThreadBRTest, BRRemoveExternalRouteErrorNone)
247 {
248         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
249         EXPECT_EQ(THREAD_ERROR_NONE, thread_br_enable(instance));
250         EXPECT_EQ(THREAD_ERROR_NONE,
251                 thread_br_add_external_route(instance, ipv6Prefix, ipv6PrefixLen,
252                 rloc16, preference, isStable, isDeviceNextHop, &routeInfo));
253         EXPECT_EQ(THREAD_ERROR_NONE,
254                 thread_br_remove_external_route(instance, routeInfo));
255 }
256
257 TEST_F(ThreadBRTest, BRAddOnmeshPrefixNotInitialized)
258 {
259         EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
260         EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED,
261                 thread_br_add_onmesh_prefix(instance, onmeshIpv6Prefix,
262                         onmeshIpv6PrefixLen, preference, preferred, slaac,
263                         dhcp, configure, defaultRoute, onMesh, stable, &onmeshPrefix));
264 }
265
266 TEST_F(ThreadBRTest, BRAddOnmeshPrefixInvalidParameter)
267 {
268         EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER,
269                 thread_br_add_onmesh_prefix(instance, onmeshIpv6Prefix,
270                         onmeshIpv6PrefixLen, preference, preferred, slaac,
271                         dhcp, configure, defaultRoute, onMesh, stable, &onmeshPrefix));
272 }
273
274 TEST_F(ThreadBRTest, BRAddOnmeshPrefixNotEnabled)
275 {
276         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
277         EXPECT_EQ(THREAD_ERROR_NOT_ENABLED,
278                 thread_br_add_onmesh_prefix(instance, onmeshIpv6Prefix,
279                         onmeshIpv6PrefixLen, preference, preferred, slaac,
280                         dhcp, configure, defaultRoute, onMesh, stable, &onmeshPrefix));
281 }
282
283 TEST_F(ThreadBRTest, BRAddOnmeshPrefixErrorNone)
284 {
285         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
286         EXPECT_EQ(THREAD_ERROR_NONE, thread_br_enable(instance));
287         EXPECT_EQ(THREAD_ERROR_NONE,
288                 thread_br_add_onmesh_prefix(instance, onmeshIpv6Prefix,
289                         onmeshIpv6PrefixLen, preference, preferred, slaac,
290                         dhcp, configure, defaultRoute, onMesh, stable, &onmeshPrefix));
291 }
292
293 TEST_F(ThreadBRTest, BRRemoveOnmeshPrefixNotInitialized)
294 {
295         EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
296         EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED,
297                 thread_br_remove_onmesh_prefix(instance, onmeshPrefix));
298 }
299
300 TEST_F(ThreadBRTest, BRRemoveOnmeshPrefixInvalidParameter)
301 {
302         EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER,
303                 thread_br_remove_onmesh_prefix(instance, onmeshPrefix));
304 }
305
306 TEST_F(ThreadBRTest, BRRemoveOnmeshPrefixNotEnabled)
307 {
308         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
309         EXPECT_EQ(THREAD_ERROR_NOT_ENABLED,
310                 thread_br_remove_onmesh_prefix(instance, onmeshPrefix));
311 }
312
313 TEST_F(ThreadBRTest, BRRemoveOnmeshPrefixErrorNone)
314 {
315         EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
316         EXPECT_EQ(THREAD_ERROR_NONE, thread_br_enable(instance));
317         EXPECT_EQ(THREAD_ERROR_NONE,
318                 thread_br_add_onmesh_prefix(instance, onmeshIpv6Prefix,
319                         onmeshIpv6PrefixLen, preference, preferred, slaac,
320                         dhcp, configure, defaultRoute, onMesh, stable, &onmeshPrefix));
321         EXPECT_EQ(THREAD_ERROR_NONE,
322                 thread_br_remove_onmesh_prefix(instance, onmeshPrefix));
323 }