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