Branch data Line data Source code
1 : : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : :
3 : : #include "path-lookup.h"
4 : : #include "set.h"
5 : : #include "strv.h"
6 : : #include "tests.h"
7 : : #include "unit-file.h"
8 : :
9 : 4 : static void test_unit_validate_alias_symlink_and_warn(void) {
10 [ + - ]: 4 : log_info("/* %s */", __func__);
11 : :
12 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a.service", "/other/b.service") == 0);
13 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a.service", "/other/b.socket") == -EXDEV);
14 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a.service", "/other/b.foobar") == -EXDEV);
15 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@.service", "/other/b@.service") == 0);
16 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@.service", "/other/b@.socket") == -EXDEV);
17 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@XXX.service", "/other/b@YYY.service") == -EXDEV);
18 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@XXX.service", "/other/b@YYY.socket") == -EXDEV);
19 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@.service", "/other/b@YYY.service") == -EXDEV);
20 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@XXX.service", "/other/b@XXX.service") == 0);
21 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@XXX.service", "/other/b@.service") == 0);
22 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@.service", "/other/b.service") == -EXDEV);
23 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a.service", "/other/b@.service") == -EXDEV);
24 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@.slice", "/other/b.slice") == -EINVAL);
25 [ - + ]: 4 : assert_se(unit_validate_alias_symlink_and_warn("/path/a.slice", "/other/b.slice") == -EINVAL);
26 : 4 : }
27 : :
28 : 4 : static void test_unit_file_build_name_map(char **ids) {
29 : 4 : _cleanup_(lookup_paths_free) LookupPaths lp = {};
30 : 4 : _cleanup_hashmap_free_ Hashmap *unit_ids = NULL;
31 : 4 : _cleanup_hashmap_free_ Hashmap *unit_names = NULL;
32 : : Iterator i;
33 : : const char *k, *dst;
34 : : char **v;
35 : 4 : usec_t mtime = 0;
36 : : int r;
37 : :
38 [ - + ]: 4 : assert_se(lookup_paths_init(&lp, UNIT_FILE_SYSTEM, 0, NULL) >= 0);
39 : :
40 [ - + ]: 4 : assert_se(unit_file_build_name_map(&lp, &mtime, &unit_ids, &unit_names, NULL) == 1);
41 : :
42 [ + + ]: 2032 : HASHMAP_FOREACH_KEY(dst, k, unit_ids, i)
43 [ + - ]: 2028 : log_info("ids: %s → %s", k, dst);
44 : :
45 [ + + ]: 1900 : HASHMAP_FOREACH_KEY(v, k, unit_names, i) {
46 : 1896 : _cleanup_free_ char *j = strv_join(v, ", ");
47 [ + - ]: 1896 : log_info("aliases: %s ← %s", k, j);
48 : : }
49 : :
50 : : char buf[FORMAT_TIMESTAMP_MAX];
51 [ + - ]: 4 : log_debug("Last modification time: %s", format_timestamp(buf, sizeof buf, mtime));
52 : :
53 : 4 : r = unit_file_build_name_map(&lp, &mtime, &unit_ids, &unit_names, NULL);
54 [ + - - + ]: 4 : assert_se(IN_SET(r, 0, 1));
55 [ + - ]: 4 : if (r == 0)
56 [ + - ]: 4 : log_debug("Cache rebuild skipped based on mtime.");
57 : :
58 : :
59 : : char **id;
60 [ + - - + ]: 4 : STRV_FOREACH(id, ids) {
61 : : const char *fragment, *name;
62 : : Iterator it;
63 : 0 : _cleanup_set_free_free_ Set *names = NULL;
64 [ # # ]: 0 : log_info("*** %s ***", *id);
65 : 0 : r = unit_file_find_fragment(unit_ids,
66 : : unit_names,
67 : : *id,
68 : : &fragment,
69 : : &names);
70 [ # # ]: 0 : assert(r == 0);
71 [ # # ]: 0 : log_info("fragment: %s", fragment);
72 [ # # ]: 0 : log_info("names:");
73 [ # # ]: 0 : SET_FOREACH(name, names, it)
74 [ # # ]: 0 : log_info(" %s", name);
75 : : }
76 : 4 : }
77 : :
78 : 4 : int main(int argc, char **argv) {
79 : 4 : test_setup_logging(LOG_DEBUG);
80 : :
81 : 4 : test_unit_validate_alias_symlink_and_warn();
82 : 4 : test_unit_file_build_name_map(strv_skip(argv, 1));
83 : :
84 : 4 : return 0;
85 : : }
|