1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
7 #include "hostname-util.h"
8 #include "string-util.h"
9 #include "tmpfile-util.h"
12 static void test_hostname_is_valid(void) {
13 assert_se(hostname_is_valid("foobar", 0));
14 assert_se(hostname_is_valid("foobar.com", 0));
15 assert_se(!hostname_is_valid("foobar.com.", 0));
16 assert_se(hostname_is_valid("fooBAR", 0));
17 assert_se(hostname_is_valid("fooBAR.com", 0));
18 assert_se(!hostname_is_valid("fooBAR.", 0));
19 assert_se(!hostname_is_valid("fooBAR.com.", 0));
20 assert_se(!hostname_is_valid("fööbar", 0));
21 assert_se(!hostname_is_valid("", 0));
22 assert_se(!hostname_is_valid(".", 0));
23 assert_se(!hostname_is_valid("..", 0));
24 assert_se(!hostname_is_valid("foobar.", 0));
25 assert_se(!hostname_is_valid(".foobar", 0));
26 assert_se(!hostname_is_valid("foo..bar", 0));
27 assert_se(!hostname_is_valid("foo.bar..", 0));
28 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 0));
29 assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", 0));
31 assert_se(hostname_is_valid("foobar", VALID_HOSTNAME_TRAILING_DOT
));
32 assert_se(hostname_is_valid("foobar.com", VALID_HOSTNAME_TRAILING_DOT
));
33 assert_se(hostname_is_valid("foobar.com.", VALID_HOSTNAME_TRAILING_DOT
));
34 assert_se(hostname_is_valid("fooBAR", VALID_HOSTNAME_TRAILING_DOT
));
35 assert_se(hostname_is_valid("fooBAR.com", VALID_HOSTNAME_TRAILING_DOT
));
36 assert_se(!hostname_is_valid("fooBAR.", VALID_HOSTNAME_TRAILING_DOT
));
37 assert_se(hostname_is_valid("fooBAR.com.", VALID_HOSTNAME_TRAILING_DOT
));
38 assert_se(!hostname_is_valid("fööbar", VALID_HOSTNAME_TRAILING_DOT
));
39 assert_se(!hostname_is_valid("", VALID_HOSTNAME_TRAILING_DOT
));
40 assert_se(!hostname_is_valid(".", VALID_HOSTNAME_TRAILING_DOT
));
41 assert_se(!hostname_is_valid("..", VALID_HOSTNAME_TRAILING_DOT
));
42 assert_se(!hostname_is_valid("foobar.", VALID_HOSTNAME_TRAILING_DOT
));
43 assert_se(!hostname_is_valid(".foobar", VALID_HOSTNAME_TRAILING_DOT
));
44 assert_se(!hostname_is_valid("foo..bar", VALID_HOSTNAME_TRAILING_DOT
));
45 assert_se(!hostname_is_valid("foo.bar..", VALID_HOSTNAME_TRAILING_DOT
));
46 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", VALID_HOSTNAME_TRAILING_DOT
));
49 static void test_hostname_cleanup(void) {
52 s
= strdupa("foobar");
53 assert_se(streq(hostname_cleanup(s
), "foobar"));
54 s
= strdupa("foobar.com");
55 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
56 s
= strdupa("foobar.com.");
57 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
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"));
64 s
= strdupa("fooBAR");
65 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
66 s
= strdupa("fooBAR.com");
67 assert_se(streq(hostname_cleanup(s
), "fooBAR.com"));
68 s
= strdupa("fooBAR.");
69 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
70 s
= strdupa("fooBAR.com.");
71 assert_se(streq(hostname_cleanup(s
), "fooBAR.com"));
72 s
= strdupa("fööbar");
73 assert_se(streq(hostname_cleanup(s
), "fbar"));
75 assert_se(isempty(hostname_cleanup(s
)));
77 assert_se(isempty(hostname_cleanup(s
)));
79 assert_se(isempty(hostname_cleanup(s
)));
80 s
= strdupa("foobar.");
81 assert_se(streq(hostname_cleanup(s
), "foobar"));
82 s
= strdupa(".foobar");
83 assert_se(streq(hostname_cleanup(s
), "foobar"));
84 s
= strdupa("foo..bar");
85 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
86 s
= strdupa("foo.bar..");
87 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
88 s
= strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
89 assert_se(streq(hostname_cleanup(s
), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
90 s
= strdupa("xxxx........xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
91 assert_se(streq(hostname_cleanup(s
), "xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
94 static void test_read_etc_hostname(void) {
95 char path
[] = "/tmp/hostname.XXXXXX";
99 fd
= mkostemp_safe(path
);
103 /* simple hostname */
104 assert_se(write_string_file(path
, "foo", WRITE_STRING_FILE_CREATE
) == 0);
105 assert_se(read_etc_hostname(path
, &hostname
) == 0);
106 assert_se(streq(hostname
, "foo"));
107 hostname
= mfree(hostname
);
110 assert_se(write_string_file(path
, "# comment\nfoo", WRITE_STRING_FILE_CREATE
) == 0);
111 assert_se(read_etc_hostname(path
, &hostname
) == 0);
113 assert_se(streq(hostname
, "foo"));
114 hostname
= mfree(hostname
);
116 /* with comment and extra whitespace */
117 assert_se(write_string_file(path
, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE
) == 0);
118 assert_se(read_etc_hostname(path
, &hostname
) == 0);
120 assert_se(streq(hostname
, "foo"));
121 hostname
= mfree(hostname
);
124 assert_se(write_string_file(path
, "!foo/bar.com", WRITE_STRING_FILE_CREATE
) == 0);
125 assert_se(read_etc_hostname(path
, &hostname
) == 0);
127 assert_se(streq(hostname
, "foobar.com"));
128 hostname
= mfree(hostname
);
131 hostname
= (char*) 0x1234;
132 assert_se(write_string_file(path
, "# nothing here\n", WRITE_STRING_FILE_CREATE
) == 0);
133 assert_se(read_etc_hostname(path
, &hostname
) == -ENOENT
);
134 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
136 /* nonexisting file */
137 assert_se(read_etc_hostname("/non/existing", &hostname
) == -ENOENT
);
138 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
143 static void test_hostname_malloc(void) {
144 _cleanup_free_
char *h
= NULL
, *l
= NULL
;
146 assert_se(h
= gethostname_malloc());
147 log_info("hostname_malloc: \"%s\"", h
);
149 assert_se(l
= gethostname_short_malloc());
150 log_info("hostname_short_malloc: \"%s\"", l
);
153 static void test_fallback_hostname(void) {
154 if (!hostname_is_valid(FALLBACK_HOSTNAME
, 0)) {
155 log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME
);
160 int main(int argc
, char *argv
[]) {
161 log_parse_environment();
164 test_hostname_is_valid();
165 test_hostname_cleanup();
166 test_read_etc_hostname();
167 test_hostname_malloc();
169 test_fallback_hostname();