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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 46a8c3cd was f1cd4b0, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Fix indentation

  • 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 NULL destination
253 * argument
254 */
255PCUT_TEST(send_assoc_null_dest)
256{
257 udp_assoc_t *assoc;
258 inet_ep2_t epp;
259 errno_t rc;
260 udp_msg_t *msg;
261 const char *msgstr = "Hello";
262
263 msg = udp_msg_new();
264 PCUT_ASSERT_NOT_NULL(msg);
265 msg->data_size = str_size(msgstr) + 1;
266 msg->data = str_dup(msgstr);
267
268 inet_ep2_init(&epp);
269 inet_addr(&epp.remote.addr, 127, 0, 0, 1);
270 epp.remote.port = 42;
271 inet_addr(&epp.local.addr, 127, 0, 0, 1);
272 epp.local.port = 1;
273
274 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
275 PCUT_ASSERT_NOT_NULL(assoc);
276
277 rc = udp_assoc_add(assoc);
278 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
279
280 sent_epp = NULL;
281 sent_msg = NULL;
282
283 rc = udp_assoc_send(assoc, NULL, msg);
284 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
285 PCUT_ASSERT_EQUALS(msg, sent_msg);
286 PCUT_ASSERT_TRUE(inet_addr_compare(&epp.remote.addr, &sent_epp->remote.addr));
287 PCUT_ASSERT_EQUALS(epp.remote.port, sent_epp->remote.port);
288
289 udp_msg_delete(msg);
290
291 udp_assoc_remove(assoc);
292 udp_assoc_delete(assoc);
293}
294
295/** Sending message with destination set in association and unset destination
296 * argument should return EINVAL
297 */
298PCUT_TEST(send_assoc_unset_dest)
299{
300 udp_assoc_t *assoc;
301 inet_ep2_t epp;
302 inet_ep_t dest;
303 errno_t rc;
304 udp_msg_t *msg;
305 const char *msgstr = "Hello";
306
307 msg = udp_msg_new();
308 PCUT_ASSERT_NOT_NULL(msg);
309 msg->data_size = str_size(msgstr) + 1;
310 msg->data = str_dup(msgstr);
311
312 inet_ep2_init(&epp);
313 inet_addr(&epp.remote.addr, 127, 0, 0, 1);
314 epp.remote.port = 42;
315 inet_addr(&epp.local.addr, 127, 0, 0, 1);
316 epp.local.port = 1;
317 inet_ep_init(&dest);
318
319 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
320 PCUT_ASSERT_NOT_NULL(assoc);
321
322 rc = udp_assoc_add(assoc);
323 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
324
325 rc = udp_assoc_send(assoc, &dest, msg);
326 PCUT_ASSERT_ERRNO_VAL(EINVAL, rc);
327
328 udp_msg_delete(msg);
329
330 udp_assoc_remove(assoc);
331 udp_assoc_delete(assoc);
332}
333
334PCUT_TEST(recv)
335{
336 // XXX Looks like currently udp_assoc_recv() is not used at all
337}
338
339/** Test udp_assoc_received() */
340PCUT_TEST(received)
341{
342 udp_assoc_t *assoc;
343 inet_ep2_t epp;
344 errno_t rc;
345 udp_msg_t *msg;
346 const char *msgstr = "Hello";
347 bool received;
348
349 msg = udp_msg_new();
350 PCUT_ASSERT_NOT_NULL(msg);
351 msg->data_size = str_size(msgstr) + 1;
352 msg->data = str_dup(msgstr);
353
354 inet_ep2_init(&epp);
355 inet_addr(&epp.remote.addr, 127, 0, 0, 1);
356 epp.remote.port = 1;
357 inet_addr(&epp.local.addr, 127, 0, 0, 1);
358 epp.local.port = 2;
359
360 assoc = udp_assoc_new(&epp, &test_assoc_cb, (void *) &received);
361 PCUT_ASSERT_NOT_NULL(assoc);
362
363 rc = udp_assoc_add(assoc);
364 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
365
366 received = false;
367 udp_assoc_received(&epp, msg);
368 PCUT_ASSERT_TRUE(received);
369
370 udp_assoc_remove(assoc);
371 udp_assoc_delete(assoc);
372}
373
374/** Test udp_assoc_reset() */
375PCUT_TEST(reset)
376{
377 udp_assoc_t *assoc;
378 inet_ep2_t epp;
379
380 inet_ep2_init(&epp);
381 assoc = udp_assoc_new(&epp, &test_assoc_cb, NULL);
382 PCUT_ASSERT_NOT_NULL(assoc);
383
384 udp_assoc_reset(assoc);
385 udp_assoc_delete(assoc);
386}
387
388static void test_recv_msg(void *arg, inet_ep2_t *epp, udp_msg_t *msg)
389{
390 bool *received = (bool *) arg;
391
392 *received = true;
393}
394
395static errno_t test_get_srcaddr(inet_addr_t *remote, uint8_t tos,
396 inet_addr_t *local)
397{
398 inet_addr(local, 127, 0, 0, 1);
399 return EOK;
400}
401
402static errno_t test_transmit_msg(inet_ep2_t *epp, udp_msg_t *msg)
403{
404 sent_epp = epp;
405 sent_msg = msg;
406 return EOK;
407}
408
409PCUT_EXPORT(assoc);
Note: See TracBrowser for help on using the repository browser.