]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/test-routing-policy-rule.c
2 SPDX-License-Identifier: LGPL-2.1+
9 #include "network-internal.h"
10 #include "networkd-manager.h"
11 #include "string-util.h"
13 #include "tmpfile-util.h"
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";
21 _cleanup_fclose_
FILE *f
= NULL
, *f2
= NULL
, *f3
= NULL
;
23 _cleanup_free_
char *buf
= NULL
;
26 log_info("========== %s ==========", title
);
27 log_info("put:\n%s\n", ruleset
);
29 fd
= mkostemp_safe(pattern
);
31 assert_se(f
= fdopen(fd
, "a+"));
32 assert_se(write_string_stream(f
, ruleset
, 0) == 0);
34 assert_se(routing_policy_load_rules(pattern
, &rules
) == 0);
36 fd2
= mkostemp_safe(pattern2
);
38 assert_se(f2
= fdopen(fd2
, "a+"));
40 assert_se(routing_policy_serialize_rules(rules
, f2
) == 0);
41 assert_se(fflush_and_check(f2
) == 0);
43 assert_se(read_full_file(pattern2
, &buf
, &buf_size
) == 0);
45 log_info("got:\n%s", buf
);
47 fd3
= mkostemp_safe(pattern3
);
49 assert_se(f3
= fdopen(fd3
, "w"));
50 assert_se(write_string_stream(f3
, expected
?: ruleset
, 0) == 0);
52 cmd
= strjoina("diff -u ", pattern3
, " ", pattern2
);
53 log_info("$ %s", cmd
);
54 assert_se(system(cmd
) == 0);
59 int main(int argc
, char **argv
) {
60 _cleanup_free_
char *p
= NULL
;
62 test_setup_logging(LOG_DEBUG
);
64 test_rule_serialization("basic parsing",
65 "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
);
67 test_rule_serialization("ignored values",
68 "RULE=something=to=ignore from=1.2.3.4/32 from=1.2.3.4/32"
69 " \t to=2.3.4.5/24 to=2.3.4.5/32 tos=5 fwmark=2 fwmark=1 table=10 table=20",
70 "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");
72 test_rule_serialization("ipv6",
73 "RULE=family=AF_INET6 from=1::2/64 to=2::3/64 invert_rule=yes table=6", NULL
);
75 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);
76 test_rule_serialization("default table",
77 "RULE=from=1::2/64 to=2::3/64", p
);
79 test_rule_serialization("incoming interface",
80 "RULE=from=1::2/64 to=2::3/64 table=1 iif=lo",
81 "RULE=family=AF_INET6 from=1::2/64 to=2::3/64 iif=lo invert_rule=no table=1");
83 test_rule_serialization("outgoing interface",
84 "RULE=family=AF_INET6 from=1::2/64 to=2::3/64 oif=eth0 invert_rule=no table=1", NULL
);
86 test_rule_serialization("freeing interface names",
87 "RULE=from=1::2/64 to=2::3/64 family=AF_INET6 iif=e0 iif=e1 oif=e0 oif=e1 table=1",
88 "RULE=family=AF_INET6 from=1::2/64 to=2::3/64 iif=e1 oif=e1 invert_rule=no table=1");
90 test_rule_serialization("ignoring invalid family",
91 "RULE=from=1::2/64 to=2::3/64 family=AF_UNSEPC family=AF_INET table=1",
92 "RULE=family=AF_INET6 from=1::2/64 to=2::3/64 invert_rule=no table=1");