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