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