XBPS Library API 20250615
The X Binary Package System
config/main.c
1/*-
2 * Copyright (c) 2014 Enno Boland.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *-
25 */
26#include <atf-c.h>
27#include <xbps.h>
28#include <stdlib.h>
29#include <limits.h>
30
31ATF_TC(config_include_test);
32ATF_TC_HEAD(config_include_test, tc)
33{
34 atf_tc_set_md_var(tc, "descr", "Test including files by file globbing");
35}
36
37ATF_TC_BODY(config_include_test, tc)
38{
39 struct xbps_handle xh;
40 const char *tcsdir;
41 char *buf, *buf2, pwd[PATH_MAX];
42 int ret;
43
44 /* get test source dir */
45 tcsdir = atf_tc_get_config_var(tc, "srcdir");
46
47 memset(&xh, 0, sizeof(xh));
48 buf = getcwd(pwd, sizeof(pwd));
49
50 xbps_strlcpy(xh.rootdir, pwd, sizeof(xh.rootdir));
51 xbps_strlcpy(xh.metadir, pwd, sizeof(xh.metadir));
52 ret = snprintf(xh.confdir, sizeof(xh.confdir), "%s/xbps.d", pwd);
53 ATF_REQUIRE_EQ((ret >= 0), 1);
54 ATF_REQUIRE_EQ(((size_t)ret < sizeof(xh.confdir)), 1);
55
56 ATF_REQUIRE_EQ(xbps_mkpath(xh.confdir, 0755), 0);
57
58 buf = xbps_xasprintf("%s/xbps.cf", tcsdir);
59 buf2 = xbps_xasprintf("%s/xbps.d/xbps.conf", pwd);
60 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
61 free(buf);
62 free(buf2);
63
64 buf = xbps_xasprintf("%s/1.include.cf", tcsdir);
65 buf2 = xbps_xasprintf("%s/xbps.d/1.include.conf", pwd);
66 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
67 free(buf);
68 free(buf2);
69
70 buf = xbps_xasprintf("%s/2.include.cf", tcsdir);
71 buf2 = xbps_xasprintf("%s/xbps.d/2.include.conf", pwd);
72 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
73 free(buf);
74 free(buf2);
75
76 xh.flags = XBPS_FLAG_DEBUG;
77 ATF_REQUIRE_EQ(xbps_init(&xh), 0);
78 /* should contain both repositories defined in [12].include.conf */
79 ATF_REQUIRE_EQ(xbps_array_count(xh.repositories), 2);
80}
81
82
83ATF_TC(config_include_nomatch_test);
84ATF_TC_HEAD(config_include_nomatch_test, tc)
85{
86 atf_tc_set_md_var(tc, "descr", "Test finds no files to include");
87}
88
89ATF_TC_BODY(config_include_nomatch_test, tc)
90{
91 struct xbps_handle xh;
92 const char *tcsdir;
93 char *buf, *buf2, pwd[PATH_MAX];
94 int ret;
95
96 /* get test source dir */
97 tcsdir = atf_tc_get_config_var(tc, "srcdir");
98
99 memset(&xh, 0, sizeof(xh));
100 buf = getcwd(pwd, sizeof(pwd));
101
102 xbps_strlcpy(xh.rootdir, tcsdir, sizeof(xh.rootdir));
103 xbps_strlcpy(xh.metadir, tcsdir, sizeof(xh.metadir));
104 ret = snprintf(xh.confdir, sizeof(xh.confdir), "%s/xbps.d", pwd);
105 ATF_REQUIRE_EQ((ret >= 0), 1);
106 ATF_REQUIRE_EQ(((size_t)ret < sizeof(xh.confdir)), 1);
107
108 ATF_REQUIRE_EQ(xbps_mkpath(xh.confdir, 0755), 0);
109
110 buf = xbps_xasprintf("%s/xbps_nomatch.cf", tcsdir);
111 buf2 = xbps_xasprintf("%s/xbps.d/nomatch.conf", pwd);
112 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
113 free(buf);
114 free(buf2);
115
116 xh.flags = XBPS_FLAG_DEBUG;
117 ATF_REQUIRE_EQ(xbps_init(&xh), 0);
118
119 /* should contain no repositories */
120 ATF_REQUIRE_EQ(xbps_array_count(xh.repositories), 0);
121}
122
123ATF_TC(config_include_absolute);
124ATF_TC_HEAD(config_include_absolute, tc)
125{
126 atf_tc_set_md_var(tc, "descr", "Test including files by absolute path");
127}
128
129ATF_TC_BODY(config_include_absolute, tc)
130{
131 struct xbps_handle xh;
132 const char *tcsdir;
133 char *cfg, *buf, *buf2, pwd[PATH_MAX];
134 int ret;
135
136 /* get test source dir */
137 tcsdir = atf_tc_get_config_var(tc, "srcdir");
138
139 memset(&xh, 0, sizeof(xh));
140 buf = getcwd(pwd, sizeof(pwd));
141
142 xbps_strlcpy(xh.rootdir, pwd, sizeof(xh.rootdir));
143 xbps_strlcpy(xh.metadir, pwd, sizeof(xh.metadir));
144 ret = snprintf(xh.confdir, sizeof(xh.confdir), "%s/xbps.d", pwd);
145 ATF_REQUIRE_EQ((ret >= 0), 1);
146 ATF_REQUIRE_EQ(((size_t)ret < sizeof(xh.confdir)), 1);
147
148 ATF_REQUIRE_EQ(xbps_mkpath(xh.confdir, 0755), 0);
149
150 cfg = xbps_xasprintf("%s/xbps2.d", pwd);
151 ATF_REQUIRE_EQ(xbps_mkpath(cfg, 0755), 0);
152
153 buf = xbps_xasprintf("%s/xbps_absolute.cf", tcsdir);
154 buf2 = xbps_xasprintf("%s/xbps.d/xbps.conf", pwd);
155 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
156 free(buf);
157 free(buf2);
158
159 buf = xbps_xasprintf("%s/1.include.cf", tcsdir);
160 buf2 = xbps_xasprintf("%s/xbps2.d/1.include.conf", pwd);
161 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
162 free(buf);
163 free(buf2);
164
165 xh.flags = XBPS_FLAG_DEBUG;
166 ATF_REQUIRE_EQ(xbps_init(&xh), 0);
167 /* should contain one repository defined in 1.include.conf */
168 ATF_REQUIRE_EQ(xbps_array_count(xh.repositories), 1);
169}
170
171ATF_TC(config_include_absolute_glob);
172ATF_TC_HEAD(config_include_absolute_glob, tc)
173{
174 atf_tc_set_md_var(tc, "descr", "Test including files by absolute path with globbing");
175}
176
177ATF_TC_BODY(config_include_absolute_glob, tc)
178{
179 struct xbps_handle xh;
180 const char *tcsdir;
181 char *cfg, *buf, *buf2, pwd[PATH_MAX];
182 int ret;
183
184 /* get test source dir */
185 tcsdir = atf_tc_get_config_var(tc, "srcdir");
186
187 memset(&xh, 0, sizeof(xh));
188 buf = getcwd(pwd, sizeof(pwd));
189
190 xbps_strlcpy(xh.rootdir, pwd, sizeof(xh.rootdir));
191 xbps_strlcpy(xh.metadir, pwd, sizeof(xh.metadir));
192 ret = snprintf(xh.confdir, sizeof(xh.confdir), "%s/xbps.d", pwd);
193 ATF_REQUIRE_EQ((ret >= 0), 1);
194 ATF_REQUIRE_EQ(((size_t)ret < sizeof(xh.confdir)), 1);
195
196 ATF_REQUIRE_EQ(xbps_mkpath(xh.confdir, 0755), 0);
197
198 cfg = xbps_xasprintf("%s/xbps2.d", pwd);
199 ATF_REQUIRE_EQ(xbps_mkpath(cfg, 0755), 0);
200
201 buf = xbps_xasprintf("%s/xbps_absolute_glob.cf", tcsdir);
202 buf2 = xbps_xasprintf("%s/xbps.d/xbps.conf", pwd);
203 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
204 free(buf);
205 free(buf2);
206
207 buf = xbps_xasprintf("%s/1.include.cf", tcsdir);
208 buf2 = xbps_xasprintf("%s/xbps2.d/1.include.conf", pwd);
209 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
210 free(buf);
211 free(buf2);
212
213 buf = xbps_xasprintf("%s/2.include.cf", tcsdir);
214 buf2 = xbps_xasprintf("%s/xbps2.d/2.include.conf", pwd);
215 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
216 free(buf);
217 free(buf2);
218
219 xh.flags = XBPS_FLAG_DEBUG;
220 ATF_REQUIRE_EQ(xbps_init(&xh), 0);
221 /* should contain both repositories defined in [12].include.conf */
222 ATF_REQUIRE_EQ(xbps_array_count(xh.repositories), 2);
223}
224
225ATF_TC(config_masking);
226ATF_TC_HEAD(config_masking, tc)
227{
228 atf_tc_set_md_var(tc, "descr", "Test file masking");
229}
230
231ATF_TC_BODY(config_masking, tc)
232{
233 struct xbps_handle xh;
234 const char *tcsdir, *repo;
235 char *buf, *buf2, pwd[PATH_MAX];
236 int ret;
237
238 /* get test source dir */
239 tcsdir = atf_tc_get_config_var(tc, "srcdir");
240
241 memset(&xh, 0, sizeof(xh));
242 buf = getcwd(pwd, sizeof(pwd));
243
244 xbps_strlcpy(xh.rootdir, tcsdir, sizeof(xh.rootdir));
245 xbps_strlcpy(xh.metadir, tcsdir, sizeof(xh.metadir));
246 ret = snprintf(xh.confdir, sizeof(xh.confdir), "%s/xbps.d", pwd);
247 ATF_REQUIRE_EQ((ret >= 0), 1);
248 ATF_REQUIRE_EQ(((size_t)ret < sizeof(xh.confdir)), 1);
249 ret = snprintf(xh.sysconfdir, sizeof(xh.sysconfdir), "%s/sys-xbps.d", pwd);
250 ATF_REQUIRE_EQ((ret >= 0), 1);
251 ATF_REQUIRE_EQ(((size_t)ret < sizeof(xh.sysconfdir)), 1);
252
253 ATF_REQUIRE_EQ(xbps_mkpath(xh.confdir, 0755), 0);
254 ATF_REQUIRE_EQ(xbps_mkpath(xh.sysconfdir, 0755), 0);
255
256 buf = xbps_xasprintf("%s/1.include.cf", tcsdir);
257 buf2 = xbps_xasprintf("%s/xbps.d/repo.conf", pwd);
258 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
259 free(buf);
260 free(buf2);
261
262 buf = xbps_xasprintf("%s/2.include.cf", tcsdir);
263 buf2 = xbps_xasprintf("%s/sys-xbps.d/repo.conf", pwd);
264 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
265 free(buf);
266 free(buf2);
267
268 xh.flags = XBPS_FLAG_DEBUG;
269 ATF_REQUIRE_EQ(xbps_init(&xh), 0);
270
271 /* should contain one repository */
272 ATF_REQUIRE_EQ(xbps_array_count(xh.repositories), 1);
273
274 /* should contain repository=1 */
275 ATF_REQUIRE_EQ(xbps_array_get_cstring_nocopy(xh.repositories, 0, &repo), true);
276 ATF_REQUIRE_STREQ(repo, "1");
277}
278
279ATF_TC(config_trim_values);
280ATF_TC_HEAD(config_trim_values, tc)
281{
282 atf_tc_set_md_var(tc, "descr", "Test trimming of values");
283}
284
285ATF_TC_BODY(config_trim_values, tc)
286{
287 struct xbps_handle xh;
288 const char *tcsdir, *repo;
289 char *buf, *buf2, pwd[PATH_MAX];
290 int ret;
291
292 /* get test source dir */
293 tcsdir = atf_tc_get_config_var(tc, "srcdir");
294
295 memset(&xh, 0, sizeof(xh));
296 buf = getcwd(pwd, sizeof(pwd));
297
298 xbps_strlcpy(xh.rootdir, tcsdir, sizeof(xh.rootdir));
299 xbps_strlcpy(xh.metadir, tcsdir, sizeof(xh.metadir));
300 ret = snprintf(xh.confdir, sizeof(xh.confdir), "%s/xbps.d", pwd);
301 ATF_REQUIRE_EQ((ret >= 0), 1);
302 ATF_REQUIRE_EQ(((size_t)ret < sizeof(xh.confdir)), 1);
303 ret = snprintf(xh.sysconfdir, sizeof(xh.sysconfdir), "%s/sys-xbps.d", pwd);
304 ATF_REQUIRE_EQ((ret >= 0), 1);
305 ATF_REQUIRE_EQ(((size_t)ret < sizeof(xh.sysconfdir)), 1);
306
307 ATF_REQUIRE_EQ(xbps_mkpath(xh.confdir, 0755), 0);
308 ATF_REQUIRE_EQ(xbps_mkpath(xh.sysconfdir, 0755), 0);
309
310 buf = xbps_xasprintf("%s/trim.cf", tcsdir);
311 buf2 = xbps_xasprintf("%s/xbps.d/1.conf", pwd);
312 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
313 free(buf);
314 free(buf2);
315
316 xh.flags = XBPS_FLAG_DEBUG;
317 ATF_REQUIRE_EQ(xbps_init(&xh), 0);
318
319 /* should contain one repository */
320 ATF_REQUIRE_EQ(xbps_array_count(xh.repositories), 2);
321
322 /* should contain repository=1 */
323 ATF_REQUIRE_EQ(xbps_array_get_cstring_nocopy(xh.repositories, 0, &repo), true);
324 ATF_REQUIRE_STREQ(repo, "1");
325 /* should contain repository=2 */
326 ATF_REQUIRE_EQ(xbps_array_get_cstring_nocopy(xh.repositories, 1, &repo), true);
327 ATF_REQUIRE_STREQ(repo, "2");
328}
329
330ATF_TC(config_no_trailing_newline);
331ATF_TC_HEAD(config_no_trailing_newline, tc)
332{
333 atf_tc_set_md_var(tc, "descr", "Test configuration files without trailing newline");
334}
335
336ATF_TC_BODY(config_no_trailing_newline, tc)
337{
338 struct xbps_handle xh;
339 const char *tcsdir, *repo;
340 char *buf, *buf2, pwd[PATH_MAX];
341 int ret;
342
343 /* get test source dir */
344 tcsdir = atf_tc_get_config_var(tc, "srcdir");
345
346 memset(&xh, 0, sizeof(xh));
347 buf = getcwd(pwd, sizeof(pwd));
348
349 xbps_strlcpy(xh.rootdir, tcsdir, sizeof(xh.rootdir));
350 xbps_strlcpy(xh.metadir, tcsdir, sizeof(xh.metadir));
351 ret = snprintf(xh.confdir, sizeof(xh.confdir), "%s/xbps.d", pwd);
352 ATF_REQUIRE_EQ((ret >= 0), 1);
353 ATF_REQUIRE_EQ(((size_t)ret < sizeof(xh.confdir)), 1);
354 ret = snprintf(xh.sysconfdir, sizeof(xh.sysconfdir), "%s/sys-xbps.d", pwd);
355 ATF_REQUIRE_EQ((ret >= 0), 1);
356 ATF_REQUIRE_EQ(((size_t)ret < sizeof(xh.sysconfdir)), 1);
357
358 ATF_REQUIRE_EQ(xbps_mkpath(xh.confdir, 0755), 0);
359 ATF_REQUIRE_EQ(xbps_mkpath(xh.sysconfdir, 0755), 0);
360
361 buf = xbps_xasprintf("%s/no-trailing-nl.cf", tcsdir);
362 buf2 = xbps_xasprintf("%s/xbps.d/1.conf", pwd);
363 ATF_REQUIRE_EQ(symlink(buf, buf2), 0);
364 free(buf);
365 free(buf2);
366
367 xh.flags = XBPS_FLAG_DEBUG;
368 ATF_REQUIRE_EQ(xbps_init(&xh), 0);
369
370 /* should contain one repository */
371 ATF_REQUIRE_EQ(xbps_array_count(xh.repositories), 1);
372
373 /* should contain repository=test */
374 ATF_REQUIRE_EQ(xbps_array_get_cstring_nocopy(xh.repositories, 0, &repo), true);
375 ATF_REQUIRE_STREQ(repo, "test");
376}
377
378ATF_TP_ADD_TCS(tp)
379{
380 ATF_TP_ADD_TC(tp, config_include_test);
381 ATF_TP_ADD_TC(tp, config_include_nomatch_test);
382 ATF_TP_ADD_TC(tp, config_include_absolute);
383 ATF_TP_ADD_TC(tp, config_include_absolute_glob);
384 ATF_TP_ADD_TC(tp, config_masking);
385 ATF_TP_ADD_TC(tp, config_trim_values);
386 ATF_TP_ADD_TC(tp, config_no_trailing_newline);
387
388 return atf_no_error();
389}
Generic XBPS structure handler for initialization.
Definition xbps.h:559
void void void void void int xbps_init(struct xbps_handle *xhp)
Definition initend.c:46
char * xbps_xasprintf(const char *fmt,...) __attribute__((format(printf
int xbps_mkpath(const char *path, mode_t mode)
Definition mkpath.c:42
size_t xbps_strlcpy(char *dst, const char *src, size_t dstsize)
Definition util.c:575