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