]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/test-signature.c
importer: Drop EDROP as it has been merged into DROP
[people/ms/libloc.git] / src / test-signature.c
index 4401f431c813e0ab7e956ca090ea5cb1cb3c7f54..e1be5b1889122d6b4ff9f2ad51eeb135307aaeb5 100644 (file)
 #include <ctype.h>
 #include <errno.h>
 #include <unistd.h>
+#include <syslog.h>
 
-#include <loc/libloc.h>
-#include <loc/database.h>
-#include <loc/writer.h>
+#include <libloc/libloc.h>
+#include <libloc/database.h>
+#include <libloc/writer.h>
 
 int main(int argc, char** argv) {
        int err;
@@ -33,14 +34,20 @@ int main(int argc, char** argv) {
        // Open public key
        FILE* public_key = fopen(ABS_SRCDIR "/examples/public-key.pem", "r");
        if (!public_key) {
-               fprintf(stderr, "Could not open public key file: %s\n", strerror(errno));
+               fprintf(stderr, "Could not open public key file: %m\n");
                exit(EXIT_FAILURE);
        }
 
        // Open private key
-       FILE* private_key = fopen(ABS_SRCDIR "/examples/private-key.pem", "r");
-       if (!private_key) {
-               fprintf(stderr, "Could not open private key file: %s\n", strerror(errno));
+       FILE* private_key1 = fopen(ABS_SRCDIR "/examples/private-key.pem", "r");
+       if (!private_key1) {
+               fprintf(stderr, "Could not open private key file: %m\n");
+               exit(EXIT_FAILURE);
+       }
+
+       FILE* private_key2 = fopen(ABS_SRCDIR "/examples/private-key.pem", "r");
+       if (!private_key2) {
+               fprintf(stderr, "Could not open private key file: %m\n");
                exit(EXIT_FAILURE);
        }
 
@@ -49,39 +56,33 @@ int main(int argc, char** argv) {
        if (err < 0)
                exit(EXIT_FAILURE);
 
+       // Enable debug logging
+       loc_set_log_priority(ctx, LOG_DEBUG);
+
        // Create an empty database
        struct loc_writer* writer;
-       err = loc_writer_new(ctx, &writer, private_key);
+       err = loc_writer_new(ctx, &writer, private_key1, private_key2);
        if (err < 0)
                exit(EXIT_FAILURE);
 
-       FILE* f = fopen("test.db", "w+");
+       FILE* f = tmpfile();
        if (!f) {
-               fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
+               fprintf(stderr, "Could not open file for writing: %m\n");
                exit(EXIT_FAILURE);
        }
 
-       err = loc_writer_write(writer, f);
+       err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
        if (err) {
-               fprintf(stderr, "Could not write database: %s\n", strerror(err));
+               fprintf(stderr, "Could not write database: %m\n");
                exit(EXIT_FAILURE);
        }
        loc_writer_unref(writer);
 
-       // Close the file
-       fclose(f);
-
        // And open it again from disk
-       f = fopen("test.db", "r");
-       if (!f) {
-               fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
-               exit(EXIT_FAILURE);
-       }
-
        struct loc_database* db;
        err = loc_database_new(ctx, &db, f);
        if (err) {
-               fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+               fprintf(stderr, "Could not open database: %m\n");
                exit(EXIT_FAILURE);
        }
 
@@ -93,9 +94,9 @@ int main(int argc, char** argv) {
        }
 
        // Open another public key
-       public_key = freopen(ABS_SRCDIR "/src/signing-key.pem", "r", public_key);
+       public_key = freopen(ABS_SRCDIR "/data/signing-key.pem", "r", public_key);
        if (!public_key) {
-               fprintf(stderr, "Could not open public key file: %s\n", strerror(errno));
+               fprintf(stderr, "Could not open public key file: %m\n");
                exit(EXIT_FAILURE);
        }
 
@@ -108,10 +109,11 @@ int main(int argc, char** argv) {
 
        // Close the database
        loc_database_unref(db);
-
        loc_unref(ctx);
+       fclose(f);
 
-       fclose(private_key);
+       fclose(private_key1);
+       fclose(private_key2);
        fclose(public_key);
 
        return EXIT_SUCCESS;