test_files.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <errno.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <setjmp.h>
  8. #include <cmocka.h>
  9. #include "files.h"
  10. typedef struct test_file test_file;
  11. struct test_file {
  12. char *path;
  13. FILE *file;
  14. };
  15. static test_file *test_file_new(void) {
  16. test_file *tf = malloc(sizeof(test_file));
  17. if (!tf) {
  18. return NULL;
  19. }
  20. tf->path = strdup("xxx_test_files_xxx.test");
  21. if (!tf->path) {
  22. free(tf);
  23. return NULL;
  24. }
  25. tf->file = fopen(tf->path, "w+b");
  26. if (!tf->file) {
  27. free(tf->path);
  28. free(tf);
  29. return NULL;
  30. }
  31. return tf;
  32. }
  33. static void test_file_free(test_file *tf) {
  34. assert_non_null(tf);
  35. int rc = remove(tf->path);
  36. assert_return_code(rc, errno);
  37. free(tf->path);
  38. fclose(tf->file);
  39. free(tf);
  40. }
  41. static int test_setup(void **state) {
  42. test_file *tf = test_file_new();
  43. assert_non_null(tf);
  44. *state = tf;
  45. return 0;
  46. }
  47. static int test_teardown(void **state) {
  48. test_file *tf = (test_file *) *state;
  49. test_file_free(tf);
  50. return 0;
  51. }
  52. static test_file *test_file_from_state(void **state) {
  53. test_file *f = (test_file *) *state;
  54. assert_non_null(f);
  55. return f;
  56. }
  57. #define READ_WRITE_TEST(size, expected) \
  58. static void test_file_read_write_##size(void **state) { \
  59. \
  60. FILE *f = test_file_from_state(state)->file; \
  61. \
  62. bool res = files_write_##size(f, expected); \
  63. assert_true(res); \
  64. \
  65. rewind(f); \
  66. \
  67. UINT##size found; \
  68. res = files_read_##size(f, &found); \
  69. assert_true(res); \
  70. \
  71. assert_int_equal(found, expected); \
  72. }
  73. READ_WRITE_TEST(16, 0xABCD)
  74. READ_WRITE_TEST(32, 0x11223344)
  75. READ_WRITE_TEST(64, 0x1122334455667788)
  76. static void test_file_read_write_bytes(void **state) {
  77. FILE *f = test_file_from_state(state)->file;
  78. UINT8 expected[1024];
  79. memset(expected, 0xBB, sizeof(expected));
  80. bool res = files_write_bytes(f, expected, sizeof(expected));
  81. assert_true(res);
  82. rewind(f);
  83. UINT8 found[1024] = { 0 };
  84. res = files_read_bytes(f, found, sizeof(found));
  85. assert_true(res);
  86. assert_memory_equal(expected, found, sizeof(found));
  87. }
  88. static void test_file_read_write_0_bytes(void **state) {
  89. FILE *f = test_file_from_state(state)->file;
  90. UINT8 data[1];
  91. bool res = files_write_bytes(f, data, 0);
  92. assert_true(res);
  93. res = files_read_bytes(f, data, 0);
  94. assert_true(res);
  95. }
  96. static void test_file_read_write_header(void **state) {
  97. FILE *f = test_file_from_state(state)->file;
  98. UINT32 expected = 0xAABBCCDD;
  99. bool res = files_write_header(f, expected);
  100. assert_true(res);
  101. rewind(f);
  102. UINT32 found;
  103. res = files_read_header(f, &found);
  104. assert_true(res);
  105. assert_int_equal(expected, found);
  106. }
  107. #define READ_WRITE_TEST_BAD_PARAMS(size) \
  108. static void test_file_read_write_bad_params_##size(void **state) { \
  109. \
  110. UINT##size expected = 42; \
  111. FILE *f = test_file_from_state(state)->file; \
  112. bool res = files_write_##size(NULL, expected); \
  113. assert_false(res); \
  114. \
  115. UINT##size found; \
  116. res = files_read_##size(NULL, &found); \
  117. assert_false(res); \
  118. \
  119. res = files_read_##size(f, NULL); \
  120. assert_false(res); \
  121. \
  122. res = files_read_##size(NULL, NULL); \
  123. assert_false(res); \
  124. }
  125. READ_WRITE_TEST_BAD_PARAMS(16)
  126. READ_WRITE_TEST_BAD_PARAMS(32)
  127. READ_WRITE_TEST_BAD_PARAMS(64)
  128. static void test_file_read_write_bad_params_bytes(void **state) {
  129. FILE *f = test_file_from_state(state)->file;
  130. UINT8 data[1];
  131. bool res = files_write_bytes(f, NULL, sizeof(data));
  132. assert_false(res);
  133. res = files_write_bytes(NULL, data, sizeof(data));
  134. assert_false(res);
  135. res = files_read_bytes(f, NULL, sizeof(data));
  136. assert_false(res);
  137. res = files_read_bytes(NULL, data, sizeof(data));
  138. assert_false(res);
  139. }
  140. static void test_file_size(void **state) {
  141. test_file *tf = test_file_from_state(state);
  142. UINT8 data[128] = { 0 };
  143. bool res = files_write_bytes(tf->file, data, sizeof(data));
  144. assert_true(res);
  145. int rc = fflush(tf->file);
  146. assert_return_code(rc, errno);
  147. unsigned long file_size;
  148. res = files_get_file_size_path(tf->path, &file_size);
  149. assert_true(res);
  150. assert_int_equal(file_size, sizeof(data));
  151. }
  152. static void test_file_size_bad_args(void **state) {
  153. unsigned long file_size;
  154. bool res = files_get_file_size_path("this_should_be_a_bad_path",
  155. &file_size);
  156. assert_false(res);
  157. res = files_get_file_size_path(NULL, &file_size);
  158. assert_false(res);
  159. test_file *tf = test_file_from_state(state);
  160. res = files_get_file_size_path(tf->path, NULL);
  161. assert_false(res);
  162. }
  163. static void test_file_exists(void **state) {
  164. test_file *tf = test_file_from_state(state);
  165. bool res = files_does_file_exist(tf->path);
  166. assert_true(res);
  167. }
  168. static void test_file_exists_bad_args(void **state) {
  169. (void) state;
  170. bool res = files_does_file_exist("this_should_be_a_bad_path");
  171. assert_false(res);
  172. res = files_does_file_exist(NULL);
  173. assert_false(res);
  174. }
  175. /* link required symbol, but tpm2_tool.c declares it AND main, which
  176. * we have a main below for cmocka tests.
  177. */
  178. bool output_enabled = true;
  179. int main(int argc, char* argv[]) {
  180. (void) argc;
  181. (void) argv;
  182. const struct CMUnitTest tests[] = {
  183. cmocka_unit_test_setup_teardown(test_file_read_write_16,
  184. test_setup, test_teardown),
  185. cmocka_unit_test_setup_teardown(test_file_read_write_32,
  186. test_setup, test_teardown),
  187. cmocka_unit_test_setup_teardown(test_file_read_write_64,
  188. test_setup, test_teardown),
  189. cmocka_unit_test_setup_teardown(test_file_read_write_bytes,
  190. test_setup, test_teardown),
  191. cmocka_unit_test_setup_teardown(test_file_read_write_0_bytes,
  192. test_setup, test_teardown),
  193. cmocka_unit_test_setup_teardown(test_file_read_write_header,
  194. test_setup, test_teardown),
  195. cmocka_unit_test_setup_teardown(test_file_read_write_bad_params_16,
  196. test_setup, test_teardown),
  197. cmocka_unit_test_setup_teardown(test_file_read_write_bad_params_32,
  198. test_setup, test_teardown),
  199. cmocka_unit_test_setup_teardown(test_file_read_write_bad_params_64,
  200. test_setup, test_teardown),
  201. cmocka_unit_test_setup_teardown(test_file_read_write_bad_params_bytes,
  202. test_setup, test_teardown),
  203. cmocka_unit_test_setup_teardown(test_file_size,
  204. test_setup, test_teardown),
  205. cmocka_unit_test_setup_teardown(test_file_size_bad_args,
  206. test_setup, test_teardown),
  207. cmocka_unit_test_setup_teardown(test_file_exists,
  208. test_setup, test_teardown),
  209. cmocka_unit_test_setup_teardown(test_file_exists_bad_args,
  210. test_setup, test_teardown),
  211. };
  212. return cmocka_run_group_tests(tests, NULL, NULL);
  213. }