Limit the output of the enc -ciphers command to just the ciphers enc can
[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 <assert.h>
14 #include <openssl/crypto.h>
15
16 static long saved_argl;
17 static void *saved_argp;
18 static int saved_idx;
19
20 static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
21           int idx, long argl, void *argp)
22 {
23     assert(idx == saved_idx);
24     assert(argl == saved_argl);
25     assert(argp == saved_argp);
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     assert(idx == saved_idx);
32     assert(argl == saved_argl);
33     assert(argp == saved_argp);
34     return 0;
35 }
36
37 static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
38             int idx, long argl, void *argp)
39 {
40     assert(idx == saved_idx);
41     assert(argl == saved_argl);
42     assert(argp == saved_argp);
43 }
44
45 typedef struct myobj_st {
46     CRYPTO_EX_DATA ex_data;
47     int id;
48     int st;
49 } MYOBJ;
50
51 static MYOBJ *MYOBJ_new()
52 {
53     static int count = 0;
54     MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
55
56     obj->id = ++count;
57     obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
58     assert(obj->st != 0);
59     return obj;
60 }
61
62 static void MYOBJ_sethello(MYOBJ *obj, char *cp)
63 {
64     obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
65     assert(obj->st != 0);
66 }
67
68 static char *MYOBJ_gethello(MYOBJ *obj)
69 {
70     return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
71 }
72
73 static void MYOBJ_free(MYOBJ *obj)
74 {
75     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
76     OPENSSL_free(obj);
77 }
78
79 int main()
80 {
81     MYOBJ *t1, *t2;
82     const char *cp;
83     char *p;
84
85     p = strdup("hello world");
86     saved_argl = 21;
87     saved_argp = malloc(1);
88     saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
89                                         saved_argl, saved_argp,
90                                         exnew, exdup, exfree);
91     t1 = MYOBJ_new();
92     t2 = MYOBJ_new();
93     MYOBJ_sethello(t1, p);
94     cp = MYOBJ_gethello(t1);
95     assert(cp == p);
96     if (cp != p)
97         return 1;
98     cp = MYOBJ_gethello(t2);
99     assert(cp == NULL);
100     if (cp != NULL)
101         return 1;
102     MYOBJ_free(t1);
103     MYOBJ_free(t2);
104     free(saved_argp);
105     free(p);
106     return 0;
107 }