| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/socket.h> |
| 4 |
#include <sys/utsname.h> |
| 5 |
|
| 6 |
#include "sd-id128.h" |
| 7 |
|
| 8 |
#include "errno-util.h" |
| 9 |
#include "strv.h" |
| 10 |
#include "sysctl-util.h" |
| 11 |
#include "tests.h" |
| 12 |
|
| 13 |
static const char* const cases[] = { |
| 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 |
|
| 29 |
TEST(sysctl_normalize) { |
| 30 |
STRV_FOREACH_PAIR(s, expected, cases) { |
| 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_STREQ(t, *expected); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
TEST(sysctl_read) { |
| 42 |
_cleanup_free_ char *s = NULL; |
| 43 |
struct utsname u; |
| 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, NULL); |
| 57 |
assert_se(r >= 0 || ERRNO_IS_NEG_FS_WRITE_REFUSED(r)); |
| 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, NULL); |
| 64 |
assert_se(r >= 0 || ERRNO_IS_NEG_FS_WRITE_REFUSED(r)); |
| 65 |
s = mfree(s); |
| 66 |
|
| 67 |
assert_se(sysctl_read("kernel/hostname", &s) >= 0); |
| 68 |
assert_se(uname(&u) >= 0); |
| 69 |
ASSERT_STREQ(s, u.nodename); |
| 70 |
|
| 71 |
r = sysctl_write("kernel/hostname", s); |
| 72 |
assert_se(r >= 0 || ERRNO_IS_NEG_FS_WRITE_REFUSED(r)); |
| 73 |
} |
| 74 |
|
| 75 |
DEFINE_TEST_MAIN(LOG_INFO); |