]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/test-as.c
importer: Drop EDROP as it has been merged into DROP
[people/ms/libloc.git] / src / test-as.c
index 08c8730f419ca872e7b8cda4b454dd0dfd4e6219..b135c6b9ffd91d1369b1da94c99123c25a85ce0a 100644 (file)
 #include <errno.h>
 #include <stdlib.h>
 #include <string.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>
 
 #define TEST_AS_COUNT 5000
 
@@ -32,9 +33,12 @@ int main(int argc, char** argv) {
        if (err < 0)
                exit(EXIT_FAILURE);
 
+       // Enable debug logging
+       loc_set_log_priority(ctx, LOG_DEBUG);
+
        // 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);
 
@@ -49,32 +53,25 @@ int main(int argc, char** argv) {
                loc_as_unref(as);
        }
 
-       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);
        }
-       fclose(f);
 
        loc_writer_unref(writer);
 
        // 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);
        }
 
@@ -95,8 +92,37 @@ int main(int argc, char** argv) {
                loc_as_unref(as);
        }
 
+       // Enumerator
+
+       struct loc_database_enumerator* enumerator;
+       err = loc_database_enumerator_new(&enumerator, db, LOC_DB_ENUMERATE_ASES, 0);
+       if (err) {
+               fprintf(stderr, "Could not create a database enumerator\n");
+               exit(EXIT_FAILURE);
+       }
+
+       loc_database_enumerator_set_string(enumerator, "10");
+
+       err = loc_database_enumerator_next_as(enumerator, &as);
+       if (err) {
+               fprintf(stderr, "Could not enumerate next AS\n");
+               exit(EXIT_FAILURE);
+       }
+
+       while (as) {
+               printf("Found AS%d: %s\n", loc_as_get_number(as), loc_as_get_name(as));
+
+               err = loc_database_enumerator_next_as(enumerator, &as);
+               if (err) {
+                       fprintf(stderr, "Could not enumerate next AS\n");
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       loc_database_enumerator_unref(enumerator);
        loc_database_unref(db);
        loc_unref(ctx);
+       fclose(f);
 
        return EXIT_SUCCESS;
 }