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