]> git.ipfire.org Git - people/ms/libloc.git/blame - src/test-signature.c
Bump database version to "1"
[people/ms/libloc.git] / src / test-signature.c
CommitLineData
e8fd8389
MT
1/*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2019 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
30int main(int argc, char** argv) {
31 int err;
32
33 // Open public key
34 FILE* public_key = fopen(ABS_SRCDIR "/examples/public-key.pem", "r");
35 if (!public_key) {
36 fprintf(stderr, "Could not open public key file: %s\n", strerror(errno));
37 exit(EXIT_FAILURE);
38 }
39
40 // Open private key
41 FILE* private_key = fopen(ABS_SRCDIR "/examples/private-key.pem", "r");
42 if (!private_key) {
43 fprintf(stderr, "Could not open private key file: %s\n", strerror(errno));
44 exit(EXIT_FAILURE);
45 }
46
47 struct loc_ctx* ctx;
48 err = loc_new(&ctx);
49 if (err < 0)
50 exit(EXIT_FAILURE);
51
52 // Create an empty database
53 struct loc_writer* writer;
b904896a 54 err = loc_writer_new(ctx, &writer, LOC_DATABASE_VERSION_LATEST, private_key);
e8fd8389
MT
55 if (err < 0)
56 exit(EXIT_FAILURE);
57
58 FILE* f = fopen("test.db", "w+");
59 if (!f) {
60 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
61 exit(EXIT_FAILURE);
62 }
63
64 err = loc_writer_write(writer, f);
65 if (err) {
66 fprintf(stderr, "Could not write database: %s\n", strerror(err));
67 exit(EXIT_FAILURE);
68 }
69 loc_writer_unref(writer);
70
71 // Close the file
72 fclose(f);
73
74 // And open it again from disk
75 f = fopen("test.db", "r");
76 if (!f) {
77 fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
78 exit(EXIT_FAILURE);
79 }
80
81 struct loc_database* db;
82 err = loc_database_new(ctx, &db, f);
83 if (err) {
84 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
85 exit(EXIT_FAILURE);
86 }
87
88 // Verify the database signature
89 err = loc_database_verify(db, public_key);
90 if (err) {
91 fprintf(stderr, "Could not verify the database: %d\n", err);
92 exit(EXIT_FAILURE);
93 }
94
95 // Open another public key
0600eac2 96 public_key = freopen(ABS_SRCDIR "/src/signing-key.pem", "r", public_key);
e8fd8389
MT
97 if (!public_key) {
98 fprintf(stderr, "Could not open public key file: %s\n", strerror(errno));
99 exit(EXIT_FAILURE);
100 }
101
102 // Verify with an incorrect key
103 err = loc_database_verify(db, public_key);
104 if (err == 0) {
105 fprintf(stderr, "Database was verified with an incorrect key: %d\n", err);
106 exit(EXIT_FAILURE);
107 }
108
109 // Close the database
110 loc_database_unref(db);
111
112 loc_unref(ctx);
113
114 fclose(private_key);
115 fclose(public_key);
116
117 return EXIT_SUCCESS;
118}