]> git.ipfire.org Git - pakfire.git/blame - tests/testsuite.c
downloader: Actually fail if the checksum doesn't match
[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
35bb0e8f 31struct testsuite ts;
1413be07 32
68d81d63 33static int test_run(int i, struct test* t) {
6dbda957
MT
34 char root[PATH_MAX];
35 int r;
36
37 // Create test root directory
38 snprintf(root, PATH_MAX - 1, "%s/pakfire-test-XXXXXX", TEST_ROOTFS);
95946a55
MT
39 char* tmp = pakfire_mkdtemp(root);
40 if (!tmp) {
41 LOG("Could not create temporary directory %s: %m\n", root);
42 exit(1);
43 }
6dbda957
MT
44
45 LOG("running %s (%s)\n", t->name, root);
46
47 // Create a pakfire instance
4cab8693
MT
48 r = pakfire_create(&t->pakfire, root, NULL, TEST_SRC_PATH "/pakfire.conf", 0,
49 pakfire_log_stderr, NULL);
6dbda957 50 if (r) {
b107a018 51 LOG("ERROR: Could not initialize pakfire: %m\n");
6dbda957
MT
52 exit(1);
53 }
7b463f34 54
faa4dbb2
MT
55 // Enable debug logging
56 pakfire_log_set_priority(t->pakfire, LOG_DEBUG);
57
6dbda957 58 r = t->func(t);
8e43fe98
MT
59 if (r)
60 LOG("Test failed with error code: %d\n", r);
7b463f34 61
f907e1a2 62 // Release pakfire
b107a018 63 struct pakfire* p = pakfire_unref(t->pakfire);
0f9411f7
MT
64
65 // Check if Pakfire was actually released
6dbda957 66 if (p) {
68d81d63 67 LOG("Error: Pakfire instance was not released in test %d\n", i);
0f9411f7
MT
68 return 1;
69 }
f907e1a2 70
6dbda957
MT
71 // Cleanup root
72 // TODO
73
7b463f34
MT
74 return r;
75}
76
35bb0e8f 77int __testsuite_add_test(const char* name, int (*func)(const struct test* t)) {
1413be07
MT
78 // Check if any space is left
79 if (ts.num >= MAX_TESTS) {
80 LOG("ERROR: We are out of space for tests\n");
7b463f34 81 exit(EXIT_FAILURE);
7b463f34
MT
82 }
83
1413be07
MT
84 struct test* test = &ts.tests[ts.num++];
85
86 // Set parameters
87 test->name = name;
88 test->func = func;
7b463f34
MT
89
90 return 0;
91}
92
1413be07
MT
93int testsuite_run() {
94 for (unsigned int i = 0; i < ts.num; i++) {
68d81d63 95 int r = test_run(i, &ts.tests[i]);
7b463f34
MT
96 if (r)
97 exit(r);
98 }
99
100 return EXIT_SUCCESS;
101}
436677a3
MT
102
103FILE* test_mktemp() {
104 char path[] = "/tmp/.pakfire-test.XXXXXX";
105
106 int fd = mkstemp(path);
107 if (fd < 0)
108 return NULL;
109
a8c8f799 110 return fdopen(fd, "w+");
436677a3 111}
67849b24
MT
112
113char* test_mkdtemp() {
114 char path[] = "/tmp/.pakfire-test.XXXXXX";
115
116 char* p = mkdtemp(path);
117 if (!p)
118 return NULL;
119
120 return strdup(path);
121}