source: mainline/uspace/srv/net/udp/test/assoc.c

Last change on this file was b9be9b0, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Fix handling of UDP default destination in udp_assoc_send_msg()

Fixes netecho failing to send any message.

  • Property mode set to 100644
File size: 9.1 KB
Line 
1/*
2 * Copyright (c) 2019 Jiri Svoboda
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
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <errno.h>
30#include <inet/endpoint.h>
31#include <io/log.h>
32#include <pcut/pcut.h>
33#include <str.h>
34
35#include "../assoc.h"
36#include "../msg.h"
37
38PCUT_INIT;
39
40PCUT_TEST_SUITE(assoc);
41
42static void test_recv_msg(void *, inet_ep2_t *, udp_msg_t *);
43
44static udp_assoc_cb_t test_assoc_cb = {
45 .recv_msg = test_recv_msg
46};
47
48static errno_t test_get_srcaddr(inet_addr_t *, uint8_t, inet_addr_t *);
49static errno_t test_transmit_msg(inet_ep2_t *, udp_msg_t *);
50
51static udp_assocs_dep_t test_assocs_dep = {
52 .get_srcaddr = test_get_srcaddr,
53 .transmit_msg = test_transmit_msg
54};
55
56static inet_ep2_t *sent_epp;
57static udp_msg_t *sent_msg;
58
59PCUT_TEST_BEFORE
60{
61 errno_t rc;
62
63 /* We will be calling functions that perform logging */
64 rc = log_init("test-udp");
65 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
66
67 rc = udp_assocs_init(&test_assocs_dep);
68 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
69}
70
71PCUT_TEST_AFTER
72{
73 udp_assocs_fini();
74}
75
76/** Test creating and deleting association */
77PCUT_TEST(new_delete)
78{
79 udp_assoc_t *assoc;
80 inet_ep2_t epp;
81
82 inet_ep2_init(&epp);
83 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
84 PCUT_ASSERT_NOT_NULL(assoc);
85
86 udp_assoc_delete(assoc);
87}
88
89/** Test adding, removing association */
90PCUT_TEST(add_remove)
91{
92 udp_assoc_t *assoc;
93 inet_ep2_t epp;
94 errno_t rc;
95
96 inet_ep2_init(&epp);
97
98 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
99 PCUT_ASSERT_NOT_NULL(assoc);
100
101 rc = udp_assoc_add(assoc);
102 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
103
104 udp_assoc_remove(assoc);
105 udp_assoc_delete(assoc);
106}
107
108/** Test adding, removing a reference to association */
109PCUT_TEST(addref_delref)
110{
111 udp_assoc_t *assoc;
112 inet_ep2_t epp;
113
114 inet_ep2_init(&epp);
115
116 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
117 PCUT_ASSERT_NOT_NULL(assoc);
118
119 udp_assoc_addref(assoc);
120 udp_assoc_delref(assoc);
121
122 udp_assoc_delete(assoc);
123}
124
125/** Test setting IP link in association */
126PCUT_TEST(set_iplink)
127{
128 udp_assoc_t *assoc;
129 inet_ep2_t epp;
130
131 inet_ep2_init(&epp);
132
133 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
134 PCUT_ASSERT_NOT_NULL(assoc);
135
136 udp_assoc_set_iplink(assoc, 42);
137 PCUT_ASSERT_INT_EQUALS(42, assoc->ident.local_link);
138
139 udp_assoc_delete(assoc);
140}
141
142/** Sending message with destination not set in association and NULL
143 * destination argument should fail with EINVAL.
144 */
145PCUT_TEST(send_null_dest)
146{
147 udp_assoc_t *assoc;
148 inet_ep2_t epp;
149 inet_ep_t dest;
150 errno_t rc;
151 udp_msg_t *msg;
152 const char *msgstr = "Hello";
153
154 msg = udp_msg_new();
155 PCUT_ASSERT_NOT_NULL(msg);
156 msg->data_size = str_size(msgstr) + 1;
157 msg->data = str_dup(msgstr);
158
159 inet_ep2_init(&epp);
160 inet_ep_init(&dest);
161
162 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
163 PCUT_ASSERT_NOT_NULL(assoc);
164
165 rc = udp_assoc_add(assoc);
166 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
167
168 rc = udp_assoc_send(assoc, &dest, msg);
169 PCUT_ASSERT_ERRNO_VAL(EINVAL, rc);
170
171 udp_msg_delete(msg);
172
173 udp_assoc_remove(assoc);
174 udp_assoc_delete(assoc);
175}
176
177/** Sending message with destination not set in association and unset
178 * destination argument should fail with EINVAL.
179 */
180PCUT_TEST(send_unset_dest)
181{
182 udp_assoc_t *assoc;
183 inet_ep2_t epp;
184 inet_ep_t dest;
185 errno_t rc;
186 udp_msg_t *msg;
187 const char *msgstr = "Hello";
188
189 msg = udp_msg_new();
190 PCUT_ASSERT_NOT_NULL(msg);
191 msg->data_size = str_size(msgstr) + 1;
192 msg->data = str_dup(msgstr);
193
194 inet_ep2_init(&epp);
195 inet_ep_init(&dest);
196
197 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
198 PCUT_ASSERT_NOT_NULL(assoc);
199
200 rc = udp_assoc_add(assoc);
201 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
202
203 rc = udp_assoc_send(assoc, &dest, msg);
204 PCUT_ASSERT_ERRNO_VAL(EINVAL, rc);
205
206 udp_msg_delete(msg);
207
208 udp_assoc_remove(assoc);
209 udp_assoc_delete(assoc);
210}
211
212/** Sending message with explicit destination */
213PCUT_TEST(send_explicit_dest)
214{
215 udp_assoc_t *assoc;
216 inet_ep2_t epp;
217 inet_ep_t dest;
218 errno_t rc;
219 udp_msg_t *msg;
220 const char *msgstr = "Hello";
221
222 msg = udp_msg_new();
223 PCUT_ASSERT_NOT_NULL(msg);
224 msg->data_size = str_size(msgstr) + 1;
225 msg->data = str_dup(msgstr);
226
227 inet_ep2_init(&epp);
228 inet_addr(&dest.addr, 127, 0, 0, 1);
229 dest.port = 42;
230
231 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
232 PCUT_ASSERT_NOT_NULL(assoc);
233
234 rc = udp_assoc_add(assoc);
235 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
236
237 sent_epp = NULL;
238 sent_msg = NULL;
239
240 rc = udp_assoc_send(assoc, &dest, msg);
241 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
242 PCUT_ASSERT_EQUALS(msg, sent_msg);
243 PCUT_ASSERT_TRUE(inet_addr_compare(&dest.addr, &sent_epp->remote.addr));
244 PCUT_ASSERT_EQUALS(dest.port, sent_epp->remote.port);
245
246 udp_msg_delete(msg);
247
248 udp_assoc_remove(assoc);
249 udp_assoc_delete(assoc);
250}
251
252/** Sending message with destination set in association and inet_addr_any /
253 * inet_port_any destination argument
254 */
255PCUT_TEST(send_assoc_any_dest)
256{
257 udp_assoc_t *assoc;
258 inet_ep2_t epp;
259 inet_ep_t ep;
260 errno_t rc;
261 udp_msg_t *msg;
262 const char *msgstr = "Hello";
263
264 msg = udp_msg_new();
265 PCUT_ASSERT_NOT_NULL(msg);
266 msg->data_size = str_size(msgstr) + 1;
267 msg->data = str_dup(msgstr);
268
269 inet_ep2_init(&epp);
270 inet_addr(&epp.remote.addr, 127, 0, 0, 1);
271 epp.remote.port = 42;
272 inet_addr(&epp.local.addr, 127, 0, 0, 1);
273 epp.local.port = 1;
274
275 inet_ep_init(&ep);
276
277 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
278 PCUT_ASSERT_NOT_NULL(assoc);
279
280 rc = udp_assoc_add(assoc);
281 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
282
283 sent_epp = NULL;
284 sent_msg = NULL;
285
286 rc = udp_assoc_send(assoc, &ep, msg);
287 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
288 PCUT_ASSERT_EQUALS(msg, sent_msg);
289 PCUT_ASSERT_TRUE(inet_addr_compare(&epp.remote.addr, &sent_epp->remote.addr));
290 PCUT_ASSERT_EQUALS(epp.remote.port, sent_epp->remote.port);
291
292 udp_msg_delete(msg);
293
294 udp_assoc_remove(assoc);
295 udp_assoc_delete(assoc);
296}
297
298/** Sending message with destination set in association and unset destination
299 * argument should return EINVAL
300 */
301PCUT_TEST(send_assoc_unset_dest)
302{
303 udp_assoc_t *assoc;
304 inet_ep2_t epp;
305 inet_ep_t dest;
306 errno_t rc;
307 udp_msg_t *msg;
308 const char *msgstr = "Hello";
309
310 msg = udp_msg_new();
311 PCUT_ASSERT_NOT_NULL(msg);
312 msg->data_size = str_size(msgstr) + 1;
313 msg->data = str_dup(msgstr);
314
315 inet_ep2_init(&epp);
316 inet_addr(&epp.local.addr, 127, 0, 0, 1);
317 epp.local.port = 1;
318 inet_ep_init(&dest);
319
320 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
321 PCUT_ASSERT_NOT_NULL(assoc);
322
323 rc = udp_assoc_add(assoc);
324 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
325
326 rc = udp_assoc_send(assoc, &dest, msg);
327 PCUT_ASSERT_ERRNO_VAL(EINVAL, rc);
328
329 udp_msg_delete(msg);
330
331 udp_assoc_remove(assoc);
332 udp_assoc_delete(assoc);
333}
334
335PCUT_TEST(recv)
336{
337 // XXX Looks like currently udp_assoc_recv() is not used at all
338}
339
340/** Test udp_assoc_received() */
341PCUT_TEST(received)
342{
343 udp_assoc_t *assoc;
344 inet_ep2_t epp;
345 errno_t rc;
346 udp_msg_t *msg;
347 const char *msgstr = "Hello";
348 bool received;
349
350 msg = udp_msg_new();
351 PCUT_ASSERT_NOT_NULL(msg);
352 msg->data_size = str_size(msgstr) + 1;
353 msg->data = str_dup(msgstr);
354
355 inet_ep2_init(&epp);
356 inet_addr(&epp.remote.addr, 127, 0, 0, 1);
357 epp.remote.port = 1;
358 inet_addr(&epp.local.addr, 127, 0, 0, 1);
359 epp.local.port = 2;
360
361 assoc = udp_assoc_new(&epp, &test_assoc_cb, (void *) &received);
362 PCUT_ASSERT_NOT_NULL(assoc);
363
364 rc = udp_assoc_add(assoc);
365 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
366
367 received = false;
368 udp_assoc_received(&epp, msg);
369 PCUT_ASSERT_TRUE(received);
370
371 udp_assoc_remove(assoc);
372 udp_assoc_delete(assoc);
373}
374
375/** Test udp_assoc_reset() */
376PCUT_TEST(reset)
377{
378 udp_assoc_t *assoc;
379 inet_ep2_t epp;
380
381 inet_ep2_init(&epp);
382 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
383 PCUT_ASSERT_NOT_NULL(assoc);
384
385 udp_assoc_reset(assoc);
386 udp_assoc_delete(assoc);
387}
388
389static void test_recv_msg(void *arg, inet_ep2_t *epp, udp_msg_t *msg)
390{
391 bool *received = (bool *) arg;
392
393 *received = true;
394}
395
396static errno_t test_get_srcaddr(inet_addr_t *remote, uint8_t tos,
397 inet_addr_t *local)
398{
399 inet_addr(local, 127, 0, 0, 1);
400 return EOK;
401}
402
403static errno_t test_transmit_msg(inet_ep2_t *epp, udp_msg_t *msg)
404{
405 sent_epp = epp;
406 sent_msg = msg;
407 return EOK;
408}
409
410PCUT_EXPORT(assoc);
Note: See TracBrowser for help on using the repository browser.