.PHONY: clean-local-check
clean-local-check:
- -rm -rf $(TEST_ROOTFS)
+ -rm -rf $(TEST_ROOTFS)/*
return -1;
// Create a new Pakfire instance
- self->pakfire = pakfire_create(path, arch);
- if (!self->pakfire) {
- switch (errno) {
+ int r = pakfire_create(&self->pakfire, path, arch);
+ if (r) {
+ switch (r) {
// Invalid architecture
case -EINVAL:
PyErr_SetString(PyExc_ValueError, "Invalid architecture or path");
#include <pakfire/types.h>
-Pakfire pakfire_create(const char* path, const char* arch);
+int pakfire_create(Pakfire* pakfire, const char* path, const char* arch);
Pakfire pakfire_ref(Pakfire pakfire);
Pakfire pakfire_unref(Pakfire pakfire);
return 0;
}
-PAKFIRE_EXPORT Pakfire pakfire_create(const char* path, const char* arch) {
+PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char* arch) {
// Default to the native architecture
if (!arch)
arch = pakfire_arch_native();
// Check if the architecture is supported
if (!pakfire_arch_supported(arch)) {
- errno = -EINVAL;
- return NULL;
+ return -EINVAL;
}
// Path must be absolute
if (!pakfire_string_startswith(path, "/")) {
- errno = -EINVAL;
- return NULL;
+ return -EINVAL;
}
// Check if path exists
if (!pakfire_path_isdir(path)) {
- errno = -ENOENT;
- return NULL;
+ return -ENOENT;
}
- Pakfire pakfire = pakfire_calloc(1, sizeof(*pakfire));
- if (pakfire) {
- pakfire->nrefs = 1;
+ Pakfire p = pakfire_calloc(1, sizeof(*p) * 2);
+ if (!p)
+ return -ENOMEM;
- pakfire->path = pakfire_strdup(path);
+ p->nrefs = 1;
- // Set architecture
- pakfire->arch = pakfire_strdup(arch);
+ p->path = pakfire_strdup(path);
- // Setup logging
- pakfire->log_function = pakfire_log_syslog;
+ // Set architecture
+ p->arch = pakfire_strdup(arch);
- const char* env = secure_getenv("PAKFIRE_LOG");
- if (env)
- pakfire_log_set_priority(pakfire, log_priority(env));
+ // Setup logging
+ p->log_function = pakfire_log_syslog;
- DEBUG(pakfire, "Pakfire initialized at %p\n", pakfire);
- DEBUG(pakfire, " arch = %s\n", pakfire_get_arch(pakfire));
- DEBUG(pakfire, " path = %s\n", pakfire_get_path(pakfire));
+ const char* env = secure_getenv("PAKFIRE_LOG");
+ if (env)
+ pakfire_log_set_priority(p, log_priority(env));
- // Initialize the pool
- pakfire->pool = pool_create();
+ DEBUG(p, "Pakfire initialized at %p\n", p);
+ DEBUG(p, " arch = %s\n", pakfire_get_arch(p));
+ DEBUG(p, " path = %s\n", pakfire_get_path(p));
- // Set architecture of the pool
- pool_setarch(pakfire->pool, pakfire->arch);
+ // Initialize the pool
+ p->pool = pool_create();
- // Initialise cache
- pakfire_set_cache_path(pakfire, CACHE_PATH);
- }
+ // Set architecture of the pool
+ pool_setarch(p->pool, p->arch);
- return pakfire;
+ // Initialise cache
+ pakfire_set_cache_path(p, CACHE_PATH);
+
+ *pakfire = p;
+
+ return 0;
}
PAKFIRE_EXPORT Pakfire pakfire_ref(Pakfire pakfire) {
return EXIT_SUCCESS;
}
-static int test_import(const struct test* t) {
+static int test_import_export(const struct test* t) {
// Try to delete the key just in case it
// has been imported before
PakfireKey key = pakfire_key_get(t->pakfire, TEST_KEY_FINGERPRINT);
const char* fingerprint = pakfire_key_get_fingerprint(key);
ASSERT(strcmp(fingerprint, TEST_KEY_FINGERPRINT) == 0);
- pakfire_key_unref(key);
-
- return EXIT_SUCCESS;
-}
-
-static int test_export(const struct test* t) {
- PakfireKey key = pakfire_key_get(t->pakfire, TEST_KEY_FINGERPRINT);
- ASSERT(key);
-
// Dump key description
char* dump = pakfire_key_dump(key);
ASSERT(dump);
LOG("%s\n", dump);
pakfire_free(dump);
+ // Export the key
char* data = pakfire_key_export(key, 0);
ASSERT(data);
int main(int argc, char** argv) {
testsuite_add_test(test_init);
- testsuite_add_test(test_import);
- testsuite_add_test(test_export);
+ testsuite_add_test(test_import_export);
return testsuite_run();
}
static int test_path(const struct test* t) {
const char* path = pakfire_get_path(t->pakfire);
- ASSERT_STRING_EQUALS(path, TEST_ROOTFS);
+ ASSERT_STRING_STARTSWITH(path, TEST_ROOTFS);
return EXIT_SUCCESS;
}
# #
#############################################################################*/
+#include <linux/limits.h>
+#include <stdlib.h>
+
#include "testsuite.h"
#include <pakfire/logging.h>
struct testsuite ts;
static int test_run(struct test* t) {
- LOG("running %s\n", t->name);
+ char root[PATH_MAX];
+ int r;
+
+ // Create test root directory
+ snprintf(root, PATH_MAX - 1, "%s/pakfire-test-XXXXXX", TEST_ROOTFS);
+ char* tmp = mkdtemp(root);
+ ASSERT(root == tmp);
+
+ LOG("running %s (%s)\n", t->name, root);
+
+ // Create a pakfire instance
+ r = pakfire_create(&t->pakfire, root, NULL);
+ if (r) {
+ LOG("ERROR: Could not initialize Pakfire: %s\n", strerror(-r));
+ exit(1);
+ }
- t->pakfire = pakfire_create(TEST_ROOTFS, NULL);
ASSERT(t->pakfire);
// Log to stderr
// Enable debug logging
pakfire_log_set_priority(t->pakfire, LOG_DEBUG);
- int r = t->func(t);
+ r = t->func(t);
if (r)
LOG("Test failed with error code: %d\n", r);
// Release pakfire
- t->pakfire = pakfire_unref(t->pakfire);
+ Pakfire p = pakfire_unref(t->pakfire);
// Check if Pakfire was actually released
- if (t->pakfire) {
+ if (p) {
LOG("Error: Pakfire instance was not released\n");
return 1;
}
+ // Cleanup root
+ // TODO
+
return r;
}
} \
} while (0)
+#define ASSERT_STRING_STARTSWITH(string, start) \
+ do { \
+ if (strncmp(string, start, strlen(start)) != 0) { \
+ LOG_ERROR("Failed assertion: " #string " does not start with " #start " %s:%d %s\n", \
+ __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ return EXIT_FAILURE; \
+ } \
+ } while (0)
+
#endif /* PAKFIRE_TESTSUITE_H */