]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hostname-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
7 Copyright 2013 Thomas H.P. Andersen
8 Copyright 2015 Zbigniew Jędrzejewski-Szmek
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "hostname-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));
46 assert_se(hostname_is_valid("foobar", true));
47 assert_se(hostname_is_valid("foobar.com", true));
48 assert_se(hostname_is_valid("foobar.com.", true));
49 assert_se(hostname_is_valid("fooBAR", 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("fööbar", true));
54 assert_se(!hostname_is_valid("", true));
55 assert_se(!hostname_is_valid(".", true));
56 assert_se(!hostname_is_valid("..", true));
57 assert_se(!hostname_is_valid("foobar.", true));
58 assert_se(!hostname_is_valid(".foobar", true));
59 assert_se(!hostname_is_valid("foo..bar", true));
60 assert_se(!hostname_is_valid("foo.bar..", true));
61 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));
64 static void test_hostname_cleanup(void) {
67 s
= strdupa("foobar");
68 assert_se(streq(hostname_cleanup(s
), "foobar"));
69 s
= strdupa("foobar.com");
70 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
71 s
= strdupa("foobar.com.");
72 assert_se(streq(hostname_cleanup(s
), "foobar.com"));
73 s
= strdupa("fooBAR");
74 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
75 s
= strdupa("fooBAR.com");
76 assert_se(streq(hostname_cleanup(s
), "fooBAR.com"));
77 s
= strdupa("fooBAR.");
78 assert_se(streq(hostname_cleanup(s
), "fooBAR"));
79 s
= strdupa("fooBAR.com.");
80 assert_se(streq(hostname_cleanup(s
), "fooBAR.com"));
81 s
= strdupa("fööbar");
82 assert_se(streq(hostname_cleanup(s
), "fbar"));
84 assert_se(isempty(hostname_cleanup(s
)));
86 assert_se(isempty(hostname_cleanup(s
)));
88 assert_se(isempty(hostname_cleanup(s
)));
89 s
= strdupa("foobar.");
90 assert_se(streq(hostname_cleanup(s
), "foobar"));
91 s
= strdupa(".foobar");
92 assert_se(streq(hostname_cleanup(s
), "foobar"));
93 s
= strdupa("foo..bar");
94 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
95 s
= strdupa("foo.bar..");
96 assert_se(streq(hostname_cleanup(s
), "foo.bar"));
97 s
= strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
98 assert_se(streq(hostname_cleanup(s
), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
101 static void test_read_hostname_config(void) {
102 char path
[] = "/tmp/hostname.XXXXXX";
106 fd
= mkostemp_safe(path
, O_RDWR
|O_CLOEXEC
);
110 /* simple hostname */
111 write_string_file(path
, "foo", WRITE_STRING_FILE_CREATE
);
112 assert_se(read_hostname_config(path
, &hostname
) == 0);
113 assert_se(streq(hostname
, "foo"));
114 hostname
= mfree(hostname
);
117 write_string_file(path
, "# comment\nfoo", WRITE_STRING_FILE_CREATE
);
118 assert_se(read_hostname_config(path
, &hostname
) == 0);
120 assert_se(streq(hostname
, "foo"));
121 hostname
= mfree(hostname
);
123 /* with comment and extra whitespace */
124 write_string_file(path
, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE
);
125 assert_se(read_hostname_config(path
, &hostname
) == 0);
127 assert_se(streq(hostname
, "foo"));
128 hostname
= mfree(hostname
);
131 write_string_file(path
, "!foo/bar.com", WRITE_STRING_FILE_CREATE
);
132 assert_se(read_hostname_config(path
, &hostname
) == 0);
134 assert_se(streq(hostname
, "foobar.com"));
135 hostname
= mfree(hostname
);
138 hostname
= (char*) 0x1234;
139 write_string_file(path
, "# nothing here\n", WRITE_STRING_FILE_CREATE
);
140 assert_se(read_hostname_config(path
, &hostname
) == -ENOENT
);
141 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
143 /* nonexisting file */
144 assert_se(read_hostname_config("/non/existing", &hostname
) == -ENOENT
);
145 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
150 int main(int argc
, char *argv
[]) {
151 log_parse_environment();
154 test_hostname_is_valid();
155 test_hostname_cleanup();
156 test_read_hostname_config();