xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/sanitytest.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1c7da899bSchristos /*
2*b0d17251Schristos  * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
3c7da899bSchristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5c7da899bSchristos  * this file except in compliance with the License.  You can obtain a copy
6c7da899bSchristos  * in the file LICENSE in the source distribution or at
7c7da899bSchristos  * https://www.openssl.org/source/license.html
8c7da899bSchristos  */
9c7da899bSchristos 
10c7da899bSchristos #include <string.h>
11*b0d17251Schristos #include <openssl/types.h>
1213d40330Schristos #include "testutil.h"
1313d40330Schristos #include "internal/numbers.h"
14c7da899bSchristos 
test_sanity_null_zero(void)1513d40330Schristos static int test_sanity_null_zero(void)
1613d40330Schristos {
1713d40330Schristos     char *p;
1813d40330Schristos     char bytes[sizeof(p)];
19c7da899bSchristos 
2013d40330Schristos     /* Is NULL equivalent to all-bytes-zero? */
2113d40330Schristos     p = NULL;
2213d40330Schristos     memset(bytes, 0, sizeof(bytes));
2313d40330Schristos     return TEST_mem_eq(&p, sizeof(p), bytes, sizeof(bytes));
2413d40330Schristos }
25c7da899bSchristos 
test_sanity_enum_size(void)2613d40330Schristos static int test_sanity_enum_size(void)
2713d40330Schristos {
28c7da899bSchristos     enum smallchoices { sa, sb, sc };
29c7da899bSchristos     enum medchoices { ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml };
30c7da899bSchristos     enum largechoices {
31c7da899bSchristos         a01, b01, c01, d01, e01, f01, g01, h01, i01, j01,
32c7da899bSchristos         a02, b02, c02, d02, e02, f02, g02, h02, i02, j02,
33c7da899bSchristos         a03, b03, c03, d03, e03, f03, g03, h03, i03, j03,
34c7da899bSchristos         a04, b04, c04, d04, e04, f04, g04, h04, i04, j04,
35c7da899bSchristos         a05, b05, c05, d05, e05, f05, g05, h05, i05, j05,
36c7da899bSchristos         a06, b06, c06, d06, e06, f06, g06, h06, i06, j06,
37c7da899bSchristos         a07, b07, c07, d07, e07, f07, g07, h07, i07, j07,
38c7da899bSchristos         a08, b08, c08, d08, e08, f08, g08, h08, i08, j08,
39c7da899bSchristos         a09, b09, c09, d09, e09, f09, g09, h09, i09, j09,
40c7da899bSchristos         a10, b10, c10, d10, e10, f10, g10, h10, i10, j10,
41c7da899bSchristos         xxx };
42c7da899bSchristos 
43c7da899bSchristos     /* Enum size */
4413d40330Schristos     if (!TEST_size_t_eq(sizeof(enum smallchoices), sizeof(int))
4513d40330Schristos         || !TEST_size_t_eq(sizeof(enum medchoices), sizeof(int))
4613d40330Schristos         || !TEST_size_t_eq(sizeof(enum largechoices), sizeof(int)))
4713d40330Schristos         return 0;
4813d40330Schristos     return 1;
49c7da899bSchristos }
5013d40330Schristos 
test_sanity_twos_complement(void)5113d40330Schristos static int test_sanity_twos_complement(void)
5213d40330Schristos {
5313d40330Schristos     /* Basic two's complement checks. */
5413d40330Schristos     if (!TEST_int_eq(~(-1), 0)
5513d40330Schristos         || !TEST_long_eq(~(-1L), 0L))
5613d40330Schristos         return 0;
5713d40330Schristos     return 1;
5813d40330Schristos }
5913d40330Schristos 
test_sanity_sign(void)6013d40330Schristos static int test_sanity_sign(void)
6113d40330Schristos {
6213d40330Schristos     /* Check that values with sign bit 1 and value bits 0 are valid */
6313d40330Schristos     if (!TEST_int_eq(-(INT_MIN + 1), INT_MAX)
6413d40330Schristos         || !TEST_long_eq(-(LONG_MIN + 1), LONG_MAX))
6513d40330Schristos         return 0;
6613d40330Schristos     return 1;
6713d40330Schristos }
6813d40330Schristos 
test_sanity_unsigned_conversion(void)6913d40330Schristos static int test_sanity_unsigned_conversion(void)
7013d40330Schristos {
7113d40330Schristos     /* Check that unsigned-to-signed conversions preserve bit patterns */
7213d40330Schristos     if (!TEST_int_eq((int)((unsigned int)INT_MAX + 1), INT_MIN)
7313d40330Schristos         || !TEST_long_eq((long)((unsigned long)LONG_MAX + 1), LONG_MIN))
7413d40330Schristos         return 0;
7513d40330Schristos     return 1;
7613d40330Schristos }
7713d40330Schristos 
test_sanity_range(void)7813d40330Schristos static int test_sanity_range(void)
7913d40330Schristos {
80*b0d17251Schristos     /* Verify some types are the correct size */
81*b0d17251Schristos     if (!TEST_size_t_eq(sizeof(int8_t), 1)
82*b0d17251Schristos             || !TEST_size_t_eq(sizeof(uint8_t), 1)
83*b0d17251Schristos             || !TEST_size_t_eq(sizeof(int16_t), 2)
84*b0d17251Schristos             || !TEST_size_t_eq(sizeof(uint16_t), 2)
85*b0d17251Schristos             || !TEST_size_t_eq(sizeof(int32_t), 4)
86*b0d17251Schristos             || !TEST_size_t_eq(sizeof(uint32_t), 4)
87*b0d17251Schristos             || !TEST_size_t_eq(sizeof(int64_t), 8)
88*b0d17251Schristos             || !TEST_size_t_eq(sizeof(uint64_t), 8)
89*b0d17251Schristos #ifdef UINT128_MAX
90*b0d17251Schristos             || !TEST_size_t_eq(sizeof(int128_t), 16)
91*b0d17251Schristos             || !TEST_size_t_eq(sizeof(uint128_t), 16)
92*b0d17251Schristos #endif
93*b0d17251Schristos             || !TEST_size_t_eq(sizeof(char), 1)
94*b0d17251Schristos             || !TEST_size_t_eq(sizeof(unsigned char), 1))
95*b0d17251Schristos         return 0;
96*b0d17251Schristos 
97*b0d17251Schristos     /* We want our long longs to be at least 64 bits */
98*b0d17251Schristos     if (!TEST_size_t_ge(sizeof(long long int), 8)
99*b0d17251Schristos             || !TEST_size_t_ge(sizeof(unsigned long long int), 8))
100*b0d17251Schristos         return 0;
101*b0d17251Schristos 
102*b0d17251Schristos     /*
103*b0d17251Schristos      * Verify intmax_t.
104*b0d17251Schristos      * Some platforms defined intmax_t to be 64 bits but still support
105*b0d17251Schristos      * an int128_t, so this check is for at least 64 bits.
106*b0d17251Schristos      */
107*b0d17251Schristos     if (!TEST_size_t_ge(sizeof(ossl_intmax_t), 8)
108*b0d17251Schristos             || !TEST_size_t_ge(sizeof(ossl_uintmax_t), 8)
109*b0d17251Schristos             || !TEST_size_t_ge(sizeof(ossl_uintmax_t), sizeof(size_t)))
110*b0d17251Schristos         return 0;
111*b0d17251Schristos 
11213d40330Schristos     /* This isn't possible to check using the framework functions */
11313d40330Schristos     if (SIZE_MAX < INT_MAX) {
11413d40330Schristos         TEST_error("int must not be wider than size_t");
11513d40330Schristos         return 0;
11613d40330Schristos     }
117*b0d17251Schristos 
118*b0d17251Schristos     /* SIZE_MAX is always greater than 2*INT_MAX */
119*b0d17251Schristos     if (SIZE_MAX - INT_MAX <= INT_MAX) {
120*b0d17251Schristos         TEST_error("SIZE_MAX must exceed 2*INT_MAX");
121*b0d17251Schristos         return 0;
122*b0d17251Schristos     }
123*b0d17251Schristos 
12413d40330Schristos     return 1;
12513d40330Schristos }
12613d40330Schristos 
test_sanity_memcmp(void)12713d40330Schristos static int test_sanity_memcmp(void)
12813d40330Schristos {
12913d40330Schristos     return CRYPTO_memcmp("ab", "cd", 2);
13013d40330Schristos }
13113d40330Schristos 
setup_tests(void)13213d40330Schristos int setup_tests(void)
13313d40330Schristos {
13413d40330Schristos     ADD_TEST(test_sanity_null_zero);
13513d40330Schristos     ADD_TEST(test_sanity_enum_size);
13613d40330Schristos     ADD_TEST(test_sanity_twos_complement);
13713d40330Schristos     ADD_TEST(test_sanity_sign);
13813d40330Schristos     ADD_TEST(test_sanity_unsigned_conversion);
13913d40330Schristos     ADD_TEST(test_sanity_range);
14013d40330Schristos     ADD_TEST(test_sanity_memcmp);
14113d40330Schristos     return 1;
14213d40330Schristos }
14313d40330Schristos 
144