1 /* main.c - Application main entry point */
2
3 /*
4 * Copyright (c) 2015 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <zephyr/logging/log.h>
10 LOG_MODULE_REGISTER(net_test, CONFIG_NET_TRICKLE_LOG_LEVEL);
11
12 #include <zephyr/types.h>
13 #include <zephyr/ztest.h>
14 #include <stdbool.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <zephyr/sys/printk.h>
19 #include <zephyr/linker/sections.h>
20
21 #include <zephyr/tc_util.h>
22
23 #include <zephyr/net/buf.h>
24 #include <zephyr/net/net_ip.h>
25 #include <zephyr/net/net_if.h>
26
27 #include <zephyr/net/trickle.h>
28
29 #include "net_private.h"
30
31 #if defined(CONFIG_NET_TRICKLE_LOG_LEVEL_DBG)
32 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
33 #else
34 #define DBG(fmt, ...)
35 #endif
36
37 static int token1 = 1, token2 = 2;
38
39 static struct k_sem wait;
40 static struct k_sem wait2;
41 static bool cb_1_called;
42 static bool cb_2_called;
43
44 #define WAIT_TIME K_SECONDS(3)
45
46 /* Set CHECK_LONG_TIMEOUT to 1 if you want to check longer timeout.
47 * Do not do this for automated tests as those need to finish asap.
48 */
49 #define CHECK_LONG_TIMEOUT 0
50 #if CHECK_LONG_TIMEOUT > 0
51 #define WAIT_TIME_LONG K_SECONDS(10)
52 #endif
53
54 #define T1_IMIN 30
55 #define T1_IMAX 5
56 #define T1_K 20
57
58 #define T2_IMIN 80
59 #define T2_IMAX 3
60 #define T2_K 40
61
62 static struct net_trickle t1;
63 static struct net_trickle t2;
64
cb_1(struct net_trickle * trickle,bool do_suppress,void * user_data)65 static void cb_1(struct net_trickle *trickle, bool do_suppress,
66 void *user_data)
67 {
68 TC_PRINT("Trickle 1 %p callback called\n", trickle);
69
70 k_sem_give(&wait);
71
72 cb_1_called = true;
73 }
74
cb_2(struct net_trickle * trickle,bool do_suppress,void * user_data)75 static void cb_2(struct net_trickle *trickle, bool do_suppress,
76 void *user_data)
77 {
78 TC_PRINT("Trickle 2 %p callback called\n", trickle);
79
80 k_sem_give(&wait2);
81
82 cb_2_called = true;
83 }
84
test_trickle_create(void)85 static void test_trickle_create(void)
86 {
87 int ret;
88
89 ret = net_trickle_create(&t1, T1_IMIN, T1_IMAX, T1_K);
90 zassert_false(ret, "Trickle 1 create failed");
91
92 ret = net_trickle_create(&t2, T2_IMIN, T2_IMAX, T2_K);
93 zassert_false(ret, "Trickle 2 create failed");
94 }
95
test_trickle_start(void)96 static void test_trickle_start(void)
97 {
98 int ret;
99
100 cb_1_called = false;
101 cb_2_called = false;
102
103 ret = net_trickle_start(&t1, cb_1, &t1);
104 zassert_false(ret, "Trickle 1 start failed");
105
106 ret = net_trickle_start(&t2, cb_2, &t2);
107 zassert_false(ret, "Trickle 2 start failed");
108 }
109
test_trickle_stop(void)110 static void test_trickle_stop(void)
111 {
112 zassert_false(net_trickle_stop(&t1),
113 "Trickle 1 stop failed");
114
115 zassert_false(net_trickle_stop(&t2),
116 "Trickle 2 stop failed");
117 }
118
test_trickle_1_status(void)119 static void test_trickle_1_status(void)
120 {
121 zassert_true(net_trickle_is_running(&t1), "Trickle 1 not running");
122
123 if (token1 != token2) {
124 net_trickle_inconsistency(&t1);
125 } else {
126 net_trickle_consistency(&t1);
127 }
128 }
129
test_trickle_2_status(void)130 static void test_trickle_2_status(void)
131 {
132 zassert_true(net_trickle_is_running(&t2), "Trickle 2 not running");
133
134 if (token1 == token2) {
135 net_trickle_consistency(&t2);
136 } else {
137 net_trickle_inconsistency(&t2);
138 }
139 }
140
test_trickle_1_wait(void)141 static void test_trickle_1_wait(void)
142 {
143 k_sem_take(&wait, WAIT_TIME);
144
145 zassert_true(cb_1_called, "Trickle 1 no timeout");
146
147 zassert_true(net_trickle_is_running(&t1), "Trickle 1 not running");
148 }
149
150 #if CHECK_LONG_TIMEOUT > 0
test_trickle_1_wait_long(void)151 static void test_trickle_1_wait_long(void)
152 {
153 cb_1_called = false;
154
155 k_sem_take(&wait, WAIT_TIME_LONG);
156
157 zassert_false(cb_1_called, "Trickle 1 no timeout");
158
159 zassert_true(net_trickle_is_running(&t1), "Trickle 1 not running");
160 }
161 #else
test_trickle_1_wait_long(void)162 static void test_trickle_1_wait_long(void)
163 {
164 ztest_test_skip();
165 }
166 #endif
167
test_trickle_2_wait(void)168 static void test_trickle_2_wait(void)
169 {
170 k_sem_take(&wait2, WAIT_TIME);
171
172 zassert_true(cb_2_called, "Trickle 2 no timeout");
173
174 zassert_true(net_trickle_is_running(&t2), "Trickle 2 not running");
175 }
176
test_trickle_1_stopped(void)177 static void test_trickle_1_stopped(void)
178 {
179 zassert_false(net_trickle_is_running(&t1), "Trickle 1 running");
180 }
181
test_trickle_2_inc(void)182 static void test_trickle_2_inc(void)
183 {
184 zassert_true(net_trickle_is_running(&t2), "Trickle 2 is not running");
185 token2++;
186 }
187
test_trickle_1_update(void)188 static void test_trickle_1_update(void)
189 {
190 zassert_true(net_trickle_is_running(&t1), "trickle 1 is not running");
191
192 token1 = token2;
193 }
194
test_init(void)195 static void test_init(void)
196 {
197 k_sem_init(&wait, 0, UINT_MAX);
198 k_sem_init(&wait2, 0, UINT_MAX);
199 }
200
ZTEST(net_trickle,test_trickle)201 ZTEST(net_trickle, test_trickle)
202 {
203 test_init();
204 test_trickle_create();
205 test_trickle_start();
206 test_trickle_1_status();
207 test_trickle_2_status();
208 test_trickle_1_wait();
209 test_trickle_2_wait();
210 test_trickle_1_update();
211 test_trickle_2_inc();
212 test_trickle_1_status();
213 test_trickle_1_wait_long();
214 test_trickle_stop();
215 test_trickle_1_stopped();
216 }
217
218 ZTEST_SUITE(net_trickle, NULL, NULL, NULL, NULL, NULL);
219