]>
Commit | Line | Data |
---|---|---|
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 | ||
d64b6299 RL |
25 | typedef struct foo_st { |
26 | int i; | |
27 | void *data; | |
28 | } FOO; | |
29 | ||
1aedc35f | 30 | static void *foo_new(OPENSSL_CTX *ctx) |
d64b6299 RL |
31 | { |
32 | FOO *ptr = OPENSSL_zalloc(sizeof(*ptr)); | |
33 | if (ptr != NULL) | |
34 | ptr->i = 42; | |
35 | return ptr; | |
36 | } | |
37 | static void foo_free(void *ptr) | |
38 | { | |
39 | OPENSSL_free(ptr); | |
40 | } | |
41 | static const OPENSSL_CTX_METHOD foo_method = { | |
42 | foo_new, | |
43 | foo_free | |
44 | }; | |
45 | ||
d64b6299 RL |
46 | /* |
47 | * END EXAMPLE | |
48 | * ====================================================================== | |
49 | */ | |
50 | ||
51 | static int test_context(OPENSSL_CTX *ctx) | |
52 | { | |
53 | FOO *data = NULL; | |
54 | ||
1aedc35f | 55 | return TEST_ptr(data = openssl_ctx_get_data(ctx, 0, &foo_method)) |
0b76ce99 RL |
56 | /* OPENSSL_zalloc in foo_new() initialized it to zero */ |
57 | && TEST_int_eq(data->i, 42); | |
d64b6299 RL |
58 | } |
59 | ||
60 | static int test_app_context(void) | |
61 | { | |
62 | OPENSSL_CTX *ctx = NULL; | |
0b76ce99 | 63 | int result = |
1aedc35f | 64 | TEST_ptr(ctx = OPENSSL_CTX_new()) |
0b76ce99 | 65 | && test_context(ctx); |
d64b6299 RL |
66 | |
67 | OPENSSL_CTX_free(ctx); | |
68 | return result; | |
69 | } | |
70 | ||
71 | static int test_def_context(void) | |
72 | { | |
73 | return test_context(NULL); | |
74 | } | |
75 | ||
76 | int setup_tests(void) | |
77 | { | |
d64b6299 RL |
78 | ADD_TEST(test_app_context); |
79 | ADD_TEST(test_def_context); | |
80 | return 1; | |
81 | } |