File: | build-scan/../src/journal/test-catalog.c |
Warning: | line 62, column 9 Value stored to 'h' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | |
3 | #include <errno(*__errno_location ()).h> |
4 | #include <fcntl.h> |
5 | #include <locale.h> |
6 | #include <unistd.h> |
7 | |
8 | #include "sd-messages.h" |
9 | |
10 | #include "alloc-util.h" |
11 | #include "catalog.h" |
12 | #include "fd-util.h" |
13 | #include "fs-util.h" |
14 | #include "fileio.h" |
15 | #include "log.h" |
16 | #include "macro.h" |
17 | #include "path-util.h" |
18 | #include "string-util.h" |
19 | #include "strv.h" |
20 | #include "tests.h" |
21 | #include "util.h" |
22 | |
23 | static char** catalog_dirs = NULL((void*)0); |
24 | static const char *no_catalog_dirs[] = { |
25 | "/bin/hopefully/with/no/catalog", |
26 | NULL((void*)0) |
27 | }; |
28 | |
29 | static Hashmap* test_import(const char* contents, ssize_t size, int code) { |
30 | _cleanup_(unlink_tempfilep)__attribute__((cleanup(unlink_tempfilep))) char name[] = "/tmp/test-catalog.XXXXXX"; |
31 | _cleanup_close___attribute__((cleanup(closep))) int fd; |
32 | Hashmap *h; |
33 | |
34 | if (size < 0) |
35 | size = strlen(contents); |
36 | |
37 | assert_se(h = hashmap_new(&catalog_hash_ops))do { if ((__builtin_expect(!!(!(h = internal_hashmap_new(& catalog_hash_ops ))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("h = hashmap_new(&catalog_hash_ops)"), "../src/journal/test-catalog.c" , 37, __PRETTY_FUNCTION__); } while (0); |
38 | |
39 | fd = mkostemp_safe(name); |
40 | assert_se(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/journal/test-catalog.c" , 40, __PRETTY_FUNCTION__); } while (0); |
41 | assert_se(write(fd, contents, size) == size)do { if ((__builtin_expect(!!(!(write(fd, contents, size) == size )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("write(fd, contents, size) == size" ), "../src/journal/test-catalog.c", 41, __PRETTY_FUNCTION__); } while (0); |
42 | |
43 | assert_se(catalog_import_file(h, name) == code)do { if ((__builtin_expect(!!(!(catalog_import_file(h, name) == code)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("catalog_import_file(h, name) == code" ), "../src/journal/test-catalog.c", 43, __PRETTY_FUNCTION__); } while (0); |
44 | |
45 | return h; |
46 | } |
47 | |
48 | static void test_catalog_import_invalid(void) { |
49 | _cleanup_hashmap_free_free_free___attribute__((cleanup(hashmap_free_free_freep))) Hashmap *h = NULL((void*)0); |
50 | |
51 | h = test_import("xxx", -1, -EINVAL22); |
52 | assert_se(hashmap_isempty(h))do { if ((__builtin_expect(!!(!(hashmap_isempty(h))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("hashmap_isempty(h)"), "../src/journal/test-catalog.c" , 52, __PRETTY_FUNCTION__); } while (0); |
53 | } |
54 | |
55 | static void test_catalog_import_badid(void) { |
56 | _cleanup_hashmap_free_free_free___attribute__((cleanup(hashmap_free_free_freep))) Hashmap *h = NULL((void*)0); |
57 | const char *input = |
58 | "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \ |
59 | "Subject: message\n" \ |
60 | "\n" \ |
61 | "payload\n"; |
62 | h = test_import(input, -1, -EINVAL22); |
Value stored to 'h' is never read | |
63 | } |
64 | |
65 | static void test_catalog_import_one(void) { |
66 | _cleanup_hashmap_free_free_free___attribute__((cleanup(hashmap_free_free_freep))) Hashmap *h = NULL((void*)0); |
67 | char *payload; |
68 | Iterator j; |
69 | |
70 | const char *input = |
71 | "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \ |
72 | "Subject: message\n" \ |
73 | "\n" \ |
74 | "payload\n"; |
75 | const char *expect = |
76 | "Subject: message\n" \ |
77 | "\n" \ |
78 | "payload\n"; |
79 | |
80 | h = test_import(input, -1, 0); |
81 | assert_se(hashmap_size(h) == 1)do { if ((__builtin_expect(!!(!(hashmap_size(h) == 1)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("hashmap_size(h) == 1"), "../src/journal/test-catalog.c" , 81, __PRETTY_FUNCTION__); } while (0); |
82 | |
83 | HASHMAP_FOREACH(payload, h, j)for ((j) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((h), &(j), (void **)&(payload), ((void*)0)); ) { |
84 | printf("expect: %s\n", expect); |
85 | printf("actual: %s\n", payload); |
86 | assert_se(streq(expect, payload))do { if ((__builtin_expect(!!(!((strcmp((expect),(payload)) == 0))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("streq(expect, payload)" ), "../src/journal/test-catalog.c", 86, __PRETTY_FUNCTION__); } while (0); |
87 | } |
88 | } |
89 | |
90 | static void test_catalog_import_merge(void) { |
91 | _cleanup_hashmap_free_free_free___attribute__((cleanup(hashmap_free_free_freep))) Hashmap *h = NULL((void*)0); |
92 | char *payload; |
93 | Iterator j; |
94 | |
95 | const char *input = |
96 | "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \ |
97 | "Subject: message\n" \ |
98 | "Defined-By: me\n" \ |
99 | "\n" \ |
100 | "payload\n" \ |
101 | "\n" \ |
102 | "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \ |
103 | "Subject: override subject\n" \ |
104 | "X-Header: hello\n" \ |
105 | "\n" \ |
106 | "override payload\n"; |
107 | |
108 | const char *combined = |
109 | "Subject: override subject\n" \ |
110 | "X-Header: hello\n" \ |
111 | "Subject: message\n" \ |
112 | "Defined-By: me\n" \ |
113 | "\n" \ |
114 | "override payload\n"; |
115 | |
116 | h = test_import(input, -1, 0); |
117 | assert_se(hashmap_size(h) == 1)do { if ((__builtin_expect(!!(!(hashmap_size(h) == 1)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("hashmap_size(h) == 1"), "../src/journal/test-catalog.c" , 117, __PRETTY_FUNCTION__); } while (0); |
118 | |
119 | HASHMAP_FOREACH(payload, h, j)for ((j) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((h), &(j), (void **)&(payload), ((void*)0)); ) { |
120 | assert_se(streq(combined, payload))do { if ((__builtin_expect(!!(!((strcmp((combined),(payload)) == 0))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("streq(combined, payload)" ), "../src/journal/test-catalog.c", 120, __PRETTY_FUNCTION__) ; } while (0); |
121 | } |
122 | } |
123 | |
124 | static void test_catalog_import_merge_no_body(void) { |
125 | _cleanup_hashmap_free_free_free___attribute__((cleanup(hashmap_free_free_freep))) Hashmap *h = NULL((void*)0); |
126 | char *payload; |
127 | Iterator j; |
128 | |
129 | const char *input = |
130 | "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \ |
131 | "Subject: message\n" \ |
132 | "Defined-By: me\n" \ |
133 | "\n" \ |
134 | "payload\n" \ |
135 | "\n" \ |
136 | "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \ |
137 | "Subject: override subject\n" \ |
138 | "X-Header: hello\n" \ |
139 | "\n"; |
140 | |
141 | const char *combined = |
142 | "Subject: override subject\n" \ |
143 | "X-Header: hello\n" \ |
144 | "Subject: message\n" \ |
145 | "Defined-By: me\n" \ |
146 | "\n" \ |
147 | "payload\n"; |
148 | |
149 | h = test_import(input, -1, 0); |
150 | assert_se(hashmap_size(h) == 1)do { if ((__builtin_expect(!!(!(hashmap_size(h) == 1)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("hashmap_size(h) == 1"), "../src/journal/test-catalog.c" , 150, __PRETTY_FUNCTION__); } while (0); |
151 | |
152 | HASHMAP_FOREACH(payload, h, j)for ((j) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((h), &(j), (void **)&(payload), ((void*)0)); ) { |
153 | assert_se(streq(combined, payload))do { if ((__builtin_expect(!!(!((strcmp((combined),(payload)) == 0))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("streq(combined, payload)" ), "../src/journal/test-catalog.c", 153, __PRETTY_FUNCTION__) ; } while (0); |
154 | } |
155 | } |
156 | |
157 | static void test_catalog_update(const char *database) { |
158 | int r; |
159 | |
160 | /* Test what happens if there are no files. */ |
161 | r = catalog_update(database, NULL((void*)0), NULL((void*)0)); |
162 | assert_se(r == 0)do { if ((__builtin_expect(!!(!(r == 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("r == 0"), "../src/journal/test-catalog.c" , 162, __PRETTY_FUNCTION__); } while (0); |
163 | |
164 | /* Test what happens if there are no files in the directory. */ |
165 | r = catalog_update(database, NULL((void*)0), no_catalog_dirs); |
166 | assert_se(r == 0)do { if ((__builtin_expect(!!(!(r == 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("r == 0"), "../src/journal/test-catalog.c" , 166, __PRETTY_FUNCTION__); } while (0); |
167 | |
168 | /* Make sure that we at least have some files loaded or the |
169 | * catalog_list below will fail. */ |
170 | r = catalog_update(database, NULL((void*)0), (const char * const *) catalog_dirs); |
171 | assert_se(r == 0)do { if ((__builtin_expect(!!(!(r == 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("r == 0"), "../src/journal/test-catalog.c" , 171, __PRETTY_FUNCTION__); } while (0); |
172 | } |
173 | |
174 | static void test_catalog_file_lang(void) { |
175 | _cleanup_free___attribute__((cleanup(freep))) char *lang = NULL((void*)0), *lang2 = NULL((void*)0), *lang3 = NULL((void*)0), *lang4 = NULL((void*)0); |
176 | |
177 | assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang) == 1)do { if ((__builtin_expect(!!(!(catalog_file_lang("systemd.de_DE.catalog" , &lang) == 1)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("catalog_file_lang(\"systemd.de_DE.catalog\", &lang) == 1" ), "../src/journal/test-catalog.c", 177, __PRETTY_FUNCTION__) ; } while (0); |
178 | assert_se(streq(lang, "de_DE"))do { if ((__builtin_expect(!!(!((strcmp((lang),("de_DE")) == 0 ))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("streq(lang, \"de_DE\")" ), "../src/journal/test-catalog.c", 178, __PRETTY_FUNCTION__) ; } while (0); |
179 | |
180 | assert_se(catalog_file_lang("systemd..catalog", &lang2) == 0)do { if ((__builtin_expect(!!(!(catalog_file_lang("systemd..catalog" , &lang2) == 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("catalog_file_lang(\"systemd..catalog\", &lang2) == 0" ), "../src/journal/test-catalog.c", 180, __PRETTY_FUNCTION__) ; } while (0); |
181 | assert_se(lang2 == NULL)do { if ((__builtin_expect(!!(!(lang2 == ((void*)0))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("lang2 == NULL"), "../src/journal/test-catalog.c" , 181, __PRETTY_FUNCTION__); } while (0); |
182 | |
183 | assert_se(catalog_file_lang("systemd.fr.catalog", &lang2) == 1)do { if ((__builtin_expect(!!(!(catalog_file_lang("systemd.fr.catalog" , &lang2) == 1)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("catalog_file_lang(\"systemd.fr.catalog\", &lang2) == 1" ), "../src/journal/test-catalog.c", 183, __PRETTY_FUNCTION__) ; } while (0); |
184 | assert_se(streq(lang2, "fr"))do { if ((__builtin_expect(!!(!((strcmp((lang2),("fr")) == 0) )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("streq(lang2, \"fr\")" ), "../src/journal/test-catalog.c", 184, __PRETTY_FUNCTION__) ; } while (0); |
185 | |
186 | assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3) == 0)do { if ((__builtin_expect(!!(!(catalog_file_lang("systemd.fr.catalog.gz" , &lang3) == 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("catalog_file_lang(\"systemd.fr.catalog.gz\", &lang3) == 0" ), "../src/journal/test-catalog.c", 186, __PRETTY_FUNCTION__) ; } while (0); |
187 | assert_se(lang3 == NULL)do { if ((__builtin_expect(!!(!(lang3 == ((void*)0))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("lang3 == NULL"), "../src/journal/test-catalog.c" , 187, __PRETTY_FUNCTION__); } while (0); |
188 | |
189 | assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3) == 0)do { if ((__builtin_expect(!!(!(catalog_file_lang("systemd.01234567890123456789012345678901.catalog" , &lang3) == 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("catalog_file_lang(\"systemd.01234567890123456789012345678901.catalog\", &lang3) == 0" ), "../src/journal/test-catalog.c", 189, __PRETTY_FUNCTION__) ; } while (0); |
190 | assert_se(lang3 == NULL)do { if ((__builtin_expect(!!(!(lang3 == ((void*)0))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("lang3 == NULL"), "../src/journal/test-catalog.c" , 190, __PRETTY_FUNCTION__); } while (0); |
191 | |
192 | assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3) == 1)do { if ((__builtin_expect(!!(!(catalog_file_lang("systemd.0123456789012345678901234567890.catalog" , &lang3) == 1)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("catalog_file_lang(\"systemd.0123456789012345678901234567890.catalog\", &lang3) == 1" ), "../src/journal/test-catalog.c", 192, __PRETTY_FUNCTION__) ; } while (0); |
193 | assert_se(streq(lang3, "0123456789012345678901234567890"))do { if ((__builtin_expect(!!(!((strcmp((lang3),("0123456789012345678901234567890" )) == 0))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("streq(lang3, \"0123456789012345678901234567890\")" ), "../src/journal/test-catalog.c", 193, __PRETTY_FUNCTION__) ; } while (0); |
194 | |
195 | assert_se(catalog_file_lang("/x/y/systemd.catalog", &lang4) == 0)do { if ((__builtin_expect(!!(!(catalog_file_lang("/x/y/systemd.catalog" , &lang4) == 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("catalog_file_lang(\"/x/y/systemd.catalog\", &lang4) == 0" ), "../src/journal/test-catalog.c", 195, __PRETTY_FUNCTION__) ; } while (0); |
196 | assert_se(lang4 == NULL)do { if ((__builtin_expect(!!(!(lang4 == ((void*)0))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("lang4 == NULL"), "../src/journal/test-catalog.c" , 196, __PRETTY_FUNCTION__); } while (0); |
197 | |
198 | assert_se(catalog_file_lang("/x/y/systemd.ru_RU.catalog", &lang4) == 1)do { if ((__builtin_expect(!!(!(catalog_file_lang("/x/y/systemd.ru_RU.catalog" , &lang4) == 1)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("catalog_file_lang(\"/x/y/systemd.ru_RU.catalog\", &lang4) == 1" ), "../src/journal/test-catalog.c", 198, __PRETTY_FUNCTION__) ; } while (0); |
199 | assert_se(streq(lang4, "ru_RU"))do { if ((__builtin_expect(!!(!((strcmp((lang4),("ru_RU")) == 0))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("streq(lang4, \"ru_RU\")" ), "../src/journal/test-catalog.c", 199, __PRETTY_FUNCTION__) ; } while (0); |
200 | } |
201 | |
202 | int main(int argc, char *argv[]) { |
203 | _cleanup_(unlink_tempfilep)__attribute__((cleanup(unlink_tempfilep))) char database[] = "/tmp/test-catalog.XXXXXX"; |
204 | _cleanup_free___attribute__((cleanup(freep))) char *text = NULL((void*)0), *catalog_dir = NULL((void*)0); |
205 | int r; |
206 | |
207 | setlocale(LC_ALL6, "de_DE.UTF-8"); |
208 | |
209 | log_set_max_level(LOG_DEBUG)log_set_max_level_realm(LOG_REALM_SYSTEMD, (7)); |
210 | log_parse_environment()log_parse_environment_realm(LOG_REALM_SYSTEMD); |
211 | log_open(); |
212 | |
213 | /* If test-catalog is located at the build directory, then use catalogs in that. |
214 | * If it is not, e.g. installed by systemd-tests package, then use installed catalogs. */ |
215 | if (test_is_running_from_builddir(NULL((void*)0))) { |
216 | assert_se(catalog_dir = path_join(NULL, ABS_BUILD_DIR, "catalog"))do { if ((__builtin_expect(!!(!(catalog_dir = path_join(((void *)0), "/home/mrc0mmand/repos/@redhat-plumbers/systemd-rhel8/build-scan" , "catalog"))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("catalog_dir = path_join(NULL, ABS_BUILD_DIR, \"catalog\")" ), "../src/journal/test-catalog.c", 216, __PRETTY_FUNCTION__) ; } while (0); |
217 | catalog_dirs = STRV_MAKE(catalog_dir)((char**) ((const char*[]) { catalog_dir, ((void*)0) })); |
218 | } else |
219 | catalog_dirs = STRV_MAKE(CATALOG_DIR)((char**) ((const char*[]) { "/usr/lib/systemd/catalog", ((void *)0) })); |
220 | |
221 | assert_se(access(catalog_dirs[0], F_OK) >= 0)do { if ((__builtin_expect(!!(!(access(catalog_dirs[0], 0) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("access(catalog_dirs[0], F_OK) >= 0" ), "../src/journal/test-catalog.c", 221, __PRETTY_FUNCTION__) ; } while (0); |
222 | log_notice("Using catalog directory '%s'", catalog_dirs[0])({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/journal/test-catalog.c", 222, __func__, "Using catalog directory '%s'" , catalog_dirs[0]) : -abs(_e); }); |
223 | |
224 | test_catalog_file_lang(); |
225 | |
226 | test_catalog_import_invalid(); |
227 | test_catalog_import_badid(); |
228 | test_catalog_import_one(); |
229 | test_catalog_import_merge(); |
230 | test_catalog_import_merge_no_body(); |
231 | |
232 | assert_se(mkostemp_safe(database) >= 0)do { if ((__builtin_expect(!!(!(mkostemp_safe(database) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("mkostemp_safe(database) >= 0" ), "../src/journal/test-catalog.c", 232, __PRETTY_FUNCTION__) ; } while (0); |
233 | |
234 | test_catalog_update(database); |
235 | |
236 | r = catalog_list(stdoutstdout, database, true1); |
237 | assert_se(r >= 0)do { if ((__builtin_expect(!!(!(r >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("r >= 0"), "../src/journal/test-catalog.c" , 237, __PRETTY_FUNCTION__); } while (0); |
238 | |
239 | r = catalog_list(stdoutstdout, database, false0); |
240 | assert_se(r >= 0)do { if ((__builtin_expect(!!(!(r >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("r >= 0"), "../src/journal/test-catalog.c" , 240, __PRETTY_FUNCTION__); } while (0); |
241 | |
242 | assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0)do { if ((__builtin_expect(!!(!(catalog_get(database, ((const sd_id128_t) { .bytes = { 0xfc, 0x2e, 0x22, 0xbc, 0x6e, 0xe6, 0x47, 0xb6, 0xb9, 0x07, 0x29, 0xab, 0x34, 0xa2, 0x50, 0xb1 } }), &text) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0" ), "../src/journal/test-catalog.c", 242, __PRETTY_FUNCTION__) ; } while (0); |
243 | printf(">>>%s<<<\n", text); |
244 | |
245 | return 0; |
246 | } |