test_utils.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* test_utils.h - TinyCrypt interface to common functions for tests */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * test_utils.h -- Interface to common functions for tests.
  32. */
  33. #ifndef __TEST_UTILS_H__
  34. #define __TEST_UTILS_H__
  35. #include <tinycrypt/constants.h>
  36. #include <stdint.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #define PRINT_DATA(fmt, ...) printf(fmt, ##__VA_ARGS__)
  41. #define PRINT_LINE \
  42. PRINT_DATA( \
  43. "============================================================" \
  44. "=======\n")
  45. #define FAIL "FAIL"
  46. #define PASS "PASS"
  47. #define FMT_ERROR "%s - %s@%d. "
  48. /* TC_ here stands for 'Test Case' not 'TinyCrypt' */
  49. #define TC_PASS 0
  50. #define TC_FAIL 1
  51. #define TC_ERROR(fmt, ...) \
  52. do { \
  53. PRINT_DATA(FMT_ERROR, FAIL, __func__, __LINE__); \
  54. PRINT_DATA(fmt, ##__VA_ARGS__); \
  55. } while (0)
  56. #define TC_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
  57. #define TC_START(name) PRINT_DATA("tc_start() - %s\n", name)
  58. #define TC_END(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
  59. /* prints result and the function name */
  60. #define TC_END_RESULT(result) \
  61. do { \
  62. PRINT_LINE; \
  63. TC_END(result, "%s - %s.\n", \
  64. result == TC_PASS ? PASS : FAIL, __func__); \
  65. } while (0)
  66. #define TC_END_REPORT(result) \
  67. do { \
  68. PRINT_LINE; \
  69. TC_END(result, \
  70. "PROJECT EXECUTION %s\n", \
  71. result == TC_PASS ? "SUCCESSFUL" : "FAILED"); \
  72. } while (0)
  73. static inline void show_str(const char *label, const uint8_t *s, size_t len)
  74. {
  75. unsigned int i;
  76. TC_PRINT("%s = ", label);
  77. for (i = 0; i < (unsigned int) len; ++i) {
  78. TC_PRINT("%02x", s[i]);
  79. }
  80. TC_PRINT("\n");
  81. }
  82. static inline void fatal(unsigned int testnum, const void *expected, size_t expectedlen,
  83. const void *computed, size_t computedlen)
  84. {
  85. TC_ERROR("\tTest #%d Failed!\n", testnum);
  86. show_str("\t\tExpected", expected, expectedlen);
  87. show_str("\t\tComputed ", computed, computedlen);
  88. TC_PRINT("\n");
  89. }
  90. static inline unsigned int check_result(unsigned int testnum, const void *expected, size_t expectedlen,
  91. const void *computed, size_t computedlen)
  92. {
  93. unsigned int result = TC_PASS;
  94. if (expectedlen != computedlen) {
  95. TC_ERROR("The length of the computed buffer (%zu)", computedlen);
  96. TC_ERROR("does not match the expected length (%zu).", expectedlen);
  97. result = TC_FAIL;
  98. } else if (memcmp(computed, expected, computedlen) != 0) {
  99. fatal(testnum, expected, expectedlen, computed, computedlen);
  100. result = TC_FAIL;
  101. }
  102. return result;
  103. }
  104. #endif /* __TEST_UTILS_H__ */