LCOV - code coverage report
Current view: top level - test - test-hostname-util.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 116 116 100.0 %
Date: 2019-08-22 15:41:25 Functions: 4 4 100.0 %

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

Generated by: LCOV version 1.14