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