]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/test-network.c
Implement signing/verifying databases
[people/ms/libloc.git] / src / test-network.c
index 40a6d32c8e5cc978b3faf509e3c71e9b2003bd55..9a745661d331c2b93509649200e774c16e2818fb 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>
 
@@ -92,7 +93,7 @@ int main(int argc, char** argv) {
 
        // Create a database
        struct loc_writer* writer;
-       err = loc_writer_new(ctx, &writer);
+       err = loc_writer_new(ctx, &writer, NULL);
        if (err < 0)
                exit(EXIT_FAILURE);
 
@@ -116,7 +117,10 @@ int main(int argc, char** argv) {
        // Set country code
        loc_network_set_country_code(network4, "XY");
 
-       FILE* f = fopen("test.db", "w");
+       // 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));
                exit(EXIT_FAILURE);
@@ -136,6 +140,37 @@ int main(int argc, char** argv) {
        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 address in the subnet
+       err = loc_database_lookup_from_string(db, "2001:db8::", &network1);
+       if (err) {
+               fprintf(stderr, "Could not look up 2001:db8::\n");
+               exit(EXIT_FAILURE);
+       }
+       loc_network_unref(network1);
+
+       // Lookup an address outside the subnet
+       err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1);
+       if (err == 0) {
+               fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
+               exit(EXIT_FAILURE);
+       }
+       loc_network_unref(network1);
+
        loc_unref(ctx);
 
        return EXIT_SUCCESS;