sanitytest.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2015-2018 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 "testutil.h"
  11. #include "internal/numbers.h"
  12. static int test_sanity_null_zero(void)
  13. {
  14. char *p;
  15. char bytes[sizeof(p)];
  16. /* Is NULL equivalent to all-bytes-zero? */
  17. p = NULL;
  18. memset(bytes, 0, sizeof(bytes));
  19. return TEST_mem_eq(&p, sizeof(p), bytes, sizeof(bytes));
  20. }
  21. static int test_sanity_enum_size(void)
  22. {
  23. enum smallchoices { sa, sb, sc };
  24. enum medchoices { ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml };
  25. enum largechoices {
  26. a01, b01, c01, d01, e01, f01, g01, h01, i01, j01,
  27. a02, b02, c02, d02, e02, f02, g02, h02, i02, j02,
  28. a03, b03, c03, d03, e03, f03, g03, h03, i03, j03,
  29. a04, b04, c04, d04, e04, f04, g04, h04, i04, j04,
  30. a05, b05, c05, d05, e05, f05, g05, h05, i05, j05,
  31. a06, b06, c06, d06, e06, f06, g06, h06, i06, j06,
  32. a07, b07, c07, d07, e07, f07, g07, h07, i07, j07,
  33. a08, b08, c08, d08, e08, f08, g08, h08, i08, j08,
  34. a09, b09, c09, d09, e09, f09, g09, h09, i09, j09,
  35. a10, b10, c10, d10, e10, f10, g10, h10, i10, j10,
  36. xxx };
  37. /* Enum size */
  38. if (!TEST_size_t_eq(sizeof(enum smallchoices), sizeof(int))
  39. || !TEST_size_t_eq(sizeof(enum medchoices), sizeof(int))
  40. || !TEST_size_t_eq(sizeof(enum largechoices), sizeof(int)))
  41. return 0;
  42. return 1;
  43. }
  44. static int test_sanity_twos_complement(void)
  45. {
  46. /* Basic two's complement checks. */
  47. if (!TEST_int_eq(~(-1), 0)
  48. || !TEST_long_eq(~(-1L), 0L))
  49. return 0;
  50. return 1;
  51. }
  52. static int test_sanity_sign(void)
  53. {
  54. /* Check that values with sign bit 1 and value bits 0 are valid */
  55. if (!TEST_int_eq(-(INT_MIN + 1), INT_MAX)
  56. || !TEST_long_eq(-(LONG_MIN + 1), LONG_MAX))
  57. return 0;
  58. return 1;
  59. }
  60. static int test_sanity_unsigned_conversion(void)
  61. {
  62. /* Check that unsigned-to-signed conversions preserve bit patterns */
  63. if (!TEST_int_eq((int)((unsigned int)INT_MAX + 1), INT_MIN)
  64. || !TEST_long_eq((long)((unsigned long)LONG_MAX + 1), LONG_MIN))
  65. return 0;
  66. return 1;
  67. }
  68. static int test_sanity_range(void)
  69. {
  70. /* This isn't possible to check using the framework functions */
  71. if (SIZE_MAX < INT_MAX) {
  72. TEST_error("int must not be wider than size_t");
  73. return 0;
  74. }
  75. return 1;
  76. }
  77. static int test_sanity_memcmp(void)
  78. {
  79. return CRYPTO_memcmp("ab","cd",2);
  80. }
  81. int setup_tests(void)
  82. {
  83. ADD_TEST(test_sanity_null_zero);
  84. ADD_TEST(test_sanity_enum_size);
  85. ADD_TEST(test_sanity_twos_complement);
  86. ADD_TEST(test_sanity_sign);
  87. ADD_TEST(test_sanity_unsigned_conversion);
  88. ADD_TEST(test_sanity_range);
  89. ADD_TEST(test_sanity_memcmp);
  90. return 1;
  91. }