XBPS Library API 20250615
The X Binary Package System
find_pkg_orphans/main.c
1/*-
2 * Copyright (c) 2013-2015 Juan Romero Pardines.
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
29static const char expected_output[] =
30 "xbps-git-20130310_2\n"
31 "libxbps-git-20130310_2\n"
32 "confuse-2.7_2\n"
33 "libarchive-3.1.2_1\n"
34 "bzip2-1.0.5_1\n"
35 "liblzma-5.0.4_3\n"
36 "expat-2.1.0_3\n"
37 "attr-2.4.46_5\n"
38 "proplib-0.6.3_1\n"
39 "libfetch-2.34_1\n"
40 "libssl-1.0.1e_3\n"
41 "zlib-1.2.7_1\n"
42 "glibc-2.20_1\n"
43 "xbps-triggers-1.0_1\n";
44
45static const char expected_output_all[] =
46 "orphan2-0_1\n"
47 "unexistent-pkg-0_1\n"
48 "orphan1-0_1\n"
49 "orphan0-0_1\n";
50
51ATF_TC(find_pkg_orphans_test);
52ATF_TC_HEAD(find_pkg_orphans_test, tc)
53{
54 atf_tc_set_md_var(tc, "descr", "Test xbps_find_pkg_orphans() for target pkg");
55}
56
57ATF_TC_BODY(find_pkg_orphans_test, tc)
58{
59 struct xbps_handle xh;
60 xbps_array_t a, res;
61 xbps_dictionary_t pkgd;
62 xbps_string_t pstr;
63 const char *pkgver, *tcsdir;
64 unsigned int i;
65
66 /* get test source dir */
67 tcsdir = atf_tc_get_config_var(tc, "srcdir");
68
69 memset(&xh, 0, sizeof(xh));
70 xbps_strlcpy(xh.rootdir, tcsdir, sizeof(xh.rootdir));
71 xbps_strlcpy(xh.metadir, tcsdir, sizeof(xh.metadir));
72 ATF_REQUIRE_EQ(xbps_init(&xh), 0);
73
74 a = xbps_array_create();
75 xbps_array_add_cstring_nocopy(a, "xbps-git");
76
77 pstr = xbps_string_create();
78 res = xbps_find_pkg_orphans(&xh, a);
79 for (i = 0; i < xbps_array_count(res); i++) {
80 pkgd = xbps_array_get(res, i);
81 xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
82 xbps_string_append_cstring(pstr, pkgver);
83 xbps_string_append_cstring(pstr, "\n");
84 }
85 ATF_REQUIRE_STREQ(xbps_string_cstring_nocopy(pstr), expected_output);
86}
87
88ATF_TC(find_all_orphans_test);
89ATF_TC_HEAD(find_all_orphans_test, tc)
90{
91 atf_tc_set_md_var(tc, "descr", "Test xbps_find_pkg_orphans() for all pkgs");
92}
93
94ATF_TC_BODY(find_all_orphans_test, tc)
95{
96 struct xbps_handle xh;
97 xbps_array_t res;
98 xbps_dictionary_t pkgd;
99 xbps_string_t pstr;
100 const char *pkgver, *tcsdir;
101 unsigned int i;
102
103 /* get test source dir */
104 tcsdir = atf_tc_get_config_var(tc, "srcdir");
105
106 memset(&xh, 0, sizeof(xh));
107 xbps_strlcpy(xh.rootdir, tcsdir, sizeof(xh.rootdir));
108 xbps_strlcpy(xh.metadir, tcsdir, sizeof(xh.metadir));
109 ATF_REQUIRE_EQ(xbps_init(&xh), 0);
110
111 pstr = xbps_string_create();
112 res = xbps_find_pkg_orphans(&xh, NULL);
113 for (i = 0; i < xbps_array_count(res); i++) {
114 pkgd = xbps_array_get(res, i);
115 xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
116 xbps_string_append_cstring(pstr, pkgver);
117 xbps_string_append_cstring(pstr, "\n");
118 }
119 printf("%s", xbps_string_cstring_nocopy(pstr));
120
121 ATF_REQUIRE_STREQ(xbps_string_cstring_nocopy(pstr), expected_output_all);
122}
123
124ATF_TP_ADD_TCS(tp)
125{
126 /* First test: find orphans for xbps-git pkg */
127 ATF_TP_ADD_TC(tp, find_pkg_orphans_test);
128 /* Second test: find all orphans */
129 ATF_TP_ADD_TC(tp, find_all_orphans_test);
130
131 return atf_no_error();
132}
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
xbps_array_t xbps_find_pkg_orphans(struct xbps_handle *xhp, xbps_array_t orphans)
size_t xbps_strlcpy(char *dst, const char *src, size_t dstsize)
Definition util.c:575