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