]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/context_internal_test.c
Add support for openssl_ctx_run_once and openssl_ctx_onfree
[thirdparty/openssl.git] / test / context_internal_test.c
CommitLineData
d64b6299
RL
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 */
26static int foo_index = -1;
27
28typedef struct foo_st {
29 int i;
30 void *data;
31} FOO;
32
33static void *foo_new(void)
34{
35 FOO *ptr = OPENSSL_zalloc(sizeof(*ptr));
36 if (ptr != NULL)
37 ptr->i = 42;
38 return ptr;
39}
40static void foo_free(void *ptr)
41{
42 OPENSSL_free(ptr);
43}
44static const OPENSSL_CTX_METHOD foo_method = {
45 foo_new,
46 foo_free
47};
48
0b76ce99
RL
49static int foo_init(void)
50{
51 if (foo_index == -1)
52 foo_index = openssl_ctx_new_index(&foo_method);
d64b6299
RL
53
54 return foo_index != -1;
55}
56
57/*
58 * END EXAMPLE
59 * ======================================================================
60 */
61
62static int test_context(OPENSSL_CTX *ctx)
63{
64 FOO *data = NULL;
65
0b76ce99
RL
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);
d64b6299
RL
71}
72
73static int test_app_context(void)
74{
75 OPENSSL_CTX *ctx = NULL;
0b76ce99
RL
76 int result =
77 TEST_true(foo_init())
78 && TEST_ptr(ctx = OPENSSL_CTX_new())
79 && test_context(ctx);
d64b6299
RL
80
81 OPENSSL_CTX_free(ctx);
82 return result;
83}
84
85static int test_def_context(void)
86{
87 return test_context(NULL);
88}
89
90int setup_tests(void)
91{
d64b6299
RL
92 ADD_TEST(test_app_context);
93 ADD_TEST(test_def_context);
94 return 1;
95}