]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/test-routing-policy-rule.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / network / test-routing-policy-rule.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "fd-util.h"
4 #include "fileio.h"
5 #include "networkd-routing-policy-rule.h"
6 #include "string-util.h"
7 #include "tests.h"
8 #include "tmpfile-util.h"
9
10 static void test_rule_serialization(const char *title, const char *ruleset, const char *expected) {
11 char pattern[] = "/tmp/systemd-test-routing-policy-rule.XXXXXX",
12 pattern2[] = "/tmp/systemd-test-routing-policy-rule.XXXXXX",
13 pattern3[] = "/tmp/systemd-test-routing-policy-rule.XXXXXX";
14 const char *cmd;
15 int fd, fd2, fd3;
16 _cleanup_fclose_ FILE *f = NULL, *f2 = NULL, *f3 = NULL;
17 Set *rules = NULL;
18 _cleanup_free_ char *buf = NULL;
19 size_t buf_size;
20
21 log_info("========== %s ==========", title);
22 log_info("put:\n%s\n", ruleset);
23
24 fd = mkostemp_safe(pattern);
25 assert_se(fd >= 0);
26 assert_se(f = fdopen(fd, "a+"));
27 assert_se(write_string_stream(f, ruleset, 0) == 0);
28
29 assert_se(routing_policy_load_rules(pattern, &rules) == 0);
30
31 fd2 = mkostemp_safe(pattern2);
32 assert_se(fd2 >= 0);
33 assert_se(f2 = fdopen(fd2, "a+"));
34
35 assert_se(routing_policy_serialize_rules(rules, f2) == 0);
36 assert_se(fflush_and_check(f2) == 0);
37
38 assert_se(read_full_file(pattern2, &buf, &buf_size) == 0);
39
40 log_info("got:\n%s", buf);
41
42 fd3 = mkostemp_safe(pattern3);
43 assert_se(fd3 >= 0);
44 assert_se(f3 = fdopen(fd3, "w"));
45 assert_se(write_string_stream(f3, expected ?: ruleset, 0) == 0);
46
47 cmd = strjoina("diff -u ", pattern3, " ", pattern2);
48 log_info("$ %s", cmd);
49 assert_se(system(cmd) == 0);
50
51 set_free(rules);
52 }
53
54 int main(int argc, char **argv) {
55 _cleanup_free_ char *p = NULL;
56
57 test_setup_logging(LOG_DEBUG);
58
59 test_rule_serialization("basic parsing",
60 "RULE=family=AF_INET from=1.2.3.4/32 to=2.3.4.5/32 tos=5 priority=10 fwmark=1/2 invert_rule=yes table=10", NULL);
61
62 test_rule_serialization("ignored values",
63 "RULE=something=to=ignore from=1.2.3.4/32 from=1.2.3.4/32"
64 " \t to=2.3.4.5/24 to=2.3.4.5/32 tos=5 fwmark=2 fwmark=1 table=10 table=20",
65 "RULE=family=AF_INET from=1.2.3.4/32 to=2.3.4.5/32 tos=5 fwmark=1 invert_rule=no table=20");
66
67 test_rule_serialization("ipv6",
68 "RULE=family=AF_INET6 from=1::2/64 to=2::3/64 invert_rule=yes table=6", NULL);
69
70 assert_se(asprintf(&p, "RULE=family=AF_INET6 from=1::2/64 to=2::3/64 invert_rule=no table=%d", RT_TABLE_MAIN) >= 0);
71 test_rule_serialization("default table",
72 "RULE=from=1::2/64 to=2::3/64", p);
73
74 test_rule_serialization("incoming interface",
75 "RULE=from=1::2/64 to=2::3/64 table=1 iif=lo",
76 "RULE=family=AF_INET6 from=1::2/64 to=2::3/64 iif=lo invert_rule=no table=1");
77
78 test_rule_serialization("outgoing interface",
79 "RULE=family=AF_INET6 from=1::2/64 to=2::3/64 oif=eth0 invert_rule=no table=1", NULL);
80
81 test_rule_serialization("freeing interface names",
82 "RULE=from=1::2/64 to=2::3/64 family=AF_INET6 iif=e0 iif=e1 oif=e0 oif=e1 table=1",
83 "RULE=family=AF_INET6 from=1::2/64 to=2::3/64 iif=e1 oif=e1 invert_rule=no table=1");
84
85 test_rule_serialization("ignoring invalid family",
86 "RULE=from=1::2/64 to=2::3/64 family=AF_UNSEPC family=AF_INET table=1",
87 "RULE=family=AF_INET6 from=1::2/64 to=2::3/64 invert_rule=no table=1");
88
89 return 0;
90 }