xref: /freebsd/crypto/openssl/test/sanitytest.c (revision 62a52c15422470f97fc7b311d89c83f910bcc1b1)
1 /*
2  * Copyright 2015-2022 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 #include <string.h>
11 #include <openssl/types.h>
12 #include "testutil.h"
13 #include "internal/numbers.h"
14 
15 static int test_sanity_null_zero(void)
16 {
17     char *p;
18     char bytes[sizeof(p)];
19 
20     /* Is NULL equivalent to all-bytes-zero? */
21     p = NULL;
22     memset(bytes, 0, sizeof(bytes));
23     return TEST_mem_eq(&p, sizeof(p), bytes, sizeof(bytes));
24 }
25 
26 static int test_sanity_enum_size(void)
27 {
28     enum smallchoices { sa, sb, sc };
29     enum medchoices { ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml };
30     enum largechoices {
31         a01, b01, c01, d01, e01, f01, g01, h01, i01, j01,
32         a02, b02, c02, d02, e02, f02, g02, h02, i02, j02,
33         a03, b03, c03, d03, e03, f03, g03, h03, i03, j03,
34         a04, b04, c04, d04, e04, f04, g04, h04, i04, j04,
35         a05, b05, c05, d05, e05, f05, g05, h05, i05, j05,
36         a06, b06, c06, d06, e06, f06, g06, h06, i06, j06,
37         a07, b07, c07, d07, e07, f07, g07, h07, i07, j07,
38         a08, b08, c08, d08, e08, f08, g08, h08, i08, j08,
39         a09, b09, c09, d09, e09, f09, g09, h09, i09, j09,
40         a10, b10, c10, d10, e10, f10, g10, h10, i10, j10,
41         xxx };
42 
43     /* Enum size */
44     if (!TEST_size_t_eq(sizeof(enum smallchoices), sizeof(int))
45         || !TEST_size_t_eq(sizeof(enum medchoices), sizeof(int))
46         || !TEST_size_t_eq(sizeof(enum largechoices), sizeof(int)))
47         return 0;
48     return 1;
49 }
50 
51 static int test_sanity_twos_complement(void)
52 {
53     /* Basic two's complement checks. */
54     if (!TEST_int_eq(~(-1), 0)
55         || !TEST_long_eq(~(-1L), 0L))
56         return 0;
57     return 1;
58 }
59 
60 static int test_sanity_sign(void)
61 {
62     /* Check that values with sign bit 1 and value bits 0 are valid */
63     if (!TEST_int_eq(-(INT_MIN + 1), INT_MAX)
64         || !TEST_long_eq(-(LONG_MIN + 1), LONG_MAX))
65         return 0;
66     return 1;
67 }
68 
69 static int test_sanity_unsigned_conversion(void)
70 {
71     /* Check that unsigned-to-signed conversions preserve bit patterns */
72     if (!TEST_int_eq((int)((unsigned int)INT_MAX + 1), INT_MIN)
73         || !TEST_long_eq((long)((unsigned long)LONG_MAX + 1), LONG_MIN))
74         return 0;
75     return 1;
76 }
77 
78 static int test_sanity_range(void)
79 {
80     /* Verify some types are the correct size */
81     if (!TEST_size_t_eq(sizeof(int8_t), 1)
82             || !TEST_size_t_eq(sizeof(uint8_t), 1)
83             || !TEST_size_t_eq(sizeof(int16_t), 2)
84             || !TEST_size_t_eq(sizeof(uint16_t), 2)
85             || !TEST_size_t_eq(sizeof(int32_t), 4)
86             || !TEST_size_t_eq(sizeof(uint32_t), 4)
87             || !TEST_size_t_eq(sizeof(int64_t), 8)
88             || !TEST_size_t_eq(sizeof(uint64_t), 8)
89 #ifdef UINT128_MAX
90             || !TEST_size_t_eq(sizeof(int128_t), 16)
91             || !TEST_size_t_eq(sizeof(uint128_t), 16)
92 #endif
93             || !TEST_size_t_eq(sizeof(char), 1)
94             || !TEST_size_t_eq(sizeof(unsigned char), 1))
95         return 0;
96 
97     /* We want our long longs to be at least 64 bits */
98     if (!TEST_size_t_ge(sizeof(long long int), 8)
99             || !TEST_size_t_ge(sizeof(unsigned long long int), 8))
100         return 0;
101 
102     /*
103      * Verify intmax_t.
104      * Some platforms defined intmax_t to be 64 bits but still support
105      * an int128_t, so this check is for at least 64 bits.
106      */
107     if (!TEST_size_t_ge(sizeof(ossl_intmax_t), 8)
108             || !TEST_size_t_ge(sizeof(ossl_uintmax_t), 8)
109             || !TEST_size_t_ge(sizeof(ossl_uintmax_t), sizeof(size_t)))
110         return 0;
111 
112     /* This isn't possible to check using the framework functions */
113     if (SIZE_MAX < INT_MAX) {
114         TEST_error("int must not be wider than size_t");
115         return 0;
116     }
117 
118     /* SIZE_MAX is always greater than 2*INT_MAX */
119     if (SIZE_MAX - INT_MAX <= INT_MAX) {
120         TEST_error("SIZE_MAX must exceed 2*INT_MAX");
121         return 0;
122     }
123 
124     return 1;
125 }
126 
127 static int test_sanity_memcmp(void)
128 {
129     return CRYPTO_memcmp("ab", "cd", 2);
130 }
131 
132 int setup_tests(void)
133 {
134     ADD_TEST(test_sanity_null_zero);
135     ADD_TEST(test_sanity_enum_size);
136     ADD_TEST(test_sanity_twos_complement);
137     ADD_TEST(test_sanity_sign);
138     ADD_TEST(test_sanity_unsigned_conversion);
139     ADD_TEST(test_sanity_range);
140     ADD_TEST(test_sanity_memcmp);
141     return 1;
142 }
143 
144