tctildr-dl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright 2018, Fraunhofer SIT sponsored by Infineon Technologies AG
  4. * Copyright 2019, Intel Corporation
  5. * All rights reserved.
  6. *******************************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <stdio.h>
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include <dlfcn.h>
  14. #include <setjmp.h>
  15. #include <cmocka.h>
  16. #include "tss2_tcti.h"
  17. #include "tss2-tcti/tctildr-interface.h"
  18. #include "tss2-tcti/tctildr-dl.h"
  19. #include "tss2-tcti/tctildr.h"
  20. #define LOGMODULE test
  21. #include "util/log.h"
  22. /* global TCTI object, use to return reference from */
  23. static TSS2_TCTI_CONTEXT_COMMON_V2 tcti_instance = { 0, };
  24. void *
  25. __wrap_dlopen(const char *filename, int flags)
  26. {
  27. LOG_TRACE("Called with filename %s and flags %x", filename, flags);
  28. check_expected_ptr(filename);
  29. check_expected(flags);
  30. return mock_type(void *);
  31. }
  32. int
  33. __wrap_dlclose(void *handle)
  34. {
  35. LOG_TRACE("Called with handle %p", handle);
  36. check_expected_ptr(handle);
  37. return mock_type(int);
  38. }
  39. void *
  40. __wrap_dlsym(void *handle, const char *symbol)
  41. {
  42. LOG_TRACE("Called with handle %p and symbol %s", handle, symbol);
  43. check_expected_ptr(handle);
  44. check_expected_ptr(symbol);
  45. return mock_type(void *);
  46. }
  47. void *
  48. __wrap___dlsym_time64(void *handle, const char *symbol)
  49. {
  50. return __wrap_dlsym(handle, symbol);
  51. }
  52. TSS2_TCTI_INFO *
  53. __wrap_Tss2_Tcti_Fake_Info(void)
  54. {
  55. LOG_TRACE("Called.");
  56. return mock_type(TSS2_TCTI_INFO *);
  57. }
  58. TSS2_RC
  59. __wrap_tcti_from_init(TSS2_TCTI_INIT_FUNC init,
  60. const char* conf,
  61. TSS2_TCTI_CONTEXT **tcti)
  62. {
  63. printf("%s", __func__);
  64. return mock_type (TSS2_RC);
  65. }
  66. TSS2_RC
  67. __wrap_tcti_from_info(TSS2_TCTI_INFO_FUNC infof,
  68. const char* conf,
  69. TSS2_TCTI_CONTEXT **tcti)
  70. {
  71. check_expected (infof);
  72. check_expected (conf);
  73. check_expected (tcti);
  74. if (tcti != NULL)
  75. *tcti = mock_type (TSS2_TCTI_CONTEXT*);
  76. return mock_type (TSS2_RC);
  77. }
  78. #define TEST_HANDLE (void*)0xade0
  79. static void
  80. test_info_from_handle_null (void **state)
  81. {
  82. const TSS2_TCTI_INFO* info = info_from_handle (NULL);
  83. assert_null (info);
  84. }
  85. static void
  86. test_info_from_handle_dlsym_fail (void **state)
  87. {
  88. expect_value(__wrap_dlsym, handle, TEST_HANDLE);
  89. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  90. will_return(__wrap_dlsym, NULL);
  91. const TSS2_TCTI_INFO* info = info_from_handle (TEST_HANDLE);
  92. assert_null (info);
  93. }
  94. static void
  95. test_info_from_handle_success (void **state)
  96. {
  97. TSS2_TCTI_INFO info_instance = { 0, };
  98. const TSS2_TCTI_INFO *info = { 0, };
  99. expect_value(__wrap_dlsym, handle, TEST_HANDLE);
  100. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  101. will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
  102. will_return(__wrap_Tss2_Tcti_Fake_Info, &info_instance);
  103. info = info_from_handle (TEST_HANDLE);
  104. assert_ptr_equal (info, &info_instance);
  105. }
  106. static void
  107. test_fail_null(void **state)
  108. {
  109. TSS2_RC r = tctildr_get_default(NULL, NULL);
  110. assert_int_equal(r, TSS2_TCTI_RC_BAD_REFERENCE);
  111. }
  112. static void
  113. test_handle_from_name_null_handle (void **state)
  114. {
  115. TSS2_RC rc = handle_from_name (NULL, NULL);
  116. assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
  117. }
  118. #define TEST_TCTI_NAME "test-tcti"
  119. #define TEST_TCTI_CONF "test-conf"
  120. static void
  121. test_handle_from_name_first_dlopen_success (void **state)
  122. {
  123. TSS2_RC rc;
  124. void *handle = NULL;
  125. expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME);
  126. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  127. will_return(__wrap_dlopen, TEST_HANDLE);
  128. rc = handle_from_name (TEST_TCTI_NAME, &handle);
  129. assert_int_equal (rc, TSS2_RC_SUCCESS);
  130. assert_int_equal (handle, TEST_HANDLE);
  131. }
  132. #define TEST_TCTI_NAME_SO_0 TCTI_PREFIX"-"TEST_TCTI_NAME""TCTI_SUFFIX_0
  133. static void
  134. test_handle_from_name_second_dlopen_success (void **state)
  135. {
  136. TSS2_RC rc;
  137. void *handle = NULL;
  138. expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME);
  139. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  140. will_return(__wrap_dlopen, NULL);
  141. expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME_SO_0);
  142. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  143. will_return(__wrap_dlopen, TEST_HANDLE);
  144. rc = handle_from_name (TEST_TCTI_NAME, &handle);
  145. assert_int_equal (rc, TSS2_RC_SUCCESS);
  146. assert_int_equal (handle, TEST_HANDLE);
  147. }
  148. #define TEST_TCTI_NAME_SO TCTI_PREFIX"-"TEST_TCTI_NAME""TCTI_SUFFIX
  149. static void
  150. test_handle_from_name_third_dlopen_success (void **state)
  151. {
  152. TSS2_RC rc;
  153. void *handle = NULL;
  154. expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME);
  155. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  156. will_return(__wrap_dlopen, NULL);
  157. expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME_SO_0);
  158. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  159. will_return(__wrap_dlopen, NULL);
  160. expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME_SO);
  161. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  162. will_return(__wrap_dlopen, TEST_HANDLE);
  163. rc = handle_from_name (TEST_TCTI_NAME, &handle);
  164. assert_int_equal (rc, TSS2_RC_SUCCESS);
  165. assert_int_equal (handle, TEST_HANDLE);
  166. }
  167. static void
  168. test_tcti_from_file_null_tcti (void **state)
  169. {
  170. TSS2_RC rc = tcti_from_file (NULL, NULL, NULL, NULL);
  171. assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
  172. }
  173. #define HANDLE (void *)123321
  174. #ifndef ESYS_TCTI_DEFAULT_MODULE
  175. static void
  176. test_get_info_default_null (void **state)
  177. {
  178. TSS2_RC rc = get_info_default (NULL, NULL);
  179. assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
  180. }
  181. static void
  182. test_get_info_default_success (void **state)
  183. {
  184. const TSS2_TCTI_INFO info_instance = { 0, };
  185. TSS2_TCTI_INFO *info = { 0, };
  186. void *handle;
  187. expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
  188. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  189. will_return(__wrap_dlopen, NULL);
  190. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so.0");
  191. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  192. will_return(__wrap_dlopen, NULL);
  193. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so");
  194. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  195. will_return(__wrap_dlopen, NULL);
  196. expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
  197. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  198. will_return(__wrap_dlopen, HANDLE);
  199. expect_value(__wrap_dlsym, handle, HANDLE);
  200. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  201. will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
  202. will_return(__wrap_Tss2_Tcti_Fake_Info, &info_instance);
  203. TSS2_RC rc = get_info_default (&info, &handle);
  204. assert_int_equal (rc, TSS2_RC_SUCCESS);
  205. assert_ptr_equal (info, &info_instance);
  206. }
  207. static void
  208. test_get_info_default_info_fail (void **state)
  209. {
  210. TSS2_TCTI_INFO *info = { 0, };
  211. void *handle;
  212. expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
  213. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  214. will_return(__wrap_dlopen, NULL);
  215. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so.0");
  216. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  217. will_return(__wrap_dlopen, NULL);
  218. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so");
  219. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  220. will_return(__wrap_dlopen, NULL);
  221. expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
  222. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  223. will_return(__wrap_dlopen, HANDLE);
  224. expect_value(__wrap_dlsym, handle, HANDLE);
  225. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  226. will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
  227. will_return(__wrap_Tss2_Tcti_Fake_Info, NULL);
  228. expect_value(__wrap_dlclose, handle, HANDLE);
  229. will_return(__wrap_dlclose, 0);
  230. TSS2_RC rc = get_info_default (&info, &handle);
  231. assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
  232. assert_ptr_equal (info, NULL);
  233. }
  234. /** Test for tcti
  235. * { "libtss2-tcti-default.so", NULL, "", "Access libtss2-tcti-default.so" }
  236. */
  237. static void
  238. test_tcti_default(void **state)
  239. {
  240. TSS2_TCTI_CONTEXT *tcti;
  241. expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
  242. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  243. will_return(__wrap_dlopen, HANDLE);
  244. expect_value(__wrap_dlsym, handle, HANDLE);
  245. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  246. will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
  247. expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
  248. expect_value(__wrap_tcti_from_info, conf, NULL);
  249. expect_value(__wrap_tcti_from_info, tcti, &tcti);
  250. will_return(__wrap_tcti_from_info, &tcti_instance);
  251. will_return(__wrap_tcti_from_info, TSS2_RC_SUCCESS);
  252. TSS2_RC r;
  253. void *handle = NULL;
  254. r = tctildr_get_default(&tcti, &handle);
  255. assert_int_equal(r, TSS2_RC_SUCCESS);
  256. }
  257. /** Test for failure on tcti
  258. * { "libtss2-tcti-default.so", NULL, "", "Access libtss2-tcti-default.so" }
  259. */
  260. static void
  261. test_tcti_default_fail_sym(void **state)
  262. {
  263. TSS2_TCTI_CONTEXT *tcti;
  264. #define HANDLE (void *)123321
  265. expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
  266. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  267. will_return(__wrap_dlopen, HANDLE);
  268. expect_value(__wrap_dlsym, handle, HANDLE);
  269. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  270. will_return(__wrap_dlsym, NULL);
  271. expect_value(__wrap_dlclose, handle, HANDLE);
  272. will_return(__wrap_dlclose, 0);
  273. /** Now test
  274. *{ "libtss2-tcti-tabrmd.so", NULL, "", "Access libtss2-tcti-tabrmd.so"},
  275. */
  276. expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
  277. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  278. will_return(__wrap_dlopen, HANDLE);
  279. expect_value(__wrap_dlsym, handle, HANDLE);
  280. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  281. will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
  282. expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
  283. expect_value(__wrap_tcti_from_info, conf, NULL);
  284. expect_value(__wrap_tcti_from_info, tcti, &tcti);
  285. will_return(__wrap_tcti_from_info, &tcti_instance);
  286. will_return(__wrap_tcti_from_info, TSS2_RC_SUCCESS);
  287. TSS2_RC r;
  288. r = tctildr_get_default(&tcti, NULL);
  289. assert_int_equal(r, TSS2_RC_SUCCESS);
  290. }
  291. /** Test for failure on tcti
  292. * { "libtss2-tcti-default.so", NULL, "", "Access libtss2-tcti-default.so" }
  293. */
  294. static void
  295. test_tcti_default_fail_info(void **state)
  296. {
  297. TSS2_TCTI_CONTEXT *tcti;
  298. #define HANDLE (void *)123321
  299. #define TEST_RC 0x55687
  300. /** Test for failure on tcti
  301. * { "libtss2-tcti-default.so", NULL, "", "Access libtss2-tcti-default.so" }
  302. */
  303. expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
  304. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  305. will_return(__wrap_dlopen, HANDLE);
  306. expect_value(__wrap_dlsym, handle, HANDLE);
  307. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  308. will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
  309. expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
  310. expect_value(__wrap_tcti_from_info, conf, NULL);
  311. expect_value(__wrap_tcti_from_info, tcti, &tcti);
  312. will_return(__wrap_tcti_from_info, &tcti_instance);
  313. will_return(__wrap_tcti_from_info, TEST_RC);
  314. expect_value(__wrap_dlclose, handle, HANDLE);
  315. will_return(__wrap_dlclose, 0);
  316. /** Now test
  317. *{ "libtss2-tcti-tabrmd.so", NULL, "", "Access libtss2-tcti-tabrmd.so"},
  318. */
  319. expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
  320. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  321. will_return(__wrap_dlopen, HANDLE);
  322. expect_value(__wrap_dlsym, handle, HANDLE);
  323. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  324. will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
  325. expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
  326. expect_value(__wrap_tcti_from_info, conf, NULL);
  327. expect_value(__wrap_tcti_from_info, tcti, &tcti);
  328. will_return(__wrap_tcti_from_info, &tcti_instance);
  329. will_return(__wrap_tcti_from_info, TSS2_RC_SUCCESS);
  330. TSS2_RC r;
  331. r = tctildr_get_default(&tcti, NULL);
  332. assert_int_equal(r, TSS2_RC_SUCCESS);
  333. }
  334. static void
  335. test_tcti_fail_all (void **state)
  336. {
  337. /* skip over libtss2-tcti-default.so */
  338. expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
  339. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  340. will_return(__wrap_dlopen, NULL);
  341. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so.0");
  342. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  343. will_return(__wrap_dlopen, NULL);
  344. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so");
  345. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  346. will_return(__wrap_dlopen, NULL);
  347. /* Skip over libtss2-tcti-tabrmd.so */
  348. expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
  349. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  350. will_return(__wrap_dlopen, NULL);
  351. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-tabrmd.so.0.so.0");
  352. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  353. will_return(__wrap_dlopen, NULL);
  354. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-tabrmd.so.0.so");
  355. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  356. will_return(__wrap_dlopen, NULL);
  357. /* Skip over libtss2-tcti-device.so, /dev/tpmrm0 */
  358. expect_string(__wrap_dlopen, filename, "libtss2-tcti-device.so.0");
  359. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  360. will_return(__wrap_dlopen, NULL);
  361. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-device.so.0.so.0");
  362. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  363. will_return(__wrap_dlopen, NULL);
  364. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-device.so.0.so");
  365. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  366. will_return(__wrap_dlopen, NULL);
  367. /* Skip over libtss2-tcti-device.so, /dev/tpm0 */
  368. expect_string(__wrap_dlopen, filename, "libtss2-tcti-device.so.0");
  369. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  370. will_return(__wrap_dlopen, NULL);
  371. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-device.so.0.so.0");
  372. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  373. will_return(__wrap_dlopen, NULL);
  374. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-device.so.0.so");
  375. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  376. will_return(__wrap_dlopen, NULL);
  377. /* Skip over libtss2-tcti-swtpm.so */
  378. expect_string(__wrap_dlopen, filename, "libtss2-tcti-swtpm.so.0");
  379. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  380. will_return(__wrap_dlopen, NULL);
  381. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-swtpm.so.0.so.0");
  382. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  383. will_return(__wrap_dlopen, NULL);
  384. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-swtpm.so.0.so");
  385. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  386. will_return(__wrap_dlopen, NULL);
  387. /* Skip over libtss2-tcti-mssim.so */
  388. expect_string(__wrap_dlopen, filename, "libtss2-tcti-mssim.so.0");
  389. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  390. will_return(__wrap_dlopen, NULL);
  391. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-mssim.so.0.so.0");
  392. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  393. will_return(__wrap_dlopen, NULL);
  394. expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-mssim.so.0.so");
  395. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  396. will_return(__wrap_dlopen, NULL);
  397. TSS2_RC r;
  398. TSS2_TCTI_CONTEXT *tcti;
  399. r = tctildr_get_default(&tcti, NULL);
  400. assert_int_equal(r, TSS2_TCTI_RC_IO_ERROR);
  401. }
  402. #endif
  403. void
  404. test_info_from_name_null (void **state)
  405. {
  406. TSS2_RC rc = info_from_name (NULL, NULL, NULL);
  407. assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
  408. }
  409. void
  410. test_info_from_name_handle_fail (void **state)
  411. {
  412. const TSS2_TCTI_INFO *info;
  413. void *data;
  414. expect_string(__wrap_dlopen, filename, "foo");
  415. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  416. will_return(__wrap_dlopen, NULL);
  417. expect_string(__wrap_dlopen, filename, "libtss2-tcti-foo.so.0");
  418. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  419. will_return(__wrap_dlopen, NULL);
  420. expect_string(__wrap_dlopen, filename, "libtss2-tcti-foo.so");
  421. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  422. will_return(__wrap_dlopen, NULL);
  423. TSS2_RC rc = info_from_name ("foo", &info, &data);
  424. assert_int_equal (rc, TSS2_TCTI_RC_NOT_SUPPORTED);
  425. }
  426. void
  427. test_info_from_name_info_fail (void **state)
  428. {
  429. const TSS2_TCTI_INFO *info = { 0, };
  430. void *data = HANDLE;
  431. expect_string(__wrap_dlopen, filename, "foo");
  432. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  433. will_return(__wrap_dlopen, HANDLE);
  434. expect_value(__wrap_dlsym, handle, HANDLE);
  435. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  436. will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
  437. will_return(__wrap_Tss2_Tcti_Fake_Info, NULL);
  438. expect_value(__wrap_dlclose, handle, HANDLE);
  439. will_return(__wrap_dlclose, 0);
  440. TSS2_RC rc = info_from_name ("foo", &info, &data);
  441. assert_int_equal (rc, TSS2_TCTI_RC_IO_ERROR);
  442. }
  443. void
  444. test_info_from_name_success (void **state)
  445. {
  446. const TSS2_TCTI_INFO *info = { 0, };
  447. TSS2_TCTI_INFO info_instance = { 0, };
  448. void *data;
  449. expect_string(__wrap_dlopen, filename, "foo");
  450. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  451. will_return(__wrap_dlopen, HANDLE);
  452. expect_value(__wrap_dlsym, handle, HANDLE);
  453. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  454. will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
  455. will_return(__wrap_Tss2_Tcti_Fake_Info, &info_instance);
  456. TSS2_RC rc = info_from_name ("foo", &info, &data);
  457. assert_int_equal (rc, TSS2_RC_SUCCESS);
  458. assert_ptr_equal (info, &info_instance);
  459. assert_ptr_equal (data, HANDLE);
  460. }
  461. void
  462. test_get_tcti_null (void **state)
  463. {
  464. TSS2_RC rc = tctildr_get_tcti (NULL, NULL, NULL, NULL);
  465. assert_int_equal(rc, TSS2_TCTI_RC_BAD_REFERENCE);
  466. }
  467. void
  468. test_get_tcti_default (void **state)
  469. {
  470. TSS2_TCTI_CONTEXT *tcti;
  471. expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
  472. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  473. will_return(__wrap_dlopen, HANDLE);
  474. expect_value(__wrap_dlsym, handle, HANDLE);
  475. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  476. will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
  477. expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
  478. expect_value(__wrap_tcti_from_info, conf, NULL);
  479. expect_value(__wrap_tcti_from_info, tcti, &tcti);
  480. will_return(__wrap_tcti_from_info, &tcti_instance);
  481. will_return(__wrap_tcti_from_info, TSS2_RC_SUCCESS);
  482. void *data;
  483. TSS2_RC rc = tctildr_get_tcti (NULL, NULL, &tcti, &data);
  484. assert_int_equal(rc, TSS2_RC_SUCCESS);
  485. }
  486. void
  487. test_get_tcti_from_name (void **state)
  488. {
  489. TSS2_TCTI_CONTEXT *tcti;
  490. expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
  491. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  492. will_return(__wrap_dlopen, HANDLE);
  493. expect_value(__wrap_dlsym, handle, HANDLE);
  494. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  495. will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
  496. expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
  497. expect_value(__wrap_tcti_from_info, conf, NULL);
  498. expect_value(__wrap_tcti_from_info, tcti, &tcti);
  499. will_return(__wrap_tcti_from_info, &tcti_instance);
  500. will_return(__wrap_tcti_from_info, TSS2_RC_SUCCESS);
  501. void *data;
  502. TSS2_RC rc = tctildr_get_tcti ("libtss2-tcti-default.so", NULL, &tcti, &data);
  503. assert_int_equal(rc, TSS2_RC_SUCCESS);
  504. }
  505. void
  506. test_tctildr_get_info_from_name (void **state)
  507. {
  508. const TSS2_TCTI_INFO *info;
  509. void *data;
  510. expect_string(__wrap_dlopen, filename, "foo");
  511. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  512. will_return(__wrap_dlopen, NULL);
  513. expect_string(__wrap_dlopen, filename, "libtss2-tcti-foo.so.0");
  514. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  515. will_return(__wrap_dlopen, NULL);
  516. expect_string(__wrap_dlopen, filename, "libtss2-tcti-foo.so");
  517. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  518. will_return(__wrap_dlopen, NULL);
  519. TSS2_RC rc = tctildr_get_info ("foo", &info, &data);
  520. assert_int_equal (rc, TSS2_TCTI_RC_NOT_SUPPORTED);
  521. }
  522. void
  523. test_tctildr_get_info_default (void **state)
  524. {
  525. const TSS2_TCTI_INFO *info;
  526. void *data;
  527. expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
  528. expect_value(__wrap_dlopen, flags, RTLD_NOW);
  529. will_return(__wrap_dlopen, HANDLE);
  530. expect_value(__wrap_dlsym, handle, HANDLE);
  531. expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
  532. will_return(__wrap_dlsym, NULL);
  533. expect_value(__wrap_dlclose, handle, HANDLE);
  534. will_return(__wrap_dlclose, 0);
  535. TSS2_RC rc = tctildr_get_info (NULL, &info, &data);
  536. assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
  537. }
  538. void
  539. test_finalize_data (void **state)
  540. {
  541. void *data = HANDLE;
  542. expect_value(__wrap_dlclose, handle, data);
  543. will_return(__wrap_dlclose, 0);
  544. tctildr_finalize_data (&data);
  545. assert_null (data);
  546. }
  547. int
  548. main(void)
  549. {
  550. const struct CMUnitTest tests[] = {
  551. cmocka_unit_test(test_info_from_handle_null),
  552. cmocka_unit_test(test_info_from_handle_dlsym_fail),
  553. cmocka_unit_test(test_info_from_handle_success),
  554. cmocka_unit_test(test_handle_from_name_null_handle),
  555. cmocka_unit_test(test_handle_from_name_first_dlopen_success),
  556. cmocka_unit_test(test_handle_from_name_second_dlopen_success),
  557. cmocka_unit_test(test_handle_from_name_third_dlopen_success),
  558. cmocka_unit_test(test_fail_null),
  559. cmocka_unit_test(test_tcti_from_file_null_tcti),
  560. #ifndef ESYS_TCTI_DEFAULT_MODULE
  561. cmocka_unit_test(test_get_info_default_null),
  562. cmocka_unit_test(test_get_info_default_success),
  563. cmocka_unit_test(test_get_info_default_info_fail),
  564. cmocka_unit_test(test_tcti_default),
  565. cmocka_unit_test(test_tcti_default_fail_sym),
  566. cmocka_unit_test(test_tcti_default_fail_info),
  567. cmocka_unit_test(test_tcti_fail_all),
  568. cmocka_unit_test(test_get_tcti_null),
  569. cmocka_unit_test(test_get_tcti_default),
  570. cmocka_unit_test(test_get_tcti_from_name),
  571. cmocka_unit_test(test_tctildr_get_info_from_name),
  572. cmocka_unit_test(test_tctildr_get_info_default),
  573. #endif
  574. cmocka_unit_test(test_info_from_name_null),
  575. cmocka_unit_test(test_info_from_name_handle_fail),
  576. cmocka_unit_test(test_info_from_name_info_fail),
  577. cmocka_unit_test(test_info_from_name_success),
  578. cmocka_unit_test(test_finalize_data),
  579. };
  580. return cmocka_run_group_tests (tests, NULL, NULL);
  581. }