]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/test-database.c
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 int main(int argc
, char** argv
) {
44 FILE* public_key
= fopen(ABS_SRCDIR
"/examples/public-key.pem", "r");
46 fprintf(stderr
, "Could not open public key file: %s\n", strerror(errno
));
51 FILE* private_key
= fopen(ABS_SRCDIR
"/examples/private-key.pem", "r");
53 fprintf(stderr
, "Could not open private key file: %s\n", strerror(errno
));
63 struct loc_writer
* writer
;
64 err
= loc_writer_new(ctx
, &writer
, private_key
);
69 err
= loc_writer_set_vendor(writer
, VENDOR
);
71 fprintf(stderr
, "Could not set vendor\n");
76 const char* vendor
= loc_writer_get_vendor(writer
);
78 printf("Vendor is: %s\n", vendor
);
80 fprintf(stderr
, "Could not retrieve vendor\n");
85 err
= loc_writer_set_description(writer
, DESCRIPTION
);
87 fprintf(stderr
, "Could not set description\n");
91 // Retrieve description
92 const char* description
= loc_writer_get_description(writer
);
94 printf("Description is: %s\n", description
);
96 fprintf(stderr
, "Could not retrieve description\n");
101 err
= loc_writer_set_license(writer
, LICENSE
);
103 fprintf(stderr
, "Could not set license\n");
108 const char* license
= loc_writer_get_license(writer
);
110 printf("License is: %s\n", license
);
112 fprintf(stderr
, "Could not retrieve license\n");
116 FILE* f
= fopen("test.db", "w+");
118 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
122 err
= loc_writer_write(writer
, f
);
124 fprintf(stderr
, "Could not write database: %s\n", strerror(err
));
127 loc_writer_unref(writer
);
132 // And open it again from disk
133 f
= fopen("test.db", "r");
135 fprintf(stderr
, "Could not open file for reading: %s\n", strerror(errno
));
139 struct loc_database
* db
;
140 err
= loc_database_new(ctx
, &db
, f
);
142 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
146 // Verify the database signature
147 err
= loc_database_verify(db
, public_key
);
149 fprintf(stderr
, "Could not verify the database: %d\n", err
);
153 // Try reading something from the database
154 vendor
= loc_database_get_vendor(db
);
156 fprintf(stderr
, "Could not retrieve vendor\n");
158 } else if (strcmp(vendor
, VENDOR
) != 0) {
159 fprintf(stderr
, "Vendor doesn't match: %s != %s\n", vendor
, VENDOR
);
163 // Close the database
164 loc_database_unref(db
);