]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hostname-util.c
9a271136967879cb7263997f12f86552d87a68da
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
4 Copyright 2013 Thomas H.P. Andersen
5 Copyright 2015 Zbigniew Jędrzejewski-Szmek
8 #include "alloc-util.h"
10 #include "hostname-util.h"
11 #include "string-util.h"
14 static void test_hostname_is_valid(void) {
15 assert_se(hostname_is_valid("foobar", false));
16 assert_se(hostname_is_valid("foobar.com", 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("fooBAR.", false));
21 assert_se(!hostname_is_valid("fooBAR.com.", false));
22 assert_se(!hostname_is_valid("fööbar", false));
23 assert_se(!hostname_is_valid("", false));
24 assert_se(!hostname_is_valid(".", false));
25 assert_se(!hostname_is_valid("..", false));
26 assert_se(!hostname_is_valid("foobar.", false));
27 assert_se(!hostname_is_valid(".foobar", false));
28 assert_se(!hostname_is_valid("foo..bar", false));
29 assert_se(!hostname_is_valid("foo.bar..", false));
30 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", false));
31 assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", false));
33 assert_se(hostname_is_valid("foobar", true));
34 assert_se(hostname_is_valid("foobar.com", 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("fooBAR.", true));
39 assert_se(hostname_is_valid("fooBAR.com.", true));
40 assert_se(!hostname_is_valid("fööbar", true));
41 assert_se(!hostname_is_valid("", true));
42 assert_se(!hostname_is_valid(".", true));
43 assert_se(!hostname_is_valid("..", true));
44 assert_se(!hostname_is_valid("foobar.", true));
45 assert_se(!hostname_is_valid(".foobar", true));
46 assert_se(!hostname_is_valid("foo..bar", true));
47 assert_se(!hostname_is_valid("foo.bar..", true));
48 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));
51 static void test_hostname_cleanup(void) {
54 s
= strdupa("foobar");
55 assert_se(streq(hostname_cleanup(s
), "foobar"));
56 s
= strdupa("foobar.com");
57 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
58 s
= strdupa("foobar.com.");
59 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
60 s
= strdupa("fooBAR");
61 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
62 s
= strdupa("fooBAR.com");
63 assert_se(streq(hostname_cleanup(s
), "fooBAR.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("fööbar");
69 assert_se(streq(hostname_cleanup(s
), "fbar"));
71 assert_se(isempty(hostname_cleanup(s
)));
73 assert_se(isempty(hostname_cleanup(s
)));
75 assert_se(isempty(hostname_cleanup(s
)));
76 s
= strdupa("foobar.");
77 assert_se(streq(hostname_cleanup(s
), "foobar"));
78 s
= strdupa(".foobar");
79 assert_se(streq(hostname_cleanup(s
), "foobar"));
80 s
= strdupa("foo..bar");
81 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
82 s
= strdupa("foo.bar..");
83 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
84 s
= strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
85 assert_se(streq(hostname_cleanup(s
), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
88 static void test_read_etc_hostname(void) {
89 char path
[] = "/tmp/hostname.XXXXXX";
93 fd
= mkostemp_safe(path
);
98 assert_se(write_string_file(path
, "foo", WRITE_STRING_FILE_CREATE
) == 0);
99 assert_se(read_etc_hostname(path
, &hostname
) == 0);
100 assert_se(streq(hostname
, "foo"));
101 hostname
= mfree(hostname
);
104 assert_se(write_string_file(path
, "# comment\nfoo", WRITE_STRING_FILE_CREATE
) == 0);
105 assert_se(read_etc_hostname(path
, &hostname
) == 0);
107 assert_se(streq(hostname
, "foo"));
108 hostname
= mfree(hostname
);
110 /* with comment and extra whitespace */
111 assert_se(write_string_file(path
, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE
) == 0);
112 assert_se(read_etc_hostname(path
, &hostname
) == 0);
114 assert_se(streq(hostname
, "foo"));
115 hostname
= mfree(hostname
);
118 assert_se(write_string_file(path
, "!foo/bar.com", WRITE_STRING_FILE_CREATE
) == 0);
119 assert_se(read_etc_hostname(path
, &hostname
) == 0);
121 assert_se(streq(hostname
, "foobar.com"));
122 hostname
= mfree(hostname
);
125 hostname
= (char*) 0x1234;
126 assert_se(write_string_file(path
, "# nothing here\n", WRITE_STRING_FILE_CREATE
) == 0);
127 assert_se(read_etc_hostname(path
, &hostname
) == -ENOENT
);
128 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
130 /* nonexisting file */
131 assert_se(read_etc_hostname("/non/existing", &hostname
) == -ENOENT
);
132 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
137 int main(int argc
, char *argv
[]) {
138 log_parse_environment();
141 test_hostname_is_valid();
142 test_hostname_cleanup();
143 test_read_etc_hostname();