]>
Commit | Line | Data |
---|---|---|
8fb49443 ZJS |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | Copyright 2013 Thomas H.P. Andersen | |
8 | Copyright 2015 Zbigniew Jędrzejewski-Szmek | |
9 | ||
10 | systemd is free software; you can redistribute it and/or modify it | |
11 | under the terms of the GNU Lesser General Public License as published by | |
12 | the Free Software Foundation; either version 2.1 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | systemd is distributed in the hope that it will be useful, but | |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | Lesser General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU Lesser General Public License | |
21 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
22 | ***/ | |
23 | ||
24 | #include "util.h" | |
25 | #include "fileio.h" | |
26 | #include "hostname-util.h" | |
07630cea | 27 | #include "string-util.h" |
8fb49443 ZJS |
28 | |
29 | static void test_hostname_is_valid(void) { | |
30 | assert_se(hostname_is_valid("foobar", false)); | |
31 | assert_se(hostname_is_valid("foobar.com", false)); | |
32 | assert_se(!hostname_is_valid("foobar.com.", false)); | |
33 | assert_se(hostname_is_valid("fooBAR", false)); | |
34 | assert_se(hostname_is_valid("fooBAR.com", false)); | |
35 | assert_se(!hostname_is_valid("fooBAR.", false)); | |
36 | assert_se(!hostname_is_valid("fooBAR.com.", false)); | |
37 | assert_se(!hostname_is_valid("fööbar", false)); | |
38 | assert_se(!hostname_is_valid("", false)); | |
39 | assert_se(!hostname_is_valid(".", false)); | |
40 | assert_se(!hostname_is_valid("..", false)); | |
41 | assert_se(!hostname_is_valid("foobar.", false)); | |
42 | assert_se(!hostname_is_valid(".foobar", false)); | |
43 | assert_se(!hostname_is_valid("foo..bar", false)); | |
44 | assert_se(!hostname_is_valid("foo.bar..", false)); | |
45 | assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", false)); | |
46 | ||
47 | assert_se(hostname_is_valid("foobar", true)); | |
48 | assert_se(hostname_is_valid("foobar.com", true)); | |
49 | assert_se(hostname_is_valid("foobar.com.", true)); | |
50 | assert_se(hostname_is_valid("fooBAR", true)); | |
51 | assert_se(hostname_is_valid("fooBAR.com", true)); | |
52 | assert_se(!hostname_is_valid("fooBAR.", true)); | |
53 | assert_se(hostname_is_valid("fooBAR.com.", true)); | |
54 | assert_se(!hostname_is_valid("fööbar", true)); | |
55 | assert_se(!hostname_is_valid("", true)); | |
56 | assert_se(!hostname_is_valid(".", true)); | |
57 | assert_se(!hostname_is_valid("..", true)); | |
58 | assert_se(!hostname_is_valid("foobar.", true)); | |
59 | assert_se(!hostname_is_valid(".foobar", true)); | |
60 | assert_se(!hostname_is_valid("foo..bar", true)); | |
61 | assert_se(!hostname_is_valid("foo.bar..", true)); | |
62 | assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true)); | |
63 | } | |
64 | ||
65 | static void test_hostname_cleanup(void) { | |
66 | char *s; | |
67 | ||
68 | s = strdupa("foobar"); | |
ae691c1d | 69 | assert_se(streq(hostname_cleanup(s), "foobar")); |
8fb49443 | 70 | s = strdupa("foobar.com"); |
ae691c1d | 71 | assert_se(streq(hostname_cleanup(s), "foobar.com")); |
8fb49443 | 72 | s = strdupa("foobar.com."); |
ae691c1d | 73 | assert_se(streq(hostname_cleanup(s), "foobar.com")); |
8fb49443 | 74 | s = strdupa("fooBAR"); |
ae691c1d | 75 | assert_se(streq(hostname_cleanup(s), "fooBAR")); |
8fb49443 | 76 | s = strdupa("fooBAR.com"); |
ae691c1d | 77 | assert_se(streq(hostname_cleanup(s), "fooBAR.com")); |
8fb49443 | 78 | s = strdupa("fooBAR."); |
ae691c1d | 79 | assert_se(streq(hostname_cleanup(s), "fooBAR")); |
8fb49443 | 80 | s = strdupa("fooBAR.com."); |
ae691c1d | 81 | assert_se(streq(hostname_cleanup(s), "fooBAR.com")); |
8fb49443 | 82 | s = strdupa("fööbar"); |
ae691c1d | 83 | assert_se(streq(hostname_cleanup(s), "fbar")); |
8fb49443 | 84 | s = strdupa(""); |
ae691c1d | 85 | assert_se(isempty(hostname_cleanup(s))); |
8fb49443 | 86 | s = strdupa("."); |
ae691c1d | 87 | assert_se(isempty(hostname_cleanup(s))); |
8fb49443 | 88 | s = strdupa(".."); |
ae691c1d | 89 | assert_se(isempty(hostname_cleanup(s))); |
8fb49443 | 90 | s = strdupa("foobar."); |
ae691c1d | 91 | assert_se(streq(hostname_cleanup(s), "foobar")); |
8fb49443 | 92 | s = strdupa(".foobar"); |
ae691c1d | 93 | assert_se(streq(hostname_cleanup(s), "foobar")); |
8fb49443 | 94 | s = strdupa("foo..bar"); |
ae691c1d | 95 | assert_se(streq(hostname_cleanup(s), "foo.bar")); |
8fb49443 | 96 | s = strdupa("foo.bar.."); |
ae691c1d | 97 | assert_se(streq(hostname_cleanup(s), "foo.bar")); |
8fb49443 | 98 | s = strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); |
ae691c1d | 99 | assert_se(streq(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); |
8fb49443 ZJS |
100 | } |
101 | ||
102 | static void test_read_hostname_config(void) { | |
103 | char path[] = "/tmp/hostname.XXXXXX"; | |
104 | char *hostname; | |
105 | int fd; | |
106 | ||
107 | fd = mkostemp_safe(path, O_RDWR|O_CLOEXEC); | |
108 | assert(fd > 0); | |
109 | close(fd); | |
110 | ||
111 | /* simple hostname */ | |
112 | write_string_file(path, "foo", WRITE_STRING_FILE_CREATE); | |
113 | assert_se(read_hostname_config(path, &hostname) == 0); | |
114 | assert_se(streq(hostname, "foo")); | |
73974f67 | 115 | hostname = mfree(hostname); |
8fb49443 ZJS |
116 | |
117 | /* with comment */ | |
118 | write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE); | |
119 | assert_se(read_hostname_config(path, &hostname) == 0); | |
120 | assert_se(hostname); | |
121 | assert_se(streq(hostname, "foo")); | |
73974f67 | 122 | hostname = mfree(hostname); |
8fb49443 ZJS |
123 | |
124 | /* with comment and extra whitespace */ | |
125 | write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE); | |
126 | assert_se(read_hostname_config(path, &hostname) == 0); | |
127 | assert_se(hostname); | |
128 | assert_se(streq(hostname, "foo")); | |
73974f67 | 129 | hostname = mfree(hostname); |
8fb49443 ZJS |
130 | |
131 | /* cleans up name */ | |
132 | write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE); | |
133 | assert_se(read_hostname_config(path, &hostname) == 0); | |
134 | assert_se(hostname); | |
135 | assert_se(streq(hostname, "foobar.com")); | |
73974f67 | 136 | hostname = mfree(hostname); |
8fb49443 ZJS |
137 | |
138 | /* no value set */ | |
139 | hostname = (char*) 0x1234; | |
140 | write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE); | |
141 | assert_se(read_hostname_config(path, &hostname) == -ENOENT); | |
142 | assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */ | |
143 | ||
144 | /* nonexisting file */ | |
145 | assert_se(read_hostname_config("/non/existing", &hostname) == -ENOENT); | |
146 | assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */ | |
147 | ||
148 | unlink(path); | |
149 | } | |
150 | ||
151 | int main(int argc, char *argv[]) { | |
152 | log_parse_environment(); | |
153 | log_open(); | |
154 | ||
155 | test_hostname_is_valid(); | |
156 | test_hostname_cleanup(); | |
157 | test_read_hostname_config(); | |
158 | ||
159 | return 0; | |
160 | } |