Debian Codegraph @

src/test/test-socket-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <grp.h>
5 #include <linux/pkt_sched.h>
6 #include <netinet/ip.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "escape.h"
11 #include "fd-util.h"
12 #include "fs-util.h"
13 #include "in-addr-util.h"
14 #include "iovec-util.h"
15 #include "log.h"
16 #include "path-util.h"
17 #include "pidref.h"
18 #include "process-util.h"
19 #include "random-util.h"
20 #include "rm-rf.h"
21 #include "socket-util.h"
22 #include "tests.h"
23 #include "tmpfile-util.h"
24 #include "user-util.h"
25
26 assert_cc(SUN_PATH_LEN == 108);
27
28 TEST(ifname_valid) {
29 assert_se( ifname_valid("foo"));
30 assert_se( ifname_valid("eth0"));
31
32 assert_se(!ifname_valid("0"));
33 assert_se(!ifname_valid("99"));
34 assert_se( ifname_valid("a99"));
35 assert_se( ifname_valid("99a"));
36
37 assert_se(!ifname_valid(NULL));
38 assert_se(!ifname_valid(""));
39 assert_se(!ifname_valid(" "));
40 assert_se(!ifname_valid(" foo"));
41 assert_se(!ifname_valid("bar\n"));
42 assert_se(!ifname_valid("."));
43 assert_se(!ifname_valid(".."));
44 assert_se(ifname_valid("foo.bar"));
45 assert_se(!ifname_valid("x:y"));
46
47 assert_se( ifname_valid_full("xxxxxxxxxxxxxxx", 0));
48 assert_se(!ifname_valid_full("xxxxxxxxxxxxxxxx", 0));
49 assert_se( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
50 assert_se( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
51 assert_se(!ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE));
52 assert_se( ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
53 assert_se(!ifname_valid_full("0", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
54 }
55
56 static void test_socket_print_unix_one(const char *in, size_t len_in, const char *expected) {
57 _cleanup_free_ char *out = NULL, *c = NULL;
58
59 assert_se(len_in <= SUN_PATH_LEN);
60 SocketAddress a = { .sockaddr = { .un = { .sun_family = AF_UNIX } },
61 .size = offsetof(struct sockaddr_un, sun_path) + len_in,
62 .type = SOCK_STREAM,
63 };
64 memcpy(a.sockaddr.un.sun_path, in, len_in);
65
66 assert_se(socket_address_print(&a, &out) >= 0);
67 assert_se(c = cescape(in));
68 log_info("\"%s\" → \"%s\" (expect \"%s\")", in, out, expected);
69 ASSERT_STREQ(out, expected);
70 }
71
72 TEST(socket_print_unix) {
73 /* Some additional tests for abstract addresses which we don't parse */
74
75 test_socket_print_unix_one("\0\0\0\0", 4, "@\\000\\000\\000");
76 test_socket_print_unix_one("@abs", 5, "@abs");
77 test_socket_print_unix_one("\n", 2, "\\n");
78 test_socket_print_unix_one("", 1, "<unnamed>");
79 test_socket_print_unix_one("\0", 1, "<unnamed>");
80 test_socket_print_unix_one("\0_________________________there's 108 characters in this string_____________________________________________", 108,
81 "@_________________________there\\'s 108 characters in this string_____________________________________________");
82 test_socket_print_unix_one("////////////////////////////////////////////////////////////////////////////////////////////////////////////", 108,
83 "////////////////////////////////////////////////////////////////////////////////////////////////////////////");
84 test_socket_print_unix_one("\0\a\b\n\255", 6, "@\\a\\b\\n\\255\\000");
85 }
86
87 TEST(sockaddr_equal) {
88 union sockaddr_union a = {
89 .in.sin_family = AF_INET,
90 .in.sin_port = 0,
91 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
92 };
93 union sockaddr_union b = {
94 .in.sin_family = AF_INET,
95 .in.sin_port = 0,
96 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
97 };
98 union sockaddr_union c = {
99 .in.sin_family = AF_INET,
100 .in.sin_port = 0,
101 .in.sin_addr.s_addr = htobe32(1234),
102 };
103 union sockaddr_union d = {
104 .in6.sin6_family = AF_INET6,
105 .in6.sin6_port = 0,
106 .in6.sin6_addr = IN6ADDR_ANY_INIT,
107 };
108 union sockaddr_union e = {
109 .vm.svm_family = AF_VSOCK,
110 .vm.svm_port = 0,
111 .vm.svm_cid = VMADDR_CID_ANY,
112 };
113
114 assert_se(sockaddr_equal(&a, &a));
115 assert_se(sockaddr_equal(&a, &b));
116 assert_se(sockaddr_equal(&d, &d));
117 assert_se(sockaddr_equal(&e, &e));
118 assert_se(!sockaddr_equal(&a, &c));
119 assert_se(!sockaddr_equal(&b, &c));
120 assert_se(!sockaddr_equal(&a, &e));
121 }
122
123 TEST(sockaddr_un_len) {
124 static const struct sockaddr_un fs = {
125 .sun_family = AF_UNIX,
126 .sun_path = "/foo/bar/waldo",
127 };
128
129 static const struct sockaddr_un abstract = {
130 .sun_family = AF_UNIX,
131 .sun_path = "\0foobar",
132 };
133
134 assert_se(sockaddr_un_len(&fs) == offsetof(struct sockaddr_un, sun_path) + strlen(fs.sun_path) + 1);
135 assert_se(sockaddr_un_len(&abstract) == offsetof(struct sockaddr_un, sun_path) + 1 + strlen(abstract.sun_path + 1));
136 }
137
138 TEST(in_addr_is_multicast) {
139 union in_addr_union a, b;
140 int f;
141
142 assert_se(in_addr_from_string_auto("192.168.3.11", &f, &a) >= 0);
143 assert_se(in_addr_is_multicast(f, &a) == 0);
144
145 assert_se(in_addr_from_string_auto("224.0.0.1", &f, &a) >= 0);
146 assert_se(in_addr_is_multicast(f, &a) == 1);
147
148 assert_se(in_addr_from_string_auto("FF01:0:0:0:0:0:0:1", &f, &b) >= 0);
149 assert_se(in_addr_is_multicast(f, &b) == 1);
150
151 assert_se(in_addr_from_string_auto("2001:db8::c:69b:aeff:fe53:743e", &f, &b) >= 0);
152 assert_se(in_addr_is_multicast(f, &b) == 0);
153 }
154
155 TEST(getpeercred_getpeergroups) {
156 int r;
157
158 r = ASSERT_OK(pidref_safe_fork("(getpeercred)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
159
160 if (r == 0) {
161 static const gid_t gids[] = { 3, 4, 5, 6, 7 };
162 gid_t *test_gids;
163 size_t n_test_gids;
164 uid_t test_uid;
165 gid_t test_gid;
166 struct ucred ucred;
167 int pair[2] = EBADF_PAIR;
168
169 if (geteuid() == 0 && !userns_has_single_user()) {
170 test_uid = 1;
171 test_gid = 2;
172 test_gids = (gid_t*) gids;
173 n_test_gids = ELEMENTSOF(gids);
174
175 assert_se(fully_set_uid_gid(test_uid, test_gid, test_gids, n_test_gids) >= 0);
176 } else {
177 long ngroups_max;
178
179 test_uid = getuid();
180 test_gid = getgid();
181
182 ngroups_max = sysconf(_SC_NGROUPS_MAX);
183 assert_se(ngroups_max > 0);
184
185 test_gids = newa(gid_t, ngroups_max);
186
187 r = getgroups(ngroups_max, test_gids);
188 assert_se(r >= 0);
189 n_test_gids = (size_t) r;
190 }
191
192 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);
193
194 assert_se(getpeercred(pair[0], &ucred) >= 0);
195
196 assert_se(ucred.uid == test_uid);
197 assert_se(ucred.gid == test_gid);
198 assert_se(ucred.pid == getpid_cached());
199
200 {
201 _cleanup_free_ gid_t *peer_groups = NULL;
202
203 r = getpeergroups(pair[0], &peer_groups);
204 assert_se(r >= 0 || IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT));
205
206 if (r >= 0) {
207 assert_se((size_t) r == n_test_gids);
208 assert_se(memcmp(peer_groups, test_gids, sizeof(gid_t) * n_test_gids) == 0);
209 }
210 }
211
212 safe_close_pair(pair);
213 _exit(EXIT_SUCCESS);
214 }
215 }
216
217 TEST(passfd_read) {
218 static const char file_contents[] = "test contents for passfd";
219 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
220 int r;
221
222 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
223
224 r = ASSERT_OK(pidref_safe_fork("(passfd_read)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
225
226 if (r == 0) {
227 /* Child */
228 pair[0] = safe_close(pair[0]);
229
230 char tmpfile[] = "/tmp/test-socket-util-passfd-read-XXXXXX";
231 assert_se(write_tmpfile(tmpfile, file_contents) == 0);
232
233 _cleanup_close_ int tmpfd = open(tmpfile, O_RDONLY);
234 assert_se(tmpfd >= 0);
235 assert_se(unlink(tmpfile) == 0);
236
237 assert_se(send_one_fd(pair[1], tmpfd, MSG_DONTWAIT) == 0);
238 _exit(EXIT_SUCCESS);
239 }
240
241 /* Parent */
242 char buf[64];
243 struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
244 _cleanup_close_ int fd = -EBADF;
245
246 pair[1] = safe_close(pair[1]);
247
248 assert_se(receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd) == 0);
249
250 assert_se(fd >= 0);
251 ssize_t n = read(fd, buf, sizeof(buf)-1);
252 assert_se(n >= 0);
253 buf[n] = 0;
254 ASSERT_STREQ(buf, file_contents);
255 }
256
257 TEST(passfd_contents_read) {
258 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
259 static const char file_contents[] = "test contents in the file";
260 static const char wire_contents[] = "test contents on the wire";
261 int r;
262
263 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
264
265 r = ASSERT_OK(pidref_safe_fork("(passfd_contents_read)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
266
267 if (r == 0) {
268 /* Child */
269 struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
270 char tmpfile[] = "/tmp/test-socket-util-passfd-contents-read-XXXXXX";
271
272 pair[0] = safe_close(pair[0]);
273
274 assert_se(write_tmpfile(tmpfile, file_contents) == 0);
275
276 _cleanup_close_ int tmpfd = open(tmpfile, O_RDONLY);
277 assert_se(tmpfd >= 0);
278 assert_se(unlink(tmpfile) == 0);
279
280 assert_se(send_one_fd_iov(pair[1], tmpfd, &iov, 1, MSG_DONTWAIT) > 0);
281 _exit(EXIT_SUCCESS);
282 }
283
284 /* Parent */
285 char buf[64];
286 struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
287 _cleanup_close_ int fd = -EBADF;
288 ssize_t k;
289
290 pair[1] = safe_close(pair[1]);
291
292 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
293 assert_se(k > 0);
294 buf[k] = 0;
295 ASSERT_STREQ(buf, wire_contents);
296
297 assert_se(fd >= 0);
298 r = read(fd, buf, sizeof(buf)-1);
299 assert_se(r >= 0);
300 buf[r] = 0;
301 ASSERT_STREQ(buf, file_contents);
302 }
303
304 TEST(receive_nopassfd) {
305 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
306 static const char wire_contents[] = "no fd passed here";
307 int r;
308
309 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
310
311 r = ASSERT_OK(pidref_safe_fork("(receive_nopassfd)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
312 assert_se(r >= 0);
313
314 if (r == 0) {
315 /* Child */
316 struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
317
318 pair[0] = safe_close(pair[0]);
319
320 assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) > 0);
321 _exit(EXIT_SUCCESS);
322 }
323
324 /* Parent */
325 char buf[64];
326 struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
327 int fd = -999;
328 ssize_t k;
329
330 pair[1] = safe_close(pair[1]);
331
332 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
333 assert_se(k > 0);
334 buf[k] = 0;
335 ASSERT_STREQ(buf, wire_contents);
336
337 /* no fd passed here, confirm it was reset */
338 assert_se(fd == -EBADF);
339 }
340
341 TEST(send_nodata_nofd) {
342 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
343 int r;
344
345 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
346
347 r = ASSERT_OK(pidref_safe_fork("(send_nodata_nofd)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
348
349 if (r == 0) {
350 /* Child */
351 pair[0] = safe_close(pair[0]);
352
353 assert_se(send_one_fd_iov(pair[1], -1, NULL, 0, MSG_DONTWAIT) == -EINVAL);
354 _exit(EXIT_SUCCESS);
355 }
356
357 /* Parent */
358 char buf[64];
359 struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
360 int fd = -999;
361 ssize_t k;
362
363 pair[1] = safe_close(pair[1]);
364
365 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
366 /* recvmsg() will return errno EAGAIN if nothing was sent */
367 assert_se(k == -EAGAIN);
368
369 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
370 assert_se(fd == -999);
371 }
372
373 TEST(send_emptydata) {
374 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
375 int r;
376
377 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
378
379 r = ASSERT_OK(pidref_safe_fork("(send_emptydata)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
380
381 if (r == 0) {
382 /* Child */
383 pair[0] = safe_close(pair[0]);
384
385 /* This will succeed, since iov is set. */
386 assert_se(send_one_fd_iov(pair[1], -1, &iovec_empty, 1, MSG_DONTWAIT) == 0);
387 _exit(EXIT_SUCCESS);
388 }
389
390 /* Parent */
391 char buf[64];
392 struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
393 int fd = -999;
394 ssize_t k;
395
396 pair[1] = safe_close(pair[1]);
397
398 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
399 /* receive_one_fd_iov() returns -EIO if an fd is not found and no data was returned. */
400 assert_se(k == -EIO);
401
402 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
403 assert_se(fd == -999);
404 }
405
406 TEST(flush_accept) {
407 _cleanup_close_ int listen_stream, listen_dgram, listen_seqpacket, connect_stream, connect_dgram, connect_seqpacket;
408 static const union sockaddr_union sa = { .un.sun_family = AF_UNIX };
409 union sockaddr_union lsa;
410 socklen_t l;
411
412 listen_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
413 assert_se(listen_stream >= 0);
414
415 listen_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
416 assert_se(listen_dgram >= 0);
417
418 listen_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
419 assert_se(listen_seqpacket >= 0);
420
421 assert_se(flush_accept(listen_stream) < 0);
422 assert_se(flush_accept(listen_dgram) < 0);
423 assert_se(flush_accept(listen_seqpacket) < 0);
424
425 assert_se(bind(listen_stream, &sa.sa, sizeof(sa_family_t)) >= 0);
426 assert_se(bind(listen_dgram, &sa.sa, sizeof(sa_family_t)) >= 0);
427 assert_se(bind(listen_seqpacket, &sa.sa, sizeof(sa_family_t)) >= 0);
428
429 assert_se(flush_accept(listen_stream) < 0);
430 assert_se(flush_accept(listen_dgram) < 0);
431 assert_se(flush_accept(listen_seqpacket) < 0);
432
433 assert_se(listen(listen_stream, SOMAXCONN_DELUXE) >= 0);
434 assert_se(listen(listen_dgram, SOMAXCONN_DELUXE) < 0);
435 assert_se(listen(listen_seqpacket, SOMAXCONN_DELUXE) >= 0);
436
437 assert_se(flush_accept(listen_stream) >= 0);
438 assert_se(flush_accept(listen_dgram) < 0);
439 assert_se(flush_accept(listen_seqpacket) >= 0);
440
441 connect_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
442 assert_se(connect_stream >= 0);
443
444 connect_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
445 assert_se(connect_dgram >= 0);
446
447 connect_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
448 assert_se(connect_seqpacket >= 0);
449
450 l = sizeof(lsa);
451 assert_se(getsockname(listen_stream, &lsa.sa, &l) >= 0);
452 assert_se(connect(connect_stream, &lsa.sa, l) >= 0);
453
454 l = sizeof(lsa);
455 assert_se(getsockname(listen_dgram, &lsa.sa, &l) >= 0);
456 assert_se(connect(connect_dgram, &lsa.sa, l) >= 0);
457
458 l = sizeof(lsa);
459 assert_se(getsockname(listen_seqpacket, &lsa.sa, &l) >= 0);
460 assert_se(connect(connect_seqpacket, &lsa.sa, l) >= 0);
461
462 assert_se(flush_accept(listen_stream) >= 0);
463 assert_se(flush_accept(listen_dgram) < 0);
464 assert_se(flush_accept(listen_seqpacket) >= 0);
465 }
466
467 TEST(ipv6_enabled) {
468 log_info("IPv6 supported: %s", yes_no(socket_ipv6_is_supported()));
469 log_info("IPv6 enabled: %s", yes_no(socket_ipv6_is_enabled()));
470 }
471
472 TEST(sockaddr_un_set_path) {
473 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
474 _cleanup_(unlink_and_freep) char *sh = NULL;
475 _cleanup_free_ char *j = NULL;
476 union sockaddr_union sa;
477 _cleanup_close_ int fd1, fd2, fd3;
478
479 assert_se(mkdtemp_malloc("/tmp/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXXXXXX", &t) >= 0);
480 assert_se(strlen(t) > SUN_PATH_LEN);
481
482 assert_se(j = path_join(t, "sock"));
483 assert_se(sockaddr_un_set_path(&sa.un, j) == -ENAMETOOLONG); /* too long for AF_UNIX socket */
484
485 assert_se(asprintf(&sh, "/tmp/%" PRIx64, random_u64()) >= 0);
486 assert_se(symlink(t, sh) >= 0); /* create temporary symlink, to access it anyway */
487
488 free(j);
489 assert_se(j = path_join(sh, "sock"));
490 assert_se(sockaddr_un_set_path(&sa.un, j) >= 0);
491
492 fd1 = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
493 assert_se(fd1 >= 0);
494 assert_se(bind(fd1, &sa.sa, sockaddr_len(&sa)) >= 0);
495 assert_se(listen(fd1, 1) >= 0);
496
497 sh = unlink_and_free(sh); /* remove temporary symlink */
498
499 fd2 = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
500 assert_se(fd2 >= 0);
501 assert_se(connect(fd2, &sa.sa, sockaddr_len(&sa)) < 0);
502 assert_se(errno == ENOENT); /* we removed the symlink, must fail */
503
504 free(j);
505 assert_se(j = path_join(t, "sock"));
506
507 fd3 = open(j, O_CLOEXEC|O_PATH|O_NOFOLLOW);
508 assert_se(fd3 > 0);
509 assert_se(sockaddr_un_set_path(&sa.un, FORMAT_PROC_FD_PATH(fd3)) >= 0); /* connect via O_PATH instead, circumventing 108ch limit */
510
511 assert_se(connect(fd2, &sa.sa, sockaddr_len(&sa)) >= 0);
512 }
513
514 TEST(getpeerpidref) {
515 _cleanup_close_pair_ int fd[2] = EBADF_PAIR;
516
517 ASSERT_OK(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fd));
518
519 _cleanup_(pidref_done) PidRef pidref0 = PIDREF_NULL, pidref1 = PIDREF_NULL, pidref_self = PIDREF_NULL, pidref_pid1 = PIDREF_NULL;
520 ASSERT_OK(getpeerpidref(fd[0], &pidref0));
521 ASSERT_OK(getpeerpidref(fd[1], &pidref1));
522
523 ASSERT_OK(pidref_set_self(&pidref_self));
524 ASSERT_OK(pidref_set_pid(&pidref_pid1, 1));
525
526 ASSERT_TRUE(pidref_equal(&pidref0, &pidref1));
527 ASSERT_TRUE(pidref_equal(&pidref0, &pidref_self));
528 ASSERT_TRUE(pidref_equal(&pidref1, &pidref_self));
529
530 ASSERT_TRUE(!pidref_equal(&pidref_self, &pidref_pid1));
531 ASSERT_TRUE(!pidref_equal(&pidref1, &pidref_pid1));
532 ASSERT_TRUE(!pidref_equal(&pidref0, &pidref_pid1));
533 }
534
535 TEST(tos_to_priority) {
536 ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS7), TC_PRIO_CONTROL);
537 ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS6), TC_PRIO_CONTROL);
538 ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS5), TC_PRIO_INTERACTIVE);
539 ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS4), TC_PRIO_INTERACTIVE);
540 ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS3), TC_PRIO_INTERACTIVE_BULK);
541 ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS2), TC_PRIO_INTERACTIVE_BULK);
542 ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS1), TC_PRIO_BULK);
543 ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS0), TC_PRIO_BESTEFFORT);
544
545 /* check if lower bits are correctly filtered. */
546 ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS7 | IPTOS_LOWDELAY), TC_PRIO_CONTROL);
547 ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS1 | IPTOS_LOWCOST), TC_PRIO_BULK);
548
549 ASSERT_EQ(tos_to_priority(0x00), TC_PRIO_BESTEFFORT);
550 ASSERT_EQ(tos_to_priority(0xff), TC_PRIO_CONTROL);
551 }
552
553 DEFINE_TEST_MAIN(LOG_DEBUG);