]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hostname-util.c
hostname-util: get rid of unused parameter of hostname_cleanup()
[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
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
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));
62 }
63
64 static void test_hostname_cleanup(void) {
65 char *s;
66
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"));
83 s = strdupa("");
84 assert_se(isempty(hostname_cleanup(s)));
85 s = strdupa(".");
86 assert_se(isempty(hostname_cleanup(s)));
87 s = strdupa("..");
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"));
99 }
100
101 static void test_read_hostname_config(void) {
102 char path[] = "/tmp/hostname.XXXXXX";
103 char *hostname;
104 int fd;
105
106 fd = mkostemp_safe(path, O_RDWR|O_CLOEXEC);
107 assert(fd > 0);
108 close(fd);
109
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 free(hostname);
115 hostname = NULL;
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 free(hostname);
123 hostname = NULL;
124
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);
128 assert_se(hostname);
129 assert_se(streq(hostname, "foo"));
130 free(hostname);
131 hostname = NULL;
132
133 /* cleans up name */
134 write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE);
135 assert_se(read_hostname_config(path, &hostname) == 0);
136 assert_se(hostname);
137 assert_se(streq(hostname, "foobar.com"));
138 free(hostname);
139 hostname = NULL;
140
141 /* no value set */
142 hostname = (char*) 0x1234;
143 write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE);
144 assert_se(read_hostname_config(path, &hostname) == -ENOENT);
145 assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
146
147 /* nonexisting file */
148 assert_se(read_hostname_config("/non/existing", &hostname) == -ENOENT);
149 assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
150
151 unlink(path);
152 }
153
154 int main(int argc, char *argv[]) {
155 log_parse_environment();
156 log_open();
157
158 test_hostname_is_valid();
159 test_hostname_cleanup();
160 test_read_hostname_config();
161
162 return 0;
163 }