]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hostname-util.c
6dd4ea033c30cb31d152c1dc3aeb06d317b89112
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Thomas H.P. Andersen
4 Copyright 2015 Zbigniew Jędrzejewski-Szmek
7 #include "alloc-util.h"
9 #include "hostname-util.h"
10 #include "string-util.h"
13 static void test_hostname_is_valid(void) {
14 assert_se(hostname_is_valid("foobar", false));
15 assert_se(hostname_is_valid("foobar.com", false));
16 assert_se(!hostname_is_valid("foobar.com.", false));
17 assert_se(hostname_is_valid("fooBAR", false));
18 assert_se(hostname_is_valid("fooBAR.com", false));
19 assert_se(!hostname_is_valid("fooBAR.", false));
20 assert_se(!hostname_is_valid("fooBAR.com.", false));
21 assert_se(!hostname_is_valid("fööbar", false));
22 assert_se(!hostname_is_valid("", false));
23 assert_se(!hostname_is_valid(".", false));
24 assert_se(!hostname_is_valid("..", false));
25 assert_se(!hostname_is_valid("foobar.", false));
26 assert_se(!hostname_is_valid(".foobar", false));
27 assert_se(!hostname_is_valid("foo..bar", false));
28 assert_se(!hostname_is_valid("foo.bar..", false));
29 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", false));
30 assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", false));
32 assert_se(hostname_is_valid("foobar", true));
33 assert_se(hostname_is_valid("foobar.com", true));
34 assert_se(hostname_is_valid("foobar.com.", true));
35 assert_se(hostname_is_valid("fooBAR", true));
36 assert_se(hostname_is_valid("fooBAR.com", true));
37 assert_se(!hostname_is_valid("fooBAR.", true));
38 assert_se(hostname_is_valid("fooBAR.com.", true));
39 assert_se(!hostname_is_valid("fööbar", true));
40 assert_se(!hostname_is_valid("", true));
41 assert_se(!hostname_is_valid(".", true));
42 assert_se(!hostname_is_valid("..", true));
43 assert_se(!hostname_is_valid("foobar.", true));
44 assert_se(!hostname_is_valid(".foobar", true));
45 assert_se(!hostname_is_valid("foo..bar", true));
46 assert_se(!hostname_is_valid("foo.bar..", true));
47 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));
50 static void test_hostname_cleanup(void) {
53 s
= strdupa("foobar");
54 assert_se(streq(hostname_cleanup(s
), "foobar"));
55 s
= strdupa("foobar.com");
56 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
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("fooBAR.");
64 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
65 s
= strdupa("fooBAR.com.");
66 assert_se(streq(hostname_cleanup(s
), "fooBAR.com"));
67 s
= strdupa("fööbar");
68 assert_se(streq(hostname_cleanup(s
), "fbar"));
70 assert_se(isempty(hostname_cleanup(s
)));
72 assert_se(isempty(hostname_cleanup(s
)));
74 assert_se(isempty(hostname_cleanup(s
)));
75 s
= strdupa("foobar.");
76 assert_se(streq(hostname_cleanup(s
), "foobar"));
77 s
= strdupa(".foobar");
78 assert_se(streq(hostname_cleanup(s
), "foobar"));
79 s
= strdupa("foo..bar");
80 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
81 s
= strdupa("foo.bar..");
82 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
83 s
= strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
84 assert_se(streq(hostname_cleanup(s
), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
87 static void test_read_etc_hostname(void) {
88 char path
[] = "/tmp/hostname.XXXXXX";
92 fd
= mkostemp_safe(path
);
97 assert_se(write_string_file(path
, "foo", WRITE_STRING_FILE_CREATE
) == 0);
98 assert_se(read_etc_hostname(path
, &hostname
) == 0);
99 assert_se(streq(hostname
, "foo"));
100 hostname
= mfree(hostname
);
103 assert_se(write_string_file(path
, "# comment\nfoo", WRITE_STRING_FILE_CREATE
) == 0);
104 assert_se(read_etc_hostname(path
, &hostname
) == 0);
106 assert_se(streq(hostname
, "foo"));
107 hostname
= mfree(hostname
);
109 /* with comment and extra whitespace */
110 assert_se(write_string_file(path
, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE
) == 0);
111 assert_se(read_etc_hostname(path
, &hostname
) == 0);
113 assert_se(streq(hostname
, "foo"));
114 hostname
= mfree(hostname
);
117 assert_se(write_string_file(path
, "!foo/bar.com", WRITE_STRING_FILE_CREATE
) == 0);
118 assert_se(read_etc_hostname(path
, &hostname
) == 0);
120 assert_se(streq(hostname
, "foobar.com"));
121 hostname
= mfree(hostname
);
124 hostname
= (char*) 0x1234;
125 assert_se(write_string_file(path
, "# nothing here\n", WRITE_STRING_FILE_CREATE
) == 0);
126 assert_se(read_etc_hostname(path
, &hostname
) == -ENOENT
);
127 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
129 /* nonexisting file */
130 assert_se(read_etc_hostname("/non/existing", &hostname
) == -ENOENT
);
131 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
136 int main(int argc
, char *argv
[]) {
137 log_parse_environment();
140 test_hostname_is_valid();
141 test_hostname_cleanup();
142 test_read_etc_hostname();