]>
git.proxmox.com Git - 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/>.
24 #include "alloc-util.h"
26 #include "hostname-util.h"
27 #include "string-util.h"
30 static void test_hostname_is_valid(void) {
31 assert_se(hostname_is_valid("foobar", false));
32 assert_se(hostname_is_valid("foobar.com", 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("fooBAR.", false));
37 assert_se(!hostname_is_valid("fooBAR.com.", false));
38 assert_se(!hostname_is_valid("fööbar", false));
39 assert_se(!hostname_is_valid("", false));
40 assert_se(!hostname_is_valid(".", false));
41 assert_se(!hostname_is_valid("..", false));
42 assert_se(!hostname_is_valid("foobar.", false));
43 assert_se(!hostname_is_valid(".foobar", false));
44 assert_se(!hostname_is_valid("foo..bar", false));
45 assert_se(!hostname_is_valid("foo.bar..", false));
46 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 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_hostname_config(void) {
104 char path
[] = "/tmp/hostname.XXXXXX";
108 fd
= mkostemp_safe(path
, O_RDWR
|O_CLOEXEC
);
112 /* simple hostname */
113 write_string_file(path
, "foo", WRITE_STRING_FILE_CREATE
);
114 assert_se(read_hostname_config(path
, &hostname
) == 0);
115 assert_se(streq(hostname
, "foo"));
116 hostname
= mfree(hostname
);
119 write_string_file(path
, "# comment\nfoo", WRITE_STRING_FILE_CREATE
);
120 assert_se(read_hostname_config(path
, &hostname
) == 0);
122 assert_se(streq(hostname
, "foo"));
123 hostname
= mfree(hostname
);
125 /* with comment and extra whitespace */
126 write_string_file(path
, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE
);
127 assert_se(read_hostname_config(path
, &hostname
) == 0);
129 assert_se(streq(hostname
, "foo"));
130 hostname
= mfree(hostname
);
133 write_string_file(path
, "!foo/bar.com", WRITE_STRING_FILE_CREATE
);
134 assert_se(read_hostname_config(path
, &hostname
) == 0);
136 assert_se(streq(hostname
, "foobar.com"));
137 hostname
= mfree(hostname
);
140 hostname
= (char*) 0x1234;
141 write_string_file(path
, "# nothing here\n", WRITE_STRING_FILE_CREATE
);
142 assert_se(read_hostname_config(path
, &hostname
) == -ENOENT
);
143 assert_se(hostname
== (char*) 0x1234); /* does not touch argument on error */
145 /* nonexisting file */
146 assert_se(read_hostname_config("/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_hostname_config();