2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* Internal tests for the OpenSSL library context */
12 #include "internal/cryptlib.h"
16 * Everything between BEGIN EXAMPLE and END EXAMPLE is copied from
17 * doc/internal/man3/openssl_ctx_get_data.pod
21 * ======================================================================
25 /* The index will always be entirely global, and dynamically allocated */
26 static int foo_index = -1;
28 typedef struct foo_st {
33 static void *foo_new(void)
35 FOO *ptr = OPENSSL_zalloc(sizeof(*ptr));
40 static void foo_free(void *ptr)
44 static const OPENSSL_CTX_METHOD foo_method = {
49 static int foo_init(void) {
50 foo_index = openssl_ctx_new_index(&foo_method);
52 return foo_index != -1;
57 * ======================================================================
60 static int test_context(OPENSSL_CTX *ctx)
64 return (TEST_ptr(data = openssl_ctx_get_data(ctx, foo_index))
65 /* OPENSSL_zalloc in foo_new() initialized it to zero */
66 && TEST_int_eq(data->i, 42));
69 static int test_app_context(void)
71 OPENSSL_CTX *ctx = NULL;
72 int result = (TEST_ptr(ctx = OPENSSL_CTX_new()) && test_context(ctx));
74 OPENSSL_CTX_free(ctx);
78 static int test_def_context(void)
80 return test_context(NULL);
86 ADD_TEST(test_app_context);
87 ADD_TEST(test_def_context);