]> git.ipfire.org Git - location/libloc.git/blame - src/test-database.c
importer: Drop EDROP as it has been merged into DROP
[location/libloc.git] / src / test-database.c
CommitLineData
2601e83e
MT
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>
37317e4f 25#include <syslog.h>
2601e83e 26
c12a1385
MT
27#include <libloc/libloc.h>
28#include <libloc/database.h>
29#include <libloc/writer.h>
2601e83e 30
64ce6e2f 31const char* VENDOR = "Test Vendor";
2601e83e
MT
32const 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.";
4bf49d00 39const char* LICENSE = "CC";
2601e83e 40
e96704f4
MT
41const 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
fccd8798
MT
49static 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
2601e83e
MT
70int 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
37317e4f
MT
78 // Enable debug logging
79 loc_set_log_priority(ctx, LOG_DEBUG);
80
fccd8798
MT
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
2601e83e 96 // Create a database
c182393f 97 struct loc_writer* writer;
5ce881d4 98 err = loc_writer_new(ctx, &writer, NULL, NULL);
2601e83e
MT
99 if (err < 0)
100 exit(EXIT_FAILURE);
101
102 // Set the vendor
64ce6e2f 103 err = loc_writer_set_vendor(writer, VENDOR);
2601e83e
MT
104 if (err) {
105 fprintf(stderr, "Could not set vendor\n");
106 exit(EXIT_FAILURE);
107 }
108
109 // Retrieve vendor
64ce6e2f
MT
110 const char* vendor = loc_writer_get_vendor(writer);
111 if (vendor) {
112 printf("Vendor is: %s\n", vendor);
2601e83e
MT
113 } else {
114 fprintf(stderr, "Could not retrieve vendor\n");
115 exit(EXIT_FAILURE);
116 }
117
118 // Set a description
c182393f 119 err = loc_writer_set_description(writer, DESCRIPTION);
2601e83e
MT
120 if (err) {
121 fprintf(stderr, "Could not set description\n");
122 exit(EXIT_FAILURE);
123 }
124
125 // Retrieve description
c182393f 126 const char* description = loc_writer_get_description(writer);
2601e83e
MT
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
4bf49d00
MT
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
e96704f4
MT
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
6254dca6 168 FILE* f = tmpfile();
2601e83e 169 if (!f) {
6cd02f1d 170 fprintf(stderr, "Could not open file for writing: %m\n");
2601e83e
MT
171 exit(EXIT_FAILURE);
172 }
173
22c7b98b 174 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
2601e83e 175 if (err) {
198e382c 176 fprintf(stderr, "Could not write database: %m\n");
2601e83e
MT
177 exit(EXIT_FAILURE);
178 }
64ce6e2f 179 loc_writer_unref(writer);
2601e83e 180
2601e83e 181 // And open it again from disk
c182393f
MT
182 struct loc_database* db;
183 err = loc_database_new(ctx, &db, f);
2601e83e 184 if (err) {
198e382c 185 fprintf(stderr, "Could not open database: %m\n");
2601e83e
MT
186 exit(EXIT_FAILURE);
187 }
188
726f9984 189 // Try reading something from the database
64ce6e2f
MT
190 vendor = loc_database_get_vendor(db);
191 if (!vendor) {
2601e83e
MT
192 fprintf(stderr, "Could not retrieve vendor\n");
193 exit(EXIT_FAILURE);
64ce6e2f
MT
194 } else if (strcmp(vendor, VENDOR) != 0) {
195 fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor, VENDOR);
2601e83e
MT
196 exit(EXIT_FAILURE);
197 }
198
e96704f4
MT
199 // Enumerator
200 struct loc_database_enumerator* enumerator;
d3ae93c2 201 err = loc_database_enumerator_new(&enumerator, db, LOC_DB_ENUMERATE_NETWORKS, 0);
e96704f4
MT
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
0a0a289a 218 const char* s = loc_network_str(network);
e96704f4 219 printf("Got network: %s\n", s);
e96704f4
MT
220 }
221
222 // Free the enumerator
223 loc_database_enumerator_unref(enumerator);
224
2601e83e 225 // Close the database
c182393f 226 loc_database_unref(db);
2601e83e 227 loc_unref(ctx);
6254dca6 228 fclose(f);
2601e83e
MT
229
230 return EXIT_SUCCESS;
231}