]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
f3b136a4 | 2 | |
9383fa08 YW |
3 | #include <sys/utsname.h> |
4 | ||
3448711b LP |
5 | #include "sd-id128.h" |
6 | ||
7 | #include "errno-util.h" | |
8 | #include "hostname-util.h" | |
f3b136a4 ZJS |
9 | #include "strv.h" |
10 | #include "sysctl-util.h" | |
11 | #include "tests.h" | |
12 | ||
89edc243 | 13 | static const char* const cases[] = { |
f3b136a4 ZJS |
14 | "a.b.c", "a/b/c", |
15 | "a/b/c", "a/b/c", | |
16 | "a/b.c/d", "a/b.c/d", | |
17 | "a.b/c.d", "a/b.c/d", | |
18 | ||
19 | "net.ipv4.conf.enp3s0/200.forwarding", "net/ipv4/conf/enp3s0.200/forwarding", | |
20 | "net/ipv4/conf/enp3s0.200/forwarding", "net/ipv4/conf/enp3s0.200/forwarding", | |
21 | ||
22 | "a...b...c", "a/b/c", | |
23 | "a///b///c", "a/b/c", | |
24 | ".a...b...c", "a/b/c", | |
25 | "/a///b///c", "a/b/c", | |
26 | NULL, | |
27 | }; | |
28 | ||
4f7452a8 | 29 | TEST(sysctl_normalize) { |
89edc243 | 30 | STRV_FOREACH_PAIR(s, expected, (const char**) cases) { |
f3b136a4 ZJS |
31 | _cleanup_free_ char *t; |
32 | ||
33 | assert_se(t = strdup(*s)); | |
34 | assert_se(sysctl_normalize(t) == t); | |
35 | ||
36 | log_info("\"%s\" → \"%s\", expected \"%s\"", *s, t, *expected); | |
37 | assert_se(streq(t, *expected)); | |
38 | } | |
39 | } | |
40 | ||
4f7452a8 | 41 | TEST(sysctl_read) { |
9383fa08 YW |
42 | _cleanup_free_ char *s = NULL; |
43 | struct utsname u; | |
3448711b LP |
44 | sd_id128_t a, b; |
45 | int r; | |
46 | ||
47 | assert_se(sysctl_read("kernel/random/boot_id", &s) >= 0); | |
48 | assert_se(sd_id128_from_string(s, &a) >= 0); | |
49 | assert_se(sd_id128_get_boot(&b) >= 0); | |
50 | assert_se(sd_id128_equal(a, b)); | |
51 | s = mfree(s); | |
52 | ||
53 | assert_se(sysctl_read_ip_property(AF_INET, "lo", "forwarding", &s)); | |
54 | assert_se(STR_IN_SET(s, "0", "1")); | |
55 | ||
56 | r = sysctl_write_ip_property(AF_INET, "lo", "forwarding", s); | |
57 | assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || r == -EROFS); | |
58 | s = mfree(s); | |
59 | ||
60 | assert_se(sysctl_read_ip_property(AF_INET, NULL, "ip_forward", &s)); | |
61 | assert_se(STR_IN_SET(s, "0", "1")); | |
62 | ||
63 | r = sysctl_write_ip_property(AF_INET, NULL, "ip_forward", s); | |
64 | assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || r == -EROFS); | |
65 | s = mfree(s); | |
66 | ||
67 | assert_se(sysctl_read("kernel/hostname", &s) >= 0); | |
9383fa08 YW |
68 | assert_se(uname(&u) >= 0); |
69 | assert_se(streq_ptr(s, u.nodename)); | |
3448711b LP |
70 | |
71 | r = sysctl_write("kernel/hostname", s); | |
72 | assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || r == -EROFS); | |
73 | } | |
74 | ||
4f7452a8 | 75 | DEFINE_TEST_MAIN(LOG_INFO); |