ahci: prohibit "restarting" the FIS or CLB engines
[qemu.git] / tests / test-crypto-secret.c
bloba49c8e31e884c0d307765993244c09cfbd3c2f13
1 /*
2 * QEMU Crypto secret handling
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include <glib.h>
23 #include "crypto/init.h"
24 #include "crypto/secret.h"
26 static void test_secret_direct(void)
28 Object *sec = object_new_with_props(
29 TYPE_QCRYPTO_SECRET,
30 object_get_objects_root(),
31 "sec0",
32 &error_abort,
33 "data", "123456",
34 NULL);
36 char *pw = qcrypto_secret_lookup_as_utf8("sec0",
37 &error_abort);
39 g_assert_cmpstr(pw, ==, "123456");
41 object_unparent(sec);
42 g_free(pw);
46 static void test_secret_indirect_good(void)
48 Object *sec;
49 char *fname = NULL;
50 int fd = g_file_open_tmp("secretXXXXXX",
51 &fname,
52 NULL);
54 g_assert(fd >= 0);
55 g_assert_nonnull(fname);
57 g_assert(write(fd, "123456", 6) == 6);
59 sec = object_new_with_props(
60 TYPE_QCRYPTO_SECRET,
61 object_get_objects_root(),
62 "sec0",
63 &error_abort,
64 "file", fname,
65 NULL);
67 char *pw = qcrypto_secret_lookup_as_utf8("sec0",
68 &error_abort);
70 g_assert_cmpstr(pw, ==, "123456");
72 object_unparent(sec);
73 g_free(pw);
74 close(fd);
75 g_free(fname);
79 static void test_secret_indirect_badfile(void)
81 Object *sec = object_new_with_props(
82 TYPE_QCRYPTO_SECRET,
83 object_get_objects_root(),
84 "sec0",
85 NULL,
86 "file", "does-not-exist",
87 NULL);
89 g_assert(sec == NULL);
93 static void test_secret_indirect_emptyfile(void)
95 Object *sec;
96 char *fname = NULL;
97 int fd = g_file_open_tmp("secretXXXXXX",
98 &fname,
99 NULL);
101 g_assert(fd >= 0);
102 g_assert_nonnull(fname);
104 sec = object_new_with_props(
105 TYPE_QCRYPTO_SECRET,
106 object_get_objects_root(),
107 "sec0",
108 &error_abort,
109 "file", fname,
110 NULL);
112 char *pw = qcrypto_secret_lookup_as_utf8("sec0",
113 &error_abort);
115 g_assert_cmpstr(pw, ==, "");
117 object_unparent(sec);
118 g_free(pw);
119 close(fd);
120 g_free(fname);
124 static void test_secret_noconv_base64_good(void)
126 Object *sec = object_new_with_props(
127 TYPE_QCRYPTO_SECRET,
128 object_get_objects_root(),
129 "sec0",
130 &error_abort,
131 "data", "MTIzNDU2",
132 "format", "base64",
133 NULL);
135 char *pw = qcrypto_secret_lookup_as_base64("sec0",
136 &error_abort);
138 g_assert_cmpstr(pw, ==, "MTIzNDU2");
140 object_unparent(sec);
141 g_free(pw);
145 static void test_secret_noconv_base64_bad(void)
147 Object *sec = object_new_with_props(
148 TYPE_QCRYPTO_SECRET,
149 object_get_objects_root(),
150 "sec0",
151 NULL,
152 "data", "MTI$NDU2",
153 "format", "base64",
154 NULL);
156 g_assert(sec == NULL);
160 static void test_secret_noconv_utf8(void)
162 Object *sec = object_new_with_props(
163 TYPE_QCRYPTO_SECRET,
164 object_get_objects_root(),
165 "sec0",
166 &error_abort,
167 "data", "123456",
168 "format", "raw",
169 NULL);
171 char *pw = qcrypto_secret_lookup_as_utf8("sec0",
172 &error_abort);
174 g_assert_cmpstr(pw, ==, "123456");
176 object_unparent(sec);
177 g_free(pw);
181 static void test_secret_conv_base64_utf8valid(void)
183 Object *sec = object_new_with_props(
184 TYPE_QCRYPTO_SECRET,
185 object_get_objects_root(),
186 "sec0",
187 &error_abort,
188 "data", "MTIzNDU2",
189 "format", "base64",
190 NULL);
192 char *pw = qcrypto_secret_lookup_as_utf8("sec0",
193 &error_abort);
195 g_assert_cmpstr(pw, ==, "123456");
197 object_unparent(sec);
198 g_free(pw);
202 static void test_secret_conv_base64_utf8invalid(void)
204 Object *sec = object_new_with_props(
205 TYPE_QCRYPTO_SECRET,
206 object_get_objects_root(),
207 "sec0",
208 &error_abort,
209 "data", "f0VMRgIBAQAAAA==",
210 "format", "base64",
211 NULL);
213 char *pw = qcrypto_secret_lookup_as_utf8("sec0",
214 NULL);
215 g_assert(pw == NULL);
217 object_unparent(sec);
221 static void test_secret_conv_utf8_base64(void)
223 Object *sec = object_new_with_props(
224 TYPE_QCRYPTO_SECRET,
225 object_get_objects_root(),
226 "sec0",
227 &error_abort,
228 "data", "123456",
229 NULL);
231 char *pw = qcrypto_secret_lookup_as_base64("sec0",
232 &error_abort);
234 g_assert_cmpstr(pw, ==, "MTIzNDU2");
236 object_unparent(sec);
237 g_free(pw);
241 static void test_secret_crypt_raw(void)
243 Object *master = object_new_with_props(
244 TYPE_QCRYPTO_SECRET,
245 object_get_objects_root(),
246 "master",
247 &error_abort,
248 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
249 "format", "base64",
250 NULL);
251 Object *sec = object_new_with_props(
252 TYPE_QCRYPTO_SECRET,
253 object_get_objects_root(),
254 "sec0",
255 &error_abort,
256 "data",
257 "\xCC\xBF\xF7\x09\x46\x19\x0B\x52\x2A\x3A\xB4\x6B\xCD\x7A\xB0\xB0",
258 "format", "raw",
259 "keyid", "master",
260 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
261 NULL);
263 char *pw = qcrypto_secret_lookup_as_utf8("sec0",
264 &error_abort);
266 g_assert_cmpstr(pw, ==, "123456");
268 object_unparent(sec);
269 object_unparent(master);
270 g_free(pw);
274 static void test_secret_crypt_base64(void)
276 Object *master = object_new_with_props(
277 TYPE_QCRYPTO_SECRET,
278 object_get_objects_root(),
279 "master",
280 &error_abort,
281 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
282 "format", "base64",
283 NULL);
284 Object *sec = object_new_with_props(
285 TYPE_QCRYPTO_SECRET,
286 object_get_objects_root(),
287 "sec0",
288 &error_abort,
289 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
290 "format", "base64",
291 "keyid", "master",
292 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
293 NULL);
295 char *pw = qcrypto_secret_lookup_as_utf8("sec0",
296 &error_abort);
298 g_assert_cmpstr(pw, ==, "123456");
300 object_unparent(sec);
301 object_unparent(master);
302 g_free(pw);
306 static void test_secret_crypt_short_key(void)
308 Object *master = object_new_with_props(
309 TYPE_QCRYPTO_SECRET,
310 object_get_objects_root(),
311 "master",
312 &error_abort,
313 "data", "9miloPQCzGy+TL6aonfzVc",
314 "format", "base64",
315 NULL);
316 Object *sec = object_new_with_props(
317 TYPE_QCRYPTO_SECRET,
318 object_get_objects_root(),
319 "sec0",
320 NULL,
321 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
322 "format", "raw",
323 "keyid", "master",
324 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
325 NULL);
327 g_assert(sec == NULL);
328 object_unparent(master);
332 static void test_secret_crypt_short_iv(void)
334 Object *master = object_new_with_props(
335 TYPE_QCRYPTO_SECRET,
336 object_get_objects_root(),
337 "master",
338 &error_abort,
339 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
340 "format", "base64",
341 NULL);
342 Object *sec = object_new_with_props(
343 TYPE_QCRYPTO_SECRET,
344 object_get_objects_root(),
345 "sec0",
346 NULL,
347 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
348 "format", "raw",
349 "keyid", "master",
350 "iv", "0I7Gw/TKuA+Old2W2a",
351 NULL);
353 g_assert(sec == NULL);
354 object_unparent(master);
358 static void test_secret_crypt_missing_iv(void)
360 Object *master = object_new_with_props(
361 TYPE_QCRYPTO_SECRET,
362 object_get_objects_root(),
363 "master",
364 &error_abort,
365 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
366 "format", "base64",
367 NULL);
368 Object *sec = object_new_with_props(
369 TYPE_QCRYPTO_SECRET,
370 object_get_objects_root(),
371 "sec0",
372 NULL,
373 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
374 "format", "raw",
375 "keyid", "master",
376 NULL);
378 g_assert(sec == NULL);
379 object_unparent(master);
383 static void test_secret_crypt_bad_iv(void)
385 Object *master = object_new_with_props(
386 TYPE_QCRYPTO_SECRET,
387 object_get_objects_root(),
388 "master",
389 &error_abort,
390 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
391 "format", "base64",
392 NULL);
393 Object *sec = object_new_with_props(
394 TYPE_QCRYPTO_SECRET,
395 object_get_objects_root(),
396 "sec0",
397 NULL,
398 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
399 "format", "raw",
400 "keyid", "master",
401 "iv", "0I7Gw/TK$$uA+Old2W2a",
402 NULL);
404 g_assert(sec == NULL);
405 object_unparent(master);
409 int main(int argc, char **argv)
411 module_call_init(MODULE_INIT_QOM);
412 g_test_init(&argc, &argv, NULL);
414 g_assert(qcrypto_init(NULL) == 0);
416 g_test_add_func("/crypto/secret/direct",
417 test_secret_direct);
418 g_test_add_func("/crypto/secret/indirect/good",
419 test_secret_indirect_good);
420 g_test_add_func("/crypto/secret/indirect/badfile",
421 test_secret_indirect_badfile);
422 g_test_add_func("/crypto/secret/indirect/emptyfile",
423 test_secret_indirect_emptyfile);
425 g_test_add_func("/crypto/secret/noconv/base64/good",
426 test_secret_noconv_base64_good);
427 g_test_add_func("/crypto/secret/noconv/base64/bad",
428 test_secret_noconv_base64_bad);
429 g_test_add_func("/crypto/secret/noconv/utf8",
430 test_secret_noconv_utf8);
431 g_test_add_func("/crypto/secret/conv/base64/utf8valid",
432 test_secret_conv_base64_utf8valid);
433 g_test_add_func("/crypto/secret/conv/base64/utf8invalid",
434 test_secret_conv_base64_utf8invalid);
435 g_test_add_func("/crypto/secret/conv/utf8/base64",
436 test_secret_conv_utf8_base64);
438 g_test_add_func("/crypto/secret/crypt/raw",
439 test_secret_crypt_raw);
440 g_test_add_func("/crypto/secret/crypt/base64",
441 test_secret_crypt_base64);
442 g_test_add_func("/crypto/secret/crypt/shortkey",
443 test_secret_crypt_short_key);
444 g_test_add_func("/crypto/secret/crypt/shortiv",
445 test_secret_crypt_short_iv);
446 g_test_add_func("/crypto/secret/crypt/missingiv",
447 test_secret_crypt_missing_iv);
448 g_test_add_func("/crypto/secret/crypt/badiv",
449 test_secret_crypt_bad_iv);
451 return g_test_run();