]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-hostname-util.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / test / test-hostname-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
8fb49443 2
ca78ad1d
ZJS
3#include <unistd.h>
4
b5efdb8a 5#include "alloc-util.h"
8fb49443
ZJS
6#include "fileio.h"
7#include "hostname-util.h"
07630cea 8#include "string-util.h"
e4de7287 9#include "tmpfile-util.h"
b5efdb8a 10#include "util.h"
8fb49443
ZJS
11
12static void test_hostname_is_valid(void) {
13 assert_se(hostname_is_valid("foobar", false));
14 assert_se(hostname_is_valid("foobar.com", false));
15 assert_se(!hostname_is_valid("foobar.com.", false));
16 assert_se(hostname_is_valid("fooBAR", false));
17 assert_se(hostname_is_valid("fooBAR.com", false));
18 assert_se(!hostname_is_valid("fooBAR.", false));
19 assert_se(!hostname_is_valid("fooBAR.com.", false));
20 assert_se(!hostname_is_valid("fööbar", false));
21 assert_se(!hostname_is_valid("", false));
22 assert_se(!hostname_is_valid(".", false));
23 assert_se(!hostname_is_valid("..", false));
24 assert_se(!hostname_is_valid("foobar.", false));
25 assert_se(!hostname_is_valid(".foobar", false));
26 assert_se(!hostname_is_valid("foo..bar", false));
27 assert_se(!hostname_is_valid("foo.bar..", false));
28 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", false));
2c85bbaa 29 assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", false));
8fb49443
ZJS
30
31 assert_se(hostname_is_valid("foobar", true));
32 assert_se(hostname_is_valid("foobar.com", true));
33 assert_se(hostname_is_valid("foobar.com.", true));
34 assert_se(hostname_is_valid("fooBAR", true));
35 assert_se(hostname_is_valid("fooBAR.com", true));
36 assert_se(!hostname_is_valid("fooBAR.", true));
37 assert_se(hostname_is_valid("fooBAR.com.", true));
38 assert_se(!hostname_is_valid("fööbar", true));
39 assert_se(!hostname_is_valid("", true));
40 assert_se(!hostname_is_valid(".", true));
41 assert_se(!hostname_is_valid("..", true));
42 assert_se(!hostname_is_valid("foobar.", true));
43 assert_se(!hostname_is_valid(".foobar", true));
44 assert_se(!hostname_is_valid("foo..bar", true));
45 assert_se(!hostname_is_valid("foo.bar..", true));
46 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));
47}
48
49static void test_hostname_cleanup(void) {
50 char *s;
51
52 s = strdupa("foobar");
ae691c1d 53 assert_se(streq(hostname_cleanup(s), "foobar"));
8fb49443 54 s = strdupa("foobar.com");
ae691c1d 55 assert_se(streq(hostname_cleanup(s), "foobar.com"));
8fb49443 56 s = strdupa("foobar.com.");
ae691c1d 57 assert_se(streq(hostname_cleanup(s), "foobar.com"));
d65652f1
ZJS
58 s = strdupa("foo-bar.-com-.");
59 assert_se(streq(hostname_cleanup(s), "foo-bar.com"));
60 s = strdupa("foo-bar-.-com-.");
61 assert_se(streq(hostname_cleanup(s), "foo-bar--com"));
62 s = strdupa("--foo-bar.-com");
63 assert_se(streq(hostname_cleanup(s), "foo-bar.com"));
8fb49443 64 s = strdupa("fooBAR");
ae691c1d 65 assert_se(streq(hostname_cleanup(s), "fooBAR"));
8fb49443 66 s = strdupa("fooBAR.com");
ae691c1d 67 assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
8fb49443 68 s = strdupa("fooBAR.");
ae691c1d 69 assert_se(streq(hostname_cleanup(s), "fooBAR"));
8fb49443 70 s = strdupa("fooBAR.com.");
ae691c1d 71 assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
8fb49443 72 s = strdupa("fööbar");
ae691c1d 73 assert_se(streq(hostname_cleanup(s), "fbar"));
8fb49443 74 s = strdupa("");
ae691c1d 75 assert_se(isempty(hostname_cleanup(s)));
8fb49443 76 s = strdupa(".");
ae691c1d 77 assert_se(isempty(hostname_cleanup(s)));
8fb49443 78 s = strdupa("..");
ae691c1d 79 assert_se(isempty(hostname_cleanup(s)));
8fb49443 80 s = strdupa("foobar.");
ae691c1d 81 assert_se(streq(hostname_cleanup(s), "foobar"));
8fb49443 82 s = strdupa(".foobar");
ae691c1d 83 assert_se(streq(hostname_cleanup(s), "foobar"));
8fb49443 84 s = strdupa("foo..bar");
ae691c1d 85 assert_se(streq(hostname_cleanup(s), "foo.bar"));
8fb49443 86 s = strdupa("foo.bar..");
ae691c1d 87 assert_se(streq(hostname_cleanup(s), "foo.bar"));
8fb49443 88 s = strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
ae691c1d 89 assert_se(streq(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
5fe7a0a7
ZJS
90 s = strdupa("xxxx........xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
91 assert_se(streq(hostname_cleanup(s), "xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
8fb49443
ZJS
92}
93
f35cb39e 94static void test_read_etc_hostname(void) {
8fb49443
ZJS
95 char path[] = "/tmp/hostname.XXXXXX";
96 char *hostname;
97 int fd;
98
646853bd 99 fd = mkostemp_safe(path);
8fb49443
ZJS
100 assert(fd > 0);
101 close(fd);
102
103 /* simple hostname */
7beb3baf 104 assert_se(write_string_file(path, "foo", WRITE_STRING_FILE_CREATE) == 0);
f35cb39e 105 assert_se(read_etc_hostname(path, &hostname) == 0);
8fb49443 106 assert_se(streq(hostname, "foo"));
73974f67 107 hostname = mfree(hostname);
8fb49443
ZJS
108
109 /* with comment */
7beb3baf 110 assert_se(write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE) == 0);
f35cb39e 111 assert_se(read_etc_hostname(path, &hostname) == 0);
8fb49443
ZJS
112 assert_se(hostname);
113 assert_se(streq(hostname, "foo"));
73974f67 114 hostname = mfree(hostname);
8fb49443
ZJS
115
116 /* with comment and extra whitespace */
7beb3baf 117 assert_se(write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE) == 0);
f35cb39e 118 assert_se(read_etc_hostname(path, &hostname) == 0);
8fb49443
ZJS
119 assert_se(hostname);
120 assert_se(streq(hostname, "foo"));
73974f67 121 hostname = mfree(hostname);
8fb49443
ZJS
122
123 /* cleans up name */
7beb3baf 124 assert_se(write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE) == 0);
f35cb39e 125 assert_se(read_etc_hostname(path, &hostname) == 0);
8fb49443
ZJS
126 assert_se(hostname);
127 assert_se(streq(hostname, "foobar.com"));
73974f67 128 hostname = mfree(hostname);
8fb49443
ZJS
129
130 /* no value set */
131 hostname = (char*) 0x1234;
7beb3baf 132 assert_se(write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE) == 0);
f35cb39e 133 assert_se(read_etc_hostname(path, &hostname) == -ENOENT);
8fb49443
ZJS
134 assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
135
136 /* nonexisting file */
f35cb39e 137 assert_se(read_etc_hostname("/non/existing", &hostname) == -ENOENT);
8fb49443
ZJS
138 assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
139
140 unlink(path);
141}
142
e97708fa
ZJS
143static void test_hostname_malloc(void) {
144 _cleanup_free_ char *h = NULL, *l = NULL;
145
146 assert_se(h = gethostname_malloc());
147 log_info("hostname_malloc: \"%s\"", h);
148
149 assert_se(l = gethostname_short_malloc());
150 log_info("hostname_short_malloc: \"%s\"", l);
151}
152
eef4b800
ZJS
153static void test_fallback_hostname(void) {
154 if (!hostname_is_valid(FALLBACK_HOSTNAME, false)) {
155 log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME);
156 exit(EXIT_FAILURE);
157 }
158}
159
8fb49443
ZJS
160int main(int argc, char *argv[]) {
161 log_parse_environment();
162 log_open();
163
164 test_hostname_is_valid();
165 test_hostname_cleanup();
f35cb39e 166 test_read_etc_hostname();
e97708fa 167 test_hostname_malloc();
8fb49443 168
eef4b800
ZJS
169 test_fallback_hostname();
170
8fb49443
ZJS
171 return 0;
172}