]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/test-routing-policy-rule.c
c619dbc1594387959d93a94b2d4692337cd4727d
[thirdparty/systemd.git] / src / network / test-routing-policy-rule.c
1 /***
2 SPDX-License-Identifier: LGPL-2.1+
3
4 This file is part of systemd.
5
6 Copyright 2017 Zbigniew Jędrzejewski-Szmek
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "fd-util.h"
23 #include "fileio.h"
24 #include "log.h"
25 #include "macro.h"
26 #include "network-internal.h"
27 #include "networkd-manager.h"
28 #include "string-util.h"
29
30 static void test_rule_serialization(const char *title, const char *ruleset, const char *expected) {
31 char pattern[] = "/tmp/systemd-test-routing-policy-rule.XXXXXX",
32 pattern2[] = "/tmp/systemd-test-routing-policy-rule.XXXXXX",
33 pattern3[] = "/tmp/systemd-test-routing-policy-rule.XXXXXX";
34 const char *cmd;
35 int fd, fd2, fd3;
36 _cleanup_fclose_ FILE *f = NULL, *f2 = NULL, *f3 = NULL;
37 _cleanup_set_free_free_ Set *rules = NULL;
38 _cleanup_free_ char *buf = NULL;
39 size_t buf_size;
40
41 log_info("========== %s ==========", title);
42 log_info("put:\n%s\n", ruleset);
43
44 assert_se((fd = mkostemp_safe(pattern)) >= 0);
45 assert_se(f = fdopen(fd, "a+e"));
46 assert_se(write_string_stream(f, ruleset, 0) == 0);
47
48 assert_se(routing_policy_load_rules(pattern, &rules) == 0);
49
50 assert_se((fd2 = mkostemp_safe(pattern2)) >= 0);
51 assert_se(f2 = fdopen(fd2, "a+e"));
52
53 assert_se(routing_policy_serialize_rules(rules, f2) == 0);
54 assert_se(fflush_and_check(f2) == 0);
55
56 assert_se(read_full_file(pattern2, &buf, &buf_size) == 0);
57
58 log_info("got:\n%s", buf);
59
60 assert_se((fd3 = mkostemp_safe(pattern3)) >= 0);
61 assert_se(f3 = fdopen(fd3, "we"));
62 assert_se(write_string_stream(f3, expected ?: ruleset, 0) == 0);
63
64 cmd = strjoina("diff -u ", pattern3, " ", pattern2);
65 log_info("$ %s", cmd);
66 assert_se(system(cmd) == 0);
67 }
68
69 int main(int argc, char **argv) {
70 _cleanup_free_ char *p = NULL;
71
72 log_set_max_level(LOG_DEBUG);
73 log_parse_environment();
74 log_open();
75
76 test_rule_serialization("basic parsing",
77 "RULE=from=1.2.3.4/32 to=2.3.4.5/32 tos=5 fwmark=1/2 table=10", NULL);
78
79 test_rule_serialization("ignored values",
80 "RULE=something=to=ignore from=1.2.3.4/32 from=1.2.3.4/32"
81 " \t to=2.3.4.5/24 to=2.3.4.5/32 tos=5 fwmark=2 fwmark=1 table=10 table=20",
82 "RULE=from=1.2.3.4/32"
83 " to=2.3.4.5/32 tos=5 fwmark=1/0 table=20");
84
85 test_rule_serialization("ipv6",
86 "RULE=from=1::2/64 to=2::3/64 table=6", NULL);
87
88 assert_se(asprintf(&p, "RULE=from=1::2/64 to=2::3/64 table=%d", RT_TABLE_MAIN) >= 0);
89 test_rule_serialization("default table",
90 "RULE=from=1::2/64 to=2::3/64", p);
91
92 test_rule_serialization("incoming interface",
93 "RULE=from=1::2/64 to=2::3/64 table=1 iif=lo",
94 "RULE=from=1::2/64 to=2::3/64 iif=lo table=1");
95
96 test_rule_serialization("outgoing interface",
97 "RULE=from=1::2/64 to=2::3/64 oif=eth0 table=1", NULL);
98
99 return 0;
100 }