]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/test-network.c
importer: Remove superfluous function call
[people/ms/libloc.git] / src / test-network.c
index 4582fbe60bda7d27a55f7c358e8ef8f608b7ddec..717ad3abd92a332bc744f1cb378fcbf904961e07 100644 (file)
 #include <syslog.h>
 
 #include <libloc/libloc.h>
+#include <libloc/address.h>
 #include <libloc/database.h>
 #include <libloc/network.h>
+#include <libloc/private.h>
 #include <libloc/writer.h>
 
 int main(int argc, char** argv) {
@@ -78,7 +80,7 @@ int main(int argc, char** argv) {
 #endif
 
        // Check if the first and last addresses are correct
-       char* string = loc_network_format_first_address(network1);
+       const char* string = loc_network_format_first_address(network1);
        if (!string) {
                fprintf(stderr, "Did get NULL instead of a string for the first address\n");
                exit(EXIT_FAILURE);
@@ -174,13 +176,11 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
-       char* s = loc_network_str(subnet1);
+       const char* s = loc_network_str(subnet1);
        printf("Received subnet1 = %s\n", s);
-       free(s);
 
        s = loc_network_str(subnet2);
        printf("Received subnet2 = %s\n", s);
-       free(s);
 
        if (!loc_network_is_subnet(network1, subnet1)) {
                fprintf(stderr, "Subnet1 is not a subnet\n");
@@ -246,7 +246,7 @@ int main(int argc, char** argv) {
        // Try adding an invalid network
        struct loc_network* network;
        err = loc_writer_add_network(writer, &network, "xxxx:xxxx::/32");
-       if (err != -EINVAL) {
+       if (!err) {
                fprintf(stderr, "It was possible to add an invalid network (err = %d)\n", err);
                exit(EXIT_FAILURE);
        }
@@ -258,22 +258,15 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
-       // Try adding localhost
-       err = loc_writer_add_network(writer, &network, "::1/128");
-       if (err != -EINVAL) {
-               fprintf(stderr, "It was possible to add localhost (::1/128): %d\n", err);
-               exit(EXIT_FAILURE);
-       }
-
        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, 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);
        }
        loc_writer_unref(writer);
@@ -291,7 +284,7 @@ int main(int argc, char** argv) {
        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);
        }
 
@@ -309,7 +302,39 @@ int main(int argc, char** argv) {
                fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
                exit(EXIT_FAILURE);
        }
-       loc_network_unref(network1);
+
+       const struct bit_length_test {
+               const char* network;
+               unsigned int bit_length;
+       } bit_length_tests[] = {
+               { "::/0", 0 },
+               { "2001::/128", 126 },
+               { "1.0.0.0/32", 25 },
+               { "0.0.0.1/32", 1 },
+               { "255.255.255.255/32", 32 },
+               { NULL, 0, },
+       };
+
+       for (const struct bit_length_test* t = bit_length_tests; t->network; t++) {
+               err = loc_network_new_from_string(ctx, &network1, t->network);
+               if (err) {
+                       fprintf(stderr, "Could not create network %s: %m\n", t->network);
+                       exit(EXIT_FAILURE);
+               }
+
+               const struct in6_addr* addr = loc_network_get_first_address(network1);
+
+               unsigned int bit_length = loc_address_bit_length(addr);
+
+               if (bit_length != t->bit_length) {
+                       printf("Bit length of %s didn't match: %u != %u\n",
+                               t->network, t->bit_length, bit_length);
+                       loc_network_unref(network1);
+                       exit(EXIT_FAILURE);
+               }
+
+               loc_network_unref(network1);
+       }
 
        loc_unref(ctx);
        fclose(f);