]> git.ipfire.org Git - pakfire.git/blob - tests/libpakfire/dependencies.c
Move dependency functions into an own file
[pakfire.git] / tests / libpakfire / dependencies.c
1 /*#############################################################################
2 # #
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2021 Pakfire development team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <pakfire/dependencies.h>
26 #include <pakfire/parser.h>
27 #include <pakfire/util.h>
28
29 #include "../testsuite.h"
30
31 static const char* relations[] = {
32 // Simple packages
33 "beep",
34 "bash",
35 "kernel-devel",
36
37 // Packages with a specific version
38 "beep = 1",
39 "bash = 2",
40 "kernel-devel = 3",
41
42 // Packages with version and release
43 "beep = 1-1.ip3",
44 "bash = 2-2.ip3",
45 "kernel-devel = 3-3.ip3",
46
47 // Packages with version, release and arch
48 "beep = 1-1.ip3.x86_64",
49 "bash = 2-2.ip3.aarch64",
50 "kernel-devel = 3-3.x86_64",
51
52 // Packages with a version greater/smaller/greater or equal/smaller or equal than
53 "beep > 1",
54 "bash > 2",
55 "kernel-devel > 3",
56 "beep < 1",
57 "bash < 2",
58 "kernel-devel < 3",
59 "beep >= 1",
60 "bash >= 2",
61 "kernel-devel >= 3",
62 "beep >= 1",
63 "bash >= 2",
64 "kernel-devel >= 3",
65
66 // Namespaces
67 "pakfire(test)",
68
69 // Rich dependencies
70 "(foo if bar else baz)",
71 "(foo if bar else baz >= 2)",
72 "(foo >= 1.0 with foo < 2.0)",
73 "(kernel with flavor = desktop)",
74 "(beep or bash or kernel)",
75 "((pkg1 with feature2) or (pkg2 without feature1))",
76 "(foo or (bar and baz))",
77
78 // Nested rich dependencies
79 "(beep or (bash and kernel))",
80 "(beep or (bash and kernel) or foo)",
81
82 NULL,
83 };
84
85 static const char* invalid_relations[] = {
86 "beep something-else",
87 "beep >= 1 something else",
88
89 // Broken rich deps
90 "()",
91 "(a ",
92 "(a unless)",
93 "(a unless )",
94
95 // Broken nested rich deps
96 "(a and (b or c)",
97
98 NULL,
99 };
100
101 static int test_dependencies(const struct test* t) {
102 Id dep = 0;
103 const char* result = NULL;
104
105 // Check some invalid inputs
106 ASSERT_ERRNO(pakfire_str2dep(t->pakfire, NULL) == 0, EINVAL);
107 ASSERT_ERRNO(pakfire_str2dep(t->pakfire, "") == 0, EINVAL);
108
109 // Check some valid inputs
110 for (const char** relation = relations; *relation; relation++) {
111 printf("Parsing '%s'...\n", *relation);
112
113 // Parse relation
114 dep = pakfire_str2dep(t->pakfire, *relation);
115
116 // Convert it back to string
117 result = pakfire_dep2str(t->pakfire, dep);
118 ASSERT(result);
119
120 // Check if the output matches the input
121 ASSERT_STRING_EQUALS(*relation, result);
122 }
123
124 // Check invalid inputs
125 for (const char** relation = invalid_relations; *relation; relation++) {
126 printf("Parsing '%s'...\n", *relation);
127
128 // Parse relation
129 dep = pakfire_str2dep(t->pakfire, *relation);
130
131 // The return value must be ID_NULL
132 ASSERT(dep == ID_NULL);
133 }
134
135 return EXIT_SUCCESS;
136
137 FAIL:
138 return EXIT_FAILURE;
139 }
140
141 int main(int argc, const char* argv[]) {
142 testsuite_add_test(test_dependencies);
143
144 return testsuite_run(argc, argv);
145 }