]> git.ipfire.org Git - people/ms/libloc.git/blob - src/test-database.c
Implement signing/verifying databases
[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 int main(int argc, char** argv) {
41 int err;
42
43 // Open public key
44 FILE* public_key = fopen(ABS_SRCDIR "/examples/public-key.pem", "r");
45 if (!public_key) {
46 fprintf(stderr, "Could not open public key file: %s\n", strerror(errno));
47 exit(EXIT_FAILURE);
48 }
49
50 // Open private key
51 FILE* private_key = fopen(ABS_SRCDIR "/examples/private-key.pem", "r");
52 if (!private_key) {
53 fprintf(stderr, "Could not open private key file: %s\n", strerror(errno));
54 exit(EXIT_FAILURE);
55 }
56
57 struct loc_ctx* ctx;
58 err = loc_new(&ctx);
59 if (err < 0)
60 exit(EXIT_FAILURE);
61
62 // Create a database
63 struct loc_writer* writer;
64 err = loc_writer_new(ctx, &writer, private_key);
65 if (err < 0)
66 exit(EXIT_FAILURE);
67
68 // Set the vendor
69 err = loc_writer_set_vendor(writer, VENDOR);
70 if (err) {
71 fprintf(stderr, "Could not set vendor\n");
72 exit(EXIT_FAILURE);
73 }
74
75 // Retrieve vendor
76 const char* vendor = loc_writer_get_vendor(writer);
77 if (vendor) {
78 printf("Vendor is: %s\n", vendor);
79 } else {
80 fprintf(stderr, "Could not retrieve vendor\n");
81 exit(EXIT_FAILURE);
82 }
83
84 // Set a description
85 err = loc_writer_set_description(writer, DESCRIPTION);
86 if (err) {
87 fprintf(stderr, "Could not set description\n");
88 exit(EXIT_FAILURE);
89 }
90
91 // Retrieve description
92 const char* description = loc_writer_get_description(writer);
93 if (description) {
94 printf("Description is: %s\n", description);
95 } else {
96 fprintf(stderr, "Could not retrieve description\n");
97 exit(EXIT_FAILURE);
98 }
99
100 // Set a license
101 err = loc_writer_set_license(writer, LICENSE);
102 if (err) {
103 fprintf(stderr, "Could not set license\n");
104 exit(EXIT_FAILURE);
105 }
106
107 // Retrieve license
108 const char* license = loc_writer_get_license(writer);
109 if (license) {
110 printf("License is: %s\n", license);
111 } else {
112 fprintf(stderr, "Could not retrieve license\n");
113 exit(EXIT_FAILURE);
114 }
115
116 FILE* f = fopen("test.db", "w+");
117 if (!f) {
118 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
119 exit(EXIT_FAILURE);
120 }
121
122 err = loc_writer_write(writer, f);
123 if (err) {
124 fprintf(stderr, "Could not write database: %s\n", strerror(err));
125 exit(EXIT_FAILURE);
126 }
127 loc_writer_unref(writer);
128
129 // Close the file
130 fclose(f);
131
132 // And open it again from disk
133 f = fopen("test.db", "r");
134 if (!f) {
135 fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
136 exit(EXIT_FAILURE);
137 }
138
139 struct loc_database* db;
140 err = loc_database_new(ctx, &db, f);
141 if (err) {
142 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
143 exit(EXIT_FAILURE);
144 }
145
146 // Verify the database signature
147 err = loc_database_verify(db, public_key);
148 if (err) {
149 fprintf(stderr, "Could not verify the database: %d\n", err);
150 exit(EXIT_FAILURE);
151 }
152
153 // Try reading something from the database
154 vendor = loc_database_get_vendor(db);
155 if (!vendor) {
156 fprintf(stderr, "Could not retrieve vendor\n");
157 exit(EXIT_FAILURE);
158 } else if (strcmp(vendor, VENDOR) != 0) {
159 fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor, VENDOR);
160 exit(EXIT_FAILURE);
161 }
162
163 // Close the database
164 loc_database_unref(db);
165
166 loc_unref(ctx);
167
168 fclose(private_key);
169 fclose(public_key);
170
171 return EXIT_SUCCESS;
172 }