1 /*
2  * Copyright 2015-2023 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 #include "internal/time.h"
15 
16 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
17 # include <signal.h>
18 #endif
19 
test_sanity_null_zero(void)20 static int test_sanity_null_zero(void)
21 {
22     char *p;
23     char bytes[sizeof(p)];
24 
25     /* Is NULL equivalent to all-bytes-zero? */
26     p = NULL;
27     memset(bytes, 0, sizeof(bytes));
28     return TEST_mem_eq(&p, sizeof(p), bytes, sizeof(bytes));
29 }
30 
test_sanity_enum_size(void)31 static int test_sanity_enum_size(void)
32 {
33     enum smallchoices { sa, sb, sc };
34     enum medchoices { ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml };
35     enum largechoices {
36         a01, b01, c01, d01, e01, f01, g01, h01, i01, j01,
37         a02, b02, c02, d02, e02, f02, g02, h02, i02, j02,
38         a03, b03, c03, d03, e03, f03, g03, h03, i03, j03,
39         a04, b04, c04, d04, e04, f04, g04, h04, i04, j04,
40         a05, b05, c05, d05, e05, f05, g05, h05, i05, j05,
41         a06, b06, c06, d06, e06, f06, g06, h06, i06, j06,
42         a07, b07, c07, d07, e07, f07, g07, h07, i07, j07,
43         a08, b08, c08, d08, e08, f08, g08, h08, i08, j08,
44         a09, b09, c09, d09, e09, f09, g09, h09, i09, j09,
45         a10, b10, c10, d10, e10, f10, g10, h10, i10, j10,
46         xxx };
47 
48     /* Enum size */
49     if (!TEST_size_t_eq(sizeof(enum smallchoices), sizeof(int))
50         || !TEST_size_t_eq(sizeof(enum medchoices), sizeof(int))
51         || !TEST_size_t_eq(sizeof(enum largechoices), sizeof(int)))
52         return 0;
53     return 1;
54 }
55 
test_sanity_twos_complement(void)56 static int test_sanity_twos_complement(void)
57 {
58     /* Basic two's complement checks. */
59     if (!TEST_int_eq(~(-1), 0)
60         || !TEST_long_eq(~(-1L), 0L))
61         return 0;
62     return 1;
63 }
64 
test_sanity_sign(void)65 static int test_sanity_sign(void)
66 {
67     /* Check that values with sign bit 1 and value bits 0 are valid */
68     if (!TEST_int_eq(-(INT_MIN + 1), INT_MAX)
69         || !TEST_long_eq(-(LONG_MIN + 1), LONG_MAX))
70         return 0;
71     return 1;
72 }
73 
test_sanity_unsigned_conversion(void)74 static int test_sanity_unsigned_conversion(void)
75 {
76     /* Check that unsigned-to-signed conversions preserve bit patterns */
77     if (!TEST_int_eq((int)((unsigned int)INT_MAX + 1), INT_MIN)
78         || !TEST_long_eq((long)((unsigned long)LONG_MAX + 1), LONG_MIN))
79         return 0;
80     return 1;
81 }
82 
test_sanity_range(void)83 static int test_sanity_range(void)
84 {
85     /* Verify some types are the correct size */
86     if (!TEST_size_t_eq(sizeof(int8_t), 1)
87             || !TEST_size_t_eq(sizeof(uint8_t), 1)
88             || !TEST_size_t_eq(sizeof(int16_t), 2)
89             || !TEST_size_t_eq(sizeof(uint16_t), 2)
90             || !TEST_size_t_eq(sizeof(int32_t), 4)
91             || !TEST_size_t_eq(sizeof(uint32_t), 4)
92             || !TEST_size_t_eq(sizeof(int64_t), 8)
93             || !TEST_size_t_eq(sizeof(uint64_t), 8)
94 #ifdef UINT128_MAX
95             || !TEST_size_t_eq(sizeof(int128_t), 16)
96             || !TEST_size_t_eq(sizeof(uint128_t), 16)
97 #endif
98             || !TEST_size_t_eq(sizeof(char), 1)
99             || !TEST_size_t_eq(sizeof(unsigned char), 1))
100         return 0;
101 
102     /* We want our long longs to be at least 64 bits */
103     if (!TEST_size_t_ge(sizeof(long long int), 8)
104             || !TEST_size_t_ge(sizeof(unsigned long long int), 8))
105         return 0;
106 
107     /*
108      * Verify intmax_t.
109      * Some platforms defined intmax_t to be 64 bits but still support
110      * an int128_t, so this check is for at least 64 bits.
111      */
112     if (!TEST_size_t_ge(sizeof(ossl_intmax_t), 8)
113             || !TEST_size_t_ge(sizeof(ossl_uintmax_t), 8)
114             || !TEST_size_t_ge(sizeof(ossl_uintmax_t), sizeof(size_t)))
115         return 0;
116 
117     /* This isn't possible to check using the framework functions */
118     if (SIZE_MAX < INT_MAX) {
119         TEST_error("int must not be wider than size_t");
120         return 0;
121     }
122 
123     /* SIZE_MAX is always greater than 2*INT_MAX */
124     if (SIZE_MAX - INT_MAX <= INT_MAX) {
125         TEST_error("SIZE_MAX must exceed 2*INT_MAX");
126         return 0;
127     }
128 
129     return 1;
130 }
131 
test_sanity_memcmp(void)132 static int test_sanity_memcmp(void)
133 {
134     return CRYPTO_memcmp("ab", "cd", 2);
135 }
136 
137 static const struct sleep_test_vector {
138     uint64_t val;
139 } sleep_test_vectors[] = { { 0 }, { 1 }, { 999 }, { 1000 } };
140 
141 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
142 static void
alrm_handler(int sig)143 alrm_handler(int sig)
144 {
145 }
146 #endif /* defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L */
147 
test_sanity_sleep(int i)148 static int test_sanity_sleep(int i)
149 {
150     const struct sleep_test_vector * const td = sleep_test_vectors + i;
151     OSSL_TIME start = ossl_time_now();
152     uint64_t ms;
153 
154 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
155     /*
156      * Set up an interrupt timer to check that OSSL_sleep doesn't return early
157      * due to interrupts.
158      */
159     do {
160         static const struct sigaction sa = { .sa_handler = alrm_handler };
161         static const struct itimerval it = { .it_value.tv_usec = 111111 };
162         sigset_t mask;
163 
164         if (sigaction(SIGALRM, &sa, NULL)) {
165             TEST_perror("test_sanity_sleep: sigaction");
166             break;
167         }
168 
169         sigemptyset(&mask);
170         sigaddset(&mask, SIGALRM);
171         if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
172             TEST_perror("test_sanity_sleep: sigprocmask");
173             break;
174         }
175 
176         if (setitimer(ITIMER_REAL, &it, NULL)) {
177             TEST_perror("test_sanity_sleep: arm setitimer");
178             break;
179         }
180     } while (0);
181 #endif /* defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L */
182 
183     /*
184      * On any reasonable system this must sleep at least the specified time
185      * but not more than 20 seconds more than that.
186      */
187     OSSL_sleep(td->val);
188 
189 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
190     /* disarm the timer */
191     do {
192         static const struct itimerval it;
193 
194         if (setitimer(ITIMER_REAL, &it, NULL)) {
195             TEST_perror("test_sanity_sleep: disarm setitimer");
196             break;
197         }
198     } while (0);
199 #endif /* defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L */
200 
201     ms = ossl_time2ms(ossl_time_subtract(ossl_time_now(), start));
202 
203     if (!TEST_uint64_t_ge(ms, td->val) + !TEST_uint64_t_le(ms, td->val + 20000))
204         return 0;
205     return 1;
206 }
207 
setup_tests(void)208 int setup_tests(void)
209 {
210     ADD_TEST(test_sanity_null_zero);
211     ADD_TEST(test_sanity_enum_size);
212     ADD_TEST(test_sanity_twos_complement);
213     ADD_TEST(test_sanity_sign);
214     ADD_TEST(test_sanity_unsigned_conversion);
215     ADD_TEST(test_sanity_range);
216     ADD_TEST(test_sanity_memcmp);
217     ADD_ALL_TESTS(test_sanity_sleep, OSSL_NELEM(sleep_test_vectors));
218     return 1;
219 }
220