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