]> git.ipfire.org Git - location/libloc.git/blame - src/test-country.c
country: Add simple function to test if a country code is valid
[location/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>
22
23#include <loc/libloc.h>
57146963 24#include <loc/country.h>
ec684c1a
MT
25#include <loc/database.h>
26#include <loc/network.h>
27#include <loc/writer.h>
28
29int main(int argc, char** argv) {
30 struct loc_country* country;
31 int err;
32
57146963
MT
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
ec684c1a
MT
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);
53 if (err < 0)
54 exit(EXIT_FAILURE);
55
56 // Create a country
57146963 57 err = loc_writer_add_country(writer, &country, "XX");
ec684c1a
MT
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");
57146963 65 loc_country_set_continent_code(country, "YY");
ec684c1a
MT
66
67 // Free country
68 loc_country_unref(country);
69
70 FILE* f = fopen("test.db", "w");
71 if (!f) {
72 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
73 exit(EXIT_FAILURE);
74 }
75
76 err = loc_writer_write(writer, f);
77 if (err) {
78 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
79 exit(EXIT_FAILURE);
80 }
81 fclose(f);
82
83 loc_writer_unref(writer);
84
85 // And open it again from disk
86 f = fopen("test.db", "r");
87 if (!f) {
88 fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
89 exit(EXIT_FAILURE);
90 }
91
92 struct loc_database* db;
93 err = loc_database_new(ctx, &db, f);
94 if (err) {
95 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
96 exit(EXIT_FAILURE);
97 }
98
99 // Lookup an address in the subnet
57146963 100 err = loc_database_get_country(db, &country, "XX");
ec684c1a 101 if (err) {
57146963 102 fprintf(stderr, "Could not find country XX\n");
ec684c1a
MT
103 exit(EXIT_FAILURE);
104 }
105 loc_country_unref(country);
106
107 loc_database_unref(db);
108 loc_unref(ctx);
109
110 return EXIT_SUCCESS;
111}