8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek This file is part of systemd.
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek Copyright 2010 Lennart Poettering
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek Copyright 2013 Thomas H.P. Andersen
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek Copyright 2015 Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek (at your option) any later version.
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekstatic void test_hostname_is_valid(void) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname_is_valid("foobar", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname_is_valid("foobar.com", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("foobar.com.", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname_is_valid("fooBAR", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname_is_valid("fooBAR.com", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("fooBAR.", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("fooBAR.com.", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("fööbar", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid(".", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("..", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("foobar.", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid(".foobar", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("foo..bar", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("foo.bar..", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", false));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname_is_valid("foobar", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname_is_valid("foobar.com", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname_is_valid("foobar.com.", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname_is_valid("fooBAR", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname_is_valid("fooBAR.com", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("fooBAR.", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname_is_valid("fooBAR.com.", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("fööbar", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid(".", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("..", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("foobar.", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid(".foobar", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("foo..bar", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("foo.bar..", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", true));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekstatic void test_hostname_cleanup(void) {
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foobar"));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foobar.com"));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foobar.com"));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "fooBAR"));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "fooBAR"));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "fbar"));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(isempty(hostname_cleanup(s)));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(isempty(hostname_cleanup(s)));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(isempty(hostname_cleanup(s)));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foobar"));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foobar"));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foo.bar"));
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foo.bar"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekstatic void test_read_hostname_config(void) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek char path[] = "/tmp/hostname.XXXXXX";
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek fd = mkostemp_safe(path, O_RDWR|O_CLOEXEC);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek /* simple hostname */
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek write_string_file(path, "foo", WRITE_STRING_FILE_CREATE);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(read_hostname_config(path, &hostname) == 0);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(streq(hostname, "foo"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek /* with comment */
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(read_hostname_config(path, &hostname) == 0);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(streq(hostname, "foo"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek /* with comment and extra whitespace */
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(read_hostname_config(path, &hostname) == 0);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(streq(hostname, "foo"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek /* cleans up name */
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(read_hostname_config(path, &hostname) == 0);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(streq(hostname, "foobar.com"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek /* no value set */
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(read_hostname_config(path, &hostname) == -ENOENT);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek /* nonexisting file */
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(read_hostname_config("/non/existing", &hostname) == -ENOENT);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekint main(int argc, char *argv[]) {