]> git.ipfire.org Git - people/ms/libloc.git/blob - src/test-country.c
importer: Drop EDROP as it has been merged into DROP
[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 #include <syslog.h>
23
24 #include <loc/libloc.h>
25 #include <loc/country.h>
26 #include <loc/database.h>
27 #include <loc/network.h>
28 #include <loc/writer.h>
29
30 int main(int argc, char** argv) {
31 struct loc_country* country;
32 int err;
33
34 // Check some valid country codes
35 if (!loc_country_code_is_valid("XX")) {
36 fprintf(stderr, "Valid country code detected as invalid: %s\n", "XX");
37 exit(EXIT_FAILURE);
38 }
39
40 // Check some invalid country codes
41 if (loc_country_code_is_valid("X1")) {
42 fprintf(stderr, "Invalid country code detected as valid: %s\n", "X1");
43 exit(EXIT_FAILURE);
44 }
45
46 struct loc_ctx* ctx;
47 err = loc_new(&ctx);
48 if (err < 0)
49 exit(EXIT_FAILURE);
50
51 // Enable debug logging
52 loc_set_log_priority(ctx, LOG_DEBUG);
53
54 // Create a database
55 struct loc_writer* writer;
56 err = loc_writer_new(ctx, &writer, NULL, NULL);
57 if (err < 0)
58 exit(EXIT_FAILURE);
59
60 // Create a country
61 err = loc_writer_add_country(writer, &country, "XX");
62 if (err) {
63 fprintf(stderr, "Could not create country\n");
64 exit(EXIT_FAILURE);
65 }
66
67 // Set name & continent
68 loc_country_set_name(country, "Testistan");
69 loc_country_set_continent_code(country, "YY");
70
71 // Free country
72 loc_country_unref(country);
73
74 // Add another country
75 err = loc_writer_add_country(writer, &country, "YY");
76 if (err) {
77 fprintf(stderr, "Could not create country: YY\n");
78 exit(EXIT_FAILURE);
79 }
80 loc_country_unref(country);
81
82 FILE* f = tmpfile();
83 if (!f) {
84 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
85 exit(EXIT_FAILURE);
86 }
87
88 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
89 if (err) {
90 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
91 exit(EXIT_FAILURE);
92 }
93 loc_writer_unref(writer);
94
95 // And open it again from disk
96 struct loc_database* db;
97 err = loc_database_new(ctx, &db, f);
98 if (err) {
99 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
100 exit(EXIT_FAILURE);
101 }
102
103 // Lookup an address in the subnet
104 err = loc_database_get_country(db, &country, "YY");
105 if (err) {
106 fprintf(stderr, "Could not find country: YY\n");
107 exit(EXIT_FAILURE);
108 }
109 loc_country_unref(country);
110
111 loc_database_unref(db);
112 loc_unref(ctx);
113 fclose(f);
114
115 return EXIT_SUCCESS;
116 }