]> git.ipfire.org Git - people/stevee/pakfire.git/blame - tests/testsuite.c
tests: Add tests for digest
[people/stevee/pakfire.git] / tests / testsuite.c
CommitLineData
7b463f34
MT
1/*#############################################################################
2# #
3# Pakfire - The IPFire package management system #
4# Copyright (C) 2017 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
51bc8c08 21#include <errno.h>
6dbda957
MT
22#include <linux/limits.h>
23#include <stdlib.h>
24
7b463f34
MT
25#include "testsuite.h"
26
c348901e
MT
27#include <pakfire/logging.h>
28#include <pakfire/pakfire.h>
95946a55 29#include <pakfire/util.h>
c348901e 30
79a0a5cd
MT
31#define TMP_TEMPLATE "/tmp/pakfire-test.XXXXXX"
32
35bb0e8f 33struct testsuite ts;
1413be07 34
68d81d63 35static int test_run(int i, struct test* t) {
0cb40254
MT
36 struct pakfire* p = NULL;
37
788da09a 38 char root[PATH_MAX] = TEST_ROOTFS "/pakfire-test-XXXXXX";
6dbda957
MT
39 int r;
40
41 // Create test root directory
95946a55
MT
42 char* tmp = pakfire_mkdtemp(root);
43 if (!tmp) {
44 LOG("Could not create temporary directory %s: %m\n", root);
45 exit(1);
46 }
6dbda957
MT
47
48 LOG("running %s (%s)\n", t->name, root);
49
50 // Create a pakfire instance
836f71d2 51 r = pakfire_create(&t->pakfire, root, NULL, TEST_SRC_PATH "/pakfire.conf",
bcb98c7b 52 0, LOG_DEBUG, pakfire_log_stderr, NULL);
6dbda957 53 if (r) {
b107a018 54 LOG("ERROR: Could not initialize pakfire: %m\n");
0cb40254
MT
55 goto ERROR;
56 }
57
84e59679
MT
58 // Check if the instance was created properly
59 if (r == 0 && !t->pakfire) {
60 LOG("ERROR: Pakfire was not initialized, but no error was raised: %m\n");
61 goto ERROR;
62 }
63
0cb40254
MT
64 // Copy command into environment
65 r = pakfire_copy_in(t->pakfire, TEST_STUB_COMMAND, "/command");
66 if (r) {
67 LOG("ERROR: Could not copy command: %m\n");
68 goto ERROR;
6dbda957 69 }
7b463f34 70
0cb40254 71 // Run test
6dbda957 72 r = t->func(t);
8e43fe98
MT
73 if (r)
74 LOG("Test failed with error code: %d\n", r);
7b463f34 75
0cb40254 76ERROR:
f907e1a2 77 // Release pakfire
84e59679
MT
78 if (t->pakfire) {
79 p = pakfire_unref(t->pakfire);
0f9411f7 80
84e59679
MT
81 // Check if Pakfire was actually released
82 if (p) {
83 LOG("Error: Pakfire instance was not released in test %d\n", i);
84 return 1;
85 }
f907e1a2 86
84e59679
MT
87 // Reset pointer (just in case)
88 t->pakfire = NULL;
89 }
0cb40254 90
6dbda957 91 // Cleanup root
788da09a 92 pakfire_rmtree(root, 0);
6dbda957 93
7b463f34
MT
94 return r;
95}
96
35bb0e8f 97int __testsuite_add_test(const char* name, int (*func)(const struct test* t)) {
1413be07
MT
98 // Check if any space is left
99 if (ts.num >= MAX_TESTS) {
100 LOG("ERROR: We are out of space for tests\n");
7b463f34 101 exit(EXIT_FAILURE);
7b463f34
MT
102 }
103
1413be07
MT
104 struct test* test = &ts.tests[ts.num++];
105
106 // Set parameters
107 test->name = name;
108 test->func = func;
7b463f34
MT
109
110 return 0;
111}
112
b0ead88b
MT
113static int check_whether_to_run(const struct test* t, const int argc, const char* argv[]) {
114 // Run all tests when nothing has been selected
115 if (argc < 2)
2e764d33 116 return 1;
b0ead88b
MT
117
118 // Check if this test has been listed
119 for (unsigned int i = 1; i < argc; i++) {
120 if (strcmp(t->name, argv[i]) == 0)
121 return 1;
122 }
123
124 return 0;
125}
126
127int testsuite_run(int argc, const char* argv[]) {
1413be07 128 for (unsigned int i = 0; i < ts.num; i++) {
b0ead88b
MT
129 struct test* test = &ts.tests[i];
130
131 // Skip any tests that should not be run
132 if (!check_whether_to_run(test, argc, argv))
133 continue;
134
135 // Run the test
136 int r = test_run(i, test);
7b463f34
MT
137 if (r)
138 exit(r);
139 }
140
141 return EXIT_SUCCESS;
142}
436677a3 143
79a0a5cd
MT
144FILE* test_mktemp(char** path) {
145 char* p = NULL;
146
147 // Reset path
148 if (path)
149 *path = NULL;
150
151 int r = asprintf(&p, "%s", TMP_TEMPLATE);
152 if (r < 0)
153 return NULL;
436677a3 154
79a0a5cd 155 int fd = mkstemp(p);
436677a3
MT
156 if (fd < 0)
157 return NULL;
158
79a0a5cd
MT
159 // If we want a named temporary file, we set path
160 if (path) {
161 *path = p;
162
163 // Otherwise we unlink the path and free p
164 } else {
165 unlink(p);
166 free(p);
167 }
292f8013 168
a8c8f799 169 return fdopen(fd, "w+");
436677a3 170}
67849b24
MT
171
172char* test_mkdtemp() {
79a0a5cd 173 char path[] = TMP_TEMPLATE;
67849b24
MT
174
175 char* p = mkdtemp(path);
176 if (!p)
177 return NULL;
178
179 return strdup(path);
180}