]> git.ipfire.org Git - thirdparty/openvpn.git/blob - tests/unit_tests/openvpn/test_crypto.c
Uncrustify the tests/unit_tests/ part of our tree.
[thirdparty/openvpn.git] / tests / unit_tests / openvpn / test_crypto.c
1 /*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2016-2018 Fox Crypto B.V. <openvpn@fox-it.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29
30 #include "syshead.h"
31
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdarg.h>
36 #include <string.h>
37 #include <setjmp.h>
38 #include <cmocka.h>
39
40 #include "crypto.h"
41
42 #include "mock_msg.h"
43
44 static const char testtext[] = "Dummy text to test PEM encoding";
45
46 static void
47 crypto_pem_encode_decode_loopback(void **state)
48 {
49 struct gc_arena gc = gc_new();
50 struct buffer src_buf;
51 buf_set_read(&src_buf, (void *)testtext, sizeof(testtext));
52
53 uint8_t dec[sizeof(testtext)];
54 struct buffer dec_buf;
55 buf_set_write(&dec_buf, dec, sizeof(dec));
56
57 struct buffer pem_buf;
58
59 assert_true(crypto_pem_encode("TESTKEYNAME", &pem_buf, &src_buf, &gc));
60 assert_true(BLEN(&src_buf) < BLEN(&pem_buf));
61
62 /* Wrong key name */
63 assert_false(crypto_pem_decode("WRONGNAME", &dec_buf, &pem_buf));
64
65 assert_true(crypto_pem_decode("TESTKEYNAME", &dec_buf, &pem_buf));
66 assert_int_equal(BLEN(&src_buf), BLEN(&dec_buf));
67 assert_memory_equal(BPTR(&src_buf), BPTR(&dec_buf), BLEN(&src_buf));
68
69 gc_free(&gc);
70 }
71
72 int
73 main(void)
74 {
75 const struct CMUnitTest tests[] = {
76 cmocka_unit_test(crypto_pem_encode_decode_loopback),
77 };
78
79 #if defined(ENABLE_CRYPTO_OPENSSL)
80 OpenSSL_add_all_algorithms();
81 #endif
82
83 int ret = cmocka_run_group_tests_name("crypto tests", tests, NULL, NULL);
84
85 #if defined(ENABLE_CRYPTO_OPENSSL)
86 EVP_cleanup();
87 #endif
88
89 return ret;
90 }