]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/context_internal_test.c
Move ossl_asn1_string_to_time_t() to libtestutil
[thirdparty/openssl.git] / test / context_internal_test.c
1 /*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /* Internal tests for the OpenSSL library context */
11
12 #include "internal/cryptlib.h"
13 #include "testutil.h"
14
15 /*
16 * Everything between BEGIN EXAMPLE and END EXAMPLE is copied from
17 * doc/internal/man3/openssl_ctx_get_data.pod
18 */
19
20 /*
21 * ======================================================================
22 * BEGIN EXAMPLE
23 */
24
25 /* The index will always be entirely global, and dynamically allocated */
26 static int foo_index = -1;
27
28 typedef struct foo_st {
29 int i;
30 void *data;
31 } FOO;
32
33 static void *foo_new(void)
34 {
35 FOO *ptr = OPENSSL_zalloc(sizeof(*ptr));
36 if (ptr != NULL)
37 ptr->i = 42;
38 return ptr;
39 }
40 static void foo_free(void *ptr)
41 {
42 OPENSSL_free(ptr);
43 }
44 static const OPENSSL_CTX_METHOD foo_method = {
45 foo_new,
46 foo_free
47 };
48
49 static int foo_init(void)
50 {
51 if (foo_index == -1)
52 foo_index = openssl_ctx_new_index(&foo_method);
53
54 return foo_index != -1;
55 }
56
57 /*
58 * END EXAMPLE
59 * ======================================================================
60 */
61
62 static int test_context(OPENSSL_CTX *ctx)
63 {
64 FOO *data = NULL;
65
66 return
67 TEST_true(foo_init())
68 && TEST_ptr(data = openssl_ctx_get_data(ctx, foo_index))
69 /* OPENSSL_zalloc in foo_new() initialized it to zero */
70 && TEST_int_eq(data->i, 42);
71 }
72
73 static int test_app_context(void)
74 {
75 OPENSSL_CTX *ctx = NULL;
76 int result =
77 TEST_true(foo_init())
78 && TEST_ptr(ctx = OPENSSL_CTX_new())
79 && test_context(ctx);
80
81 OPENSSL_CTX_free(ctx);
82 return result;
83 }
84
85 static int test_def_context(void)
86 {
87 return test_context(NULL);
88 }
89
90 int setup_tests(void)
91 {
92 ADD_TEST(test_app_context);
93 ADD_TEST(test_def_context);
94 return 1;
95 }