sanitytest.c 6.6 KB

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