]> git.ipfire.org Git - people/ms/libloc.git/blob - src/test-country.c
Implement signing/verifying databases
[people/ms/libloc.git] / src / test-country.c
1 /*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2019 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/country.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 struct loc_country* country;
31 int err;
32
33 // Check some valid country codes
34 if (!loc_country_code_is_valid("XX")) {
35 fprintf(stderr, "Valid country code detected as invalid: %s\n", "XX");
36 exit(EXIT_FAILURE);
37 }
38
39 // Check some invalid country codes
40 if (loc_country_code_is_valid("X1")) {
41 fprintf(stderr, "Invalid country code detected as valid: %s\n", "X1");
42 exit(EXIT_FAILURE);
43 }
44
45 struct loc_ctx* ctx;
46 err = loc_new(&ctx);
47 if (err < 0)
48 exit(EXIT_FAILURE);
49
50 // Create a database
51 struct loc_writer* writer;
52 err = loc_writer_new(ctx, &writer, NULL);
53 if (err < 0)
54 exit(EXIT_FAILURE);
55
56 // Create a country
57 err = loc_writer_add_country(writer, &country, "XX");
58 if (err) {
59 fprintf(stderr, "Could not create country\n");
60 exit(EXIT_FAILURE);
61 }
62
63 // Set name & continent
64 loc_country_set_name(country, "Testistan");
65 loc_country_set_continent_code(country, "YY");
66
67 // Free country
68 loc_country_unref(country);
69
70 // Add another country
71 err = loc_writer_add_country(writer, &country, "YY");
72 if (err) {
73 fprintf(stderr, "Could not create country: YY\n");
74 exit(EXIT_FAILURE);
75 }
76 loc_country_unref(country);
77
78 FILE* f = fopen("test.db", "w+");
79 if (!f) {
80 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
81 exit(EXIT_FAILURE);
82 }
83
84 err = loc_writer_write(writer, f);
85 if (err) {
86 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
87 exit(EXIT_FAILURE);
88 }
89 fclose(f);
90
91 loc_writer_unref(writer);
92
93 // And open it again from disk
94 f = fopen("test.db", "r");
95 if (!f) {
96 fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
97 exit(EXIT_FAILURE);
98 }
99
100 struct loc_database* db;
101 err = loc_database_new(ctx, &db, f);
102 if (err) {
103 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
104 exit(EXIT_FAILURE);
105 }
106
107 // Lookup an address in the subnet
108 err = loc_database_get_country(db, &country, "YY");
109 if (err) {
110 fprintf(stderr, "Could not find country: YY\n");
111 exit(EXIT_FAILURE);
112 }
113 loc_country_unref(country);
114
115 loc_database_unref(db);
116 loc_unref(ctx);
117
118 return EXIT_SUCCESS;
119 }