From: Michael Tremer Date: Mon, 7 Mar 2022 16:05:28 +0000 (+0000) Subject: address: Add some simple tests X-Git-Tag: 0.9.12~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=49da4308f6592f315a8a2957ebc91250fa338dad;p=people%2Fms%2Flibloc.git address: Add some simple tests Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index fac9e01..9e76955 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 = \ diff --git a/src/.gitignore b/src/.gitignore index 3ccbdb8..a0ca3cf 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -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 index 0000000..7012e41 --- /dev/null +++ b/src/test-address.c @@ -0,0 +1,138 @@ +/* + libloc - A library to determine the location of someone on the Internet + + Copyright (C) 2022 IPFire Development Team + + 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 +#include +#include + +#include +#include +#include + +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; +} diff --git a/src/test-network.c b/src/test-network.c index 3230c7d..f27a06b 100644 --- a/src/test-network.c +++ b/src/test-network.c @@ -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, }, };