From a17c353bce55a9f38f67b4b7d2425194cfa208e7 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 20 Nov 2020 14:43:31 +0000 Subject: [PATCH] test: Add tests to network-lists Signed-off-by: Michael Tremer --- Makefile.am | 10 ++++ src/.gitignore | 1 + src/test-network-list.c | 126 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 src/test-network-list.c diff --git a/Makefile.am b/Makefile.am index d0cc793..ebd7e17 100644 --- a/Makefile.am +++ b/Makefile.am @@ -318,6 +318,7 @@ check_PROGRAMS = \ src/test-database \ src/test-as \ src/test-network \ + src/test-network-list \ src/test-country \ src/test-signature @@ -357,6 +358,15 @@ src_test_network_CFLAGS = \ src_test_network_LDADD = \ src/libloc.la +src_test_network_list_SOURCES = \ + src/test-network-list.c + +src_test_network_list_CFLAGS = \ + $(TESTS_CFLAGS) + +src_test_network_list_LDADD = \ + src/libloc.la + src_test_stringpool_SOURCES = \ src/test-stringpool.c diff --git a/src/.gitignore b/src/.gitignore index caf80b5..3ccbdb8 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -10,5 +10,6 @@ test-libloc test-database test-country test-network +test-network-list test-signature test-stringpool diff --git a/src/test-network-list.c b/src/test-network-list.c new file mode 100644 index 0000000..3061d63 --- /dev/null +++ b/src/test-network-list.c @@ -0,0 +1,126 @@ +/* + libloc - A library to determine the location of someone on the Internet + + Copyright (C) 2017 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 + +#include +#include +#include + +int main(int argc, char** argv) { + int err; + + struct loc_ctx* ctx; + err = loc_new(&ctx); + if (err < 0) + exit(EXIT_FAILURE); + + // Enable debug logging + loc_set_log_priority(ctx, LOG_DEBUG); + + // Create a network + struct loc_network* network1; + err = loc_network_new_from_string(ctx, &network1, "2001:db8::/32"); + if (err) { + fprintf(stderr, "Could not create the network1\n"); + exit(EXIT_FAILURE); + } + + struct loc_network* subnet1; + err = loc_network_new_from_string(ctx, &subnet1, "2001:db8:a::/48"); + if (err) { + fprintf(stderr, "Could not create the subnet1\n"); + exit(EXIT_FAILURE); + } + + struct loc_network* subnet2; + err = loc_network_new_from_string(ctx, &subnet2, "2001:db8:b::/48"); + if (err) { + fprintf(stderr, "Could not create the subnet2\n"); + exit(EXIT_FAILURE); + } + + // Make a list with both subnets + struct loc_network_list* subnets; + err = loc_network_list_new(ctx, &subnets); + if (err) { + fprintf(stderr, "Could not create subnets list\n"); + exit(EXIT_FAILURE); + } + + size_t size = loc_network_list_size(subnets); + if (size > 0) { + fprintf(stderr, "The list is not empty: %zu\n", size); + exit(EXIT_FAILURE); + } + + err = loc_network_list_push(subnets, subnet1); + if (err) { + fprintf(stderr, "Could not add subnet1 to subnets list\n"); + exit(EXIT_FAILURE); + } + + if (loc_network_list_empty(subnets)) { + fprintf(stderr, "The subnets list reports that it is empty\n"); + exit(EXIT_FAILURE); + } + + err = loc_network_list_push(subnets, subnet2); + if (err) { + fprintf(stderr, "Could not add subnet2 to subnets list\n"); + exit(EXIT_FAILURE); + } + + size = loc_network_list_size(subnets); + if (size != 2) { + fprintf(stderr, "Network list is reporting an incorrect size: %zu\n", size); + exit(EXIT_FAILURE); + } + + // Exclude subnet1 from network1 + struct loc_network_list* excluded = loc_network_exclude(network1, subnet1); + if (!excluded) { + fprintf(stderr, "Received an empty result from loc_network_exclude() for subnet1\n"); + exit(EXIT_FAILURE); + } + + loc_network_list_dump(excluded); + + // Exclude all subnets from network1 + excluded = loc_network_exclude_list(network1, subnets); + if (!excluded) { + fprintf(stderr, "Received an empty result from loc_network_exclude() for subnets\n"); + exit(EXIT_FAILURE); + } + + loc_network_list_dump(excluded); + + if (excluded) + loc_network_list_unref(excluded); + + loc_network_list_unref(subnets); + loc_network_unref(network1); + loc_network_unref(subnet1); + loc_network_unref(subnet2); + loc_unref(ctx); + + return EXIT_SUCCESS; +} -- 2.39.2