]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hostname-util.c
9a271136967879cb7263997f12f86552d87a68da
[thirdparty/systemd.git] / src / test / test-hostname-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2010 Lennart Poettering
4 Copyright 2013 Thomas H.P. Andersen
5 Copyright 2015 Zbigniew Jędrzejewski-Szmek
6 ***/
7
8 #include "alloc-util.h"
9 #include "fileio.h"
10 #include "hostname-util.h"
11 #include "string-util.h"
12 #include "util.h"
13
14 static void test_hostname_is_valid(void) {
15 assert_se(hostname_is_valid("foobar", false));
16 assert_se(hostname_is_valid("foobar.com", false));
17 assert_se(!hostname_is_valid("foobar.com.", false));
18 assert_se(hostname_is_valid("fooBAR", false));
19 assert_se(hostname_is_valid("fooBAR.com", false));
20 assert_se(!hostname_is_valid("fooBAR.", false));
21 assert_se(!hostname_is_valid("fooBAR.com.", false));
22 assert_se(!hostname_is_valid("fööbar", false));
23 assert_se(!hostname_is_valid("", false));
24 assert_se(!hostname_is_valid(".", false));
25 assert_se(!hostname_is_valid("..", false));
26 assert_se(!hostname_is_valid("foobar.", false));
27 assert_se(!hostname_is_valid(".foobar", false));
28 assert_se(!hostname_is_valid("foo..bar", false));
29 assert_se(!hostname_is_valid("foo.bar..", false));
30 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", false));
31 assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", false));
32
33 assert_se(hostname_is_valid("foobar", true));
34 assert_se(hostname_is_valid("foobar.com", true));
35 assert_se(hostname_is_valid("foobar.com.", true));
36 assert_se(hostname_is_valid("fooBAR", true));
37 assert_se(hostname_is_valid("fooBAR.com", true));
38 assert_se(!hostname_is_valid("fooBAR.", true));
39 assert_se(hostname_is_valid("fooBAR.com.", true));
40 assert_se(!hostname_is_valid("fööbar", true));
41 assert_se(!hostname_is_valid("", true));
42 assert_se(!hostname_is_valid(".", true));
43 assert_se(!hostname_is_valid("..", true));
44 assert_se(!hostname_is_valid("foobar.", true));
45 assert_se(!hostname_is_valid(".foobar", true));
46 assert_se(!hostname_is_valid("foo..bar", true));
47 assert_se(!hostname_is_valid("foo.bar..", true));
48 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));
49 }
50
51 static void test_hostname_cleanup(void) {
52 char *s;
53
54 s = strdupa("foobar");
55 assert_se(streq(hostname_cleanup(s), "foobar"));
56 s = strdupa("foobar.com");
57 assert_se(streq(hostname_cleanup(s), "foobar.com"));
58 s = strdupa("foobar.com.");
59 assert_se(streq(hostname_cleanup(s), "foobar.com"));
60 s = strdupa("fooBAR");
61 assert_se(streq(hostname_cleanup(s), "fooBAR"));
62 s = strdupa("fooBAR.com");
63 assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
64 s = strdupa("fooBAR.");
65 assert_se(streq(hostname_cleanup(s), "fooBAR"));
66 s = strdupa("fooBAR.com.");
67 assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
68 s = strdupa("fööbar");
69 assert_se(streq(hostname_cleanup(s), "fbar"));
70 s = strdupa("");
71 assert_se(isempty(hostname_cleanup(s)));
72 s = strdupa(".");
73 assert_se(isempty(hostname_cleanup(s)));
74 s = strdupa("..");
75 assert_se(isempty(hostname_cleanup(s)));
76 s = strdupa("foobar.");
77 assert_se(streq(hostname_cleanup(s), "foobar"));
78 s = strdupa(".foobar");
79 assert_se(streq(hostname_cleanup(s), "foobar"));
80 s = strdupa("foo..bar");
81 assert_se(streq(hostname_cleanup(s), "foo.bar"));
82 s = strdupa("foo.bar..");
83 assert_se(streq(hostname_cleanup(s), "foo.bar"));
84 s = strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
85 assert_se(streq(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
86 }
87
88 static void test_read_etc_hostname(void) {
89 char path[] = "/tmp/hostname.XXXXXX";
90 char *hostname;
91 int fd;
92
93 fd = mkostemp_safe(path);
94 assert(fd > 0);
95 close(fd);
96
97 /* simple hostname */
98 assert_se(write_string_file(path, "foo", WRITE_STRING_FILE_CREATE) == 0);
99 assert_se(read_etc_hostname(path, &hostname) == 0);
100 assert_se(streq(hostname, "foo"));
101 hostname = mfree(hostname);
102
103 /* with comment */
104 assert_se(write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE) == 0);
105 assert_se(read_etc_hostname(path, &hostname) == 0);
106 assert_se(hostname);
107 assert_se(streq(hostname, "foo"));
108 hostname = mfree(hostname);
109
110 /* with comment and extra whitespace */
111 assert_se(write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE) == 0);
112 assert_se(read_etc_hostname(path, &hostname) == 0);
113 assert_se(hostname);
114 assert_se(streq(hostname, "foo"));
115 hostname = mfree(hostname);
116
117 /* cleans up name */
118 assert_se(write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE) == 0);
119 assert_se(read_etc_hostname(path, &hostname) == 0);
120 assert_se(hostname);
121 assert_se(streq(hostname, "foobar.com"));
122 hostname = mfree(hostname);
123
124 /* no value set */
125 hostname = (char*) 0x1234;
126 assert_se(write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE) == 0);
127 assert_se(read_etc_hostname(path, &hostname) == -ENOENT);
128 assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
129
130 /* nonexisting file */
131 assert_se(read_etc_hostname("/non/existing", &hostname) == -ENOENT);
132 assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
133
134 unlink(path);
135 }
136
137 int main(int argc, char *argv[]) {
138 log_parse_environment();
139 log_open();
140
141 test_hostname_is_valid();
142 test_hostname_cleanup();
143 test_read_etc_hostname();
144
145 return 0;
146 }