]> git.ipfire.org Git - location/libloc.git/commitdiff
test: Add tests to network-lists
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 20 Nov 2020 14:43:31 +0000 (14:43 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 20 Nov 2020 14:43:31 +0000 (14:43 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/.gitignore
src/test-network-list.c [new file with mode: 0644]

index d0cc793948d78b93cbbe6eb9f19420866cc2eab0..ebd7e1771c896d479c9a1be0439e94803d86190c 100644 (file)
@@ -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
 
index caf80b5c3719378132387c7c314381d8f00c82f7..3ccbdb802c639ff803c4078a5253fe024eda1a11 100644 (file)
@@ -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 (file)
index 0000000..3061d63
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+       libloc - A library to determine the location of someone on the Internet
+
+       Copyright (C) 2017 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 <errno.h>
+#include <stdio.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+
+#include <loc/libloc.h>
+#include <loc/network.h>
+#include <loc/network-list.h>
+
+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;
+}