]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/charon-tkm/tests/nonceg_tests.c
charon-tkm: Drop unneeded nonceg get_id function
[thirdparty/strongswan.git] / src / charon-tkm / tests / nonceg_tests.c
CommitLineData
559fe48c
RB
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
d6032bff
TB
17#include <tests/test_suite.h>
18
559fe48c
RB
19#include <tkm/client.h>
20
21#include "tkm.h"
22#include "tkm_nonceg.h"
23
24START_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}
33END_TEST
34
35START_TEST(test_nonceg_allocate_nonce)
36{
37 tkm_nonceg_t *ng = tkm_nonceg_create();
38
39 const size_t length = 256;
40 u_int8_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}
57END_TEST
58
59START_TEST(test_nonceg_get_nonce)
60{
61 tkm_nonceg_t *ng = tkm_nonceg_create();
62
63 const size_t length = 128;
64 u_int8_t zero[length];
65 memset(zero, 0, length);
66
67 u_int8_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}
83END_TEST
84
d6032bff 85Suite *make_nonceg_tests()
559fe48c 86{
d6032bff
TB
87 Suite *s;
88 TCase *tc;
89
90 s = suite_create("nonce generator");
91
92 tc = tcase_create("creation");
559fe48c 93 tcase_add_test(tc, test_nonceg_creation);
d6032bff
TB
94 suite_add_tcase(s, tc);
95
96 tc = tcase_create("allocate");
559fe48c 97 tcase_add_test(tc, test_nonceg_allocate_nonce);
d6032bff
TB
98 suite_add_tcase(s, tc);
99
100 tc = tcase_create("get");
559fe48c 101 tcase_add_test(tc, test_nonceg_get_nonce);
d6032bff 102 suite_add_tcase(s, tc);
559fe48c 103
d6032bff 104 return s;
559fe48c 105}