add ASN1_INTEGER type to d2i_test
[openssl.git] / test / secmemtest.c
1
2 #include <openssl/crypto.h>
3
4 #define perror_line()    perror_line1(__LINE__)
5 #define perror_line1(l)  perror_line2(l)
6 #define perror_line2(l)  perror("failed " #l)
7
8 int main(int argc, char **argv)
9 {
10 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
11     char *p = NULL, *q = NULL, *r = NULL, *s = NULL;
12
13     r = OPENSSL_secure_malloc(20);
14     /* r = non-secure 20 */
15     if (r == NULL) {
16         perror_line();
17         return 1;
18     }
19     if (!CRYPTO_secure_malloc_init(4096, 32)) {
20         perror_line();
21         return 1;
22     }
23     if (CRYPTO_secure_allocated(r)) {
24         perror_line();
25         return 1;
26     }
27     p = OPENSSL_secure_malloc(20);
28     /* r = non-secure 20, p = secure 20 */
29     if (!CRYPTO_secure_allocated(p)) {
30         perror_line();
31         return 1;
32     }
33     /* 20 secure -> 32-byte minimum allocaton unit */
34     if (CRYPTO_secure_used() != 32) {
35         perror_line();
36         return 1;
37     }
38     q = OPENSSL_malloc(20);
39     /* r = non-secure 20, p = secure 20, q = non-secure 20 */
40     if (CRYPTO_secure_allocated(q)) {
41         perror_line();
42         return 1;
43     }
44     s = OPENSSL_secure_malloc(20);
45     /* r = non-secure 20, p = secure 20, q = non-secure 20, s = secure 20 */
46     if (!CRYPTO_secure_allocated(s)) {
47         perror_line();
48         return 1;
49     }
50     /* 2 * 20 secure -> 64 bytes allocated */
51     if (CRYPTO_secure_used() != 64) {
52         perror_line();
53         return 1;
54     }
55     OPENSSL_secure_free(p);
56     /* 20 secure -> 32 bytes allocated */
57     if (CRYPTO_secure_used() != 32) {
58         perror_line();
59         return 1;
60     }
61     OPENSSL_free(q);
62     /* should not complete, as secure memory is still allocated */
63     if (CRYPTO_secure_malloc_done()) {
64         perror_line();
65         return 1;
66     }
67     if (!CRYPTO_secure_malloc_initialized()) {
68         perror_line();
69         return 1;
70     }
71     OPENSSL_secure_free(s);
72     /* secure memory should now be 0, so done should complete */
73     if (CRYPTO_secure_used() != 0) {
74         perror_line();
75         return 1;
76     }
77     if (!CRYPTO_secure_malloc_done()) {
78         perror_line();
79         return 1;
80     }
81     if (CRYPTO_secure_malloc_initialized()) {
82         perror_line();
83         return 1;
84     }
85     /* this can complete - it was not really secure */
86     OPENSSL_secure_free(r);
87 #else
88     /* Should fail. */
89     if (CRYPTO_secure_malloc_init(4096, 32)) {
90         perror_line();
91         return 1;
92     }
93 #endif
94     return 0;
95 }