1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <stdio.h>
4 #include <string.h>
5 
6 #include "install.h"
7 #include "tests.h"
8 
dump_changes(UnitFileChange * c,unsigned n)9 static void dump_changes(UnitFileChange *c, unsigned n) {
10         unsigned i;
11 
12         assert_se(n == 0 || c);
13 
14         for (i = 0; i < n; i++) {
15                 if (c[i].type_or_errno == UNIT_FILE_UNLINK)
16                         printf("rm '%s'\n", c[i].path);
17                 else if (c[i].type_or_errno == UNIT_FILE_SYMLINK)
18                         printf("ln -s '%s' '%s'\n", c[i].source, c[i].path);
19         }
20 }
21 
main(int argc,char * argv[])22 int main(int argc, char* argv[]) {
23         Hashmap *h;
24         UnitFileList *p;
25         int r;
26         const char *const files[] = { "avahi-daemon.service", NULL };
27         const char *const files2[] = { "/home/lennart/test.service", NULL };
28         UnitFileChange *changes = NULL;
29         size_t n_changes = 0;
30         UnitFileState state = 0;
31 
32         test_setup_logging(LOG_DEBUG);
33 
34         h = hashmap_new(&string_hash_ops);
35         r = unit_file_get_list(LOOKUP_SCOPE_SYSTEM, NULL, h, NULL, NULL);
36         assert_se(r == 0);
37 
38         HASHMAP_FOREACH(p, h) {
39                 UnitFileState s = _UNIT_FILE_STATE_INVALID;
40 
41                 r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, basename(p->path), &s);
42 
43                 assert_se((r < 0 && p->state == UNIT_FILE_BAD) ||
44                           (p->state == s));
45 
46                 fprintf(stderr, "%s (%s)\n",
47                         p->path,
48                         unit_file_state_to_string(p->state));
49         }
50 
51         unit_file_list_free(h);
52 
53         log_info("/*** enable **/");
54 
55         r = unit_file_enable(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes);
56         assert_se(r >= 0);
57 
58         log_info("/*** enable2 **/");
59 
60         r = unit_file_enable(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes);
61         assert_se(r >= 0);
62 
63         dump_changes(changes, n_changes);
64         unit_file_changes_free(changes, n_changes);
65 
66         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, files[0], &state);
67         assert_se(r >= 0);
68         assert_se(state == UNIT_FILE_ENABLED);
69 
70         log_info("/*** disable ***/");
71         changes = NULL;
72         n_changes = 0;
73 
74         r = unit_file_disable(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes);
75         assert_se(r >= 0);
76 
77         dump_changes(changes, n_changes);
78         unit_file_changes_free(changes, n_changes);
79 
80         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, files[0], &state);
81         assert_se(r >= 0);
82         assert_se(state == UNIT_FILE_DISABLED);
83 
84         log_info("/*** mask ***/");
85         changes = NULL;
86         n_changes = 0;
87 
88         r = unit_file_mask(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes);
89         assert_se(r >= 0);
90         log_info("/*** mask2 ***/");
91         r = unit_file_mask(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes);
92         assert_se(r >= 0);
93 
94         dump_changes(changes, n_changes);
95         unit_file_changes_free(changes, n_changes);
96 
97         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, files[0], &state);
98         assert_se(r >= 0);
99         assert_se(state == UNIT_FILE_MASKED);
100 
101         log_info("/*** unmask ***/");
102         changes = NULL;
103         n_changes = 0;
104 
105         r = unit_file_unmask(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes);
106         assert_se(r >= 0);
107         log_info("/*** unmask2 ***/");
108         r = unit_file_unmask(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes);
109         assert_se(r >= 0);
110 
111         dump_changes(changes, n_changes);
112         unit_file_changes_free(changes, n_changes);
113 
114         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, files[0], &state);
115         assert_se(r >= 0);
116         assert_se(state == UNIT_FILE_DISABLED);
117 
118         log_info("/*** mask ***/");
119         changes = NULL;
120         n_changes = 0;
121 
122         r = unit_file_mask(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes);
123         assert_se(r >= 0);
124 
125         dump_changes(changes, n_changes);
126         unit_file_changes_free(changes, n_changes);
127 
128         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, files[0], &state);
129         assert_se(r >= 0);
130         assert_se(state == UNIT_FILE_MASKED);
131 
132         log_info("/*** disable ***/");
133         changes = NULL;
134         n_changes = 0;
135 
136         r = unit_file_disable(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes);
137         assert_se(r >= 0);
138         log_info("/*** disable2 ***/");
139         r = unit_file_disable(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes);
140         assert_se(r >= 0);
141 
142         dump_changes(changes, n_changes);
143         unit_file_changes_free(changes, n_changes);
144 
145         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, files[0], &state);
146         assert_se(r >= 0);
147         assert_se(state == UNIT_FILE_MASKED);
148 
149         log_info("/*** umask ***/");
150         changes = NULL;
151         n_changes = 0;
152 
153         r = unit_file_unmask(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, &changes, &n_changes);
154         assert_se(r >= 0);
155 
156         dump_changes(changes, n_changes);
157         unit_file_changes_free(changes, n_changes);
158 
159         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, files[0], &state);
160         assert_se(r >= 0);
161         assert_se(state == UNIT_FILE_DISABLED);
162 
163         log_info("/*** enable files2 ***/");
164         changes = NULL;
165         n_changes = 0;
166 
167         r = unit_file_enable(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files2, &changes, &n_changes);
168         assert_se(r >= 0);
169 
170         dump_changes(changes, n_changes);
171         unit_file_changes_free(changes, n_changes);
172 
173         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, basename(files2[0]), &state);
174         assert_se(r >= 0);
175         assert_se(state == UNIT_FILE_ENABLED);
176 
177         log_info("/*** disable files2 ***/");
178         changes = NULL;
179         n_changes = 0;
180 
181         r = unit_file_disable(LOOKUP_SCOPE_SYSTEM, 0, NULL, STRV_MAKE(basename(files2[0])), &changes, &n_changes);
182         assert_se(r >= 0);
183 
184         dump_changes(changes, n_changes);
185         unit_file_changes_free(changes, n_changes);
186 
187         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, basename(files2[0]), &state);
188         assert_se(r < 0);
189 
190         log_info("/*** link files2 ***/");
191         changes = NULL;
192         n_changes = 0;
193 
194         r = unit_file_link(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files2, &changes, &n_changes);
195         assert_se(r >= 0);
196 
197         dump_changes(changes, n_changes);
198         unit_file_changes_free(changes, n_changes);
199 
200         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, basename(files2[0]), &state);
201         assert_se(r >= 0);
202         assert_se(state == UNIT_FILE_LINKED);
203 
204         log_info("/*** disable files2 ***/");
205         changes = NULL;
206         n_changes = 0;
207 
208         r = unit_file_disable(LOOKUP_SCOPE_SYSTEM, 0, NULL, STRV_MAKE(basename(files2[0])), &changes, &n_changes);
209         assert_se(r >= 0);
210 
211         dump_changes(changes, n_changes);
212         unit_file_changes_free(changes, n_changes);
213 
214         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, basename(files2[0]), &state);
215         assert_se(r < 0);
216 
217         log_info("/*** link files2 ***/");
218         changes = NULL;
219         n_changes = 0;
220 
221         r = unit_file_link(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files2, &changes, &n_changes);
222         assert_se(r >= 0);
223 
224         dump_changes(changes, n_changes);
225         unit_file_changes_free(changes, n_changes);
226 
227         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, basename(files2[0]), &state);
228         assert_se(r >= 0);
229         assert_se(state == UNIT_FILE_LINKED);
230 
231         log_info("/*** reenable files2 ***/");
232         changes = NULL;
233         n_changes = 0;
234 
235         r = unit_file_reenable(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files2, &changes, &n_changes);
236         assert_se(r >= 0);
237 
238         dump_changes(changes, n_changes);
239         unit_file_changes_free(changes, n_changes);
240 
241         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, basename(files2[0]), &state);
242         assert_se(r >= 0);
243         assert_se(state == UNIT_FILE_ENABLED);
244 
245         log_info("/*** disable files2 ***/");
246         changes = NULL;
247         n_changes = 0;
248 
249         r = unit_file_disable(LOOKUP_SCOPE_SYSTEM, 0, NULL, STRV_MAKE(basename(files2[0])), &changes, &n_changes);
250         assert_se(r >= 0);
251 
252         dump_changes(changes, n_changes);
253         unit_file_changes_free(changes, n_changes);
254 
255         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, basename(files2[0]), &state);
256         assert_se(r < 0);
257         log_info("/*** preset files ***/");
258         changes = NULL;
259         n_changes = 0;
260 
261         r = unit_file_preset(LOOKUP_SCOPE_SYSTEM, 0, NULL, (char**) files, UNIT_FILE_PRESET_FULL, &changes, &n_changes);
262         assert_se(r >= 0);
263 
264         dump_changes(changes, n_changes);
265         unit_file_changes_free(changes, n_changes);
266 
267         r = unit_file_get_state(LOOKUP_SCOPE_SYSTEM, NULL, basename(files[0]), &state);
268         assert_se(r >= 0);
269         assert_se(state == UNIT_FILE_ENABLED);
270 
271         return 0;
272 }
273