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