]> git.ipfire.org Git - location/libloc.git/blob - src/test-network-list.c
3061d63e67e6c0b22ab323dbe504c825e6ca7c3d
[location/libloc.git] / src / test-network-list.c
1 /*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 */
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <syslog.h>
23
24 #include <loc/libloc.h>
25 #include <loc/network.h>
26 #include <loc/network-list.h>
27
28 int main(int argc, char** argv) {
29 int err;
30
31 struct loc_ctx* ctx;
32 err = loc_new(&ctx);
33 if (err < 0)
34 exit(EXIT_FAILURE);
35
36 // Enable debug logging
37 loc_set_log_priority(ctx, LOG_DEBUG);
38
39 // Create a network
40 struct loc_network* network1;
41 err = loc_network_new_from_string(ctx, &network1, "2001:db8::/32");
42 if (err) {
43 fprintf(stderr, "Could not create the network1\n");
44 exit(EXIT_FAILURE);
45 }
46
47 struct loc_network* subnet1;
48 err = loc_network_new_from_string(ctx, &subnet1, "2001:db8:a::/48");
49 if (err) {
50 fprintf(stderr, "Could not create the subnet1\n");
51 exit(EXIT_FAILURE);
52 }
53
54 struct loc_network* subnet2;
55 err = loc_network_new_from_string(ctx, &subnet2, "2001:db8:b::/48");
56 if (err) {
57 fprintf(stderr, "Could not create the subnet2\n");
58 exit(EXIT_FAILURE);
59 }
60
61 // Make a list with both subnets
62 struct loc_network_list* subnets;
63 err = loc_network_list_new(ctx, &subnets);
64 if (err) {
65 fprintf(stderr, "Could not create subnets list\n");
66 exit(EXIT_FAILURE);
67 }
68
69 size_t size = loc_network_list_size(subnets);
70 if (size > 0) {
71 fprintf(stderr, "The list is not empty: %zu\n", size);
72 exit(EXIT_FAILURE);
73 }
74
75 err = loc_network_list_push(subnets, subnet1);
76 if (err) {
77 fprintf(stderr, "Could not add subnet1 to subnets list\n");
78 exit(EXIT_FAILURE);
79 }
80
81 if (loc_network_list_empty(subnets)) {
82 fprintf(stderr, "The subnets list reports that it is empty\n");
83 exit(EXIT_FAILURE);
84 }
85
86 err = loc_network_list_push(subnets, subnet2);
87 if (err) {
88 fprintf(stderr, "Could not add subnet2 to subnets list\n");
89 exit(EXIT_FAILURE);
90 }
91
92 size = loc_network_list_size(subnets);
93 if (size != 2) {
94 fprintf(stderr, "Network list is reporting an incorrect size: %zu\n", size);
95 exit(EXIT_FAILURE);
96 }
97
98 // Exclude subnet1 from network1
99 struct loc_network_list* excluded = loc_network_exclude(network1, subnet1);
100 if (!excluded) {
101 fprintf(stderr, "Received an empty result from loc_network_exclude() for subnet1\n");
102 exit(EXIT_FAILURE);
103 }
104
105 loc_network_list_dump(excluded);
106
107 // Exclude all subnets from network1
108 excluded = loc_network_exclude_list(network1, subnets);
109 if (!excluded) {
110 fprintf(stderr, "Received an empty result from loc_network_exclude() for subnets\n");
111 exit(EXIT_FAILURE);
112 }
113
114 loc_network_list_dump(excluded);
115
116 if (excluded)
117 loc_network_list_unref(excluded);
118
119 loc_network_list_unref(subnets);
120 loc_network_unref(network1);
121 loc_network_unref(subnet1);
122 loc_network_unref(subnet2);
123 loc_unref(ctx);
124
125 return EXIT_SUCCESS;
126 }