]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/test-network-list.c
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
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.
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.
24 #include <loc/libloc.h>
25 #include <loc/network.h>
26 #include <loc/network-list.h>
28 int main(int argc
, char** argv
) {
36 // Enable debug logging
37 loc_set_log_priority(ctx
, LOG_DEBUG
);
40 struct loc_network
* network1
;
41 err
= loc_network_new_from_string(ctx
, &network1
, "2001:db8::/32");
43 fprintf(stderr
, "Could not create the network1\n");
47 struct loc_network
* subnet1
;
48 err
= loc_network_new_from_string(ctx
, &subnet1
, "2001:db8:a::/48");
50 fprintf(stderr
, "Could not create the subnet1\n");
54 struct loc_network
* subnet2
;
55 err
= loc_network_new_from_string(ctx
, &subnet2
, "2001:db8:b::/48");
57 fprintf(stderr
, "Could not create the subnet2\n");
61 struct loc_network
* subnet3
;
62 err
= loc_network_new_from_string(ctx
, &subnet3
, "2001:db8:c::/48");
64 fprintf(stderr
, "Could not create the subnet3\n");
68 struct loc_network
* subnet4
;
69 err
= loc_network_new_from_string(ctx
, &subnet4
, "2001:db8:d::/48");
71 fprintf(stderr
, "Could not create the subnet4\n");
75 struct loc_network
* subnet5
;
76 err
= loc_network_new_from_string(ctx
, &subnet5
, "2001:db8:e::/48");
78 fprintf(stderr
, "Could not create the subnet5\n");
82 struct loc_network
* subnet6
;
83 err
= loc_network_new_from_string(ctx
, &subnet6
, "2001:db8:1::/48");
85 fprintf(stderr
, "Could not create the subnet6\n");
89 // Make a list with both subnets
90 struct loc_network_list
* subnets
;
91 err
= loc_network_list_new(ctx
, &subnets
);
93 fprintf(stderr
, "Could not create subnets list\n");
97 size_t size
= loc_network_list_size(subnets
);
99 fprintf(stderr
, "The list is not empty: %zu\n", size
);
103 err
= loc_network_list_push(subnets
, subnet1
);
105 fprintf(stderr
, "Could not add subnet1 to subnets list\n");
109 if (loc_network_list_empty(subnets
)) {
110 fprintf(stderr
, "The subnets list reports that it is empty\n");
114 err
= loc_network_list_push(subnets
, subnet2
);
116 fprintf(stderr
, "Could not add subnet2 to subnets list\n");
120 // Add the fourth one next
121 err
= loc_network_list_push(subnets
, subnet4
);
123 fprintf(stderr
, "Could not add subnet4 to subnets list\n");
128 err
= loc_network_list_push(subnets
, subnet3
);
130 fprintf(stderr
, "Could not add subnet3 to subnets list\n");
135 err
= loc_network_list_push(subnets
, subnet5
);
137 fprintf(stderr
, "Could not add subnet5 to subnets list\n");
141 err
= loc_network_list_push(subnets
, subnet6
);
143 fprintf(stderr
, "Could not add subnet6 to subnets list\n");
147 loc_network_list_dump(subnets
);
149 size
= loc_network_list_size(subnets
);
151 fprintf(stderr
, "Network list is reporting an incorrect size: %zu\n", size
);
155 // Exclude subnet1 from network1
156 struct loc_network_list
* excluded
= loc_network_exclude(network1
, subnet1
);
158 fprintf(stderr
, "Received an empty result from loc_network_exclude() for subnet1\n");
162 loc_network_list_dump(excluded
);
164 // Exclude all subnets from network1
165 excluded
= loc_network_exclude_list(network1
, subnets
);
167 fprintf(stderr
, "Received an empty result from loc_network_exclude() for subnets\n");
171 loc_network_list_dump(excluded
);
174 loc_network_list_unref(excluded
);
176 loc_network_list_unref(subnets
);
177 loc_network_unref(network1
);
178 loc_network_unref(subnet1
);
179 loc_network_unref(subnet2
);