8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek/***
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek This file is part of systemd.
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
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
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
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
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-Szmek***/
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek#include "fileio.h"
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek#include "hostname-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "string-util.h"
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "util.h"
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
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
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-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekstatic void test_hostname_cleanup(void) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek char *s;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("foobar");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foobar"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("foobar.com");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foobar.com"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("foobar.com.");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foobar.com"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("fooBAR");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "fooBAR"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("fooBAR.com");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("fooBAR.");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "fooBAR"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("fooBAR.com.");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "fooBAR.com"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("fööbar");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "fbar"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(isempty(hostname_cleanup(s)));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa(".");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(isempty(hostname_cleanup(s)));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("..");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(isempty(hostname_cleanup(s)));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("foobar.");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foobar"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa(".foobar");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foobar"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("foo..bar");
ae691c1d9382995ea7e28317f5c37023229c27eeZbigniew Jędrzejewski-Szmek assert_se(streq(hostname_cleanup(s), "foo.bar"));
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek s = strdupa("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-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekstatic void test_read_hostname_config(void) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek char path[] = "/tmp/hostname.XXXXXX";
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek char *hostname;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek int fd;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek fd = mkostemp_safe(path, O_RDWR|O_CLOEXEC);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert(fd > 0);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek close(fd);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
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"));
73974f6768ef5314a572eb93f5cfc7f0f29c9549Zbigniew Jędrzejewski-Szmek hostname = mfree(hostname);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
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(hostname);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(streq(hostname, "foo"));
73974f6768ef5314a572eb93f5cfc7f0f29c9549Zbigniew Jędrzejewski-Szmek hostname = mfree(hostname);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
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(hostname);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(streq(hostname, "foo"));
73974f6768ef5314a572eb93f5cfc7f0f29c9549Zbigniew Jędrzejewski-Szmek hostname = mfree(hostname);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
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(hostname);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek assert_se(streq(hostname, "foobar.com"));
73974f6768ef5314a572eb93f5cfc7f0f29c9549Zbigniew Jędrzejewski-Szmek hostname = mfree(hostname);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek /* no value set */
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek hostname = (char*) 0x1234;
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
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-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek unlink(path);
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek}
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmekint main(int argc, char *argv[]) {
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek log_parse_environment();
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek log_open();
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek test_hostname_is_valid();
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek test_hostname_cleanup();
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek test_read_hostname_config();
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek return 0;
8fb494435889dcb9e1c09b8c7220e47bab717bf9Zbigniew Jędrzejewski-Szmek}