]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hostname-util.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / test / test-hostname-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7 Copyright 2013 Thomas H.P. Andersen
8 Copyright 2015 Zbigniew Jędrzejewski-Szmek
9
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.
14
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.
19
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/>.
22 ***/
23
24 #include "util.h"
25 #include "fileio.h"
26 #include "hostname-util.h"
27 #include "string-util.h"
28
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
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));
63 }
64
65 static void test_hostname_cleanup(void) {
66 char *s;
67
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"));
84 s = strdupa("");
85 assert_se(isempty(hostname_cleanup(s)));
86 s = strdupa(".");
87 assert_se(isempty(hostname_cleanup(s)));
88 s = strdupa("..");
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"));
100 }
101
102 static void test_read_hostname_config(void) {
103 char path[] = "/tmp/hostname.XXXXXX";
104 char *hostname;
105 int fd;
106
107 fd = mkostemp_safe(path, O_RDWR|O_CLOEXEC);
108 assert(fd > 0);
109 close(fd);
110
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);
116
117 /* with comment */
118 write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE);
119 assert_se(read_hostname_config(path, &hostname) == 0);
120 assert_se(hostname);
121 assert_se(streq(hostname, "foo"));
122 hostname = mfree(hostname);
123
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);
127 assert_se(hostname);
128 assert_se(streq(hostname, "foo"));
129 hostname = mfree(hostname);
130
131 /* cleans up name */
132 write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE);
133 assert_se(read_hostname_config(path, &hostname) == 0);
134 assert_se(hostname);
135 assert_se(streq(hostname, "foobar.com"));
136 hostname = mfree(hostname);
137
138 /* no value set */
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 */
143
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 */
147
148 unlink(path);
149 }
150
151 int main(int argc, char *argv[]) {
152 log_parse_environment();
153 log_open();
154
155 test_hostname_is_valid();
156 test_hostname_cleanup();
157 test_read_hostname_config();
158
159 return 0;
160 }