]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/test-database.c
importer: Drop EDROP as it has been merged into DROP
[people/ms/libloc.git] / src / test-database.c
index 600a983235207fb6ac1d05c0671e70ce1193b0f2..8ba558a3eb4c82b34c0cfbb8787b5215c4613889 100644 (file)
 #include <ctype.h>
 #include <errno.h>
 #include <unistd.h>
+#include <syslog.h>
 
-#include <loc/libloc.h>
-#include <loc/writer.h>
-#include "database.h"
+#include <libloc/libloc.h>
+#include <libloc/database.h>
+#include <libloc/writer.h>
 
 const char* VENDOR = "Test Vendor";
 const char* DESCRIPTION =
@@ -35,6 +36,36 @@ const char* DESCRIPTION =
        "tempor felis enim. Integer congue nisi in maximus pretium. "
        "Pellentesque et turpis elementum, luctus mi at, interdum erat. "
        "Maecenas ut venenatis nunc.";
+const char* LICENSE = "CC";
+
+const char* networks[] = {
+       "2001:db8::/32",
+       "2001:db8:1000::/48",
+       "2001:db8:2000::/48",
+       "2001:db8:2020::/48",
+       NULL,
+};
+
+static int attempt_to_open(struct loc_ctx* ctx, char* path) {
+       FILE* f = fopen(path, "r");
+       if (!f)
+               return -1;
+
+       struct loc_database* db;
+       int r = loc_database_new(ctx, &db, f);
+
+       if (r == 0) {
+               fprintf(stderr, "Opening %s was unexpectedly successful\n", path);
+               loc_database_unref(db);
+
+               r = 1;
+       }
+
+       // Close the file again
+       fclose(f);
+
+       return r;
+}
 
 int main(int argc, char** argv) {
        int err;
@@ -44,9 +75,27 @@ int main(int argc, char** argv) {
        if (err < 0)
                exit(EXIT_FAILURE);
 
+       // Enable debug logging
+       loc_set_log_priority(ctx, LOG_DEBUG);
+
+       // Try opening an empty file
+       err = attempt_to_open(ctx, "/dev/null");
+       if (err == 0)
+               exit(EXIT_FAILURE);
+
+       // Try opening a file with all zeroes
+       err = attempt_to_open(ctx, "/dev/zero");
+       if (err == 0)
+               exit(EXIT_FAILURE);
+
+       // Try opening a file with random data
+       err = attempt_to_open(ctx, "/dev/urandom");
+       if (err == 0)
+               exit(EXIT_FAILURE);
+
        // Create a database
        struct loc_writer* writer;
-       err = loc_writer_new(ctx, &writer);
+       err = loc_writer_new(ctx, &writer, NULL, NULL);
        if (err < 0)
                exit(EXIT_FAILURE);
 
@@ -82,36 +131,62 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
-       FILE* f = fopen("test.db", "w");
-       if (!f) {
-               fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
+       // Set a license
+       err = loc_writer_set_license(writer, LICENSE);
+       if (err) {
+               fprintf(stderr, "Could not set license\n");
                exit(EXIT_FAILURE);
        }
 
-       err = loc_writer_write(writer, f);
-       if (err) {
-               fprintf(stderr, "Could not write database: %s\n", strerror(err));
+       // Retrieve license
+       const char* license = loc_writer_get_license(writer);
+       if (license) {
+               printf("License is: %s\n", license);
+       } else {
+               fprintf(stderr, "Could not retrieve license\n");
                exit(EXIT_FAILURE);
        }
-       loc_writer_unref(writer);
 
-       // Close the file
-       fclose(f);
+       struct loc_network* network = NULL;
 
-       // And open it again from disk
-       f = fopen("test.db", "r");
+       // Add some networks
+       const char** n = networks;
+       while (*n) {
+               err = loc_writer_add_network(writer, &network, *n);
+               if (err) {
+                       fprintf(stderr, "Could not add network %s\n", *n);
+                       exit(EXIT_FAILURE);
+               }
+
+               // Set a country
+               loc_network_set_country_code(network, "XX");
+
+               // Next one
+               n++;
+       }
+
+       FILE* f = tmpfile();
        if (!f) {
-               fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
+               fprintf(stderr, "Could not open file for writing: %m\n");
                exit(EXIT_FAILURE);
        }
 
+       err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
+       if (err) {
+               fprintf(stderr, "Could not write database: %m\n");
+               exit(EXIT_FAILURE);
+       }
+       loc_writer_unref(writer);
+
+       // And open it again from disk
        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);
        }
 
+       // Try reading something from the database
        vendor = loc_database_get_vendor(db);
        if (!vendor) {
                fprintf(stderr, "Could not retrieve vendor\n");
@@ -121,10 +196,36 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
+       // Enumerator
+       struct loc_database_enumerator* enumerator;
+       err = loc_database_enumerator_new(&enumerator, db, LOC_DB_ENUMERATE_NETWORKS, 0);
+       if (err) {
+               fprintf(stderr, "Could not initialise the enumerator: %d\n", err);
+               exit(EXIT_FAILURE);
+       }
+
+       // Walk through all networks
+       while (1) {
+               err = loc_database_enumerator_next_network(enumerator, &network);
+               if (err) {
+                       fprintf(stderr, "Error fetching the next network: %d\n", err);
+                       exit(EXIT_FAILURE);
+               }
+
+               if (!network)
+                       break;
+
+               const char* s = loc_network_str(network);
+               printf("Got network: %s\n", s);
+       }
+
+       // Free the enumerator
+       loc_database_enumerator_unref(enumerator);
+
        // Close the database
        loc_database_unref(db);
-
        loc_unref(ctx);
+       fclose(f);
 
        return EXIT_SUCCESS;
 }