OpenVPN
test_crypto.c
Go to the documentation of this file.
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
48  struct gc_arena gc = gc_new();
49  struct buffer src_buf;
50  buf_set_read(&src_buf, (void *)testtext, sizeof(testtext));
51 
52  uint8_t dec[sizeof(testtext)];
53  struct buffer dec_buf;
54  buf_set_write(&dec_buf, dec, sizeof(dec));
55 
56  struct buffer pem_buf;
57 
58  assert_true(crypto_pem_encode("TESTKEYNAME", &pem_buf, &src_buf, &gc));
59  assert_true(BLEN(&src_buf) < BLEN(&pem_buf));
60 
61  /* Wrong key name */
62  assert_false(crypto_pem_decode("WRONGNAME", &dec_buf, &pem_buf));
63 
64  assert_true(crypto_pem_decode("TESTKEYNAME", &dec_buf, &pem_buf));
65  assert_int_equal(BLEN(&src_buf), BLEN(&dec_buf));
66  assert_memory_equal(BPTR(&src_buf), BPTR(&dec_buf), BLEN(&src_buf));
67 
68  gc_free(&gc);
69 }
70 
71 int
72 main(void) {
73  const struct CMUnitTest tests[] = {
75  };
76 
77 #if defined(ENABLE_CRYPTO_OPENSSL)
78  OpenSSL_add_all_algorithms();
79 #endif
80 
81  int ret = cmocka_run_group_tests_name("crypto tests", tests, NULL, NULL);
82 
83 #if defined(ENABLE_CRYPTO_OPENSSL)
84  EVP_cleanup();
85 #endif
86 
87  return ret;
88 }
bool crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src)
Decode a PEM buffer to binary data.
#define assert_true(c)
Definition: cmocka.h:1045
#define cmocka_unit_test(f)
Initializes a CMUnitTest structure.
Definition: cmocka.h:1653
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1023
static void crypto_pem_encode_decode_loopback(void **state)
Definition: test_crypto.c:47
#define BPTR(buf)
Definition: buffer.h:124
#define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown)
Definition: cmocka.h:1818
bool crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src, struct gc_arena *gc)
Encode binary data as PEM.
#define assert_memory_equal(a, b, size)
Definition: cmocka.h:1254
static struct gc_arena gc_new(void)
Definition: buffer.h:1015
#define BLEN(buf)
Definition: buffer.h:127
unsigned __int8 uint8_t
Definition: config-msvc.h:123
int main(void)
Definition: test_crypto.c:72
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
static const char testtext[]
Definition: test_crypto.c:44
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
static void buf_set_write(struct buffer *buf, uint8_t *data, int size)
Definition: buffer.h:318
#define assert_false(c)
Definition: cmocka.h:1063
static void buf_set_read(struct buffer *buf, const uint8_t *data, int size)
Definition: buffer.h:335
#define assert_int_equal(a, b)
Definition: cmocka.h:1174