]> git.ipfire.org Git - people/ms/libloc.git/blame - src/test-country.c
downloader: Change user-agent to location
[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>
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;
5ce881d4 52 err = loc_writer_new(ctx, &writer, NULL, NULL);
ec684c1a
MT
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
8bbddd9a
MT
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
6254dca6 78 FILE* f = tmpfile();
ec684c1a
MT
79 if (!f) {
80 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
81 exit(EXIT_FAILURE);
82 }
83
22c7b98b 84 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
ec684c1a
MT
85 if (err) {
86 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
87 exit(EXIT_FAILURE);
88 }
ec684c1a
MT
89 loc_writer_unref(writer);
90
91 // And open it again from disk
ec684c1a
MT
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
8bbddd9a 100 err = loc_database_get_country(db, &country, "YY");
ec684c1a 101 if (err) {
8bbddd9a 102 fprintf(stderr, "Could not find country: YY\n");
ec684c1a
MT
103 exit(EXIT_FAILURE);
104 }
105 loc_country_unref(country);
106
107 loc_database_unref(db);
108 loc_unref(ctx);
6254dca6 109 fclose(f);
ec684c1a
MT
110
111 return EXIT_SUCCESS;
112}