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