]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/exdatatest.c
Fix the error handling in CRYPTO_dup_ex_data.
[thirdparty/openssl.git] / test / exdatatest.c
1 /*
2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (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 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <openssl/crypto.h>
14
15 static long saved_argl;
16 static void *saved_argp;
17 static int saved_idx;
18
19 static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
20 int idx, long argl, void *argp)
21 {
22 OPENSSL_assert(idx == saved_idx);
23 OPENSSL_assert(argl == saved_argl);
24 OPENSSL_assert(argp == saved_argp);
25 OPENSSL_assert(ptr == NULL);
26 }
27
28 static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
29 void *from_d, int idx, long argl, void *argp)
30 {
31 OPENSSL_assert(idx == saved_idx);
32 OPENSSL_assert(argl == saved_argl);
33 OPENSSL_assert(argp == saved_argp);
34 OPENSSL_assert(from_d != NULL);
35 return 1;
36 }
37
38 static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
39 int idx, long argl, void *argp)
40 {
41 OPENSSL_assert(idx == saved_idx);
42 OPENSSL_assert(argl == saved_argl);
43 OPENSSL_assert(argp == saved_argp);
44 }
45
46 typedef struct myobj_st {
47 CRYPTO_EX_DATA ex_data;
48 int id;
49 int st;
50 } MYOBJ;
51
52 static MYOBJ *MYOBJ_new()
53 {
54 static int count = 0;
55 MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
56
57 obj->id = ++count;
58 obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
59 OPENSSL_assert(obj->st != 0);
60 return obj;
61 }
62
63 static void MYOBJ_sethello(MYOBJ *obj, char *cp)
64 {
65 obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
66 OPENSSL_assert(obj->st != 0);
67 }
68
69 static char *MYOBJ_gethello(MYOBJ *obj)
70 {
71 return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
72 }
73
74 static void MYOBJ_free(MYOBJ *obj)
75 {
76 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
77 OPENSSL_free(obj);
78 }
79
80 static MYOBJ *MYOBJ_dup(MYOBJ *in)
81 {
82 MYOBJ *obj = MYOBJ_new();
83
84 obj->st = CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
85 &in->ex_data);
86 OPENSSL_assert(obj->st != 0);
87 return obj;
88 }
89
90 int main()
91 {
92 MYOBJ *t1, *t2, *t3;
93 const char *cp;
94 char *p;
95
96 p = strdup("hello world");
97 saved_argl = 21;
98 saved_argp = malloc(1);
99 saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
100 saved_argl, saved_argp,
101 exnew, exdup, exfree);
102 t1 = MYOBJ_new();
103 t2 = MYOBJ_new();
104 MYOBJ_sethello(t1, p);
105 cp = MYOBJ_gethello(t1);
106 OPENSSL_assert(cp == p);
107 if (cp != p)
108 return 1;
109 cp = MYOBJ_gethello(t2);
110 OPENSSL_assert(cp == NULL);
111 if (cp != NULL)
112 return 1;
113 t3 = MYOBJ_dup(t1);
114 cp = MYOBJ_gethello(t3);
115 OPENSSL_assert(cp == p);
116 if (cp != p)
117 return 1;
118 cp = MYOBJ_gethello(t2);
119 MYOBJ_free(t1);
120 MYOBJ_free(t2);
121 MYOBJ_free(t3);
122 free(saved_argp);
123 free(p);
124 return 0;
125 }