]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
test: Add tests for database enumerator
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 11 Nov 2020 21:16:45 +0000 (21:16 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 11 Nov 2020 21:16:45 +0000 (21:16 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/test-database.c

index b4a75c43880be8c47e2ba73b7a61bd3e8d1c359a..4aef94e7143b995a2a513b5172105e80e20e4434 100644 (file)
@@ -38,6 +38,14 @@ const char* DESCRIPTION =
        "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)
@@ -139,6 +147,24 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
+       struct loc_network* network = NULL;
+
+       // 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 writing: %s\n", strerror(errno));
@@ -170,6 +196,33 @@ 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);
+       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;
+
+               char* s = loc_network_str(network);
+               printf("Got network: %s\n", s);
+               free(s);
+       }
+
+       // Free the enumerator
+       loc_database_enumerator_unref(enumerator);
+
        // Close the database
        loc_database_unref(db);
        loc_unref(ctx);