swanctl: Pass optional connection name to --initiate/install/uninstall
[strongswan.git] / src / charon-tkm / tests / nonceg_tests.c
1 /*
2 * Copyright (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <tests/test_suite.h>
18
19 #include <tkm/client.h>
20
21 #include "tkm.h"
22 #include "tkm_nonceg.h"
23
24 START_TEST(test_nonceg_creation)
25 {
26 tkm_nonceg_t *ng = NULL;
27
28 ng = tkm_nonceg_create();
29 fail_if(ng == NULL, "Error creating tkm nonce generator");
30
31 ng->nonce_gen.destroy(&ng->nonce_gen);
32 }
33 END_TEST
34
35 START_TEST(test_nonceg_allocate_nonce)
36 {
37 tkm_nonceg_t *ng = tkm_nonceg_create();
38
39 const size_t length = 256;
40 uint8_t zero[length];
41 memset(zero, 0, length);
42
43 chunk_t nonce;
44 const bool got_nonce = ng->nonce_gen.allocate_nonce(&ng->nonce_gen,
45 length, &nonce);
46
47 fail_unless(got_nonce, "Call to allocate_nonce failed");
48 fail_unless(nonce.len = length, "Allocated nonce length mismatch");
49 fail_if(memcmp(nonce.ptr, zero, length) == 0, "Unable to allocate nonce");
50
51 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, 1);
52 ike_nc_reset(1);
53
54 chunk_free(&nonce);
55 ng->nonce_gen.destroy(&ng->nonce_gen);
56 }
57 END_TEST
58
59 START_TEST(test_nonceg_get_nonce)
60 {
61 tkm_nonceg_t *ng = tkm_nonceg_create();
62
63 const size_t length = 128;
64 uint8_t zero[length];
65 memset(zero, 0, length);
66
67 uint8_t *buf = malloc(length + 1);
68 memset(buf, 0, length);
69 /* set end marker */
70 buf[length] = 255;
71
72 const bool got_nonce = ng->nonce_gen.get_nonce(&ng->nonce_gen, length, buf);
73 fail_unless(got_nonce, "Call to get_nonce failed");
74 fail_if(memcmp(buf, zero, length) == 0, "Unable to get nonce");
75 fail_if(buf[length] != 255, "End marker not found");
76
77 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, 1);
78 ike_nc_reset(1);
79
80 free(buf);
81 ng->nonce_gen.destroy(&ng->nonce_gen);
82 }
83 END_TEST
84
85 Suite *make_nonceg_tests()
86 {
87 Suite *s;
88 TCase *tc;
89
90 s = suite_create("nonce generator");
91
92 tc = tcase_create("creation");
93 tcase_add_test(tc, test_nonceg_creation);
94 suite_add_tcase(s, tc);
95
96 tc = tcase_create("allocate");
97 tcase_add_test(tc, test_nonceg_allocate_nonce);
98 suite_add_tcase(s, tc);
99
100 tc = tcase_create("get");
101 tcase_add_test(tc, test_nonceg_get_nonce);
102 suite_add_tcase(s, tc);
103
104 return s;
105 }