summaryrefslogtreecommitdiff
path: root/tests/dnssec_keys.c
blob: 048230562639e4e6ee5fb408387b3eb76fa704cb (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*  Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
 */

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

#include "libknot/dnssec/key.h"
#include "libknot/dnssec/key.c" // testing static functions

int main(int argc, char *argv[])
{
	plan(26);

	// 1-3. - strndup_with_suffix()
	{
		char *result;

		result = strndup_with_suffix("begin", 5, "end");
		ok(result && strcmp(result, "beginend") == 0,
		   "strndup_with_suffix(), matching length");
		free(result);

		result = strndup_with_suffix("begin", 3, "end");
		ok(result && strcmp(result, "begend") == 0,
		   "strndup_with_suffix(), shorter length");
		free(result);

		result = strndup_with_suffix("", 0, "end");
		ok(result && strcmp(result, "end") == 0,
		   "strndup_with_suffix(), empty base string");
		free(result);
	}

	// 4.-9. - get_key_filenames()
	{
		char *public, *private;
		int result;

		result = get_key_filenames("Kexample.com.+1.+2.private",
		                           &public, &private);
		ok(result == KNOT_EOK &&
		   strcmp(public, "Kexample.com.+1.+2.key") == 0 &&
		   strcmp(private, "Kexample.com.+1.+2.private") == 0,
		   "get_key_filenames(), from private key");
		free(public);
		free(private);

		result = get_key_filenames("Kexample.com.+4.+8.key",
		                           &public, &private);
		ok(result == KNOT_EOK &&
		   strcmp(public, "Kexample.com.+4.+8.key") == 0 &&
		   strcmp(private, "Kexample.com.+4.+8.private") == 0,
		   "get_key_filenames(), from public key");
		free(public);
		free(private);

		result = get_key_filenames("nic.cz.+4.+8",
		                           &public, &private);
		ok(result == KNOT_EOK &&
		   strcmp(public, "nic.cz.+4.+8.key") == 0 &&
		   strcmp(private, "nic.cz.+4.+8.private") == 0,
		   "get_key_filenames(), without extension");
		free(public);
		free(private);

		result = get_key_filenames("nic.cz.+0.+1.",
		                           &public, &private);
		ok(result == KNOT_EOK &&
		   strcmp(public, "nic.cz.+0.+1.key") == 0 &&
		   strcmp(private, "nic.cz.+0.+1.private") == 0,
		   "get_key_filenames(), empty extension");
		free(public);
		free(private);

		result = get_key_filenames("../keys/Kfoo.bar.+5.+10.private",
		                           &public, &private);
		ok(result == KNOT_EOK &&
		   strcmp(public, "../keys/Kfoo.bar.+5.+10.key") == 0 &&
		   strcmp(private, "../keys/Kfoo.bar.+5.+10.private") == 0,
		   "get_key_filenames(), with path");
		free(public);
		free(private);

		result = get_key_filenames("keys/something.txt",
		                           &public, &private);
		ok(result == KNOT_EOK &&
		   strcmp(public, "keys/something.txt.key") == 0 &&
		   strcmp(private, "keys/something.txt.private") == 0,
		   "get_key_filenames(), nonstandard name");
		free(public);
		free(private);
	}

	// 10. - key_param_base64()
	{
		knot_binary_t output = { 0 };
		int result;

		result = key_param_base64(&output, "aGVsbG8gRE5TIHdvcmxk");

		ok(result == KNOT_EOK && output.size == 15
		   && memcmp((char *)output.data, "hello DNS world", 15) == 0,
		   "key_param_base64(), correct usage");

		knot_binary_free(&output);
	}

	// 11-16. - key_param_int()
	{
		int output = 0;
		int result;

		result = key_param_int(&output, "12345");
		ok(result == KNOT_EOK && output == 12345,
		   "key_param_int(), correct number");

		result = key_param_int(&output, "6789 whatever");
		ok(result == KNOT_EOK && output == 6789,
		   "key_param_int(), number, space, and text");

		result = key_param_int(&output, "24680\n");
		ok(result == KNOT_EOK && output == 24680,
		   "key_param_int(), number and new line");

		result = key_param_int(&output, "0");
		ok(result == KNOT_EOK && output == 0,
		   "key_param_int(), zero");

		result = key_param_int(&output, "");
		ok(result == KNOT_EINVAL,
		   "key_param_int(), empty string");

		result = key_param_int(&output, "\t \n");
		ok(result == KNOT_EINVAL,
		   "key_param_int(), only white spaces");

		result = key_param_int(&output, "4444abc");
		ok(result == KNOT_EINVAL,
		   "key_param_int(), number and text");
	}

	// 17-21. - parse_keyfile_line()
	{
		knot_key_params_t key = { 0 };
		int result;
		char *line;

		line = strdup("Algorithm: 123 ABC");
		result = parse_keyfile_line(&key, line, strlen(line));
		ok(result == KNOT_EOK && key.algorithm == 123,
		   "parse_keyfile_line(), simple line with algorithm");
		free(line);

		line = strdup("Key:   c2VjcmV0\n");
		result = parse_keyfile_line(&key, line, strlen(line));
		ok(result == KNOT_EOK && key.secret.size == 6
		   && memcmp((char *)key.secret.data, "secret", 6) == 0,
		   "parse_keyfile_line(), new line terminated line with key");
		knot_binary_free(&key.secret);
		free(line);

		line = strdup("Cool: S25vdCBETlM=");
		result = parse_keyfile_line(&key, line, strlen(line));
		ok(result == KNOT_EOK,
		   "parse_keyfile_line(), unknown parameter");
		free(line);

		line = strdup("Activate: 20130521144259\n");
		result = parse_keyfile_line(&key, line, strlen(line));
		ok(result == KNOT_EOK && key.time_activate == 1369147379,
		   "parse_keyfile_line(), timestamp parsing");
		free(line);
	}

	// 22. - knot_free_key_params()
	{
		int result;
		knot_key_params_t params = { 0 };
		knot_key_params_t empty_params = { 0 };

		params.algorithm = 42;
		knot_binary_from_base64("AQAB", &params.public_exponent);

		result = knot_free_key_params(&params);
		ok(result == KNOT_EOK
		   && memcmp(&params, &empty_params, sizeof(params)) == 0,
		   "knot_free_key_params(), regular free");
	}

	// 23-25. - knot_tsig_key_from_params()
	{
		int result;
		knot_key_params_t params = { 0 };
		knot_tsig_key_t tsig_key = { 0 };
		const char *owner = "shared.example.com.";
		knot_dname_t *name = knot_dname_from_str(owner);

		result = knot_tsig_key_from_params(&params, &tsig_key);
		ok(result == KNOT_EINVAL,
		   "knot_tsig_key_from_params(), empty parameters");

		params.secret.data = (uint8_t *)"test";
		params.secret.size = 4;
		result = knot_tsig_key_from_params(&params, &tsig_key);
		ok(result == KNOT_EINVAL,
		   "knot_tsig_key_from_params(), no key name");

		params.name = name;
		params.secret.data = NULL;
		params.secret.size = 0;
		result = knot_tsig_key_from_params(&params, &tsig_key);
		ok(result == KNOT_EINVAL,
		   "knot_tsig_key_from_params(), no shared secret");

		params.name = name;
		knot_binary_from_base64("Ok6NmA==", &params.secret);
		uint8_t decoded_secret[] = { 0x3a, 0x4e, 0x8d, 0x98 };
		result = knot_tsig_key_from_params(&params, &tsig_key);
		ok(result == KNOT_EOK
		   && tsig_key.secret.size == sizeof(decoded_secret)
		   && memcmp(tsig_key.secret.data, decoded_secret,
		             sizeof(decoded_secret)) == 0,
		   "knot_tsig_key_from_params(), secret set properly");

		knot_free_key_params(&params);
		knot_tsig_key_free(&tsig_key);
	}

	//! \todo knot_keytag()
	//! \todo get_key_info_from_public_key() -- working with files required
	//! \todo knot_load_key_params() -- working with files required
	//! \todo knot_get_key_type()

	return 0;
}