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