]> git.ipfire.org Git - people/ms/libloc.git/blob - src/test-network.c
test: Fix network test for non-existant address
[people/ms/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
23 #include <loc/libloc.h>
24 #include <loc/database.h>
25 #include <loc/network.h>
26 #include <loc/writer.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 struct loc_network_tree* tree;
37 err = loc_network_tree_new(ctx, &tree);
38 if (err) {
39 fprintf(stderr, "Could not create the network tree\n");
40 exit(EXIT_FAILURE);
41 }
42
43 // Create a network
44 struct loc_network* network1;
45 err = loc_network_new_from_string(ctx, &network1, "2001:db8::/32");
46 if (err) {
47 fprintf(stderr, "Could not create the network\n");
48 exit(EXIT_FAILURE);
49 }
50
51 err = loc_network_set_country_code(network1, "XX");
52 if (err) {
53 fprintf(stderr, "Could not set country code\n");
54 exit(EXIT_FAILURE);
55 }
56
57 // Adding network to the tree
58 err = loc_network_tree_add_network(tree, network1);
59 if (err) {
60 fprintf(stderr, "Could not add network to the tree\n");
61 exit(EXIT_FAILURE);
62 }
63
64 struct loc_network* network2;
65 err = loc_network_new_from_string(ctx, &network2, "2001:db8:ffff::/48");
66 if (err) {
67 fprintf(stderr, "Could not create the network\n");
68 exit(EXIT_FAILURE);
69 }
70
71 err = loc_network_set_country_code(network2, "XY");
72 if (err) {
73 fprintf(stderr, "Could not set country code\n");
74 exit(EXIT_FAILURE);
75 }
76
77 // Adding network to the tree
78 err = loc_network_tree_add_network(tree, network2);
79 if (err) {
80 fprintf(stderr, "Could not add network to the tree\n");
81 exit(EXIT_FAILURE);
82 }
83
84 // Dump the tree
85 err = loc_network_tree_dump(tree);
86 if (err) {
87 fprintf(stderr, "Error dumping tree: %d\n", err);
88 exit(EXIT_FAILURE);
89 }
90
91 size_t nodes = loc_network_tree_count_nodes(tree);
92 printf("The tree has %zu nodes\n", nodes);
93
94 // Create a database
95 struct loc_writer* writer;
96 err = loc_writer_new(ctx, &writer);
97 if (err < 0)
98 exit(EXIT_FAILURE);
99
100 struct loc_network* network3;
101 err = loc_writer_add_network(writer, &network3, "2001:db8::/64");
102 if (err) {
103 fprintf(stderr, "Could not add network\n");
104 exit(EXIT_FAILURE);
105 }
106
107 // Set country code
108 loc_network_set_country_code(network3, "XX");
109
110 struct loc_network* network4;
111 err = loc_writer_add_network(writer, &network4, "2001:db8:ffff::/64");
112 if (err) {
113 fprintf(stderr, "Could not add network\n");
114 exit(EXIT_FAILURE);
115 }
116
117 // Set country code
118 loc_network_set_country_code(network4, "XY");
119
120 // Set ASN
121 loc_network_set_asn(network4, 1024);
122
123 FILE* f = fopen("test.db", "w");
124 if (!f) {
125 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
126 exit(EXIT_FAILURE);
127 }
128
129 err = loc_writer_write(writer, f);
130 if (err) {
131 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
132 exit(EXIT_FAILURE);
133 }
134 fclose(f);
135
136 loc_writer_unref(writer);
137
138 loc_network_unref(network1);
139 loc_network_unref(network2);
140 loc_network_unref(network3);
141 loc_network_unref(network4);
142 loc_network_tree_unref(tree);
143
144 // And open it again from disk
145 f = fopen("test.db", "r");
146 if (!f) {
147 fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
148 exit(EXIT_FAILURE);
149 }
150
151 struct loc_database* db;
152 err = loc_database_new(ctx, &db, f);
153 if (err) {
154 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
155 exit(EXIT_FAILURE);
156 }
157
158 // Lookup an address in the subnet
159 err = loc_database_lookup_from_string(db, "2001:db8::", &network1);
160 if (err) {
161 fprintf(stderr, "Could not look up 2001:db8::\n");
162 exit(EXIT_FAILURE);
163 }
164 loc_network_unref(network1);
165
166 // Lookup an address outside the subnet
167 err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1);
168 if (err == 0) {
169 fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
170 exit(EXIT_FAILURE);
171 }
172 loc_network_unref(network1);
173
174 loc_unref(ctx);
175
176 return EXIT_SUCCESS;
177 }