fapi-io.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright 2018, Fraunhofer SIT sponsored by Infineon Technologies AG
  4. * All rights reserved.
  5. ******************************************************************************/
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif
  9. #include <stdarg.h>
  10. #include <inttypes.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16. #include <json-c/json_util.h>
  17. #include <json-c/json_tokener.h>
  18. #include <setjmp.h>
  19. #include <cmocka.h>
  20. #include <errno.h>
  21. #include "ifapi_io.h"
  22. #include "util/aux_util.h"
  23. #define LOGMODULE tests
  24. #include "util/log.h"
  25. /*
  26. * The unit tests will simulate error codes which can be returned by the
  27. * system calls for file system IO.
  28. */
  29. /* Global variables to trigger wrapper functions. If set to false the
  30. * real function will be called. */
  31. bool wrap_fcntl_test = false;
  32. bool wrap_malloc_test = false;
  33. bool wrap_read_test = false;
  34. FILE mock_stream; /**< stream will be used to activate wrapper.*/
  35. /*
  36. * Wrapper functions for file system io.
  37. */
  38. int
  39. __real_stat(const char *pathname, struct stat *statbuf, ...);
  40. int
  41. __wrap_stat(const char *pathname, struct stat *statbuf, ...)
  42. {
  43. if (strcmp(pathname, "tss_unit_dummyf")) {
  44. return __real_stat(pathname, statbuf);
  45. }
  46. statbuf->st_mode = R_OK;
  47. return 0;
  48. }
  49. FILE *
  50. __real_fopen(const char *pathname, const char* mode, ...);
  51. FILE *
  52. __wrap_fopen(const char *pathname, const char* mode, ...)
  53. {
  54. if (strcmp(pathname, "tss_unit_dummyf")) {
  55. return __real_fopen(pathname, mode);
  56. }
  57. return mock_ptr_type(FILE*);
  58. }
  59. int
  60. __real_fclose(FILE *stream, ...);
  61. int
  62. __wrap_fclose(FILE *stream, ...)
  63. {
  64. if (stream != &mock_stream) {
  65. return __real_fclose(stream);
  66. }
  67. return mock_type(int);
  68. }
  69. int
  70. __real_fseek(FILE *stream, long offset, int whence, ...);
  71. int
  72. __wrap_fseek(FILE *stream, long offset, int whence, ...)
  73. {
  74. if (stream != &mock_stream) {
  75. return __real_fseek(stream, offset, whence);
  76. }
  77. return mock_type(int);
  78. }
  79. long
  80. __real_ftell(FILE *stream, ...);
  81. long
  82. __wrap_ftell(FILE *stream, ...)
  83. {
  84. if (stream != &mock_stream) {
  85. return __real_ftell(stream);
  86. }
  87. return mock_type(int);
  88. }
  89. int
  90. __real_fcntl(int fd, int cmd, ...);
  91. int
  92. __wrap_fcntl(int fd, int cmd, ...)
  93. {
  94. if (wrap_fcntl_test)
  95. return mock_type(int);
  96. else
  97. return __real_fcntl(fd, cmd);
  98. }
  99. void *
  100. __real_malloc(size_t size, ...);
  101. void *
  102. __wrap_malloc(size_t size, ...)
  103. {
  104. if (wrap_malloc_test) {
  105. return mock_ptr_type (void*);
  106. } else {
  107. return __real_malloc(size);
  108. }
  109. }
  110. int
  111. __real_fileno(FILE *stream, ...);
  112. int
  113. __wrap_fileno(FILE *stream, ...)
  114. {
  115. if (stream != &mock_stream) {
  116. return __real_fileno(stream);
  117. }
  118. return 1;
  119. }
  120. ssize_t
  121. __real_read(int fd, void *buf, size_t count);
  122. ssize_t
  123. __wrap_read(int fd, void *buf, size_t count, ...) {
  124. if (!wrap_read_test) {
  125. return __real_read(fd, buf, count);
  126. }
  127. return mock_type(ssize_t);
  128. }
  129. /*
  130. * The return codes for error cases which can be occur in the
  131. * function: ifapi_io_read_async will be checked.
  132. */
  133. static void
  134. check_io_read_async(void **state) {
  135. IFAPI_IO io;
  136. TSS2_RC r;
  137. char *dmy_buf = "dummy";
  138. memset(&io, 0, sizeof(IFAPI_IO));
  139. io.char_rbuffer = &dmy_buf[0];
  140. r = ifapi_io_read_async(&io, "tss_unit_dummyf");
  141. assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
  142. io.char_rbuffer = NULL;
  143. will_return(__wrap_fopen, NULL);
  144. r = ifapi_io_read_async(&io, "tss_unit_dummyf");
  145. assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
  146. will_return(__wrap_fopen, NULL);
  147. io.char_buffer = "dummy";
  148. r = ifapi_io_read_async(&io, "tss_unit_dummyf");
  149. assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
  150. wrap_fcntl_test = true;
  151. will_return(__wrap_fopen, &mock_stream);
  152. will_return(__wrap_fcntl, -1);
  153. will_return_always(__wrap_fclose, 0);
  154. errno = EAGAIN;
  155. io.char_buffer = NULL;
  156. r = ifapi_io_read_async(&io, "tss_unit_dummyf");
  157. assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
  158. will_return(__wrap_fopen, &mock_stream);
  159. will_return(__wrap_fopen, &mock_stream);
  160. will_return(__wrap_fcntl, 0);
  161. will_return(__wrap_fseek, 0);
  162. will_return(__wrap_ftell, 1);
  163. will_return(__wrap_malloc, NULL);
  164. errno = 0;
  165. io.char_buffer = NULL;
  166. wrap_malloc_test = true;
  167. r = ifapi_io_read_async(&io, "tss_unit_dummyf");
  168. assert_int_equal(r, TSS2_FAPI_RC_MEMORY);
  169. wrap_malloc_test = false;
  170. will_return(__wrap_fopen, &mock_stream);
  171. will_return(__wrap_fopen, &mock_stream);
  172. will_return(__wrap_fcntl, 0);
  173. will_return(__wrap_fseek, 0);
  174. will_return(__wrap_ftell, 1);
  175. will_return(__wrap_fcntl, 0);
  176. will_return(__wrap_fcntl, -1);
  177. errno = 0;
  178. io.char_buffer = NULL;
  179. r = ifapi_io_read_async(&io, "tss_unit_dummyf");
  180. assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
  181. wrap_fcntl_test = false;
  182. }
  183. /*
  184. * The return codes for error cases which can be occur in the
  185. * function: ifapi_io_read_finish will be checked.
  186. */
  187. static void
  188. check_io_read_finish(void **state) {
  189. IFAPI_IO io;
  190. TSS2_RC r;
  191. uint8_t *buffer[10];
  192. char io_char_buffer[10];
  193. size_t count = 10;
  194. memset(&io, 0, sizeof(IFAPI_IO));
  195. wrap_read_test = true;
  196. will_return(__wrap_read, -1);
  197. // will_return_always(__wrap_fileno, 1);
  198. will_return_always(__wrap_fclose, 0);
  199. io.char_buffer = &io_char_buffer[0];
  200. io.buffer_length = 10;
  201. io.stream = &mock_stream;
  202. errno = EAGAIN;
  203. r = ifapi_io_read_finish(&io, &buffer[0], &count);
  204. assert_int_equal(r, TSS2_FAPI_RC_TRY_AGAIN);
  205. will_return(__wrap_read, -1);
  206. errno = 0;
  207. r = ifapi_io_read_finish(&io, &buffer[0], &count);
  208. assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
  209. will_return(__wrap_read, 8);
  210. errno = 0;
  211. r = ifapi_io_read_finish(&io, &buffer[0], &count);
  212. assert_int_equal(r, TSS2_FAPI_RC_TRY_AGAIN);
  213. will_return(__wrap_read, 10);
  214. errno = 0;
  215. r = ifapi_io_read_finish(&io, &buffer[0], &count);
  216. assert_int_equal(r, TSS2_RC_SUCCESS);
  217. will_return(__wrap_read, 10);
  218. errno = 0;
  219. r = ifapi_io_read_finish(&io, NULL, &count);
  220. assert_int_equal(r, TSS2_RC_SUCCESS);
  221. wrap_read_test = false;
  222. }
  223. /*
  224. * The return codes for error cases which can be occur in the
  225. * function: ifapi_io_write_async will be checked.
  226. */
  227. static void
  228. check_io_write_async(void **state) {
  229. IFAPI_IO io;
  230. TSS2_RC r;
  231. uint8_t buffer[5] = { 1, 2, 3, 4, 5 };
  232. char *char_buffer = "dummy";
  233. will_return_always(__wrap_fclose, 0);
  234. // will_return_always(__wrap_fileno, 1);
  235. memset(&io, 0, sizeof(IFAPI_IO));
  236. io.char_rbuffer = &char_buffer[0];
  237. r = ifapi_io_write_async(&io, "tss_unit_dummyf", NULL, 0);
  238. assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
  239. io.char_rbuffer = NULL;
  240. will_return(__wrap_malloc, NULL);
  241. wrap_malloc_test = true;
  242. r = ifapi_io_write_async(&io, "tss_unit_dummyf", NULL, 0);
  243. assert_int_equal(r, TSS2_FAPI_RC_MEMORY);
  244. wrap_malloc_test = false;
  245. will_return(__wrap_fopen, NULL);
  246. r = ifapi_io_write_async(&io, "tss_unit_dummyf", &buffer[0], 5);
  247. assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
  248. wrap_fcntl_test = true;
  249. will_return(__wrap_fopen, &mock_stream);
  250. will_return(__wrap_fcntl, -1);
  251. errno = EAGAIN;
  252. r = ifapi_io_write_async(&io, "tss_unit_dummyf", &buffer[0], 5);
  253. assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
  254. io.char_rbuffer = NULL;
  255. will_return(__wrap_fopen, &mock_stream);
  256. will_return(__wrap_fcntl, 0);
  257. will_return(__wrap_fcntl, 0);
  258. will_return(__wrap_fcntl, -1);
  259. errno = 0;
  260. r = ifapi_io_write_async(&io, "tss_unit_dummyf", &buffer[0], 5);
  261. assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
  262. wrap_fcntl_test = false;
  263. }
  264. bool wrap_write_test = false;
  265. ssize_t
  266. __real_write(int fd, void *buf, size_t count);
  267. ssize_t
  268. __wrap_write(int fd, void *buf, size_t count, ...) {
  269. if (!wrap_write_test) {
  270. return __real_read(fd, buf, count);
  271. }
  272. return mock_type(ssize_t);
  273. }
  274. /*
  275. * The return codes for error cases which can be occur in the
  276. * function: ifapi_io_write_finish will be checked.
  277. */
  278. static void
  279. check_io_write_finish(void **state) {
  280. IFAPI_IO io;
  281. TSS2_RC r;
  282. //uint8_t buffer[5] = { 1, 2, 3, 4, 5 };
  283. memset(&io, 0, sizeof(IFAPI_IO));
  284. // will_return_always(__wrap_fileno, 1);
  285. will_return_always(__wrap_fclose, 0);
  286. wrap_write_test = true;
  287. io.stream = &mock_stream;
  288. will_return(__wrap_write, -1);
  289. errno = EAGAIN;
  290. r = ifapi_io_write_finish(&io);
  291. assert_int_equal(r, TSS2_FAPI_RC_TRY_AGAIN);
  292. errno = 0;
  293. will_return(__wrap_write, -1);
  294. r = ifapi_io_write_finish(&io);
  295. assert_int_equal(r, TSS2_FAPI_RC_IO_ERROR);
  296. wrap_write_test = false;
  297. }
  298. int
  299. main(int argc, char *argv[])
  300. {
  301. const struct CMUnitTest tests[] = {
  302. cmocka_unit_test(check_io_read_async),
  303. cmocka_unit_test(check_io_read_finish),
  304. cmocka_unit_test(check_io_write_async),
  305. cmocka_unit_test(check_io_write_finish),
  306. };
  307. return cmocka_run_group_tests(tests, NULL, NULL);
  308. }