1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <unistd.h>
#include "alloc-util.h"
#include "fileio.h"
#include "hostname-util.h"
#include "string-util.h"
#include "tests.h"
#include "tmpfile-util.h"
static void test_hostname_is_valid(void) {
log_info("/* %s */", __func__);
assert_se(hostname_is_valid("foobar", 0));
assert_se(hostname_is_valid("foobar.com", 0));
assert_se(!hostname_is_valid("foobar.com.", 0));
assert_se(hostname_is_valid("fooBAR", 0));
assert_se(hostname_is_valid("fooBAR.com", 0));
assert_se(!hostname_is_valid("fooBAR.", 0));
assert_se(!hostname_is_valid("fooBAR.com.", 0));
assert_se(!hostname_is_valid("fööbar", 0));
assert_se(!hostname_is_valid("", 0));
assert_se(!hostname_is_valid(".", 0));
assert_se(!hostname_is_valid("..", 0));
assert_se(!hostname_is_valid("foobar.", 0));
assert_se(!hostname_is_valid(".foobar", 0));
assert_se(!hostname_is_valid("foo..bar", 0));
assert_se(!hostname_is_valid("foo.bar..", 0));
assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 0));
assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", 0));
assert_se(hostname_is_valid("foobar", VALID_HOSTNAME_TRAILING_DOT));
assert_se(hostname_is_valid("foobar.com", VALID_HOSTNAME_TRAILING_DOT));
assert_se(hostname_is_valid("foobar.com.", VALID_HOSTNAME_TRAILING_DOT));
assert_se(hostname_is_valid("fooBAR", VALID_HOSTNAME_TRAILING_DOT));
assert_se(hostname_is_valid("fooBAR.com", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("fooBAR.", VALID_HOSTNAME_TRAILING_DOT));
assert_se(hostname_is_valid("fooBAR.com.", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("fööbar", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid(".", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("..", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("foobar.", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid(".foobar", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("foo..bar", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("foo.bar..", VALID_HOSTNAME_TRAILING_DOT));
assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", VALID_HOSTNAME_TRAILING_DOT));
}
static void test_hostname_cleanup(void) {
char *s;
log_info("/* %s */", __func__);
s = strdupa("foobar");
assert_se(streq(hostname_cleanup(s), "foobar"));
s = strdupa("foobar.com");
assert_se(streq(hostname_cleanup(s), "foobar.com"));
s = strdupa("foobar.com.");
assert_se(streq(hostname_cleanup(s), "foobar.com"));
s = strdupa("foo-bar.-com-.");
assert_se(streq(hostname_cleanup(s), "foo-bar.com"));
s = strdupa("foo-bar-.-com-.");
assert_se(streq(hostname_cleanup(s), "foo-bar--com"));
s = strdupa("--foo-bar.-com");
assert_se(streq(hostname_cleanup(s), "foo-bar.com"));
s = strdupa("fooBAR");
assert_se(streq(hostname_cleanup(s), "fooBAR"));
s = strdupa("fooBAR.com");
assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
s = strdupa("fooBAR.");
assert_se(streq(hostname_cleanup(s), "fooBAR"));
s = strdupa("fooBAR.com.");
assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
s = strdupa("fööbar");
assert_se(streq(hostname_cleanup(s), "fbar"));
s = strdupa("");
assert_se(isempty(hostname_cleanup(s)));
s = strdupa(".");
assert_se(isempty(hostname_cleanup(s)));
s = strdupa("..");
assert_se(isempty(hostname_cleanup(s)));
s = strdupa("foobar.");
assert_se(streq(hostname_cleanup(s), "foobar"));
s = strdupa(".foobar");
assert_se(streq(hostname_cleanup(s), "foobar"));
s = strdupa("foo..bar");
assert_se(streq(hostname_cleanup(s), "foo.bar"));
s = strdupa("foo.bar..");
assert_se(streq(hostname_cleanup(s), "foo.bar"));
s = strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
assert_se(streq(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
s = strdupa("xxxx........xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
assert_se(streq(hostname_cleanup(s), "xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
}
static void test_hostname_malloc(void) {
_cleanup_free_ char *h = NULL, *l = NULL;
log_info("/* %s */", __func__);
assert_se(h = gethostname_malloc());
log_info("hostname_malloc: \"%s\"", h);
assert_se(l = gethostname_short_malloc());
log_info("hostname_short_malloc: \"%s\"", l);
}
static void test_default_hostname(void) {
log_info("/* %s */", __func__);
if (!hostname_is_valid(FALLBACK_HOSTNAME, 0)) {
log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME);
exit(EXIT_FAILURE);
}
_cleanup_free_ char *n = get_default_hostname();
assert_se(n);
log_info("get_default_hostname: \"%s\"", n);
assert_se(hostname_is_valid(n, 0));
}
int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
test_hostname_is_valid();
test_hostname_cleanup();
test_hostname_malloc();
test_default_hostname();
return 0;
}
|