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