]> git.proxmox.com Git - systemd.git/blob - src/test/test-hostname-util.c
Enable seccomp support on powerpc, ppc64el, and s390x
[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 "alloc-util.h"
25 #include "fileio.h"
26 #include "hostname-util.h"
27 #include "string-util.h"
28 #include "util.h"
29
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));
47
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));
64 }
65
66 static void test_hostname_cleanup(void) {
67 char *s;
68
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"));
85 s = strdupa("");
86 assert_se(isempty(hostname_cleanup(s)));
87 s = strdupa(".");
88 assert_se(isempty(hostname_cleanup(s)));
89 s = strdupa("..");
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"));
101 }
102
103 static void test_read_hostname_config(void) {
104 char path[] = "/tmp/hostname.XXXXXX";
105 char *hostname;
106 int fd;
107
108 fd = mkostemp_safe(path, O_RDWR|O_CLOEXEC);
109 assert(fd > 0);
110 close(fd);
111
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);
117
118 /* with comment */
119 write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE);
120 assert_se(read_hostname_config(path, &hostname) == 0);
121 assert_se(hostname);
122 assert_se(streq(hostname, "foo"));
123 hostname = mfree(hostname);
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 hostname = mfree(hostname);
131
132 /* cleans up name */
133 write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE);
134 assert_se(read_hostname_config(path, &hostname) == 0);
135 assert_se(hostname);
136 assert_se(streq(hostname, "foobar.com"));
137 hostname = mfree(hostname);
138
139 /* no value set */
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 */
144
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 */
148
149 unlink(path);
150 }
151
152 int main(int argc, char *argv[]) {
153 log_parse_environment();
154 log_open();
155
156 test_hostname_is_valid();
157 test_hostname_cleanup();
158 test_read_hostname_config();
159
160 return 0;
161 }