]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-hostname-util.c
util-lib: split out allocation calls into alloc-util.[ch]
[thirdparty/systemd.git] / src / test / test-hostname-util.c
CommitLineData
8fb49443
ZJS
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
b5efdb8a 24#include "alloc-util.h"
8fb49443
ZJS
25#include "fileio.h"
26#include "hostname-util.h"
07630cea 27#include "string-util.h"
b5efdb8a 28#include "util.h"
8fb49443
ZJS
29
30static 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
66static void test_hostname_cleanup(void) {
67 char *s;
68
69 s = strdupa("foobar");
ae691c1d 70 assert_se(streq(hostname_cleanup(s), "foobar"));
8fb49443 71 s = strdupa("foobar.com");
ae691c1d 72 assert_se(streq(hostname_cleanup(s), "foobar.com"));
8fb49443 73 s = strdupa("foobar.com.");
ae691c1d 74 assert_se(streq(hostname_cleanup(s), "foobar.com"));
8fb49443 75 s = strdupa("fooBAR");
ae691c1d 76 assert_se(streq(hostname_cleanup(s), "fooBAR"));
8fb49443 77 s = strdupa("fooBAR.com");
ae691c1d 78 assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
8fb49443 79 s = strdupa("fooBAR.");
ae691c1d 80 assert_se(streq(hostname_cleanup(s), "fooBAR"));
8fb49443 81 s = strdupa("fooBAR.com.");
ae691c1d 82 assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
8fb49443 83 s = strdupa("fööbar");
ae691c1d 84 assert_se(streq(hostname_cleanup(s), "fbar"));
8fb49443 85 s = strdupa("");
ae691c1d 86 assert_se(isempty(hostname_cleanup(s)));
8fb49443 87 s = strdupa(".");
ae691c1d 88 assert_se(isempty(hostname_cleanup(s)));
8fb49443 89 s = strdupa("..");
ae691c1d 90 assert_se(isempty(hostname_cleanup(s)));
8fb49443 91 s = strdupa("foobar.");
ae691c1d 92 assert_se(streq(hostname_cleanup(s), "foobar"));
8fb49443 93 s = strdupa(".foobar");
ae691c1d 94 assert_se(streq(hostname_cleanup(s), "foobar"));
8fb49443 95 s = strdupa("foo..bar");
ae691c1d 96 assert_se(streq(hostname_cleanup(s), "foo.bar"));
8fb49443 97 s = strdupa("foo.bar..");
ae691c1d 98 assert_se(streq(hostname_cleanup(s), "foo.bar"));
8fb49443 99 s = strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
ae691c1d 100 assert_se(streq(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
8fb49443
ZJS
101}
102
103static 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"));
73974f67 116 hostname = mfree(hostname);
8fb49443
ZJS
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"));
73974f67 123 hostname = mfree(hostname);
8fb49443
ZJS
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"));
73974f67 130 hostname = mfree(hostname);
8fb49443
ZJS
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"));
73974f67 137 hostname = mfree(hostname);
8fb49443
ZJS
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
152int 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}