2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
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.
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.
26 #include <loc/libloc.h>
27 #include <loc/database.h>
28 #include <loc/writer.h>
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";
40 static int attempt_to_open(struct loc_ctx
* ctx
, char* path
) {
41 FILE* f
= fopen(path
, "r");
45 struct loc_database
* db
;
46 int r
= loc_database_new(ctx
, &db
, f
);
49 fprintf(stderr
, "Opening %s was unexpectedly successful\n", path
);
50 loc_database_unref(db
);
55 // Close the file again
61 int main(int argc
, char** argv
) {
69 // Try opening an empty file
70 err
= attempt_to_open(ctx
, "/dev/null");
74 // Try opening a file with all zeroes
75 err
= attempt_to_open(ctx
, "/dev/zero");
79 // Try opening a file with random data
80 err
= attempt_to_open(ctx
, "/dev/urandom");
85 struct loc_writer
* writer
;
86 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
91 err
= loc_writer_set_vendor(writer
, VENDOR
);
93 fprintf(stderr
, "Could not set vendor\n");
98 const char* vendor
= loc_writer_get_vendor(writer
);
100 printf("Vendor is: %s\n", vendor
);
102 fprintf(stderr
, "Could not retrieve vendor\n");
107 err
= loc_writer_set_description(writer
, DESCRIPTION
);
109 fprintf(stderr
, "Could not set description\n");
113 // Retrieve description
114 const char* description
= loc_writer_get_description(writer
);
116 printf("Description is: %s\n", description
);
118 fprintf(stderr
, "Could not retrieve description\n");
123 err
= loc_writer_set_license(writer
, LICENSE
);
125 fprintf(stderr
, "Could not set license\n");
130 const char* license
= loc_writer_get_license(writer
);
132 printf("License is: %s\n", license
);
134 fprintf(stderr
, "Could not retrieve license\n");
138 FILE* f
= fopen("test.db", "w+");
140 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
144 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
146 fprintf(stderr
, "Could not write database: %s\n", strerror(err
));
149 loc_writer_unref(writer
);
154 // And open it again from disk
155 f
= fopen("test.db", "r");
157 fprintf(stderr
, "Could not open file for reading: %s\n", strerror(errno
));
161 struct loc_database
* db
;
162 err
= loc_database_new(ctx
, &db
, f
);
164 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
168 // Try reading something from the database
169 vendor
= loc_database_get_vendor(db
);
171 fprintf(stderr
, "Could not retrieve vendor\n");
173 } else if (strcmp(vendor
, VENDOR
) != 0) {
174 fprintf(stderr
, "Vendor doesn't match: %s != %s\n", vendor
, VENDOR
);
178 // Close the database
179 loc_database_unref(db
);