]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hostname-util.c
ASSERT_STREQ for simple cases
[thirdparty/systemd.git] / src / test / test-hostname-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 "tests.h"
10 #include "tmpfile-util.h"
11
12 TEST(hostname_is_valid) {
13 assert_se(hostname_is_valid("foobar", 0));
14 assert_se(hostname_is_valid("foobar.com", 0));
15 assert_se(!hostname_is_valid("foobar.com.", 0));
16 assert_se(hostname_is_valid("fooBAR", 0));
17 assert_se(hostname_is_valid("fooBAR.com", 0));
18 assert_se(!hostname_is_valid("fooBAR.", 0));
19 assert_se(!hostname_is_valid("fooBAR.com.", 0));
20 assert_se(!hostname_is_valid("fööbar", 0));
21 assert_se(!hostname_is_valid("", 0));
22 assert_se(!hostname_is_valid(".", 0));
23 assert_se(!hostname_is_valid("..", 0));
24 assert_se(!hostname_is_valid("foobar.", 0));
25 assert_se(!hostname_is_valid(".foobar", 0));
26 assert_se(!hostname_is_valid("foo..bar", 0));
27 assert_se(!hostname_is_valid("foo.bar..", 0));
28 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 0));
29 assert_se(!hostname_is_valid("au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", 0));
30
31 assert_se(hostname_is_valid("foobar", VALID_HOSTNAME_TRAILING_DOT));
32 assert_se(hostname_is_valid("foobar.com", VALID_HOSTNAME_TRAILING_DOT));
33 assert_se(hostname_is_valid("foobar.com.", VALID_HOSTNAME_TRAILING_DOT));
34 assert_se(hostname_is_valid("fooBAR", VALID_HOSTNAME_TRAILING_DOT));
35 assert_se(hostname_is_valid("fooBAR.com", VALID_HOSTNAME_TRAILING_DOT));
36 assert_se(!hostname_is_valid("fooBAR.", VALID_HOSTNAME_TRAILING_DOT));
37 assert_se(hostname_is_valid("fooBAR.com.", VALID_HOSTNAME_TRAILING_DOT));
38 assert_se(!hostname_is_valid("fööbar", VALID_HOSTNAME_TRAILING_DOT));
39 assert_se(!hostname_is_valid("", VALID_HOSTNAME_TRAILING_DOT));
40 assert_se(!hostname_is_valid(".", VALID_HOSTNAME_TRAILING_DOT));
41 assert_se(!hostname_is_valid("..", VALID_HOSTNAME_TRAILING_DOT));
42 assert_se(!hostname_is_valid("foobar.", VALID_HOSTNAME_TRAILING_DOT));
43 assert_se(!hostname_is_valid(".foobar", VALID_HOSTNAME_TRAILING_DOT));
44 assert_se(!hostname_is_valid("foo..bar", VALID_HOSTNAME_TRAILING_DOT));
45 assert_se(!hostname_is_valid("foo.bar..", VALID_HOSTNAME_TRAILING_DOT));
46 assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", VALID_HOSTNAME_TRAILING_DOT));
47 }
48
49 TEST(hostname_cleanup) {
50 char *s;
51
52 s = strdupa_safe("foobar");
53 ASSERT_STREQ(hostname_cleanup(s), "foobar");
54 s = strdupa_safe("foobar.com");
55 ASSERT_STREQ(hostname_cleanup(s), "foobar.com");
56 s = strdupa_safe("foobar.com.");
57 ASSERT_STREQ(hostname_cleanup(s), "foobar.com");
58 s = strdupa_safe("foo-bar.-com-.");
59 ASSERT_STREQ(hostname_cleanup(s), "foo-bar.com");
60 s = strdupa_safe("foo-bar-.-com-.");
61 ASSERT_STREQ(hostname_cleanup(s), "foo-bar--com");
62 s = strdupa_safe("--foo-bar.-com");
63 ASSERT_STREQ(hostname_cleanup(s), "foo-bar.com");
64 s = strdupa_safe("fooBAR");
65 ASSERT_STREQ(hostname_cleanup(s), "fooBAR");
66 s = strdupa_safe("fooBAR.com");
67 ASSERT_STREQ(hostname_cleanup(s), "fooBAR.com");
68 s = strdupa_safe("fooBAR.");
69 ASSERT_STREQ(hostname_cleanup(s), "fooBAR");
70 s = strdupa_safe("fooBAR.com.");
71 ASSERT_STREQ(hostname_cleanup(s), "fooBAR.com");
72 s = strdupa_safe("fööbar");
73 ASSERT_STREQ(hostname_cleanup(s), "fbar");
74 s = strdupa_safe("");
75 assert_se(isempty(hostname_cleanup(s)));
76 s = strdupa_safe(".");
77 assert_se(isempty(hostname_cleanup(s)));
78 s = strdupa_safe("..");
79 assert_se(isempty(hostname_cleanup(s)));
80 s = strdupa_safe("foobar.");
81 ASSERT_STREQ(hostname_cleanup(s), "foobar");
82 s = strdupa_safe(".foobar");
83 ASSERT_STREQ(hostname_cleanup(s), "foobar");
84 s = strdupa_safe("foo..bar");
85 ASSERT_STREQ(hostname_cleanup(s), "foo.bar");
86 s = strdupa_safe("foo.bar..");
87 ASSERT_STREQ(hostname_cleanup(s), "foo.bar");
88 s = strdupa_safe("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
89 ASSERT_STREQ(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
90 s = strdupa_safe("xxxx........xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
91 ASSERT_STREQ(hostname_cleanup(s), "xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
92 }
93
94 TEST(hostname_malloc) {
95 _cleanup_free_ char *h = NULL, *l = NULL;
96
97 assert_se(h = gethostname_malloc());
98 log_info("hostname_malloc: \"%s\"", h);
99
100 assert_se(l = gethostname_short_malloc());
101 log_info("hostname_short_malloc: \"%s\"", l);
102 }
103
104 TEST(default_hostname) {
105 if (!hostname_is_valid(FALLBACK_HOSTNAME, 0)) {
106 log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME);
107 exit(EXIT_FAILURE);
108 }
109
110 _cleanup_free_ char *n = get_default_hostname();
111 assert_se(n);
112 log_info("get_default_hostname: \"%s\"", n);
113 assert_se(hostname_is_valid(n, 0));
114 }
115
116 DEFINE_TEST_MAIN(LOG_DEBUG);