Debian Codegraph @

src/test/test-hostname-setup.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fnmatch.h>
4 #include <sched.h>
5 #include <stdlib.h>
6
7 #include "alloc-util.h"
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "hostname-setup.h"
11 #include "hostname-util.h"
12 #include "id128-util.h"
13 #include "pidref.h"
14 #include "process-util.h"
15 #include "tests.h"
16 #include "tmpfile-util.h"
17
18 TEST(read_etc_hostname) {
19 _cleanup_(unlink_tempfilep) char path[] = "/tmp/hostname.XXXXXX";
20 char *hostname;
21 int r;
22
23 safe_close(ASSERT_FD(mkostemp_safe(path)));
24
25 /* simple hostname */
26 ASSERT_OK(write_string_file(path, "foo", WRITE_STRING_FILE_CREATE));
27 ASSERT_OK(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname));
28 ASSERT_STREQ(hostname, "foo");
29 hostname = mfree(hostname);
30
31 /* with comment */
32 ASSERT_OK(write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE));
33 ASSERT_OK(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname));
34 ASSERT_NOT_NULL(hostname);
35 ASSERT_STREQ(hostname, "foo");
36 hostname = mfree(hostname);
37
38 /* with comment and extra whitespace */
39 ASSERT_OK(write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE));
40 ASSERT_OK(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname));
41 ASSERT_NOT_NULL(hostname);
42 ASSERT_STREQ(hostname, "foo");
43 hostname = mfree(hostname);
44
45 /* cleans up name */
46 ASSERT_OK(write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE));
47 ASSERT_OK(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname));
48 ASSERT_NOT_NULL(hostname);
49 ASSERT_STREQ(hostname, "foobar.com");
50 hostname = mfree(hostname);
51
52 /* with wildcards */
53 ASSERT_OK(write_string_file(path, "foo????????x??????????u", WRITE_STRING_FILE_CREATE));
54 ASSERT_OK(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname));
55 ASSERT_NOT_NULL(hostname);
56 ASSERT_STREQ(hostname, "foo????????x??????????u");
57 hostname = mfree(hostname);
58 r = read_etc_hostname(path, /* substitute_wildcards= */ true, &hostname);
59 if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r))
60 log_tests_skipped("skipping wildcard hostname tests, no machine ID defined");
61 else {
62 ASSERT_OK(r);
63 ASSERT_NOT_NULL(hostname);
64 ASSERT_NULL(strchr(hostname, '?'));
65 ASSERT_EQ(fnmatch("foo????????x??????????u", hostname, /* flags= */ 0), 0);
66 ASSERT_TRUE(hostname_is_valid(hostname, /* flags= */ 0));
67 hostname = mfree(hostname);
68 }
69
70 /* no value set */
71 hostname = (char*) 0x1234;
72 ASSERT_OK(write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_TRUNCATE));
73 ASSERT_ERROR(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname), ENOENT);
74 assert(hostname == (char*) 0x1234); /* does not touch argument on error */
75
76 /* nonexisting file */
77 ASSERT_ERROR(read_etc_hostname("/non/existing", /* substitute_wildcards= */ false, &hostname), ENOENT);
78 assert(hostname == (char*) 0x1234); /* does not touch argument on error */
79 }
80
81 TEST(hostname_substitute_wildcards) {
82 int r;
83
84 r = sd_id128_get_machine(NULL);
85 if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r))
86 return (void) log_tests_skipped_errno(r, "skipping wildcard hostname tests, no machine ID defined");
87
88 _cleanup_free_ char *buf = NULL;
89 ASSERT_NOT_NULL((buf = strdup("")));
90 ASSERT_OK(hostname_substitute_wildcards(buf));
91 ASSERT_STREQ(buf, "");
92 ASSERT_NULL(buf = mfree(buf));
93
94 ASSERT_NOT_NULL((buf = strdup("hogehoge")));
95 ASSERT_OK(hostname_substitute_wildcards(buf));
96 ASSERT_STREQ(buf, "hogehoge");
97 ASSERT_NULL(buf = mfree(buf));
98
99 ASSERT_NOT_NULL((buf = strdup("hoge??hoge??foo?")));
100 ASSERT_OK(hostname_substitute_wildcards(buf));
101 log_debug("hostname_substitute_wildcards(\"hoge??hoge??foo?\"): → \"%s\"", buf);
102 ASSERT_EQ(fnmatch("hoge??hoge??foo?", buf, /* flags= */ 0), 0);
103 ASSERT_TRUE(hostname_is_valid(buf, /* flags= */ 0));
104 ASSERT_NULL(buf = mfree(buf));
105 }
106
107 TEST(hostname_setup) {
108 hostname_setup(false);
109 }
110
111 TEST(hostname_malloc) {
112 _cleanup_free_ char *h = NULL, *l = NULL;
113
114 assert_se(h = gethostname_malloc());
115 log_info("hostname_malloc: \"%s\"", h);
116
117 assert_se(l = gethostname_short_malloc());
118 log_info("hostname_short_malloc: \"%s\"", l);
119 }
120
121 TEST(default_hostname) {
122 if (!hostname_is_valid(FALLBACK_HOSTNAME, 0)) {
123 log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME);
124 exit(EXIT_FAILURE);
125 }
126
127 _cleanup_free_ char *n = get_default_hostname();
128 ASSERT_NOT_NULL(n);
129 log_info("get_default_hostname: \"%s\"", n);
130 ASSERT_TRUE(hostname_is_valid(n, /* flags= */ 0));
131
132 _cleanup_free_ char *m = get_default_hostname_raw();
133 ASSERT_NOT_NULL(m);
134 log_info("get_default_hostname_raw: \"%s\"", m);
135 ASSERT_TRUE(hostname_is_valid(m, VALID_HOSTNAME_QUESTION_MARK));
136 }
137
138 TEST(pidref_gethostname_full) {
139 int r;
140
141 if (geteuid() != 0)
142 return (void) log_tests_skipped("Not privileged");
143
144 _cleanup_free_ char *original = NULL, *original_short = NULL;
145 ASSERT_NOT_NULL(original = gethostname_malloc());
146 ASSERT_NOT_NULL(original_short = gethostname_short_malloc());
147
148 _cleanup_close_pair_ int fds[2] = EBADF_PAIR;
149 ASSERT_OK_ERRNO(pipe2(fds, O_CLOEXEC));
150
151 _cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
152 r = pidref_safe_fork("(test-pidref-gethostname)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, &pidref);
153 ASSERT_OK(r);
154 if (r == 0) {
155 fds[0] = safe_close(fds[0]);
156
157 ASSERT_OK_ERRNO(unshare(CLONE_NEWUTS));
158 ASSERT_OK(sethostname_idempotent("hogehoge.example.com"));
159
160 ASSERT_OK_EQ_ERRNO(write(fds[1], &(const char[]) { 'x' }, 1), 1);
161 freeze();
162 }
163
164 fds[1] = safe_close(fds[1]);
165
166 char x;
167 ASSERT_OK_EQ_ERRNO(read(fds[0], &x, 1), 1);
168 ASSERT_EQ(x, 'x');
169
170 _cleanup_free_ char *s = NULL;
171 ASSERT_OK(pidref_gethostname_full(&pidref, /* flags= */ 0, &s));
172 ASSERT_STREQ(s, "hogehoge.example.com");
173
174 s = mfree(s);
175
176 ASSERT_OK(pidref_gethostname_full(&pidref, GET_HOSTNAME_SHORT, &s));
177 ASSERT_STREQ(s, "hogehoge");
178
179 s = mfree(s);
180
181 _cleanup_(pidref_done) PidRef self = PIDREF_NULL;
182 ASSERT_OK(pidref_set_self(&self));
183
184 ASSERT_OK(pidref_gethostname_full(&self, /* flags= */ 0, &s));
185 ASSERT_STREQ(s, original);
186
187 s = mfree(s);
188
189 ASSERT_OK(pidref_gethostname_full(&self, GET_HOSTNAME_SHORT, &s));
190 ASSERT_STREQ(s, original_short);
191
192 s = mfree(s);
193
194 ASSERT_NOT_NULL(s = gethostname_malloc());
195 ASSERT_STREQ(s, original);
196
197 s = mfree(s);
198
199 ASSERT_NOT_NULL(s = gethostname_short_malloc());
200 ASSERT_STREQ(s, original_short);
201 }
202
203 DEFINE_TEST_MAIN(LOG_DEBUG);