]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hostname-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
6 Copyright 2013 Thomas H.P. Andersen
7 Copyright 2015 Zbigniew Jędrzejewski-Szmek
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
25 #include "hostname-util.h"
26 #include "string-util.h"
29 static void test_hostname_is_valid(void) {
30 assert_se(hostname_is_valid("foobar", false));
31 assert_se(hostname_is_valid("foobar.com", false));
32 assert_se(!hostname_is_valid("foobar.com.", false));
33 assert_se(hostname_is_valid("fooBAR", false));
34 assert_se(hostname_is_valid("fooBAR.com", false));
35 assert_se(!hostname_is_valid("fooBAR.", false));
36 assert_se(!hostname_is_valid("fooBAR.com.", false));
37 assert_se(!hostname_is_valid("fööbar", false));
38 assert_se(!hostname_is_valid("", false));
39 assert_se(!hostname_is_valid(".", false));
40 assert_se(!hostname_is_valid("..", false));
41 assert_se(!hostname_is_valid("foobar.", false));
42 assert_se(!hostname_is_valid(".foobar", false));
43 assert_se(!hostname_is_valid("foo..bar", false));
44 assert_se(!hostname_is_valid("foo.bar..", false));
45 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", false));
46 assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", false));
48 assert_se(hostname_is_valid("foobar", true));
49 assert_se(hostname_is_valid("foobar.com", true));
50 assert_se(hostname_is_valid("foobar.com.", true));
51 assert_se(hostname_is_valid("fooBAR", true));
52 assert_se(hostname_is_valid("fooBAR.com", true));
53 assert_se(!hostname_is_valid("fooBAR.", true));
54 assert_se(hostname_is_valid("fooBAR.com.", true));
55 assert_se(!hostname_is_valid("fööbar", true));
56 assert_se(!hostname_is_valid("", true));
57 assert_se(!hostname_is_valid(".", true));
58 assert_se(!hostname_is_valid("..", true));
59 assert_se(!hostname_is_valid("foobar.", true));
60 assert_se(!hostname_is_valid(".foobar", true));
61 assert_se(!hostname_is_valid("foo..bar", true));
62 assert_se(!hostname_is_valid("foo.bar..", true));
63 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));
66 static void test_hostname_cleanup(void) {
69 s
= strdupa("foobar");
70 assert_se(streq(hostname_cleanup(s
), "foobar"));
71 s
= strdupa("foobar.com");
72 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
73 s
= strdupa("foobar.com.");
74 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
75 s
= strdupa("fooBAR");
76 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
77 s
= strdupa("fooBAR.com");
78 assert_se(streq(hostname_cleanup(s
), "fooBAR.com"));
79 s
= strdupa("fooBAR.");
80 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
81 s
= strdupa("fooBAR.com.");
82 assert_se(streq(hostname_cleanup(s
), "fooBAR.com"));
83 s
= strdupa("fööbar");
84 assert_se(streq(hostname_cleanup(s
), "fbar"));
86 assert_se(isempty(hostname_cleanup(s
)));
88 assert_se(isempty(hostname_cleanup(s
)));
90 assert_se(isempty(hostname_cleanup(s
)));
91 s
= strdupa("foobar.");
92 assert_se(streq(hostname_cleanup(s
), "foobar"));
93 s
= strdupa(".foobar");
94 assert_se(streq(hostname_cleanup(s
), "foobar"));
95 s
= strdupa("foo..bar");
96 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
97 s
= strdupa("foo.bar..");
98 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
99 s
= strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
100 assert_se(streq(hostname_cleanup(s
), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
103 static void test_read_etc_hostname(void) {
104 char path
[] = "/tmp/hostname.XXXXXX";
108 fd
= mkostemp_safe(path
);
112 /* simple hostname */
113 assert_se(write_string_file(path
, "foo", WRITE_STRING_FILE_CREATE
) == 0);
114 assert_se(read_etc_hostname(path
, &hostname
) == 0);
115 assert_se(streq(hostname
, "foo"));
116 hostname
= mfree(hostname
);
119 assert_se(write_string_file(path
, "# comment\nfoo", WRITE_STRING_FILE_CREATE
) == 0);
120 assert_se(read_etc_hostname(path
, &hostname
) == 0);
122 assert_se(streq(hostname
, "foo"));
123 hostname
= mfree(hostname
);
125 /* with comment and extra whitespace */
126 assert_se(write_string_file(path
, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE
) == 0);
127 assert_se(read_etc_hostname(path
, &hostname
) == 0);
129 assert_se(streq(hostname
, "foo"));
130 hostname
= mfree(hostname
);
133 assert_se(write_string_file(path
, "!foo/bar.com", WRITE_STRING_FILE_CREATE
) == 0);
134 assert_se(read_etc_hostname(path
, &hostname
) == 0);
136 assert_se(streq(hostname
, "foobar.com"));
137 hostname
= mfree(hostname
);
140 hostname
= (char*) 0x1234;
141 assert_se(write_string_file(path
, "# nothing here\n", WRITE_STRING_FILE_CREATE
) == 0);
142 assert_se(read_etc_hostname(path
, &hostname
) == -ENOENT
);
143 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
145 /* nonexisting file */
146 assert_se(read_etc_hostname("/non/existing", &hostname
) == -ENOENT
);
147 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
152 int main(int argc
, char *argv
[]) {
153 log_parse_environment();
156 test_hostname_is_valid();
157 test_hostname_cleanup();
158 test_read_etc_hostname();