| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <netinet/in.h> |
| 4 |
#include <string.h> |
| 5 |
#include <sys/un.h> |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "journald-config.h" |
| 9 |
#include "log.h" |
| 10 |
#include "socket-util.h" |
| 11 |
#include "sparse-endian.h" |
| 12 |
#include "tests.h" |
| 13 |
|
| 14 |
static void compress_parse_check(const char *str, int expected_enabled, uint64_t expected_threshold) { |
| 15 |
JournalCompressOptions conf = { .enabled = -222, .threshold_bytes = 111 }; |
| 16 |
|
| 17 |
ASSERT_OK(config_parse_compress("", "", 0, "", 0, "", 0, str, &conf, NULL)); |
| 18 |
ASSERT_EQ(expected_enabled, conf.enabled); |
| 19 |
if (conf.enabled) |
| 20 |
ASSERT_EQ(expected_threshold, conf.threshold_bytes); |
| 21 |
} |
| 22 |
|
| 23 |
TEST(config_compress) { |
| 24 |
compress_parse_check("yes", true, UINT64_MAX); |
| 25 |
compress_parse_check("no", false, UINT64_MAX); |
| 26 |
compress_parse_check("y", true, UINT64_MAX); |
| 27 |
compress_parse_check("n", false, UINT64_MAX); |
| 28 |
compress_parse_check("true", true, UINT64_MAX); |
| 29 |
compress_parse_check("false", false, UINT64_MAX); |
| 30 |
compress_parse_check("t", true, UINT64_MAX); |
| 31 |
compress_parse_check("f", false, UINT64_MAX); |
| 32 |
compress_parse_check("on", true, UINT64_MAX); |
| 33 |
compress_parse_check("off", false, UINT64_MAX); |
| 34 |
|
| 35 |
/* Weird size/bool overlapping case. We preserve backward compatibility instead of assuming these are byte |
| 36 |
* counts. */ |
| 37 |
compress_parse_check("1", true, UINT64_MAX); |
| 38 |
compress_parse_check("0", false, UINT64_MAX); |
| 39 |
|
| 40 |
/* IEC sizing */ |
| 41 |
compress_parse_check("1B", true, 1); |
| 42 |
compress_parse_check("1K", true, 1024); |
| 43 |
compress_parse_check("1M", true, 1024 * 1024); |
| 44 |
compress_parse_check("1G", true, 1024 * 1024 * 1024); |
| 45 |
|
| 46 |
/* Invalid Case */ |
| 47 |
compress_parse_check("-1", -222, 111); |
| 48 |
compress_parse_check("blah blah", -222, 111); |
| 49 |
compress_parse_check("", -1, UINT64_MAX); |
| 50 |
} |
| 51 |
|
| 52 |
static void forward_to_socket_parse_check_fails(const char *str) { |
| 53 |
SocketAddress conf = {}; |
| 54 |
|
| 55 |
ASSERT_OK(config_parse_forward_to_socket("", "", 0, "", 0, "", 0, str, &conf, NULL)); |
| 56 |
ASSERT_FAIL(socket_address_verify(&conf, true)); |
| 57 |
} |
| 58 |
|
| 59 |
static void forward_to_socket_parse_check(const char *str, const SocketAddress *expected_addr) { |
| 60 |
_cleanup_free_ char *buf = NULL, *buf2 = NULL; |
| 61 |
SocketAddress conf = {}; |
| 62 |
|
| 63 |
ASSERT_OK(config_parse_forward_to_socket("", "", 0, "", 0, "", 0, str, &conf, NULL)); |
| 64 |
ASSERT_OK(socket_address_print(&conf, &buf)); |
| 65 |
ASSERT_OK(socket_address_print(expected_addr, &buf2)); |
| 66 |
log_info("\"%s\" parsed as \"%s\", should be \"%s\"", str, buf, buf2); |
| 67 |
log_info("socket_address_verify(&expected_addr, false) = %d", socket_address_verify(expected_addr, false)); |
| 68 |
log_info("socket_address_verify(&conf, false) = %d", socket_address_verify(&conf, false)); |
| 69 |
log_info("socket_address_family(&expected_addr) = %d", socket_address_family(expected_addr)); |
| 70 |
log_info("socket_address_family(&conf) = %d", socket_address_family(&conf)); |
| 71 |
log_info("expected_addr.size = %u", expected_addr->size); |
| 72 |
log_info("conf.size = %u", conf.size); |
| 73 |
ASSERT_TRUE(socket_address_equal(&conf, expected_addr)); |
| 74 |
} |
| 75 |
|
| 76 |
TEST(config_forward_to_socket) { |
| 77 |
SocketAddress addr; |
| 78 |
|
| 79 |
/* Valid AF_UNIX */ |
| 80 |
addr = (SocketAddress) { |
| 81 |
.sockaddr.un = (struct sockaddr_un) { |
| 82 |
.sun_family = AF_UNIX, |
| 83 |
.sun_path = "/run/host/journal/socket", |
| 84 |
}, |
| 85 |
.size = offsetof(struct sockaddr_un, sun_path) + strlen("/run/host/journal/socket") + 1, |
| 86 |
}; |
| 87 |
forward_to_socket_parse_check("/run/host/journal/socket", &addr); |
| 88 |
|
| 89 |
addr.size -= 1; |
| 90 |
memcpy(addr.sockaddr.un.sun_path, "\0run/host/journal/socket", sizeof("\0run/host/journal/socket")); |
| 91 |
forward_to_socket_parse_check("@run/host/journal/socket", &addr); |
| 92 |
|
| 93 |
/* Valid AF_INET */ |
| 94 |
addr = (SocketAddress) { |
| 95 |
.sockaddr.in = (struct sockaddr_in) { |
| 96 |
.sin_family = AF_INET, |
| 97 |
.sin_addr = { htobe32(0xC0A80001) }, |
| 98 |
.sin_port = htobe16(1234), |
| 99 |
}, |
| 100 |
.size = sizeof(struct sockaddr_in), |
| 101 |
}; |
| 102 |
forward_to_socket_parse_check("192.168.0.1:1234", &addr); |
| 103 |
|
| 104 |
/* Valid AF_INET6 */ |
| 105 |
addr = (SocketAddress) { |
| 106 |
.sockaddr.in6 = (struct sockaddr_in6) { |
| 107 |
.sin6_family = AF_INET6, |
| 108 |
.sin6_addr = (struct in6_addr) { |
| 109 |
.s6_addr16 = { |
| 110 |
htobe16(0x2001), |
| 111 |
htobe16(0xdb8), |
| 112 |
htobe16(0x4006), |
| 113 |
htobe16(0x812), |
| 114 |
0, 0, 0, |
| 115 |
htobe16(0x200e) |
| 116 |
} |
| 117 |
}, |
| 118 |
.sin6_port = htobe16(8080), |
| 119 |
}, |
| 120 |
.size = sizeof(struct sockaddr_in6), |
| 121 |
}; |
| 122 |
forward_to_socket_parse_check("[2001:db8:4006:812::200e]:8080", &addr); |
| 123 |
|
| 124 |
/* Valid AF_VSOCK */ |
| 125 |
addr = (SocketAddress) { |
| 126 |
.sockaddr.vm = (struct sockaddr_vm) { |
| 127 |
.svm_family = AF_VSOCK, |
| 128 |
.svm_cid = 123456, |
| 129 |
.svm_port = 654321, |
| 130 |
}, |
| 131 |
.size = sizeof(struct sockaddr_vm), |
| 132 |
}; |
| 133 |
forward_to_socket_parse_check("vsock:123456:654321", &addr); |
| 134 |
|
| 135 |
/* Invalid IPv4 */ |
| 136 |
forward_to_socket_parse_check_fails("256.123.45.12:1235"); |
| 137 |
forward_to_socket_parse_check_fails("252.123.45.12:123500"); |
| 138 |
forward_to_socket_parse_check_fails("252.123.45.12:0"); |
| 139 |
forward_to_socket_parse_check_fails("252.123.45.12:-1"); |
| 140 |
forward_to_socket_parse_check_fails("-1.123.45.12:22"); |
| 141 |
|
| 142 |
/* Invalid IPv6 */ |
| 143 |
forward_to_socket_parse_check_fails("[2001:db8:4006:812::200e]:80800"); |
| 144 |
forward_to_socket_parse_check_fails("[1ffff:db8:4006:812::200e]:8080"); |
| 145 |
forward_to_socket_parse_check_fails("[-1:db8:4006:812::200e]:8080"); |
| 146 |
forward_to_socket_parse_check_fails("[2001:db8:4006:812::200e]:-1"); |
| 147 |
|
| 148 |
/* Invalid UNIX */ |
| 149 |
forward_to_socket_parse_check_fails("a/b/c"); |
| 150 |
|
| 151 |
/* Invalid VSock */ |
| 152 |
forward_to_socket_parse_check_fails("vsock:4294967296:1234"); |
| 153 |
forward_to_socket_parse_check_fails("vsock:1234:4294967296"); |
| 154 |
forward_to_socket_parse_check_fails("vsock:abcd:1234"); |
| 155 |
forward_to_socket_parse_check_fails("vsock:1234:abcd"); |
| 156 |
forward_to_socket_parse_check_fails("vsock:1234"); |
| 157 |
|
| 158 |
/* Invalid Case */ |
| 159 |
forward_to_socket_parse_check_fails(""); |
| 160 |
forward_to_socket_parse_check_fails("ahh yes sockets, mmh"); |
| 161 |
} |
| 162 |
|
| 163 |
DEFINE_TEST_MAIN(LOG_INFO); |