]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/test-network.c
Replace strerror(errno) with %m in format string throughout
[people/ms/libloc.git] / src / test-network.c
index 7c902241a91f2b88765af7b3276ca68185d9cc31..dcb389a6ff3eeb265527221d0b998eadfa7e37e9 100644 (file)
@@ -14,6 +14,7 @@
        GNU General Public License for more details.
 */
 
+#include <arpa/inet.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stddef.h>
 #include <string.h>
 #include <syslog.h>
 
-#include <loc/libloc.h>
-#include <loc/database.h>
-#include <loc/network.h>
-#include <loc/writer.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) {
        int err;
@@ -46,6 +49,13 @@ int main(int argc, char** argv) {
        }
 #endif
 
+       struct in6_addr address;
+       err = inet_pton(AF_INET6, "2001:db8::1", &address);
+       if (err != 1) {
+               fprintf(stderr, "Could not parse IP address\n");
+               exit(EXIT_FAILURE);
+       }
+
        // Create a network
        struct loc_network* network1;
        err = loc_network_new_from_string(ctx, &network1, "2001:db8::1/32");
@@ -54,7 +64,7 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
-       err = loc_network_set_country_code(network1, "XX");
+       err = loc_network_set_country_code(network1, "DE");
        if (err) {
                fprintf(stderr, "Could not set country code\n");
                exit(EXIT_FAILURE);
@@ -70,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);
@@ -92,6 +102,12 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
+       err = loc_network_matches_address(network1, &address);
+       if (!err) {
+               fprintf(stderr, "Network1 does not match address\n");
+               exit(EXIT_FAILURE);
+       }
+
        struct loc_network* network2;
        err = loc_network_new_from_string(ctx, &network2, "2001:db8:ffff::/48");
        if (err) {
@@ -99,7 +115,7 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
-       err = loc_network_set_country_code(network2, "XY");
+       err = loc_network_set_country_code(network2, "DE");
        if (err) {
                fprintf(stderr, "Could not set country code\n");
                exit(EXIT_FAILURE);
@@ -125,60 +141,69 @@ int main(int argc, char** argv) {
 #endif
 
        // Check equals function
-       err = loc_network_eq(network1, network1);
-       if (!err) {
+       err = loc_network_cmp(network1, network1);
+       if (err) {
                fprintf(stderr, "Network is not equal with itself\n");
                exit(EXIT_FAILURE);
        }
 
-       err = loc_network_eq(network1, network2);
-       if (err) {
+       err = loc_network_cmp(network1, network2);
+       if (!err) {
                fprintf(stderr, "Networks equal unexpectedly\n");
                exit(EXIT_FAILURE);
        }
 
        // Check subnet function
-       err = loc_network_is_subnet_of(network1, network2);
-       if (err != 0) {
+       err = loc_network_is_subnet(network1, network2);
+       if (!err) {
                fprintf(stderr, "Subnet check 1 failed: %d\n", err);
                exit(EXIT_FAILURE);
        }
 
-       err = loc_network_is_subnet_of(network2, network1);
-       if (err != 1) {
+       err = loc_network_is_subnet(network2, network1);
+       if (err) {
                fprintf(stderr, "Subnet check 2 failed: %d\n", err);
                exit(EXIT_FAILURE);
        }
 
-       // Make list of subnets
-       struct loc_network_list* subnets = loc_network_subnets(network1);
-       if (!subnets) {
-               fprintf(stderr, "Could not find subnets of network\n");
+       // Make subnets
+       struct loc_network* subnet1 = NULL;
+       struct loc_network* subnet2 = NULL;
+
+       err  = loc_network_subnets(network1, &subnet1, &subnet2);
+       if (err || !subnet1 || !subnet2) {
+               fprintf(stderr, "Could not find subnets of network: %d\n", err);
                exit(EXIT_FAILURE);
        }
 
-       loc_network_list_dump(subnets);
+       const char* s = loc_network_str(subnet1);
+       printf("Received subnet1 = %s\n", s);
 
-       while (!loc_network_list_empty(subnets)) {
-               struct loc_network* subnet = loc_network_list_pop(subnets);
-               if (!subnet) {
-                       fprintf(stderr, "Received an empty subnet\n");
-                       exit(EXIT_FAILURE);
-               }
+       s = loc_network_str(subnet2);
+       printf("Received subnet2 = %s\n", s);
 
-               char* s = loc_network_str(subnet);
-               printf("Received subnet %s\n", s);
-               free(s);
+       if (!loc_network_is_subnet(network1, subnet1)) {
+               fprintf(stderr, "Subnet1 is not a subnet\n");
+               exit(EXIT_FAILURE);
+       }
 
-               if (!loc_network_is_subnet_of(subnet, network1)) {
-                       fprintf(stderr, "Not a subnet\n");
-                       exit(EXIT_FAILURE);
-               }
+       if (!loc_network_is_subnet(network1, subnet2)) {
+               fprintf(stderr, "Subnet2 is not a subnet\n");
+               exit(EXIT_FAILURE);
+       }
 
-               loc_network_unref(subnet);
+       if (!loc_network_overlaps(network1, subnet1)) {
+               fprintf(stderr, "Network1 does not seem to contain subnet1\n");
+               exit(EXIT_FAILURE);
+       }
+
+       if (!loc_network_overlaps(network1, subnet2)) {
+               fprintf(stderr, "Network1 does not seem to contain subnet2\n");
+               exit(EXIT_FAILURE);
        }
 
-       loc_network_list_unref(subnets);
+       loc_network_unref(subnet1);
+       loc_network_unref(subnet2);
 
        struct loc_network_list* excluded = loc_network_exclude(network1, network2);
        if (!excluded) {
@@ -187,15 +212,6 @@ int main(int argc, char** argv) {
        }
 
        loc_network_list_dump(excluded);
-
-       // Reverse it
-       loc_network_list_reverse(excluded);
-       loc_network_list_dump(excluded);
-
-       // Sort them and dump them again
-       loc_network_list_sort(excluded);
-       loc_network_list_dump(excluded);
-
        loc_network_list_unref(excluded);
 
        // Create a database
@@ -230,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);
        }
@@ -242,16 +258,9 @@ 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);
        }
 
@@ -295,6 +304,39 @@ int main(int argc, char** argv) {
        }
        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);