]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/test-as.c
lua: database: Implement fetching countries
[people/ms/libloc.git] / src / test-as.c
index 1e1c91ef7a304e84a13e201553d86761ca2a8cb2..b135c6b9ffd91d1369b1da94c99123c25a85ce0a 100644 (file)
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include <syslog.h>
 
-#include <loc/libloc.h>
-#include "database.h"
+#include <libloc/libloc.h>
+#include <libloc/database.h>
+#include <libloc/writer.h>
 
-#define TEST_AS_COUNT 100
+#define TEST_AS_COUNT 5000
 
 int main(int argc, char** argv) {
        int err;
@@ -31,15 +33,19 @@ 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_database* db;
-       err = loc_database_new(ctx, &db, 1024);
+       struct loc_writer* writer;
+       err = loc_writer_new(ctx, &writer, NULL, NULL);
        if (err < 0)
                exit(EXIT_FAILURE);
 
        char name[256];
        for (unsigned int i = 1; i <= TEST_AS_COUNT; i++) {
-               struct loc_as* as = loc_database_add_as(db, i);
+               struct loc_as* as;
+               loc_writer_add_as(writer, &as, i);
 
                sprintf(name, "Test AS%u", i);
                loc_as_set_name(as, name);
@@ -47,31 +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_database_write(db, 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_database_unref(db);
+       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);
-       }
-
-       err = loc_database_open(ctx, &db, f);
+       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);
        }
 
@@ -81,8 +81,48 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
+       struct loc_as* as;
+       for (unsigned int i = 1; i <= 10; i++) {
+               err = loc_database_get_as(db, &as, i);
+               if (err) {
+                       fprintf(stderr, "Could not find AS%d\n", i);
+                       exit(EXIT_FAILURE);
+               }
+
+               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;
 }