]>
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"
9 static void test_hostname_is_valid(void) {
10 assert_se(hostname_is_valid("foobar", false));
11 assert_se(hostname_is_valid("foobar.com", false));
12 assert_se(!hostname_is_valid("foobar.com.", false));
13 assert_se(hostname_is_valid("fooBAR", false));
14 assert_se(hostname_is_valid("fooBAR.com", false));
15 assert_se(!hostname_is_valid("fooBAR.", false));
16 assert_se(!hostname_is_valid("fooBAR.com.", false));
17 assert_se(!hostname_is_valid("fööbar", false));
18 assert_se(!hostname_is_valid("", false));
19 assert_se(!hostname_is_valid(".", false));
20 assert_se(!hostname_is_valid("..", false));
21 assert_se(!hostname_is_valid("foobar.", false));
22 assert_se(!hostname_is_valid(".foobar", false));
23 assert_se(!hostname_is_valid("foo..bar", false));
24 assert_se(!hostname_is_valid("foo.bar..", false));
25 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", false));
26 assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", false));
28 assert_se(hostname_is_valid("foobar", true));
29 assert_se(hostname_is_valid("foobar.com", true));
30 assert_se(hostname_is_valid("foobar.com.", true));
31 assert_se(hostname_is_valid("fooBAR", true));
32 assert_se(hostname_is_valid("fooBAR.com", true));
33 assert_se(!hostname_is_valid("fooBAR.", true));
34 assert_se(hostname_is_valid("fooBAR.com.", true));
35 assert_se(!hostname_is_valid("fööbar", true));
36 assert_se(!hostname_is_valid("", true));
37 assert_se(!hostname_is_valid(".", true));
38 assert_se(!hostname_is_valid("..", true));
39 assert_se(!hostname_is_valid("foobar.", true));
40 assert_se(!hostname_is_valid(".foobar", true));
41 assert_se(!hostname_is_valid("foo..bar", true));
42 assert_se(!hostname_is_valid("foo.bar..", true));
43 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));
46 static void test_hostname_cleanup(void) {
49 s
= strdupa("foobar");
50 assert_se(streq(hostname_cleanup(s
), "foobar"));
51 s
= strdupa("foobar.com");
52 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
53 s
= strdupa("foobar.com.");
54 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
55 s
= strdupa("fooBAR");
56 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
57 s
= strdupa("fooBAR.com");
58 assert_se(streq(hostname_cleanup(s
), "fooBAR.com"));
59 s
= strdupa("fooBAR.");
60 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
61 s
= strdupa("fooBAR.com.");
62 assert_se(streq(hostname_cleanup(s
), "fooBAR.com"));
63 s
= strdupa("fööbar");
64 assert_se(streq(hostname_cleanup(s
), "fbar"));
66 assert_se(isempty(hostname_cleanup(s
)));
68 assert_se(isempty(hostname_cleanup(s
)));
70 assert_se(isempty(hostname_cleanup(s
)));
71 s
= strdupa("foobar.");
72 assert_se(streq(hostname_cleanup(s
), "foobar"));
73 s
= strdupa(".foobar");
74 assert_se(streq(hostname_cleanup(s
), "foobar"));
75 s
= strdupa("foo..bar");
76 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
77 s
= strdupa("foo.bar..");
78 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
79 s
= strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
80 assert_se(streq(hostname_cleanup(s
), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
83 static void test_read_etc_hostname(void) {
84 char path
[] = "/tmp/hostname.XXXXXX";
88 fd
= mkostemp_safe(path
);
93 assert_se(write_string_file(path
, "foo", WRITE_STRING_FILE_CREATE
) == 0);
94 assert_se(read_etc_hostname(path
, &hostname
) == 0);
95 assert_se(streq(hostname
, "foo"));
96 hostname
= mfree(hostname
);
99 assert_se(write_string_file(path
, "# comment\nfoo", WRITE_STRING_FILE_CREATE
) == 0);
100 assert_se(read_etc_hostname(path
, &hostname
) == 0);
102 assert_se(streq(hostname
, "foo"));
103 hostname
= mfree(hostname
);
105 /* with comment and extra whitespace */
106 assert_se(write_string_file(path
, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE
) == 0);
107 assert_se(read_etc_hostname(path
, &hostname
) == 0);
109 assert_se(streq(hostname
, "foo"));
110 hostname
= mfree(hostname
);
113 assert_se(write_string_file(path
, "!foo/bar.com", WRITE_STRING_FILE_CREATE
) == 0);
114 assert_se(read_etc_hostname(path
, &hostname
) == 0);
116 assert_se(streq(hostname
, "foobar.com"));
117 hostname
= mfree(hostname
);
120 hostname
= (char*) 0x1234;
121 assert_se(write_string_file(path
, "# nothing here\n", WRITE_STRING_FILE_CREATE
) == 0);
122 assert_se(read_etc_hostname(path
, &hostname
) == -ENOENT
);
123 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
125 /* nonexisting file */
126 assert_se(read_etc_hostname("/non/existing", &hostname
) == -ENOENT
);
127 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
132 int main(int argc
, char *argv
[]) {
133 log_parse_environment();
136 test_hostname_is_valid();
137 test_hostname_cleanup();
138 test_read_etc_hostname();