]> git.ipfire.org Git - location/libloc.git/blobdiff - src/test-network.c
database: Implement lookup
[location/libloc.git] / src / test-network.c
index a573463b0d5ee2659d2ba79f55f6784a58069c96..b7b51201231b31630b431903f06ffc06cdbeebad 100644 (file)
@@ -21,6 +21,7 @@
 #include <string.h>
 
 #include <loc/libloc.h>
+#include <loc/database.h>
 #include <loc/network.h>
 #include <loc/writer.h>
 
@@ -97,7 +98,7 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
 
        struct loc_network* network3;
-       err = loc_writer_add_network(writer, &network3, "2001:db8:1::/48");
+       err = loc_writer_add_network(writer, &network3, "2001:db8::/64");
        if (err) {
                fprintf(stderr, "Could not add network\n");
                exit(EXIT_FAILURE);
@@ -106,6 +107,19 @@ int main(int argc, char** argv) {
        // Set country code
        loc_network_set_country_code(network3, "XX");
 
+       struct loc_network* network4;
+       err = loc_writer_add_network(writer, &network4, "2001:db8:ffff::/64");
+       if (err) {
+               fprintf(stderr, "Could not add network\n");
+               exit(EXIT_FAILURE);
+       }
+
+       // Set country code
+       loc_network_set_country_code(network4, "XY");
+
+       // Set ASN
+       loc_network_set_asn(network4, 1024);
+
        FILE* f = fopen("test.db", "w");
        if (!f) {
                fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
@@ -124,7 +138,39 @@ int main(int argc, char** argv) {
        loc_network_unref(network1);
        loc_network_unref(network2);
        loc_network_unref(network3);
+       loc_network_unref(network4);
        loc_network_tree_unref(tree);
+
+       // 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));
+               exit(EXIT_FAILURE);
+       }
+
+       // Lookup an exact match
+       err = loc_database_lookup_from_string(db, "2001:db8::", &network1);
+       if (err) {
+               fprintf(stderr, "Could not look up the given IP address\n");
+               exit(EXIT_FAILURE);
+       }
+       loc_network_unref(network1);
+
+       // Lookup a non-exact match
+       err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1);
+       if (err) {
+               fprintf(stderr, "Could not look up the given IP address\n");
+               exit(EXIT_FAILURE);
+       }
+       loc_network_unref(network1);
+
        loc_unref(ctx);
 
        return EXIT_SUCCESS;