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