• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.38
/src/test/test-hostname-setup.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fnmatch.h>
4
#include <unistd.h>
5

6
#include "alloc-util.h"
7
#include "fd-util.h"
8
#include "fileio.h"
9
#include "fs-util.h"
10
#include "hostname-setup.h"
11
#include "hostname-util.h"
12
#include "id128-util.h"
13
#include "string-util.h"
14
#include "tests.h"
15
#include "tmpfile-util.h"
16

17
TEST(read_etc_hostname) {
1✔
18
        _cleanup_(unlink_tempfilep) char path[] = "/tmp/hostname.XXXXXX";
1✔
19
        char *hostname;
1✔
20
        int r;
1✔
21

22
        safe_close(ASSERT_FD(mkostemp_safe(path)));
1✔
23

24
        /* simple hostname */
25
        ASSERT_OK(write_string_file(path, "foo", WRITE_STRING_FILE_CREATE));
1✔
26
        ASSERT_OK(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname));
1✔
27
        ASSERT_STREQ(hostname, "foo");
1✔
28
        hostname = mfree(hostname);
1✔
29

30
        /* with comment */
31
        ASSERT_OK(write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE));
1✔
32
        ASSERT_OK(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname));
1✔
33
        ASSERT_NOT_NULL(hostname);
1✔
34
        ASSERT_STREQ(hostname, "foo");
1✔
35
        hostname = mfree(hostname);
1✔
36

37
        /* with comment and extra whitespace */
38
        ASSERT_OK(write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE));
1✔
39
        ASSERT_OK(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname));
1✔
40
        ASSERT_NOT_NULL(hostname);
1✔
41
        ASSERT_STREQ(hostname, "foo");
1✔
42
        hostname = mfree(hostname);
1✔
43

44
        /* cleans up name */
45
        ASSERT_OK(write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE));
1✔
46
        ASSERT_OK(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname));
1✔
47
        ASSERT_NOT_NULL(hostname);
1✔
48
        ASSERT_STREQ(hostname, "foobar.com");
1✔
49
        hostname = mfree(hostname);
1✔
50

51
        /* with wildcards */
52
        ASSERT_OK(write_string_file(path, "foo????????x??????????u", WRITE_STRING_FILE_CREATE));
1✔
53
        ASSERT_OK(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname));
1✔
54
        ASSERT_NOT_NULL(hostname);
1✔
55
        ASSERT_STREQ(hostname, "foo????????x??????????u");
1✔
56
        hostname = mfree(hostname);
1✔
57
        r = read_etc_hostname(path, /* substitute_wildcards= */ true, &hostname);
1✔
58
        if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r))
1✔
UNCOV
59
                log_tests_skipped("skipping wildcard hostname tests, no machine ID defined");
×
60
        else {
61
                ASSERT_OK(r);
1✔
62
                ASSERT_NOT_NULL(hostname);
1✔
63
                ASSERT_NULL(strchr(hostname, '?'));
1✔
64
                ASSERT_EQ(fnmatch("foo????????x??????????u", hostname, /* flags= */ 0), 0);
1✔
65
                ASSERT_TRUE(hostname_is_valid(hostname, /* flags= */ 0));
1✔
66
                hostname = mfree(hostname);
1✔
67
        }
68

69
        /* no value set */
70
        hostname = (char*) 0x1234;
1✔
71
        ASSERT_OK(write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_TRUNCATE));
1✔
72
        ASSERT_ERROR(read_etc_hostname(path, /* substitute_wildcards= */ false, &hostname), ENOENT);
1✔
73
        assert(hostname == (char*) 0x1234);  /* does not touch argument on error */
1✔
74

75
        /* nonexisting file */
76
        ASSERT_ERROR(read_etc_hostname("/non/existing", /* substitute_wildcards= */ false, &hostname), ENOENT);
1✔
77
        assert(hostname == (char*) 0x1234);  /* does not touch argument on error */
1✔
78
}
1✔
79

80
TEST(hostname_substitute_wildcards) {
1✔
81
        int r;
1✔
82

83
        r = sd_id128_get_machine(NULL);
1✔
84
        if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r))
1✔
UNCOV
85
                return (void) log_tests_skipped_errno(r, "skipping wildcard hostname tests, no machine ID defined");
×
86

87
        _cleanup_free_ char *buf = NULL;
2✔
88
        ASSERT_NOT_NULL((buf = strdup("")));
1✔
89
        ASSERT_OK(hostname_substitute_wildcards(buf));
1✔
90
        ASSERT_STREQ(buf, "");
1✔
91
        ASSERT_NULL(buf = mfree(buf));
1✔
92

93
        ASSERT_NOT_NULL((buf = strdup("hogehoge")));
1✔
94
        ASSERT_OK(hostname_substitute_wildcards(buf));
1✔
95
        ASSERT_STREQ(buf, "hogehoge");
1✔
96
        ASSERT_NULL(buf = mfree(buf));
1✔
97

98
        ASSERT_NOT_NULL((buf = strdup("hoge??hoge??foo?")));
1✔
99
        ASSERT_OK(hostname_substitute_wildcards(buf));
1✔
100
        log_debug("hostname_substitute_wildcards(\"hoge??hoge??foo?\"): → \"%s\"", buf);
1✔
101
        ASSERT_EQ(fnmatch("hoge??hoge??foo?", buf, /* flags= */ 0), 0);
1✔
102
        ASSERT_TRUE(hostname_is_valid(buf, /* flags= */ 0));
1✔
103
        ASSERT_NULL(buf = mfree(buf));
1✔
104
}
105

106
TEST(hostname_setup) {
1✔
107
        hostname_setup(false);
1✔
108
}
1✔
109

110
TEST(hostname_malloc) {
1✔
UNCOV
111
        _cleanup_free_ char *h = NULL, *l = NULL;
×
112

113
        assert_se(h = gethostname_malloc());
1✔
114
        log_info("hostname_malloc: \"%s\"", h);
1✔
115

116
        assert_se(l = gethostname_short_malloc());
1✔
117
        log_info("hostname_short_malloc: \"%s\"", l);
1✔
118
}
1✔
119

120
TEST(default_hostname) {
1✔
121
        if (!hostname_is_valid(FALLBACK_HOSTNAME, 0)) {
1✔
122
                log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME);
×
UNCOV
123
                exit(EXIT_FAILURE);
×
124
        }
125

126
        _cleanup_free_ char *n = get_default_hostname();
1✔
127
        ASSERT_NOT_NULL(n);
1✔
128
        log_info("get_default_hostname: \"%s\"", n);
1✔
129
        ASSERT_TRUE(hostname_is_valid(n, /* flags= */ 0));
1✔
130

131
        _cleanup_free_ char *m = get_default_hostname_raw();
1✔
132
        ASSERT_NOT_NULL(m);
1✔
133
        log_info("get_default_hostname_raw: \"%s\"", m);
1✔
134
        ASSERT_TRUE(hostname_is_valid(m, VALID_HOSTNAME_QUESTION_MARK));
1✔
135
}
1✔
136

137
DEFINE_TEST_MAIN(LOG_DEBUG);
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc