sanitytest.c 4.9 KB

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