]> git.ipfire.org Git - location/libloc.git/blob - src/test-network.c
database: Do not clean up python list
[location/libloc.git] / src / test-network.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/database.h>
26 #include <loc/network.h>
27 #include <loc/writer.h>
28
29 int main(int argc, char** argv) {
30 int err;
31
32 struct loc_ctx* ctx;
33 err = loc_new(&ctx);
34 if (err < 0)
35 exit(EXIT_FAILURE);
36
37 // Enable debug logging
38 loc_set_log_priority(ctx, LOG_DEBUG);
39
40 #if 0
41 struct loc_network_tree* tree;
42 err = loc_network_tree_new(ctx, &tree);
43 if (err) {
44 fprintf(stderr, "Could not create the network tree\n");
45 exit(EXIT_FAILURE);
46 }
47 #endif
48
49 // Create a network
50 struct loc_network* network1;
51 err = loc_network_new_from_string(ctx, &network1, "2001:db8::1/32");
52 if (err) {
53 fprintf(stderr, "Could not create the network\n");
54 exit(EXIT_FAILURE);
55 }
56
57 err = loc_network_set_country_code(network1, "XX");
58 if (err) {
59 fprintf(stderr, "Could not set country code\n");
60 exit(EXIT_FAILURE);
61 }
62
63 #if 0
64 // Adding network to the tree
65 err = loc_network_tree_add_network(tree, network1);
66 if (err) {
67 fprintf(stderr, "Could not add network to the tree\n");
68 exit(EXIT_FAILURE);
69 }
70 #endif
71
72 // Check if the first and last addresses are correct
73 char* string = loc_network_format_first_address(network1);
74 if (!string) {
75 fprintf(stderr, "Did get NULL instead of a string for the first address\n");
76 exit(EXIT_FAILURE);
77 }
78
79 if (strcmp(string, "2001:db8::") != 0) {
80 fprintf(stderr, "Got an incorrect first address: %s\n", string);
81 exit(EXIT_FAILURE);
82 }
83
84 string = loc_network_format_last_address(network1);
85 if (!string) {
86 fprintf(stderr, "Did get NULL instead of a string for the last address\n");
87 exit(EXIT_FAILURE);
88 }
89
90 if (strcmp(string, "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff") != 0) {
91 fprintf(stderr, "Got an incorrect last address: %s\n", string);
92 exit(EXIT_FAILURE);
93 }
94
95 struct loc_network* network2;
96 err = loc_network_new_from_string(ctx, &network2, "2001:db8:ffff::/48");
97 if (err) {
98 fprintf(stderr, "Could not create the network\n");
99 exit(EXIT_FAILURE);
100 }
101
102 err = loc_network_set_country_code(network2, "XY");
103 if (err) {
104 fprintf(stderr, "Could not set country code\n");
105 exit(EXIT_FAILURE);
106 }
107
108 #if 0
109 // Adding network to the tree
110 err = loc_network_tree_add_network(tree, network2);
111 if (err) {
112 fprintf(stderr, "Could not add network to the tree\n");
113 exit(EXIT_FAILURE);
114 }
115
116 // Dump the tree
117 err = loc_network_tree_dump(tree);
118 if (err) {
119 fprintf(stderr, "Error dumping tree: %d\n", err);
120 exit(EXIT_FAILURE);
121 }
122
123 size_t nodes = loc_network_tree_count_nodes(tree);
124 printf("The tree has %zu nodes\n", nodes);
125 #endif
126
127 // Check equals function
128 err = loc_network_eq(network1, network1);
129 if (!err) {
130 fprintf(stderr, "Network is not equal with itself\n");
131 exit(EXIT_FAILURE);
132 }
133
134 err = loc_network_eq(network1, network2);
135 if (err) {
136 fprintf(stderr, "Networks equal unexpectedly\n");
137 exit(EXIT_FAILURE);
138 }
139
140 // Check subnet function
141 err = loc_network_is_subnet_of(network1, network2);
142 if (err != 0) {
143 fprintf(stderr, "Subnet check 1 failed: %d\n", err);
144 exit(EXIT_FAILURE);
145 }
146
147 err = loc_network_is_subnet_of(network2, network1);
148 if (err != 1) {
149 fprintf(stderr, "Subnet check 2 failed: %d\n", err);
150 exit(EXIT_FAILURE);
151 }
152
153 // Make list of subnets
154 struct loc_network_list* subnets = loc_network_subnets(network1);
155 if (!subnets) {
156 fprintf(stderr, "Could not find subnets of network\n");
157 exit(EXIT_FAILURE);
158 }
159
160 loc_network_list_dump(subnets);
161
162 while (!loc_network_list_empty(subnets)) {
163 struct loc_network* subnet = loc_network_list_pop(subnets);
164 if (!subnet) {
165 fprintf(stderr, "Received an empty subnet\n");
166 exit(EXIT_FAILURE);
167 }
168
169 char* s = loc_network_str(subnet);
170 printf("Received subnet %s\n", s);
171 free(s);
172
173 if (!loc_network_is_subnet_of(subnet, network1)) {
174 fprintf(stderr, "Not a subnet\n");
175 exit(EXIT_FAILURE);
176 }
177
178 loc_network_unref(subnet);
179 }
180
181 loc_network_list_unref(subnets);
182
183 struct loc_network_list* excluded = loc_network_exclude(network1, network2);
184 if (!excluded) {
185 fprintf(stderr, "Could not create excluded list\n");
186 exit(EXIT_FAILURE);
187 }
188
189 loc_network_list_dump(excluded);
190
191 // Reverse it
192 loc_network_list_reverse(excluded);
193 loc_network_list_dump(excluded);
194
195 // Sort them and dump them again
196 loc_network_list_sort(excluded);
197 loc_network_list_dump(excluded);
198
199 loc_network_list_unref(excluded);
200
201 // Create a database
202 struct loc_writer* writer;
203 err = loc_writer_new(ctx, &writer, NULL, NULL);
204 if (err < 0)
205 exit(EXIT_FAILURE);
206
207 struct loc_network* network3;
208 err = loc_writer_add_network(writer, &network3, "2001:db8::/64");
209 if (err) {
210 fprintf(stderr, "Could not add network\n");
211 exit(EXIT_FAILURE);
212 }
213
214 // Set country code
215 loc_network_set_country_code(network3, "XX");
216
217 struct loc_network* network4;
218 err = loc_writer_add_network(writer, &network4, "2001:db8:ffff::/64");
219 if (err) {
220 fprintf(stderr, "Could not add network\n");
221 exit(EXIT_FAILURE);
222 }
223
224 // Set country code
225 loc_network_set_country_code(network4, "XY");
226
227 // Set ASN
228 loc_network_set_asn(network4, 1024);
229
230 // Try adding an invalid network
231 struct loc_network* network;
232 err = loc_writer_add_network(writer, &network, "xxxx:xxxx::/32");
233 if (err != -EINVAL) {
234 fprintf(stderr, "It was possible to add an invalid network (err = %d)\n", err);
235 exit(EXIT_FAILURE);
236 }
237
238 // Try adding a single address
239 err = loc_writer_add_network(writer, &network, "2001:db8::");
240 if (err) {
241 fprintf(stderr, "It was impossible to add an single IP address (err = %d)\n", err);
242 exit(EXIT_FAILURE);
243 }
244
245 // Try adding localhost
246 err = loc_writer_add_network(writer, &network, "::1/128");
247 if (err != -EINVAL) {
248 fprintf(stderr, "It was possible to add localhost (::1/128): %d\n", err);
249 exit(EXIT_FAILURE);
250 }
251
252 FILE* f = tmpfile();
253 if (!f) {
254 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
255 exit(EXIT_FAILURE);
256 }
257
258 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
259 if (err) {
260 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
261 exit(EXIT_FAILURE);
262 }
263 loc_writer_unref(writer);
264
265 loc_network_unref(network1);
266 loc_network_unref(network2);
267 loc_network_unref(network3);
268 loc_network_unref(network4);
269
270 #if 0
271 loc_network_tree_unref(tree);
272 #endif
273
274 // And open it again from disk
275 struct loc_database* db;
276 err = loc_database_new(ctx, &db, f);
277 if (err) {
278 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
279 exit(EXIT_FAILURE);
280 }
281
282 // Lookup an address in the subnet
283 err = loc_database_lookup_from_string(db, "2001:db8::", &network1);
284 if (err) {
285 fprintf(stderr, "Could not look up 2001:db8::\n");
286 exit(EXIT_FAILURE);
287 }
288 loc_network_unref(network1);
289
290 // Lookup an address outside the subnet
291 err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1);
292 if (err == 0) {
293 fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
294 exit(EXIT_FAILURE);
295 }
296 loc_network_unref(network1);
297
298 loc_unref(ctx);
299 fclose(f);
300
301 return EXIT_SUCCESS;
302 }