]> git.ipfire.org Git - location/libloc.git/blob - src/test-database.c
location.in: do not confuse UTC with local time zones
[location/libloc.git] / src / test-database.c
1 /*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2017 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 <stdio.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <fcntl.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <syslog.h>
26
27 #include <loc/libloc.h>
28 #include <loc/database.h>
29 #include <loc/writer.h>
30
31 const char* VENDOR = "Test Vendor";
32 const char* DESCRIPTION =
33 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
34 "Proin ultrices pulvinar dolor, et sollicitudin eros ultricies "
35 "vitae. Nam in volutpat libero. Nulla facilisi. Pellentesque "
36 "tempor felis enim. Integer congue nisi in maximus pretium. "
37 "Pellentesque et turpis elementum, luctus mi at, interdum erat. "
38 "Maecenas ut venenatis nunc.";
39 const char* LICENSE = "CC";
40
41 static int attempt_to_open(struct loc_ctx* ctx, char* path) {
42 FILE* f = fopen(path, "r");
43 if (!f)
44 return -1;
45
46 struct loc_database* db;
47 int r = loc_database_new(ctx, &db, f);
48
49 if (r == 0) {
50 fprintf(stderr, "Opening %s was unexpectedly successful\n", path);
51 loc_database_unref(db);
52
53 r = 1;
54 }
55
56 // Close the file again
57 fclose(f);
58
59 return r;
60 }
61
62 int main(int argc, char** argv) {
63 int err;
64
65 struct loc_ctx* ctx;
66 err = loc_new(&ctx);
67 if (err < 0)
68 exit(EXIT_FAILURE);
69
70 // Enable debug logging
71 loc_set_log_priority(ctx, LOG_DEBUG);
72
73 // Try opening an empty file
74 err = attempt_to_open(ctx, "/dev/null");
75 if (err == 0)
76 exit(EXIT_FAILURE);
77
78 // Try opening a file with all zeroes
79 err = attempt_to_open(ctx, "/dev/zero");
80 if (err == 0)
81 exit(EXIT_FAILURE);
82
83 // Try opening a file with random data
84 err = attempt_to_open(ctx, "/dev/urandom");
85 if (err == 0)
86 exit(EXIT_FAILURE);
87
88 // Create a database
89 struct loc_writer* writer;
90 err = loc_writer_new(ctx, &writer, NULL, NULL);
91 if (err < 0)
92 exit(EXIT_FAILURE);
93
94 // Set the vendor
95 err = loc_writer_set_vendor(writer, VENDOR);
96 if (err) {
97 fprintf(stderr, "Could not set vendor\n");
98 exit(EXIT_FAILURE);
99 }
100
101 // Retrieve vendor
102 const char* vendor = loc_writer_get_vendor(writer);
103 if (vendor) {
104 printf("Vendor is: %s\n", vendor);
105 } else {
106 fprintf(stderr, "Could not retrieve vendor\n");
107 exit(EXIT_FAILURE);
108 }
109
110 // Set a description
111 err = loc_writer_set_description(writer, DESCRIPTION);
112 if (err) {
113 fprintf(stderr, "Could not set description\n");
114 exit(EXIT_FAILURE);
115 }
116
117 // Retrieve description
118 const char* description = loc_writer_get_description(writer);
119 if (description) {
120 printf("Description is: %s\n", description);
121 } else {
122 fprintf(stderr, "Could not retrieve description\n");
123 exit(EXIT_FAILURE);
124 }
125
126 // Set a license
127 err = loc_writer_set_license(writer, LICENSE);
128 if (err) {
129 fprintf(stderr, "Could not set license\n");
130 exit(EXIT_FAILURE);
131 }
132
133 // Retrieve license
134 const char* license = loc_writer_get_license(writer);
135 if (license) {
136 printf("License is: %s\n", license);
137 } else {
138 fprintf(stderr, "Could not retrieve license\n");
139 exit(EXIT_FAILURE);
140 }
141
142 FILE* f = tmpfile();
143 if (!f) {
144 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
145 exit(EXIT_FAILURE);
146 }
147
148 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
149 if (err) {
150 fprintf(stderr, "Could not write database: %s\n", strerror(err));
151 exit(EXIT_FAILURE);
152 }
153 loc_writer_unref(writer);
154
155 // And open it again from disk
156 struct loc_database* db;
157 err = loc_database_new(ctx, &db, f);
158 if (err) {
159 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
160 exit(EXIT_FAILURE);
161 }
162
163 // Try reading something from the database
164 vendor = loc_database_get_vendor(db);
165 if (!vendor) {
166 fprintf(stderr, "Could not retrieve vendor\n");
167 exit(EXIT_FAILURE);
168 } else if (strcmp(vendor, VENDOR) != 0) {
169 fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor, VENDOR);
170 exit(EXIT_FAILURE);
171 }
172
173 // Close the database
174 loc_database_unref(db);
175 loc_unref(ctx);
176 fclose(f);
177
178 return EXIT_SUCCESS;
179 }