tctildr-getinfo.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. * Copyright 2019, Intel Corporation
  4. */
  5. #include <inttypes.h>
  6. #include <limits.h>
  7. #include <stdarg.h>
  8. #include <stdbool.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <setjmp.h>
  13. #include <cmocka.h>
  14. #include "tss2_tcti.h"
  15. #include "tss2_tctildr.h"
  16. #include "tss2-tcti/tctildr.h"
  17. #include "tss2-tcti/tcti-common.h"
  18. static char* name_dup = "name_dup";
  19. static char* description_dup = "description_dup";
  20. static char* config_help_dup = "config_help_dup";
  21. static TSS2_TCTI_INFO info_src = {
  22. .version = 1,
  23. .name = "name",
  24. .description = "description",
  25. .config_help = "config_help",
  26. };
  27. static TSS2_TCTI_INFO info_dst = { 0, };
  28. char*
  29. __real_strndup (const char *s, size_t n);
  30. char*
  31. __wrap_strndup (const char *s,
  32. size_t n)
  33. {
  34. if (s == info_src.name || \
  35. s == info_src.description || \
  36. s == info_src.config_help)
  37. {
  38. printf("%s: got known string\n", __func__);
  39. errno = mock_type (int);
  40. return mock_type (char*);
  41. } else {
  42. return __real_strndup (s, n);
  43. }
  44. }
  45. void __real_free (void *ptr);
  46. void
  47. __wrap_free (void *ptr)
  48. {
  49. printf("%s: %p\n", __func__, ptr);
  50. if (ptr == name_dup || \
  51. ptr == description_dup || \
  52. ptr == config_help_dup || \
  53. ptr == &info_dst)
  54. {
  55. printf("%s: got known memory\n", __func__);
  56. return;
  57. } else {
  58. printf("%s: real free\n", __func__);
  59. return __real_free (ptr);
  60. }
  61. }
  62. TSS2_RC
  63. __wrap_tctildr_get_info (const char *name,
  64. const TSS2_TCTI_INFO **info,
  65. void **data)
  66. {
  67. TSS2_RC rc = mock_type (TSS2_RC);
  68. if (rc == TSS2_RC_SUCCESS) {
  69. *info = mock_type (TSS2_TCTI_INFO*);
  70. *data = mock_type (void*);
  71. }
  72. return rc;
  73. }
  74. TSS2_RC
  75. __wrap_tctildr_get_tcti (const char *name,
  76. const char* conf,
  77. TSS2_TCTI_CONTEXT **tcti,
  78. void **data)
  79. {
  80. TSS2_RC rc = mock_type (TSS2_RC);
  81. if (rc == TSS2_RC_SUCCESS) {
  82. *tcti= mock_type (TSS2_TCTI_CONTEXT*);
  83. *data = mock_type (void*);
  84. }
  85. return rc;
  86. }
  87. void __wrap_tctildr_finalize_data (void **data) {}
  88. static void
  89. copyinfo_null_params (void **state)
  90. {
  91. TSS2_RC rc = copy_info (NULL, NULL);
  92. assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
  93. }
  94. static void
  95. copyinfo_strndup1_fail (void **state)
  96. {
  97. TSS2_TCTI_INFO info_dst = { 0, };
  98. will_return (__wrap_strndup, ENOMEM);
  99. will_return (__wrap_strndup, NULL);
  100. TSS2_RC rc = copy_info (&info_src, &info_dst);
  101. assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
  102. }
  103. static void
  104. copyinfo_strndup2_fail (void **state)
  105. {
  106. TSS2_TCTI_INFO info_dst = { 0, };
  107. will_return (__wrap_strndup, 0);
  108. will_return (__wrap_strndup, name_dup);
  109. will_return (__wrap_strndup, ENOMEM);
  110. will_return (__wrap_strndup, NULL);
  111. TSS2_RC rc = copy_info (&info_src, &info_dst);
  112. assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
  113. }
  114. static void
  115. copyinfo_strndup3_fail (void **state)
  116. {
  117. TSS2_TCTI_INFO info_dst = { 0, };
  118. will_return (__wrap_strndup, 0);
  119. will_return (__wrap_strndup, name_dup);
  120. will_return (__wrap_strndup, 0);
  121. will_return (__wrap_strndup, description_dup);
  122. will_return (__wrap_strndup, ENOMEM);
  123. will_return (__wrap_strndup, NULL);
  124. TSS2_RC rc = copy_info (&info_src, &info_dst);
  125. assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
  126. }
  127. static void
  128. copyinfo_success (void **state)
  129. {
  130. TSS2_TCTI_INFO info_dst = { 0, };
  131. will_return (__wrap_strndup, 0);
  132. will_return (__wrap_strndup, name_dup);
  133. will_return (__wrap_strndup, 0);
  134. will_return (__wrap_strndup, description_dup);
  135. will_return (__wrap_strndup, 0);
  136. will_return (__wrap_strndup, config_help_dup);
  137. TSS2_RC rc = copy_info (&info_src, &info_dst);
  138. assert_int_equal (rc, TSS2_RC_SUCCESS);
  139. assert_string_equal (info_dst.name, name_dup);
  140. assert_string_equal (info_dst.description, description_dup);
  141. assert_string_equal (info_dst.config_help, config_help_dup);
  142. }
  143. static void
  144. getinfo_null_info (void **state)
  145. {
  146. TSS2_RC rc = Tss2_TctiLdr_GetInfo (NULL, NULL);
  147. assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
  148. }
  149. TSS2_RC
  150. __wrap_handle_from_name(const char *file,
  151. void **handle)
  152. {
  153. printf ("%s\n", __func__);
  154. *handle = mock_type (void*);
  155. return mock_type (TSS2_RC);
  156. }
  157. const TSS2_TCTI_INFO*
  158. __wrap_info_from_handle (void *dlhandle)
  159. {
  160. printf ("%s\n", __func__);
  161. return mock_type (const TSS2_TCTI_INFO*);
  162. }
  163. TSS2_RC
  164. __wrap_get_tcti_default(TSS2_TCTI_CONTEXT ** tcticontext, void **dlhandle)
  165. {
  166. printf ("%s\n", __func__);
  167. return TSS2_RC_SUCCESS;
  168. }
  169. TSS2_RC
  170. __wrap_tcti_from_file(const char *file,
  171. const char* conf,
  172. TSS2_TCTI_CONTEXT **tcti,
  173. void **dlhandle)
  174. {
  175. printf ("%s\n", __func__);
  176. return TSS2_RC_SUCCESS;
  177. }
  178. #define TEST_HANDLE (void*)666
  179. #define HANDLE_FROM_NAME_FAIL_RC (TSS2_RC)574567392
  180. void
  181. __wrap_dlclose (void *handle)
  182. {
  183. if (handle == TEST_HANDLE) {
  184. printf ("%s got TEST_HANDLE\n", __func__);
  185. return;
  186. } else {
  187. printf ("%s got unexpected handle\n", __func__);
  188. return;
  189. }
  190. }
  191. TSS2_RC
  192. __wrap_get_info_default (TSS2_TCTI_INFO **info,
  193. void **dlhandle)
  194. {
  195. printf ("%s\n", __func__);
  196. if (*info == &info_src) {
  197. *dlhandle = mock_type (void*);
  198. return mock_type (TSS2_RC);
  199. } else {
  200. printf ("%s: this shouldn't happen", __func__);
  201. assert_true(1);
  202. return 1;
  203. }
  204. }
  205. #define TCTI_NAME "foo"
  206. static void
  207. getinfo_get_info_fail (void **state)
  208. {
  209. TSS2_TCTI_INFO *info = NULL;
  210. will_return (__wrap_tctildr_get_info, HANDLE_FROM_NAME_FAIL_RC);
  211. TSS2_RC rc = Tss2_TctiLdr_GetInfo (TCTI_NAME, &info);
  212. assert_int_equal (rc, HANDLE_FROM_NAME_FAIL_RC);
  213. }
  214. void*
  215. __wrap_calloc(size_t nmemb, size_t size)
  216. {
  217. printf("%s\n", __func__);
  218. return mock_type (void*);
  219. }
  220. #define TEST_DATA (void*)0xabcdef999
  221. static void
  222. getinfo_calloc_fail (void **state)
  223. {
  224. TSS2_TCTI_INFO *info = NULL;
  225. will_return (__wrap_tctildr_get_info, TSS2_RC_SUCCESS);
  226. will_return (__wrap_tctildr_get_info, &info_src);
  227. will_return (__wrap_tctildr_get_info, TEST_DATA);
  228. will_return (__wrap_calloc, NULL);
  229. TSS2_RC rc = Tss2_TctiLdr_GetInfo (TCTI_NAME, &info);
  230. assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
  231. }
  232. static void
  233. getinfo_copy_info_fail (void **state)
  234. {
  235. TSS2_TCTI_INFO *info = NULL;
  236. printf ("%s: pointer %p\n", __func__, &info_dst);
  237. will_return (__wrap_tctildr_get_info, TSS2_RC_SUCCESS);
  238. will_return (__wrap_tctildr_get_info, &info_src);
  239. will_return (__wrap_tctildr_get_info, TEST_DATA);
  240. will_return (__wrap_calloc, &info_dst);
  241. will_return (__wrap_strndup, ENOMEM);
  242. will_return (__wrap_strndup, NULL);
  243. TSS2_RC rc = Tss2_TctiLdr_GetInfo (TCTI_NAME, &info);
  244. assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
  245. }
  246. static void
  247. getinfo_success (void **state)
  248. {
  249. TSS2_TCTI_INFO *info = NULL;
  250. will_return (__wrap_tctildr_get_info, TSS2_RC_SUCCESS);
  251. will_return (__wrap_tctildr_get_info, &info_src);
  252. will_return (__wrap_tctildr_get_info, TEST_DATA);
  253. will_return (__wrap_calloc, &info_dst);
  254. will_return (__wrap_strndup, 0);
  255. will_return (__wrap_strndup, name_dup);
  256. will_return (__wrap_strndup, 0);
  257. will_return (__wrap_strndup, description_dup);
  258. will_return (__wrap_strndup, 0);
  259. will_return (__wrap_strndup, config_help_dup);
  260. TSS2_RC rc = Tss2_TctiLdr_GetInfo (TCTI_NAME, &info);
  261. assert_int_equal (rc, TSS2_RC_SUCCESS);
  262. }
  263. static void
  264. freeinfo_null (void **state)
  265. {
  266. TSS2_TCTI_INFO *info = NULL;
  267. Tss2_TctiLdr_FreeInfo (&info);
  268. assert_null (info);
  269. }
  270. static void
  271. freeinfo_success (void **state)
  272. {
  273. TSS2_TCTI_INFO *info = &info_dst;
  274. Tss2_TctiLdr_FreeInfo (&info);
  275. assert_null (info);
  276. }
  277. int
  278. main (int argc, char* arvg[])
  279. {
  280. const struct CMUnitTest tests[] = {
  281. cmocka_unit_test (copyinfo_null_params),
  282. cmocka_unit_test (copyinfo_strndup1_fail),
  283. cmocka_unit_test (copyinfo_strndup2_fail),
  284. cmocka_unit_test (copyinfo_strndup3_fail),
  285. cmocka_unit_test (copyinfo_success),
  286. cmocka_unit_test (getinfo_null_info),
  287. cmocka_unit_test (getinfo_get_info_fail),
  288. cmocka_unit_test (getinfo_calloc_fail),
  289. cmocka_unit_test (getinfo_copy_info_fail),
  290. cmocka_unit_test (getinfo_success),
  291. cmocka_unit_test (freeinfo_null),
  292. cmocka_unit_test (freeinfo_success),
  293. };
  294. return cmocka_run_group_tests (tests, NULL, NULL);
  295. }