Skip to content

Commit 1abe095

Browse files
JordanYateskartben
authored andcommitted
tests: net: conn_mgr_nsos: added
Add basic tests for the NSOS Connectivity Manager implementation. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent 2203a57 commit 1abe095

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(test_conn_mgr_nsos)
6+
7+
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/subsys/net/ip)
8+
target_sources(app PRIVATE src/main.c)

tests/net/conn_mgr_nsos/prj.conf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CONFIG_NETWORKING=y
2+
CONFIG_NET_TEST=y
3+
CONFIG_NET_IPV6=y
4+
CONFIG_NET_IPV4=y
5+
CONFIG_NET_MAX_CONTEXTS=4
6+
CONFIG_NET_L2_DUMMY=y
7+
CONFIG_NET_LOG=y
8+
CONFIG_NET_CONNECTION_MANAGER=y
9+
CONFIG_ENTROPY_GENERATOR=y
10+
CONFIG_TEST_RANDOM_GENERATOR=y
11+
CONFIG_ZTEST=y
12+
13+
CONFIG_ETH_NATIVE_POSIX=n
14+
CONFIG_NET_DRIVERS=y
15+
CONFIG_NET_SOCKETS=y
16+
CONFIG_NET_SOCKETS_OFFLOAD=y
17+
CONFIG_NET_NATIVE_OFFLOADED_SOCKETS=y
18+
CONFIG_NET_NATIVE_OFFLOADED_SOCKETS_CONNECTIVITY_SIM=y
19+
20+
CONFIG_HEAP_MEM_POOL_SIZE=1024

tests/net/conn_mgr_nsos/src/main.c

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Copyright (c) 2025 Embeint Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <string.h>
8+
9+
#include <zephyr/ztest.h>
10+
#include <zephyr/net/net_if.h>
11+
#include <zephyr/net/net_event.h>
12+
#include <zephyr/net/conn_mgr_monitor.h>
13+
#include <zephyr/net/conn_mgr_connectivity.h>
14+
15+
K_SEM_DEFINE(l4_connected, 0, 1);
16+
K_SEM_DEFINE(l4_disconnected, 0, 1);
17+
18+
static void l4_event_handler(struct net_mgmt_event_callback *cb, uint32_t event,
19+
struct net_if *iface)
20+
{
21+
switch (event) {
22+
case NET_EVENT_L4_CONNECTED:
23+
k_sem_give(&l4_connected);
24+
break;
25+
case NET_EVENT_L4_DISCONNECTED:
26+
k_sem_give(&l4_disconnected);
27+
break;
28+
default:
29+
break;
30+
}
31+
}
32+
33+
ZTEST(conn_mgr_nsos, test_conn_mgr_nsos_opt)
34+
{
35+
struct net_if *iface = net_if_get_default();
36+
k_timeout_t conn_delay;
37+
size_t optlen;
38+
39+
/* Default delay is 1 second */
40+
optlen = sizeof(conn_delay);
41+
zassert_equal(0, conn_mgr_if_get_opt(iface, 0, &conn_delay, &optlen));
42+
zassert_equal(sizeof(conn_delay), optlen);
43+
zassert_true(K_TIMEOUT_EQ(K_SECONDS(1), conn_delay));
44+
45+
/* Delay can be updated */
46+
conn_delay = K_SECONDS(2);
47+
zassert_equal(0, conn_mgr_if_set_opt(iface, 0, &conn_delay, sizeof(conn_delay)));
48+
optlen = sizeof(conn_delay);
49+
zassert_equal(0, conn_mgr_if_get_opt(iface, 0, &conn_delay, &optlen));
50+
zassert_equal(sizeof(conn_delay), optlen);
51+
zassert_true(K_TIMEOUT_EQ(K_SECONDS(2), conn_delay));
52+
53+
/* Reset to 1 second */
54+
conn_delay = K_SECONDS(1);
55+
zassert_equal(0, conn_mgr_if_set_opt(iface, 0, &conn_delay, sizeof(conn_delay)));
56+
}
57+
58+
ZTEST(conn_mgr_nsos, test_conn_mgr_nsos_opt_error)
59+
{
60+
struct net_if *iface = net_if_get_default();
61+
k_timeout_t conn_delay = K_SECONDS(1);
62+
size_t optlen;
63+
64+
/* Bad option */
65+
optlen = sizeof(conn_delay);
66+
zassert_equal(-EINVAL, conn_mgr_if_get_opt(iface, 1, &conn_delay, &optlen));
67+
zassert_equal(-EINVAL, conn_mgr_if_set_opt(iface, 1, &conn_delay, sizeof(conn_delay)));
68+
69+
/* Bad output size */
70+
optlen = sizeof(conn_delay) - 1;
71+
zassert_equal(-EINVAL, conn_mgr_if_get_opt(iface, 0, &conn_delay, &optlen));
72+
zassert_equal(-EINVAL, conn_mgr_if_set_opt(iface, 0, &conn_delay, sizeof(conn_delay) - 1));
73+
}
74+
75+
ZTEST(conn_mgr_nsos, test_conn_mgr_nsos)
76+
{
77+
struct net_if *iface = net_if_get_default();
78+
k_timeout_t conn_delay, conn_delay_default;
79+
size_t optlen = sizeof(conn_delay_default);
80+
81+
/* Store default delay */
82+
zassert_equal(0, conn_mgr_if_get_opt(iface, 0, &conn_delay_default, &optlen));
83+
84+
/* Not connecting by default */
85+
zassert_equal(-EAGAIN, k_sem_take(&l4_connected, K_SECONDS(2)));
86+
87+
/* Trigger the connection */
88+
zassert_equal(0, conn_mgr_if_connect(iface));
89+
90+
/* Default time to connection is 1 second */
91+
zassert_equal(-EAGAIN, k_sem_take(&l4_connected, K_MSEC(950)));
92+
zassert_equal(0, k_sem_take(&l4_connected, K_MSEC(100)));
93+
zassert_true(net_if_is_up(iface));
94+
95+
/* Small sleep to allow for network stack to return to idle */
96+
k_sleep(K_MSEC(500));
97+
98+
/* Disconnect (actioned immediately) */
99+
zassert_equal(0, conn_mgr_if_disconnect(iface));
100+
zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(100)));
101+
zassert_false(net_if_is_up(iface));
102+
103+
/* Try again with custom timeout */
104+
conn_delay = K_MSEC(500);
105+
zassert_equal(0, conn_mgr_if_set_opt(iface, 0, &conn_delay, sizeof(conn_delay)));
106+
107+
/* Trigger the connection */
108+
zassert_equal(0, conn_mgr_if_connect(iface));
109+
110+
/* Should connect after 500ms this time */
111+
zassert_equal(-EAGAIN, k_sem_take(&l4_connected, K_MSEC(450)));
112+
zassert_equal(0, k_sem_take(&l4_connected, K_MSEC(100)));
113+
zassert_true(net_if_is_up(iface));
114+
115+
/* Small sleep to allow for network stack to return to idle */
116+
k_sleep(K_MSEC(500));
117+
118+
/* Disconnect (actioned immediately) */
119+
zassert_equal(0, conn_mgr_if_disconnect(iface));
120+
zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(100)));
121+
zassert_false(net_if_is_up(iface));
122+
123+
/* Reset to default value */
124+
zassert_equal(
125+
0, conn_mgr_if_set_opt(iface, 0, &conn_delay_default, sizeof(conn_delay_default)));
126+
}
127+
128+
static void test_init(void *state)
129+
{
130+
k_sem_take(&l4_connected, K_NO_WAIT);
131+
k_sem_take(&l4_disconnected, K_NO_WAIT);
132+
}
133+
134+
static void test_after(void *fixture)
135+
{
136+
conn_mgr_all_if_disconnect(false);
137+
conn_mgr_all_if_down(false);
138+
}
139+
140+
static void *testsuite_init(void)
141+
{
142+
static struct net_mgmt_event_callback l4_callback;
143+
struct net_if *iface = net_if_get_default();
144+
struct in_addr addr;
145+
146+
net_mgmt_init_event_callback(&l4_callback, l4_event_handler,
147+
NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED);
148+
net_mgmt_add_event_callback(&l4_callback);
149+
150+
conn_mgr_all_if_down(false);
151+
152+
/* Add an IP address so that NET_EVENT_L4_CONNECTED can trigger */
153+
net_addr_pton(AF_INET, "192.0.2.1", &addr);
154+
net_if_ipv4_addr_add(iface, &addr, NET_ADDR_MANUAL, 0);
155+
156+
return NULL;
157+
}
158+
159+
ZTEST_SUITE(conn_mgr_nsos, NULL, testsuite_init, test_init, test_after, NULL);

tests/net/conn_mgr_nsos/testcase.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
common:
2+
min_ram: 16
3+
depends_on: netif
4+
tests:
5+
net.conn_mgr.nsos:
6+
platform_allow:
7+
- native_sim
8+
tags:
9+
- net
10+
- iface

0 commit comments

Comments
 (0)