Debian Codegraph @

src/test/test-hostname-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "hostname-util.h"
5 #include "string-util.h"
6 #include "strv.h"
7 #include "tests.h"
8
9 TEST(hostname_is_valid) {
10 assert_se(hostname_is_valid("foobar", 0));
11 assert_se(hostname_is_valid("foobar.com", 0));
12 assert_se(!hostname_is_valid("foobar.com.", 0));
13 assert_se(hostname_is_valid("fooBAR", 0));
14 assert_se(hostname_is_valid("fooBAR.com", 0));
15 assert_se(!hostname_is_valid("fooBAR.", 0));
16 assert_se(!hostname_is_valid("fooBAR.com.", 0));
17 assert_se(!hostname_is_valid("fööbar", 0));
18 assert_se(!hostname_is_valid("", 0));
19 assert_se(!hostname_is_valid(".", 0));
20 assert_se(!hostname_is_valid("..", 0));
21 assert_se(!hostname_is_valid("foobar.", 0));
22 assert_se(!hostname_is_valid(".foobar", 0));
23 assert_se(!hostname_is_valid("foo..bar", 0));
24 assert_se(!hostname_is_valid("foo.bar..", 0));
25 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 0));
26 assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", 0));
27
28 assert_se(hostname_is_valid("foobar", VALID_HOSTNAME_TRAILING_DOT));
29 assert_se(hostname_is_valid("foobar.com", VALID_HOSTNAME_TRAILING_DOT));
30 assert_se(hostname_is_valid("foobar.com.", VALID_HOSTNAME_TRAILING_DOT));
31 assert_se(hostname_is_valid("fooBAR", VALID_HOSTNAME_TRAILING_DOT));
32 assert_se(hostname_is_valid("fooBAR.com", VALID_HOSTNAME_TRAILING_DOT));
33 assert_se(!hostname_is_valid("fooBAR.", VALID_HOSTNAME_TRAILING_DOT));
34 assert_se(hostname_is_valid("fooBAR.com.", VALID_HOSTNAME_TRAILING_DOT));
35 assert_se(!hostname_is_valid("fööbar", VALID_HOSTNAME_TRAILING_DOT));
36 assert_se(!hostname_is_valid("", VALID_HOSTNAME_TRAILING_DOT));
37 assert_se(!hostname_is_valid(".", VALID_HOSTNAME_TRAILING_DOT));
38 assert_se(!hostname_is_valid("..", VALID_HOSTNAME_TRAILING_DOT));
39 assert_se(!hostname_is_valid("foobar.", VALID_HOSTNAME_TRAILING_DOT));
40 assert_se(!hostname_is_valid(".foobar", VALID_HOSTNAME_TRAILING_DOT));
41 assert_se(!hostname_is_valid("foo..bar", VALID_HOSTNAME_TRAILING_DOT));
42 assert_se(!hostname_is_valid("foo.bar..", VALID_HOSTNAME_TRAILING_DOT));
43 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", VALID_HOSTNAME_TRAILING_DOT));
44
45 ASSERT_FALSE(hostname_is_valid("foo??bar", 0));
46 ASSERT_TRUE(hostname_is_valid("foo??bar", VALID_HOSTNAME_QUESTION_MARK));
47 }
48
49 TEST(hostname_cleanup) {
50 char *s;
51
52 s = strdupa_safe("foobar");
53 ASSERT_STREQ(hostname_cleanup(s), "foobar");
54 s = strdupa_safe("foobar.com");
55 ASSERT_STREQ(hostname_cleanup(s), "foobar.com");
56 s = strdupa_safe("foobar.com.");
57 ASSERT_STREQ(hostname_cleanup(s), "foobar.com");
58 s = strdupa_safe("foo-bar.-com-.");
59 ASSERT_STREQ(hostname_cleanup(s), "foo-bar.com");
60 s = strdupa_safe("foo-bar-.-com-.");
61 ASSERT_STREQ(hostname_cleanup(s), "foo-bar--com");
62 s = strdupa_safe("--foo-bar.-com");
63 ASSERT_STREQ(hostname_cleanup(s), "foo-bar.com");
64 s = strdupa_safe("fooBAR");
65 ASSERT_STREQ(hostname_cleanup(s), "fooBAR");
66 s = strdupa_safe("fooBAR.com");
67 ASSERT_STREQ(hostname_cleanup(s), "fooBAR.com");
68 s = strdupa_safe("fooBAR.");
69 ASSERT_STREQ(hostname_cleanup(s), "fooBAR");
70 s = strdupa_safe("fooBAR.com.");
71 ASSERT_STREQ(hostname_cleanup(s), "fooBAR.com");
72 s = strdupa_safe("fööbar");
73 ASSERT_STREQ(hostname_cleanup(s), "fbar");
74 s = strdupa_safe("");
75 assert_se(isempty(hostname_cleanup(s)));
76 s = strdupa_safe(".");
77 assert_se(isempty(hostname_cleanup(s)));
78 s = strdupa_safe("..");
79 assert_se(isempty(hostname_cleanup(s)));
80 s = strdupa_safe("foobar.");
81 ASSERT_STREQ(hostname_cleanup(s), "foobar");
82 s = strdupa_safe(".foobar");
83 ASSERT_STREQ(hostname_cleanup(s), "foobar");
84 s = strdupa_safe("foo..bar");
85 ASSERT_STREQ(hostname_cleanup(s), "foo.bar");
86 s = strdupa_safe("foo.bar..");
87 ASSERT_STREQ(hostname_cleanup(s), "foo.bar");
88 s = strdupa_safe("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
89 ASSERT_STREQ(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
90 s = strdupa_safe("xxxx........xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
91 ASSERT_STREQ(hostname_cleanup(s), "xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
92 }
93
94 static void test_split_user_at_host_one(const char *s, const char *expected_user, const char *expected_host, int ret) {
95 _cleanup_free_ char *u = NULL, *h = NULL;
96
97 ASSERT_OK_EQ(split_user_at_host(s, &u, &h), ret);
98 ASSERT_STREQ(u, expected_user);
99 ASSERT_STREQ(h, expected_host);
100
101 u = mfree(u);
102 h = mfree(h);
103
104 ASSERT_OK_EQ(split_user_at_host(s, &u, NULL), ret);
105 ASSERT_STREQ(u, expected_user);
106
107 ASSERT_OK_EQ(split_user_at_host(s, NULL, &h), ret);
108 ASSERT_STREQ(h, expected_host);
109 }
110
111 TEST(split_user_at_host) {
112 ASSERT_ERROR(split_user_at_host("", NULL, NULL), EINVAL);
113
114 test_split_user_at_host_one("@", NULL, NULL, 1);
115 test_split_user_at_host_one("a", NULL, "a", 0);
116 test_split_user_at_host_one("a@b", "a", "b", 1);
117 test_split_user_at_host_one("@b", NULL, "b", 1);
118 test_split_user_at_host_one("a@", "a", NULL, 1);
119 test_split_user_at_host_one("aa@@@bb", "aa", "@@bb", 1);
120 }
121
122 TEST(machine_tag_is_valid) {
123 assert_se(machine_tag_is_valid("foo"));
124 assert_se(machine_tag_is_valid("foo-bar.baz"));
125 assert_se(machine_tag_is_valid("Webserver01"));
126 assert_se(machine_tag_is_valid("a"));
127
128 assert_se(!machine_tag_is_valid(NULL));
129 assert_se(!machine_tag_is_valid(""));
130 assert_se(!machine_tag_is_valid("foo:bar")); /* colon is the separator */
131 assert_se(!machine_tag_is_valid("foo bar"));
132 assert_se(!machine_tag_is_valid("fööbar")); /* non-ASCII */
133 assert_se(!machine_tag_is_valid("foo/bar"));
134 assert_se(!machine_tag_is_valid("foo_bar"));
135 assert_se(!machine_tag_is_valid("-foo"));
136 assert_se(!machine_tag_is_valid("foo-"));
137 assert_se(!machine_tag_is_valid(".foo"));
138 assert_se(!machine_tag_is_valid("foo."));
139
140 /* Length boundary: 255 characters is fine, 256 is too long */
141 _cleanup_free_ char *max = strrep("a", 255), *over = strrep("a", 256);
142 assert_se(max);
143 assert_se(over);
144 assert_se(machine_tag_is_valid(max));
145 assert_se(!machine_tag_is_valid(over));
146 }
147
148 TEST(machine_tag_list_is_valid) {
149 assert_se(machine_tag_list_is_valid(NULL)); /* empty list is valid */
150 assert_se(machine_tag_list_is_valid(STRV_MAKE("a")));
151 assert_se(machine_tag_list_is_valid(STRV_MAKE("foo", "bar", "c-d.e")));
152
153 assert_se(!machine_tag_list_is_valid(STRV_MAKE("foo", "b:c")));
154 assert_se(!machine_tag_list_is_valid(STRV_MAKE("foo", "")));
155 }
156
157 TEST(machine_tags_from_string) {
158 _cleanup_strv_free_ char **l = NULL;
159
160 ASSERT_OK(machine_tags_from_string(NULL, /* graceful= */ false, &l));
161 assert_se(strv_isempty(l));
162 l = strv_free(l);
163
164 ASSERT_OK(machine_tags_from_string("", /* graceful= */ true, &l));
165 assert_se(strv_isempty(l));
166 l = strv_free(l);
167
168 /* Sorted and deduplicated */
169 ASSERT_OK(machine_tags_from_string("foo:bar:foo:baz", /* graceful= */ false, &l));
170 assert_se(strv_equal(l, STRV_MAKE("bar", "baz", "foo")));
171 l = strv_free(l);
172
173 /* Graceful: invalid tags are dropped, valid ones kept (sorted/deduplicated) */
174 ASSERT_OK(machine_tags_from_string("foo:in valid:bar:foo", /* graceful= */ true, &l));
175 assert_se(strv_equal(l, STRV_MAKE("bar", "foo")));
176 l = strv_free(l);
177
178 /* Graceful: all tags invalid → empty list */
179 ASSERT_OK(machine_tags_from_string("in valid:also invalid", /* graceful= */ true, &l));
180 assert_se(strv_isempty(l));
181 l = strv_free(l);
182
183 /* Fatal: a single invalid tag fails the whole parse */
184 ASSERT_ERROR(machine_tags_from_string("foo:in valid:bar", /* graceful= */ false, &l), EINVAL);
185 assert_se(!l);
186 }
187
188 DEFINE_TEST_MAIN(LOG_DEBUG);