]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hostname-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
5 #include "hostname-util.h"
6 #include "string-util.h"
7 #include "tmpfile-util.h"
10 static void test_hostname_is_valid(void) {
11 assert_se(hostname_is_valid("foobar", false));
12 assert_se(hostname_is_valid("foobar.com", false));
13 assert_se(!hostname_is_valid("foobar.com.", false));
14 assert_se(hostname_is_valid("fooBAR", 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("fööbar", false));
19 assert_se(!hostname_is_valid("", false));
20 assert_se(!hostname_is_valid(".", false));
21 assert_se(!hostname_is_valid("..", false));
22 assert_se(!hostname_is_valid("foobar.", false));
23 assert_se(!hostname_is_valid(".foobar", false));
24 assert_se(!hostname_is_valid("foo..bar", false));
25 assert_se(!hostname_is_valid("foo.bar..", false));
26 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", false));
27 assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", false));
29 assert_se(hostname_is_valid("foobar", true));
30 assert_se(hostname_is_valid("foobar.com", true));
31 assert_se(hostname_is_valid("foobar.com.", true));
32 assert_se(hostname_is_valid("fooBAR", 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("fööbar", true));
37 assert_se(!hostname_is_valid("", true));
38 assert_se(!hostname_is_valid(".", true));
39 assert_se(!hostname_is_valid("..", true));
40 assert_se(!hostname_is_valid("foobar.", true));
41 assert_se(!hostname_is_valid(".foobar", true));
42 assert_se(!hostname_is_valid("foo..bar", true));
43 assert_se(!hostname_is_valid("foo.bar..", true));
44 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));
47 static void test_hostname_cleanup(void) {
50 s
= strdupa("foobar");
51 assert_se(streq(hostname_cleanup(s
), "foobar"));
52 s
= strdupa("foobar.com");
53 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
54 s
= strdupa("foobar.com.");
55 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
56 s
= strdupa("foo-bar.-com-.");
57 assert_se(streq(hostname_cleanup(s
), "foo-bar.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("fooBAR");
63 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
64 s
= strdupa("fooBAR.com");
65 assert_se(streq(hostname_cleanup(s
), "fooBAR.com"));
66 s
= strdupa("fooBAR.");
67 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
68 s
= strdupa("fooBAR.com.");
69 assert_se(streq(hostname_cleanup(s
), "fooBAR.com"));
70 s
= strdupa("fööbar");
71 assert_se(streq(hostname_cleanup(s
), "fbar"));
73 assert_se(isempty(hostname_cleanup(s
)));
75 assert_se(isempty(hostname_cleanup(s
)));
77 assert_se(isempty(hostname_cleanup(s
)));
78 s
= strdupa("foobar.");
79 assert_se(streq(hostname_cleanup(s
), "foobar"));
80 s
= strdupa(".foobar");
81 assert_se(streq(hostname_cleanup(s
), "foobar"));
82 s
= strdupa("foo..bar");
83 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
84 s
= strdupa("foo.bar..");
85 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
86 s
= strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
87 assert_se(streq(hostname_cleanup(s
), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
88 s
= strdupa("xxxx........xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
89 assert_se(streq(hostname_cleanup(s
), "xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
92 static void test_read_etc_hostname(void) {
93 char path
[] = "/tmp/hostname.XXXXXX";
97 fd
= mkostemp_safe(path
);
101 /* simple hostname */
102 assert_se(write_string_file(path
, "foo", WRITE_STRING_FILE_CREATE
) == 0);
103 assert_se(read_etc_hostname(path
, &hostname
) == 0);
104 assert_se(streq(hostname
, "foo"));
105 hostname
= mfree(hostname
);
108 assert_se(write_string_file(path
, "# comment\nfoo", WRITE_STRING_FILE_CREATE
) == 0);
109 assert_se(read_etc_hostname(path
, &hostname
) == 0);
111 assert_se(streq(hostname
, "foo"));
112 hostname
= mfree(hostname
);
114 /* with comment and extra whitespace */
115 assert_se(write_string_file(path
, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE
) == 0);
116 assert_se(read_etc_hostname(path
, &hostname
) == 0);
118 assert_se(streq(hostname
, "foo"));
119 hostname
= mfree(hostname
);
122 assert_se(write_string_file(path
, "!foo/bar.com", WRITE_STRING_FILE_CREATE
) == 0);
123 assert_se(read_etc_hostname(path
, &hostname
) == 0);
125 assert_se(streq(hostname
, "foobar.com"));
126 hostname
= mfree(hostname
);
129 hostname
= (char*) 0x1234;
130 assert_se(write_string_file(path
, "# nothing here\n", WRITE_STRING_FILE_CREATE
) == 0);
131 assert_se(read_etc_hostname(path
, &hostname
) == -ENOENT
);
132 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
134 /* nonexisting file */
135 assert_se(read_etc_hostname("/non/existing", &hostname
) == -ENOENT
);
136 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
141 int main(int argc
, char *argv
[]) {
142 log_parse_environment();
145 test_hostname_is_valid();
146 test_hostname_cleanup();
147 test_read_etc_hostname();