]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
address: Add some simple tests
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 7 Mar 2022 16:05:28 +0000 (16:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 7 Mar 2022 16:05:28 +0000 (16:05 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/.gitignore
src/test-address.c [new file with mode: 0644]
src/test-network.c

index fac9e0141db3bf49aa2c69b6b8cc0d4336e16fc5..9e76955c3d07d4cfb388b34bf89270d3ece119f5 100644 (file)
@@ -327,7 +327,8 @@ TESTS = \
        src/test-as \
        src/test-network \
        src/test-country \
-       src/test-signature
+       src/test-signature \
+       src/test-address
 
 CLEANFILES += \
        testdata.db
@@ -345,7 +346,8 @@ check_PROGRAMS = \
        src/test-network \
        src/test-network-list \
        src/test-country \
-       src/test-signature
+       src/test-signature \
+       src/test-address
 
 src_test_libloc_SOURCES = \
        src/test-libloc.c
@@ -419,6 +421,15 @@ src_test_signature_CFLAGS = \
 src_test_signature_LDADD = \
        $(TESTS_LDADD)
 
+src_test_address_SOURCES = \
+       src/test-address.c
+
+src_test_address_CFLAGS = \
+       $(TESTS_CFLAGS)
+
+src_test_address_LDADD = \
+       $(TESTS_LDADD)
+
 # ------------------------------------------------------------------------------
 
 MANPAGES = \
index 3ccbdb802c639ff803c4078a5253fe024eda1a11..a0ca3cf0c61d2f366a69ba246c33b17b45b1543e 100644 (file)
@@ -5,6 +5,7 @@
 *.lo
 *.trs
 libloc.pc
+test-address
 test-as
 test-libloc
 test-database
diff --git a/src/test-address.c b/src/test-address.c
new file mode 100644 (file)
index 0000000..7012e41
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+       libloc - A library to determine the location of someone on the Internet
+
+       Copyright (C) 2022 IPFire Development Team <info@ipfire.org>
+
+       This program is free software; you can redistribute it and/or modify
+       it under the terms of the GNU General Public License as published by
+       the Free Software Foundation; either version 2 of the License, or
+       (at your option) any later version.
+
+       This program is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+*/
+
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+
+#include <libloc/libloc.h>
+#include <libloc/address.h>
+#include <libloc/private.h>
+
+static int perform_tests(struct loc_ctx* ctx, const int family) {
+       struct in6_addr address = IN6ADDR_ANY_INIT;
+       const char* e = NULL;
+       const char* s = NULL;
+
+       // Reset IP address
+       loc_address_reset(&address, family);
+
+       if (!loc_address_all_zeroes(&address)) {
+               fprintf(stderr, "IP address isn't all zeroes\n");
+               return 1;
+       }
+
+       if (loc_address_all_ones(&address)) {
+               fprintf(stderr, "IP address isn't all ones\n");
+               return 1;
+       }
+
+       switch (family) {
+               case AF_INET6:
+                       e = "::";
+                       break;
+
+               case AF_INET:
+                       e = "0.0.0.0";
+                       break;
+       }
+
+       // Convert this to a string a few times
+       for (unsigned int i = 0; i < 100; i++) {
+               s = loc_address_str(&address);
+
+               printf("Iteration %d: %s\n", i, s);
+
+               if (strcmp(s, e) != 0) {
+                       fprintf(stderr, "IP address was formatted in an invalid format: %s\n", s);
+                       return 1;
+               }
+       }
+
+       // Increment the IP address
+       loc_address_increment(&address);
+
+       switch (family) {
+               case AF_INET6:
+                       e = "::1";
+                       break;
+
+               case AF_INET:
+                       e = "0.0.0.1";
+                       break;
+       }
+
+       s = loc_address_str(&address);
+
+       printf("Incremented IP address to %s\n", s);
+
+       if (strcmp(s, e) != 0) {
+               printf("IP address has been incremented incorrectly: %s\n", s);
+               return 1;
+       }
+
+       if (loc_address_all_zeroes(&address)) {
+               printf("IP address shouldn't be all zeroes any more\n");
+               return 1;
+       }
+
+       if (loc_address_all_ones(&address)) {
+               printf("IP address shouldn't be all ones any more\n");
+               return 1;
+       }
+
+       // Decrement the IP address
+       loc_address_decrement(&address);
+
+       s = loc_address_str(&address);
+
+       printf("Incremented IP address to %s\n", s);
+
+       if (!loc_address_all_zeroes(&address)) {
+               printf("IP address hasn't been decremented correctly: %s\n",
+                       loc_address_str(&address));
+               return 1;
+       }
+
+       return 0;
+}
+
+int main(int argc, char** argv) {
+       struct loc_ctx* ctx = NULL;
+       int r = EXIT_FAILURE;
+
+       int err = loc_new(&ctx);
+       if (err < 0)
+               exit(r);
+
+       // Enable debug logging
+       loc_set_log_priority(ctx, LOG_DEBUG);
+
+       // Perform all tests for IPv6
+       r = perform_tests(ctx, AF_INET6);
+       if (r)
+               goto ERROR;
+
+       // Perform all tests for IPv4
+       r = perform_tests(ctx, AF_INET);
+       if (r)
+               goto ERROR;
+
+ERROR:
+       loc_unref(ctx);
+
+       return r;
+}
index 3230c7d6084409bd9033a491e2a5073d336b3b8d..f27a06ba19c6244e7973823252d305bbcdeb583e 100644 (file)
@@ -311,6 +311,7 @@ int main(int argc, char** argv) {
                { "::/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, },
        };