2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
18 #include <arpa/inet.h>
21 #include "mocks/thread-mock-dummy.h"
23 class ThreadBRTest : public ::testing::Test
26 enum class RouteInfo {
30 uint8_t ipv6Prefix[THREAD_IPV6_PREFIX_SIZE];
31 uint8_t ipv6PrefixLen;
37 uint8_t onmeshIpv6Prefix[THREAD_IPV6_PREFIX_SIZE];
38 uint8_t onmeshIpv6PrefixLen;
47 thread_instance_h instance;
48 thread_route_info_h routeInfo;
49 thread_onmesh_prefix_info_h onmeshPrefix;
52 static bool GetExternalRoutesCallback(int total,
53 thread_route_info_h route_info, void *user_data)
55 RouteInfo routeInfo = static_cast<RouteInfo>(GPOINTER_TO_UINT(user_data));
58 case RouteInfo::kIpv6Prefix:
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));
78 ipv6PrefixLen = THREAD_IPV6_PREFIX_SIZE;
82 isDeviceNextHop = TRUE;
83 makeOnmeshIpv6Prefix();
84 onmeshIpv6PrefixLen = THREAD_IPV6_PREFIX_SIZE;
94 void TearDown() override
98 thread_br_disable(instance);
99 thread_disable(instance);
101 thread_deinitialize();
105 void makeIpv6Prefix()
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));
113 void makeOnmeshIpv6Prefix()
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));
122 TEST_F(ThreadBRTest, BREnableNotInitialized)
124 EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
125 EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED, thread_br_enable(instance));
128 TEST_F(ThreadBRTest, BREnableInvalidParameter)
130 EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER, thread_br_enable(instance));
133 TEST_F(ThreadBRTest, BREnableErrorNone)
135 EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
136 EXPECT_EQ(THREAD_ERROR_NONE, thread_br_enable(instance));
139 TEST_F(ThreadBRTest, BRDisableNotInitialized)
141 EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
142 EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED, thread_br_disable(instance));
145 TEST_F(ThreadBRTest, BRDisableInvalidParameter)
147 EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER, thread_br_disable(instance));
150 TEST_F(ThreadBRTest, BRDisableErrorNone)
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));
157 TEST_F(ThreadBRTest, BRGetExternalRoutesNotInitialized)
159 EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
160 EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED,
161 thread_br_get_external_routes(instance, GetExternalRoutesCallback, nullptr));
164 TEST_F(ThreadBRTest, BRGetExternalRoutesInvalidParameter)
166 EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER,
167 thread_br_get_external_routes(instance, GetExternalRoutesCallback, nullptr));
170 TEST_F(ThreadBRTest, BRGetExternalRoutesNotEnabled)
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));
177 TEST_F(ThreadBRTest, BRGetExternalRoutesErrorNone)
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));
185 TEST_F(ThreadBRTest, BRGetIpv6PrefixErrorNone)
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))));
194 TEST_F(ThreadBRTest, BRAddExternalRouteNotInitialized)
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));
202 TEST_F(ThreadBRTest, BRAddExternalRouteInvalidParameter)
204 EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER,
205 thread_br_add_external_route(instance, ipv6Prefix, ipv6PrefixLen,
206 rloc16, preference, isStable, isDeviceNextHop, &routeInfo));
209 TEST_F(ThreadBRTest, BRAddExternalRouteNotEnabled)
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));
217 TEST_F(ThreadBRTest, BRAddExternalRouteErrorNone)
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));
226 TEST_F(ThreadBRTest, BRRemoveExternalRouteNotInitialized)
228 EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
229 EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED,
230 thread_br_remove_external_route(instance, routeInfo));
233 TEST_F(ThreadBRTest, BRRemoveExternalRouteInvalidParameter)
235 EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER,
236 thread_br_remove_external_route(instance, routeInfo));
239 TEST_F(ThreadBRTest, BRRemoveExternalRouteNotEnabled)
241 EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
242 EXPECT_EQ(THREAD_ERROR_NOT_ENABLED,
243 thread_br_remove_external_route(instance, routeInfo));
246 TEST_F(ThreadBRTest, BRRemoveExternalRouteErrorNone)
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));
257 TEST_F(ThreadBRTest, BRAddOnmeshPrefixNotInitialized)
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));
266 TEST_F(ThreadBRTest, BRAddOnmeshPrefixInvalidParameter)
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));
274 TEST_F(ThreadBRTest, BRAddOnmeshPrefixNotEnabled)
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));
283 TEST_F(ThreadBRTest, BRAddOnmeshPrefixErrorNone)
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));
293 TEST_F(ThreadBRTest, BRRemoveOnmeshPrefixNotInitialized)
295 EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
296 EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED,
297 thread_br_remove_onmesh_prefix(instance, onmeshPrefix));
300 TEST_F(ThreadBRTest, BRRemoveOnmeshPrefixInvalidParameter)
302 EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER,
303 thread_br_remove_onmesh_prefix(instance, onmeshPrefix));
306 TEST_F(ThreadBRTest, BRRemoveOnmeshPrefixNotEnabled)
308 EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
309 EXPECT_EQ(THREAD_ERROR_NOT_ENABLED,
310 thread_br_remove_onmesh_prefix(instance, onmeshPrefix));
313 TEST_F(ThreadBRTest, BRRemoveOnmeshPrefixErrorNone)
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));