]> git.ipfire.org Git - location/libloc.git/blob - src/test-database.c
importer: Drop EDROP as it has been merged into DROP
[location/libloc.git] / src / test-database.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 <stdio.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <fcntl.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <syslog.h>
26
27 #include <loc/libloc.h>
28 #include <loc/database.h>
29 #include <loc/writer.h>
30
31 const char* VENDOR = "Test Vendor";
32 const char* DESCRIPTION =
33 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
34 "Proin ultrices pulvinar dolor, et sollicitudin eros ultricies "
35 "vitae. Nam in volutpat libero. Nulla facilisi. Pellentesque "
36 "tempor felis enim. Integer congue nisi in maximus pretium. "
37 "Pellentesque et turpis elementum, luctus mi at, interdum erat. "
38 "Maecenas ut venenatis nunc.";
39 const char* LICENSE = "CC";
40
41 const char* networks[] = {
42 "2001:db8::/32",
43 "2001:db8:1000::/48",
44 "2001:db8:2000::/48",
45 "2001:db8:2020::/48",
46 NULL,
47 };
48
49 static int attempt_to_open(struct loc_ctx* ctx, char* path) {
50 FILE* f = fopen(path, "r");
51 if (!f)
52 return -1;
53
54 struct loc_database* db;
55 int r = loc_database_new(ctx, &db, f);
56
57 if (r == 0) {
58 fprintf(stderr, "Opening %s was unexpectedly successful\n", path);
59 loc_database_unref(db);
60
61 r = 1;
62 }
63
64 // Close the file again
65 fclose(f);
66
67 return r;
68 }
69
70 int main(int argc, char** argv) {
71 int err;
72
73 struct loc_ctx* ctx;
74 err = loc_new(&ctx);
75 if (err < 0)
76 exit(EXIT_FAILURE);
77
78 // Enable debug logging
79 loc_set_log_priority(ctx, LOG_DEBUG);
80
81 // Try opening an empty file
82 err = attempt_to_open(ctx, "/dev/null");
83 if (err == 0)
84 exit(EXIT_FAILURE);
85
86 // Try opening a file with all zeroes
87 err = attempt_to_open(ctx, "/dev/zero");
88 if (err == 0)
89 exit(EXIT_FAILURE);
90
91 // Try opening a file with random data
92 err = attempt_to_open(ctx, "/dev/urandom");
93 if (err == 0)
94 exit(EXIT_FAILURE);
95
96 // Create a database
97 struct loc_writer* writer;
98 err = loc_writer_new(ctx, &writer, NULL, NULL);
99 if (err < 0)
100 exit(EXIT_FAILURE);
101
102 // Set the vendor
103 err = loc_writer_set_vendor(writer, VENDOR);
104 if (err) {
105 fprintf(stderr, "Could not set vendor\n");
106 exit(EXIT_FAILURE);
107 }
108
109 // Retrieve vendor
110 const char* vendor = loc_writer_get_vendor(writer);
111 if (vendor) {
112 printf("Vendor is: %s\n", vendor);
113 } else {
114 fprintf(stderr, "Could not retrieve vendor\n");
115 exit(EXIT_FAILURE);
116 }
117
118 // Set a description
119 err = loc_writer_set_description(writer, DESCRIPTION);
120 if (err) {
121 fprintf(stderr, "Could not set description\n");
122 exit(EXIT_FAILURE);
123 }
124
125 // Retrieve description
126 const char* description = loc_writer_get_description(writer);
127 if (description) {
128 printf("Description is: %s\n", description);
129 } else {
130 fprintf(stderr, "Could not retrieve description\n");
131 exit(EXIT_FAILURE);
132 }
133
134 // Set a license
135 err = loc_writer_set_license(writer, LICENSE);
136 if (err) {
137 fprintf(stderr, "Could not set license\n");
138 exit(EXIT_FAILURE);
139 }
140
141 // Retrieve license
142 const char* license = loc_writer_get_license(writer);
143 if (license) {
144 printf("License is: %s\n", license);
145 } else {
146 fprintf(stderr, "Could not retrieve license\n");
147 exit(EXIT_FAILURE);
148 }
149
150 struct loc_network* network = NULL;
151
152 // Add some networks
153 const char** n = networks;
154 while (*n) {
155 err = loc_writer_add_network(writer, &network, *n);
156 if (err) {
157 fprintf(stderr, "Could not add network %s\n", *n);
158 exit(EXIT_FAILURE);
159 }
160
161 // Set a country
162 loc_network_set_country_code(network, "XX");
163
164 // Next one
165 n++;
166 }
167
168 FILE* f = tmpfile();
169 if (!f) {
170 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
171 exit(EXIT_FAILURE);
172 }
173
174 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
175 if (err) {
176 fprintf(stderr, "Could not write database: %s\n", strerror(err));
177 exit(EXIT_FAILURE);
178 }
179 loc_writer_unref(writer);
180
181 // And open it again from disk
182 struct loc_database* db;
183 err = loc_database_new(ctx, &db, f);
184 if (err) {
185 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
186 exit(EXIT_FAILURE);
187 }
188
189 // Try reading something from the database
190 vendor = loc_database_get_vendor(db);
191 if (!vendor) {
192 fprintf(stderr, "Could not retrieve vendor\n");
193 exit(EXIT_FAILURE);
194 } else if (strcmp(vendor, VENDOR) != 0) {
195 fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor, VENDOR);
196 exit(EXIT_FAILURE);
197 }
198
199 // Enumerator
200 struct loc_database_enumerator* enumerator;
201 err = loc_database_enumerator_new(&enumerator, db, LOC_DB_ENUMERATE_NETWORKS, 0);
202 if (err) {
203 fprintf(stderr, "Could not initialise the enumerator: %d\n", err);
204 exit(EXIT_FAILURE);
205 }
206
207 // Walk through all networks
208 while (1) {
209 err = loc_database_enumerator_next_network(enumerator, &network);
210 if (err) {
211 fprintf(stderr, "Error fetching the next network: %d\n", err);
212 exit(EXIT_FAILURE);
213 }
214
215 if (!network)
216 break;
217
218 char* s = loc_network_str(network);
219 printf("Got network: %s\n", s);
220 free(s);
221 }
222
223 // Free the enumerator
224 loc_database_enumerator_unref(enumerator);
225
226 // Close the database
227 loc_database_unref(db);
228 loc_unref(ctx);
229 fclose(f);
230
231 return EXIT_SUCCESS;
232 }