]> git.ipfire.org Git - people/ms/libloc.git/blame - src/test-country.c
database: Free mmapped countries section
[people/ms/libloc.git] / src / test-country.c
CommitLineData
ec684c1a
MT
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>
37317e4f 22#include <syslog.h>
ec684c1a
MT
23
24#include <loc/libloc.h>
57146963 25#include <loc/country.h>
ec684c1a
MT
26#include <loc/database.h>
27#include <loc/network.h>
28#include <loc/writer.h>
29
30int main(int argc, char** argv) {
31 struct loc_country* country;
32 int err;
33
57146963
MT
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
ec684c1a
MT
46 struct loc_ctx* ctx;
47 err = loc_new(&ctx);
48 if (err < 0)
49 exit(EXIT_FAILURE);
50
37317e4f
MT
51 // Enable debug logging
52 loc_set_log_priority(ctx, LOG_DEBUG);
53
ec684c1a
MT
54 // Create a database
55 struct loc_writer* writer;
5ce881d4 56 err = loc_writer_new(ctx, &writer, NULL, NULL);
ec684c1a
MT
57 if (err < 0)
58 exit(EXIT_FAILURE);
59
60 // Create a country
57146963 61 err = loc_writer_add_country(writer, &country, "XX");
ec684c1a
MT
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");
57146963 69 loc_country_set_continent_code(country, "YY");
ec684c1a
MT
70
71 // Free country
72 loc_country_unref(country);
73
8bbddd9a
MT
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
6254dca6 82 FILE* f = tmpfile();
ec684c1a
MT
83 if (!f) {
84 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
85 exit(EXIT_FAILURE);
86 }
87
22c7b98b 88 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
ec684c1a
MT
89 if (err) {
90 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
91 exit(EXIT_FAILURE);
92 }
ec684c1a
MT
93 loc_writer_unref(writer);
94
95 // And open it again from disk
ec684c1a
MT
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
8bbddd9a 104 err = loc_database_get_country(db, &country, "YY");
ec684c1a 105 if (err) {
8bbddd9a 106 fprintf(stderr, "Could not find country: YY\n");
ec684c1a
MT
107 exit(EXIT_FAILURE);
108 }
109 loc_country_unref(country);
110
111 loc_database_unref(db);
112 loc_unref(ctx);
6254dca6 113 fclose(f);
ec684c1a
MT
114
115 return EXIT_SUCCESS;
116}