Debian Codegraph @

tests/nl-test-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2
3 #ifndef __NL_TEST_UTIL_H__
4 #define __NL_TEST_UTIL_H__
5
6 #include <sys/stat.h>
7 #include <check.h>
8
9 #include <netlink/object.h>
10 #include <netlink/cache.h>
11
12 #include "base/nl-base-utils.h"
13 #include "nl-aux-core/nl-core.h"
14 #include "nl-aux-route/nl-route.h"
15
16 /*****************************************************************************/
17
18 static inline void _nltst_strfreev(char **strv)
19 {
20 size_t i;
21
22 if (strv) {
23 for (i = 0; strv[i]; i++)
24 free(strv[i]);
25 free(strv);
26 }
27 }
28
29 #define _nltst_auto_strfreev _nl_auto(_nltst_auto_strfreev_fcn)
30 _NL_AUTO_DEFINE_FCN_TYPED0(char **, _nltst_auto_strfreev_fcn, _nltst_strfreev);
31
32 /*****************************************************************************/
33
34 #ifndef ck_assert_ptr_nonnull
35 #define ck_assert_ptr_nonnull(ptr) ck_assert(ptr)
36 #endif
37
38 #ifndef ck_assert_pstr_ne
39 #define ck_assert_pstr_ne(a, b) \
40 do { \
41 const char *_a = (a); \
42 const char *_b = (b); \
43 \
44 ck_assert(!(_a == _b || (_a && _b && strcmp(_a, _b) == 0))); \
45 } while (0)
46 #endif
47
48 #ifndef ck_assert_ptr_null
49 #define ck_assert_ptr_null(ptr) ck_assert(!(ptr))
50 #endif
51
52 /*****************************************************************************/
53
54 #define _nltst_assert(expr) \
55 ({ \
56 typeof(expr) _expr = (expr); \
57 \
58 if (!_expr) { \
59 ck_assert_msg(0, "assert(%s) failed", #expr); \
60 } \
61 _expr; \
62 })
63
64 #define _nltst_assert_errno(expr) \
65 do { \
66 if (expr) { \
67 } else { \
68 const int _errno = (errno); \
69 \
70 ck_assert_msg(0, "assert(%s) failed (errno=%d, %s)", \
71 #expr, _errno, strerror(_errno)); \
72 } \
73 } while (0)
74
75 #define _nltst_assert_retcode(expr) \
76 do { \
77 const int _r = (expr); \
78 \
79 if (_r < 0) { \
80 ck_assert_msg( \
81 0, "command(%s) failed with return code %d", \
82 #expr, _r); \
83 } \
84 if (_r > 0) { \
85 ck_assert_msg( \
86 0, \
87 "command(%s) has unexpected positive return code %d", \
88 #expr, _r); \
89 } \
90 } while (0)
91
92 /*****************************************************************************/
93
94 #define __nltst_assert_nonnull(uniq, x) \
95 ({ \
96 typeof(x) _NL_UNIQ_T(_x, uniq) = (x); \
97 \
98 ck_assert_ptr_nonnull(_NL_UNIQ_T(_x, uniq)); \
99 \
100 _NL_UNIQ_T(_x, uniq); \
101 })
102
103 #define _nltst_assert_nonnull(x) __nltst_assert_nonnull(_NL_UNIQ, x)
104
105 /*****************************************************************************/
106
107 static inline char *_nltst_strdup(const char *str)
108 {
109 return str ? _nltst_assert_nonnull(strdup(str)) : NULL;
110 }
111
112 #define _nltst_malloc0(size) _nltst_assert_nonnull(calloc((size), 1u))
113
114 /*****************************************************************************/
115
116 #define _nltst_sprintf(buf, p_start, p_extra_len, ...) \
117 do { \
118 char *_buf = (buf); \
119 size_t *_p_start = (p_start); \
120 size_t *_p_extra_len = (p_extra_len); \
121 int _r; \
122 \
123 _nltst_assert_nonnull(_buf); \
124 _nltst_assert_nonnull(_p_start); \
125 _nltst_assert_nonnull(_p_extra_len); \
126 \
127 _buf = &_buf[*_p_start]; \
128 _r = snprintf(_buf, *_p_extra_len, __VA_ARGS__); \
129 _nltst_assert(_r >= 0); \
130 _nltst_assert((size_t)_r < *_p_extra_len); \
131 _nltst_assert((size_t)_r == strlen(_buf)); \
132 \
133 *_p_start += (size_t)_r; \
134 *_p_extra_len -= (size_t)_r; \
135 } while (0)
136
137 /*****************************************************************************/
138
139 #define __nltst_sprintf_arr(uniq, arr, fmt, ...) \
140 ({ \
141 char *const _NL_UNIQ_T(arr, uniq) = (arr); \
142 int _NL_UNIQ_T(c, uniq); \
143 \
144 _NL_STATIC_ASSERT(sizeof(arr) > \
145 sizeof(_NL_UNIQ_T(arr, uniq))); \
146 \
147 _NL_UNIQ_T(c, uniq) = snprintf(_NL_UNIQ_T(arr, uniq), \
148 sizeof(arr), fmt, \
149 ##__VA_ARGS__); \
150 \
151 ck_assert_int_lt(_NL_UNIQ_T(c, uniq), sizeof(arr)); \
152 \
153 _NL_UNIQ_T(arr, uniq); \
154 })
155
156 #define _nltst_sprintf_arr(arr, fmt, ...) \
157 __nltst_sprintf_arr(_NL_UNIQ, arr, fmt, ##__VA_ARGS__)
158
159 /*****************************************************************************/
160
161 void _nltst_get_urandom(void *ptr, size_t len);
162
163 uint32_t _nltst_rand_u32(void);
164
165 static inline uint32_t _nltst_rand_u32_range(uint32_t n)
166 {
167 uint32_t rem;
168 uint32_t i;
169
170 if (n == 0)
171 return _nltst_rand_u32();
172 if (n == 1)
173 return 0;
174
175 rem = UINT32_MAX % n;
176 for (;;) {
177 i = _nltst_rand_u32();
178 if (i < (UINT32_MAX - rem))
179 return i % n;
180 }
181 }
182
183 static inline bool _nltst_rand_bool(void)
184 {
185 return _nltst_rand_u32() % 2 == 0;
186 }
187
188 #define _nltst_rand_select(a, ...) \
189 ({ \
190 const typeof(a) _lst[] = { (a), ##__VA_ARGS__ }; \
191 \
192 _lst[_nltst_rand_u32_range(_NL_N_ELEMENTS(_lst))]; \
193 })
194
195 /*****************************************************************************/
196
197 #define _nltst_close(fd) \
198 do { \
199 int _r; \
200 \
201 _r = _nl_close((fd)); \
202 _nltst_assert_errno(_r == 0); \
203 } while (0)
204
205 #define _nltst_fclose(f) \
206 do { \
207 int _r; \
208 \
209 _r = fclose((f)); \
210 _nltst_assert_errno(_r == 0); \
211 } while (0)
212
213 void _nltst_assert_link_exists_full(const char *ifname, bool exists);
214
215 #define _nltst_assert_link_exists(ifname) \
216 _nltst_assert_link_exists_full((ifname), true)
217
218 #define _nltst_assert_link_not_exists(ifname) \
219 _nltst_assert_link_exists_full((ifname), false)
220
221 /*****************************************************************************/
222
223 typedef union {
224 in_addr_t addr4;
225 struct in_addr a4;
226 struct in6_addr a6;
227 } NLTstIPAddr;
228
229 static inline char *_nltst_inet_ntop(int addr_family, const void *addr,
230 char buf[static INET_ADDRSTRLEN])
231 {
232 char *r;
233
234 ck_assert(addr_family == AF_INET || addr_family == AF_INET6);
235 ck_assert(addr);
236
237 r = (char *)inet_ntop(addr_family, addr, buf,
238 (addr_family == AF_INET) ? INET_ADDRSTRLEN :
239 INET6_ADDRSTRLEN);
240 ck_assert_ptr_eq(r, buf);
241 ck_assert_int_lt(strlen(r), (addr_family == AF_INET) ?
242 INET_ADDRSTRLEN :
243 INET6_ADDRSTRLEN);
244 return r;
245 }
246
247 static inline char *_nltst_inet_ntop_dup(int addr_family, const void *addr)
248 {
249 return (char *)_nltst_inet_ntop(addr_family, addr,
250 malloc((addr_family == AF_INET) ?
251 INET_ADDRSTRLEN :
252 INET6_ADDRSTRLEN));
253 }
254
255 static inline bool _nltst_inet_pton(int addr_family, const char *str,
256 int *out_addr_family, void *out_addr)
257 {
258 NLTstIPAddr a;
259 int r;
260
261 ck_assert(addr_family == AF_UNSPEC || addr_family == AF_INET ||
262 addr_family == AF_INET6);
263
264 /* when requesting @out_addr, then the addr-family must either be
265 * pre-determined or requested too. */
266 ck_assert(!out_addr || out_addr_family || addr_family != AF_UNSPEC);
267
268 if (!str)
269 return false;
270
271 if (addr_family == AF_UNSPEC)
272 addr_family = strchr(str, ':') ? AF_INET6 : AF_INET;
273
274 r = inet_pton(addr_family, str, &a);
275 if (r != 1)
276 return false;
277
278 if (out_addr) {
279 memcpy(out_addr, &a,
280 addr_family == AF_INET ? sizeof(in_addr_t) :
281 sizeof(struct in6_addr));
282 }
283 if (out_addr_family)
284 *out_addr_family = addr_family;
285
286 return true;
287 }
288
289 static inline bool _nltst_inet_valid(int addr_family, const char *addr)
290 {
291 return _nltst_inet_pton(addr_family, addr, NULL, NULL);
292 }
293
294 static inline in_addr_t _nltst_inet4(const char *addr)
295 {
296 in_addr_t addr_bin = 0;
297
298 _nltst_assert(_nltst_inet_pton(AF_INET, addr, NULL, &addr_bin));
299 return addr_bin;
300 }
301
302 static inline struct in6_addr *_nltst_inet6p(const char *addr)
303 {
304 _nl_thread_local static struct in6_addr addr_bin;
305
306 ck_assert(_nltst_inet_pton(AF_INET6, addr, NULL, &addr_bin));
307 return &addr_bin;
308 }
309
310 static inline struct in6_addr _nltst_inet6(const char *addr)
311 {
312 struct in6_addr addr_bin;
313
314 ck_assert(_nltst_inet_pton(AF_INET6, addr, NULL, &addr_bin));
315 return addr_bin;
316 }
317
318 static inline int _nltst_inet_addr_family(int addr_family, const char *addr)
319 {
320 if (!_nltst_inet_pton(addr_family, addr, &addr_family, NULL))
321 return AF_UNSPEC;
322 return addr_family;
323 }
324
325 static inline char *_nltst_inet_normalize(int addr_family, const char *addr,
326 char buf[static INET_ADDRSTRLEN])
327 {
328 NLTstIPAddr a;
329
330 buf[0] = '\0';
331 if (!_nltst_inet_pton(addr_family, addr, &addr_family, &a))
332 return NULL;
333 return _nltst_inet_ntop(addr_family, &a, buf);
334 }
335
336 /*****************************************************************************/
337
338 char *_nltst_strtok(const char **p_str);
339
340 char **_nltst_strtokv(const char *str);
341
342 #define _nltst_assert_strv_equal(strv1, strv2) \
343 do { \
344 typeof(strv1) _strv1 = (strv1); \
345 typeof(strv2) _strv2 = (strv2); \
346 _nl_unused const void *_strv1_typecheck1 = _strv1; \
347 _nl_unused const void *_strv2_typecheck1 = _strv2; \
348 _nl_unused const char *_strv1_typecheck2 = \
349 _strv1 ? _strv1[0] : NULL; \
350 _nl_unused const char *_strv2_typecheck2 = \
351 _strv2 ? _strv2[0] : NULL; \
352 size_t _i; \
353 \
354 ck_assert_int_eq(!!_strv1, !!_strv2); \
355 if (_strv1) { \
356 for (_i = 0; _strv1[_i] || _strv2[_i]; _i++) { \
357 ck_assert_str_eq(_strv1[_i], _strv2[_i]); \
358 } \
359 } \
360 } while (0)
361
362 #define _NLTST_CHARSET_SPACE " \n\r\t"
363
364 #define _nltst_char_is(ch, charset) (!!(strchr("" charset "", (ch))))
365
366 #define _nltst_char_is_space(ch) _nltst_char_is(ch, _NLTST_CHARSET_SPACE)
367
368 #define _nltst_str_skip_predicate(s, ch, predicate) \
369 ({ \
370 typeof(s) _s1 = (s); \
371 _nl_unused const char *_s1_typecheck = (_s1); \
372 \
373 if (_s1) { \
374 while (({ \
375 const char ch = _s1[0]; \
376 \
377 (ch != '\0') && (predicate); \
378 })) \
379 _s1++; \
380 } \
381 _s1; \
382 })
383
384 #define _nltst_str_skip_charset(s, charset) \
385 _nltst_str_skip_predicate(s, _ch, _nltst_char_is(_ch, "" charset ""))
386
387 #define _nltst_str_skip_space(s) \
388 _nltst_str_skip_charset(s, _NLTST_CHARSET_SPACE)
389
390 #define _nltst_str_has_prefix_and_space(s, prefix) \
391 ({ \
392 typeof(s) _s2 = (s); \
393 _nl_unused const char *_s2_typecheck = (_s2); \
394 const size_t _l = strlen("" prefix ""); \
395 \
396 if (_s2) { \
397 if ((strncmp(_s2, "" prefix "", _l)) == 0 && \
398 _nltst_char_is_space(_s2[_l])) \
399 _s2 = _nltst_str_skip_space(&_s2[_l + 1]); \
400 else \
401 _s2 = NULL; \
402 } \
403 _s2; \
404 })
405
406 #define _nltst_str_find_first_not_from_charset(s, charset) \
407 ({ \
408 typeof(s) _s3 = (s); \
409 _nl_unused const char *_s3_typecheck = (_s3); \
410 size_t _l3; \
411 \
412 _l3 = strspn(_s3, "" charset ""); \
413 \
414 &_s3[_l3]; \
415 })
416
417 #define _nltst_str_find_first_from_charset(s, charset) \
418 ({ \
419 typeof(s) _s3 = (s); \
420 _nl_unused const char *_s3_typecheck = (_s3); \
421 size_t _l3; \
422 \
423 _l3 = strcspn(_s3, "" charset ""); \
424 \
425 &_s3[_l3]; \
426 })
427
428 /*****************************************************************************/
429
430 void nltst_netns_fixture_setup(void);
431 void nltst_netns_fixture_teardown(void);
432 bool nltst_netns_fixture_is_unshared(void);
433
434 bool _nltst_skip_no_netns(void);
435
436 struct nltst_netns;
437
438 struct nltst_netns *nltst_netns_enter(void);
439 void nltst_netns_leave(struct nltst_netns *nsdata);
440
441 /*****************************************************************************/
442
443 #define _nltst_system(command) _nltst_assert_retcode(system(command))
444
445 bool _nltst_in_ci(void);
446
447 bool _nltst_has_iproute2(void);
448 bool _nltst_skip_no_iproute2(const char *msg);
449
450 /*****************************************************************************/
451
452 typedef struct {
453 int addr_family;
454 int ifindex;
455 int plen;
456 char *addr;
457 char *addr_pattern;
458 } NLTstSelectRoute;
459
460 #define _nltst_assert_select_route(select_route) \
461 do { \
462 const NLTstSelectRoute *_select_route_5 = (select_route); \
463 \
464 ck_assert_ptr_nonnull(_select_route_5); \
465 _nl_assert_addr_family_or_unspec( \
466 _select_route_5->addr_family); \
467 ck_assert_int_ge(_select_route_5->ifindex, 0); \
468 ck_assert_int_ge(_select_route_5->plen, -1); \
469 ck_assert_int_le( \
470 _select_route_5->plen, \
471 _select_route_5->addr_family == AF_INET ? 32 : 128); \
472 ck_assert(!_select_route_5->addr || ({ \
473 char _buf[INET6_ADDRSTRLEN]; \
474 const char *_s2; \
475 \
476 _s2 = _nltst_inet_normalize( \
477 _select_route_5->addr_family, \
478 _select_route_5->addr, _buf); \
479 (_select_route_5->addr_family != AF_UNSPEC && _s2 && \
480 _nl_streq(_s2, _select_route_5->addr)); \
481 })); \
482 ck_assert(!_select_route_5->addr_pattern || \
483 !_select_route_5->addr); \
484 ck_assert(!_select_route_5->addr_pattern || \
485 _select_route_5->addr_family != AF_UNSPEC); \
486 } while (0)
487
488 void _nltst_select_route_clear(NLTstSelectRoute *select_route);
489
490 #define _nltst_auto_clear_select_route \
491 _nl_auto(_nltst_auto_clear_select_route_fcn)
492 _NL_AUTO_DEFINE_FCN_STRUCT(NLTstSelectRoute, _nltst_auto_clear_select_route_fcn,
493 _nltst_select_route_clear);
494
495 int _nltst_select_route_cmp(const NLTstSelectRoute *select_route1,
496 const NLTstSelectRoute *select_route2);
497
498 static inline bool
499 _nltst_select_route_equal(const NLTstSelectRoute *select_route1,
500 const NLTstSelectRoute *select_route2)
501 {
502 return _nltst_select_route_cmp(select_route1, select_route2) == 0;
503 }
504
505 char *_nltst_select_route_to_string(const NLTstSelectRoute *select_route);
506
507 char *_nltst_objects_to_string(const char *description,
508 struct nl_object *const *objs, ssize_t len);
509
510 void _nltst_select_route_parse(const char *str,
511 NLTstSelectRoute *out_select_route);
512
513 bool _nltst_select_route_match(struct nl_object *route,
514 const NLTstSelectRoute *select_route,
515 bool do_assert);
516
517 /*****************************************************************************/
518
519 void _nltst_object_identical(const void *a, const void *b);
520
521 char *_nltst_object_to_string(const struct nl_object *obj);
522
523 struct nl_object **_nltst_cache_get_all(struct nl_cache *cache,
524 size_t *out_len);
525
526 struct rtnl_link *_nltst_cache_get_link(struct nl_cache *cache,
527 const char *ifname);
528
529 struct nl_cache *_nltst_rtnl_link_alloc_cache(struct nl_sock *sk,
530 int addr_family, unsigned flags);
531
532 struct nl_cache *_nltst_rtnl_route_alloc_cache(struct nl_sock *sk,
533 int addr_family);
534
535 struct nl_sock *_nltst_socket(int protocol);
536
537 void _nltst_add_link(struct nl_sock *sk, const char *ifname, const char *kind,
538 int *out_ifindex);
539
540 void _nltst_delete_link(struct nl_sock *sk, const char *ifname);
541
542 void _nltst_get_link(struct nl_sock *sk, const char *ifname, int *out_ifindex,
543 struct rtnl_link **out_link);
544
545 void _nltst_add_dummy_and_up(struct nl_sock *sk, const char *ifname,
546 int *out_ifindex);
547 void _nltst_add_dummy_v4_with_addr(struct nl_sock *sk, const char *ifname,
548 int *out_ifindex, const char *ip,
549 int prefixlen);
550 void _nltst_add_dummy_v6_with_addr(struct nl_sock *sk, const char *ifname,
551 int *out_ifindex, const char *ip,
552 int prefixlen);
553
554 void _nltst_addr4_add(struct nl_sock *sk, int ifindex, const char *ip,
555 int prefixlen);
556 void _nltst_addr6_add(struct nl_sock *sk, int ifindex, const char *ip,
557 int prefixlen);
558
559 void _nltst_assert_route_list(struct nl_object *const *objs, ssize_t len,
560 const char *const *expected_routes);
561
562 void _nltst_assert_route_cache_v(struct nl_cache *cache,
563 const char *const *expected_routes);
564
565 #define _nltst_assert_route_cache(cache, ...) \
566 _nltst_assert_route_cache_v(cache, \
567 ((const char *const[200]){ __VA_ARGS__ }))
568
569 static void _nltst_delete_link_no_sock(const char *ifname)
570 {
571 if (ifname)
572 _nltst_delete_link(NULL, ifname);
573 }
574 #define _nltst_auto_delete_link _nl_auto(_nltst_auto_delete_link_fcn)
575 _NL_AUTO_DEFINE_FCN_TYPED0(const char *, _nltst_auto_delete_link_fcn,
576 _nltst_delete_link_no_sock);
577
578 #endif /* __NL_TEST_UTIL_H__ */