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