test_string_bytes.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdarg.h>
  3. #include <stdbool.h>
  4. #include <stddef.h>
  5. #include <setjmp.h>
  6. #include <cmocka.h>
  7. #include "tpm2_util.h"
  8. static void test_is_big_endian(void **state) {
  9. uint16_t test = 0xFF00;
  10. uint8_t *b = (uint8_t *) &test;
  11. (void) state;
  12. bool test_host_is_big_endian = b[0] == 0xFF;
  13. bool host_is_big_endian = tpm2_util_is_big_endian();
  14. assert_true(test_host_is_big_endian == host_is_big_endian);
  15. }
  16. static void test_popcount(void **state) {
  17. (void) state;
  18. UINT32 count = tpm2_util_pop_count(0x4453E424);
  19. assert_int_equal(12, count);
  20. count = tpm2_util_pop_count(0);
  21. assert_int_equal(0, count);
  22. count = tpm2_util_pop_count(~0);
  23. assert_int_equal(32, count);
  24. }
  25. #define TEST_ENDIAN_CONVERT(size, value, expected) \
  26. static void test_convert_##size(void **state) { \
  27. \
  28. (void)state; \
  29. UINT##size test = tpm2_util_endian_swap_##size(value); \
  30. assert_int_equal(test, expected); \
  31. }
  32. TEST_ENDIAN_CONVERT(16, 0xFF00, 0x00FF)
  33. TEST_ENDIAN_CONVERT(32, 0xAABBCCDD, 0xDDCCBBAA)
  34. TEST_ENDIAN_CONVERT(64, 0x0011223344556677, 0x7766554433221100)
  35. #define TEST_ENDIAN_HTON(size, value, le_expected) \
  36. static void test_hton_##size(void **state) { \
  37. \
  38. (void)state; \
  39. UINT##size test = tpm2_util_hton_##size(value); \
  40. bool is_big_endian = tpm2_util_is_big_endian(); \
  41. UINT##size expected = is_big_endian ? value : le_expected; \
  42. assert_int_equal(test, expected); \
  43. \
  44. }
  45. TEST_ENDIAN_HTON(16, 0xFF00, 0x00FF)
  46. TEST_ENDIAN_HTON(32, 0xAABBCCDD, 0xDDCCBBAA)
  47. TEST_ENDIAN_HTON(64, 0x0011223344556677, 0x7766554433221100)
  48. #define TEST_ENDIAN_NTOH(size, value, le_expected) \
  49. static void test_ntoh_##size(void **state) { \
  50. \
  51. (void)state; \
  52. UINT##size test = tpm2_util_ntoh_##size(value); \
  53. bool is_big_endian = tpm2_util_is_big_endian(); \
  54. UINT##size expected = is_big_endian ? value : le_expected; \
  55. assert_int_equal(test, expected); \
  56. \
  57. }
  58. TEST_ENDIAN_NTOH(16, 0xFF00, 0x00FF)
  59. TEST_ENDIAN_NTOH(32, 0xAABBCCDD, 0xDDCCBBAA)
  60. TEST_ENDIAN_NTOH(64, 0x0011223344556677, 0x7766554433221100)
  61. /* link required symbol, but tpm2_tool.c declares it AND main, which
  62. * we have a main below for cmocka tests.
  63. */
  64. bool output_enabled = true;
  65. int main(int argc, char* argv[]) {
  66. (void) argc;
  67. (void) argv;
  68. const struct CMUnitTest tests[] = {
  69. cmocka_unit_test(test_is_big_endian),
  70. cmocka_unit_test(test_convert_16),
  71. cmocka_unit_test(test_convert_32),
  72. cmocka_unit_test(test_convert_64),
  73. cmocka_unit_test(test_hton_16),
  74. cmocka_unit_test(test_hton_32),
  75. cmocka_unit_test(test_hton_64),
  76. cmocka_unit_test(test_ntoh_16),
  77. cmocka_unit_test(test_ntoh_32),
  78. cmocka_unit_test(test_ntoh_64),
  79. cmocka_unit_test(test_popcount)
  80. };
  81. return cmocka_run_group_tests(tests, NULL, NULL);
  82. }