summaryrefslogtreecommitdiffstats
path: root/tests/libdnssec/test_keystore_pkcs8.c
blob: 26acbd41d0432e9e2dd26f157e1f2fb180c3e58c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*  Copyright (C) 2022 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <tap/basic.h>
#include <tap/files.h>

#include "error.h"
#include "key.h"
#include "keystore.h"

int main(void)
{
	plan_lazy();

	char *dir = test_tmpdir();
	if (!dir) {
		return 1;
	}

	// create context

	dnssec_keystore_t *store = NULL;
	int r = dnssec_keystore_init_pkcs8(&store);
	ok(r == DNSSEC_EOK, "dnssec_keystore_init_pkcs8()");

	r = dnssec_keystore_init(store, dir);
	ok(r == DNSSEC_EOK, "init");

	r = dnssec_keystore_open(store, dir);
	ok(r == DNSSEC_EOK, "open");

	// writing new content

	char *id_A = NULL;
	r = dnssec_keystore_generate(store, DNSSEC_KEY_ALGORITHM_RSA_SHA256,
	                             1024, NULL, &id_A);
	ok(r == DNSSEC_EOK, "generate A");

	char *id_B = NULL;
	r = dnssec_keystore_generate(store, DNSSEC_KEY_ALGORITHM_RSA_SHA256,
	                             1024, NULL, &id_B);
	ok(r == DNSSEC_EOK, "generate B");

	// reading existing content

	dnssec_key_t *key = NULL;
	dnssec_key_new(&key);
	dnssec_key_set_algorithm(key, DNSSEC_KEY_ALGORITHM_RSA_SHA256);
	r = dnssec_keystore_get_private(store, id_A, key);
	ok(r == DNSSEC_EOK, "read A");
	dnssec_key_free(key);

	dnssec_key_new(&key);
	dnssec_key_set_algorithm(key, DNSSEC_KEY_ALGORITHM_RSA_SHA256);
	r = dnssec_keystore_get_private(store, id_B, key);
	ok(r == DNSSEC_EOK, "read B");
	dnssec_key_free(key);

	// content removal

	r = dnssec_keystore_remove(store, id_A);
	ok(r == DNSSEC_EOK, "remove A");

	dnssec_key_new(&key);
	dnssec_key_set_algorithm(key, DNSSEC_KEY_ALGORITHM_RSA_SHA256);
	r = dnssec_keystore_get_private(store, id_A, key);
	ok(r == DNSSEC_ENOENT, "read removed");
	dnssec_key_free(key);

	// cleanup

	free(id_A);
	free(id_B);

	r = dnssec_keystore_close(store);
	ok(r == DNSSEC_EOK, "close");

	r = dnssec_keystore_deinit(store);
	ok(r == DNSSEC_EOK, "deinit");

	test_rm_rf(dir);
	free(dir);

	return 0;
}