tcti-mssim.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************;
  3. * Copyright (c) 2015 - 2018, Intel Corporation
  4. * All rights reserved.
  5. ***********************************************************************/
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif
  9. #include <inttypes.h>
  10. #include <limits.h>
  11. #include <stdio.h>
  12. #include <stdbool.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <setjmp.h>
  16. #include <cmocka.h>
  17. #include "tss2_tcti.h"
  18. #include "tss2_tcti_mssim.h"
  19. #include "tss2-tcti/tcti-common.h"
  20. #include "tss2-tcti/tcti-mssim.h"
  21. #include "util/key-value-parse.h"
  22. /*
  23. * This function is defined in the tcti-mssim module but not exposed through
  24. * the header.
  25. */
  26. TSS2_RC
  27. mssim_kv_callback (const key_value_t *key_value,
  28. void *user_data);
  29. /*
  30. * In the tests below where 'host' is set (implying TCP and excluding unix domain
  31. * sockets), we ensure that 'path' comes back NULL. Similarly, when 'path' is
  32. * set (implying unix domain sockets), we ensure that 'host' is NULL.
  33. */
  34. #define NO_HOST_VALUE "no.host.xyz"
  35. #define NO_PORT_VALUE 646
  36. #define NO_PATH_VALUE "/bad/path"
  37. /*
  38. * This tests our ability to handle conf strings that have a port component. In
  39. * this case the 'conf_str_to_host_port' function should set the 'host' and
  40. * 'port' parameters and so we check to be sure they're set. (And that 'path'
  41. * is unset.)
  42. */
  43. static void
  44. conf_str_to_host_port_success_test (void **state)
  45. {
  46. TSS2_RC rc;
  47. char conf[] = "host=127.0.0.1,port=2321";
  48. char unusedpath[] = NO_PATH_VALUE;
  49. mssim_conf_t mssim_conf = {
  50. .path = unusedpath
  51. };
  52. rc = parse_key_value_string (conf, mssim_kv_callback, &mssim_conf);
  53. assert_int_equal (rc, TSS2_RC_SUCCESS);
  54. assert_int_equal (mssim_conf.port, 2321);
  55. assert_string_equal (mssim_conf.host, "127.0.0.1");
  56. assert_null (mssim_conf.path);
  57. }
  58. /*
  59. * This tests our ability to handle conf strings that don't have the port
  60. * component of the URI. In this case the 'conf_str_to_host_port' function
  61. * should not touch the 'port' parameter and so we check to be sure it's
  62. * unchanged. (And that 'path' is unset.)
  63. */
  64. static void
  65. conf_str_to_host_port_no_port_test (void **state)
  66. {
  67. TSS2_RC rc;
  68. char conf[] = "host=127.0.0.1";
  69. char unusedpath[] = NO_PATH_VALUE;
  70. mssim_conf_t mssim_conf = {
  71. .host = "foo",
  72. .port = NO_PORT_VALUE,
  73. .path = unusedpath
  74. };
  75. rc = parse_key_value_string (conf, mssim_kv_callback, &mssim_conf);
  76. assert_int_equal (rc, TSS2_RC_SUCCESS);
  77. assert_string_equal (mssim_conf.host, "127.0.0.1");
  78. assert_int_equal (mssim_conf.port, NO_PORT_VALUE);
  79. assert_null (mssim_conf.path);
  80. }
  81. /*
  82. * This tests our ability to handle conf strings that have an IPv6 address
  83. * and port component. In this case the 'conf_str_to_host_port' function
  84. * should set the 'hostname' parameter and so we check to be sure it's
  85. * set without the [] brackets. (And that 'path' is unset.)
  86. */
  87. static void
  88. conf_str_to_host_ipv6_port_success_test (void **state)
  89. {
  90. TSS2_RC rc;
  91. char conf[] = "host=::1,port=2321";
  92. char unusedpath[] = NO_PATH_VALUE;
  93. mssim_conf_t mssim_conf = {
  94. .path = unusedpath
  95. };
  96. rc = parse_key_value_string (conf, mssim_kv_callback, &mssim_conf);
  97. assert_int_equal (rc, TSS2_RC_SUCCESS);
  98. assert_int_equal (mssim_conf.port, 2321);
  99. assert_string_equal (mssim_conf.host, "::1");
  100. assert_null (mssim_conf.path);
  101. }
  102. /*
  103. * This tests our ability to handle conf strings that have an IPv6 address
  104. * but no port component. In this case the 'conf_str_to_host_port' function
  105. * should not touch the 'port' parameter and so we check to be sure it's
  106. * unchanged. (And that 'path' is unset.)
  107. */
  108. static void
  109. conf_str_to_host_ipv6_port_no_port_test (void **state)
  110. {
  111. TSS2_RC rc;
  112. char conf[] = "host=::1";
  113. mssim_conf_t mssim_conf = {
  114. .port = NO_PORT_VALUE,
  115. .path = NO_PATH_VALUE
  116. };
  117. rc = parse_key_value_string (conf, mssim_kv_callback, &mssim_conf);
  118. assert_int_equal (rc, TSS2_RC_SUCCESS);
  119. assert_int_equal (mssim_conf.port, NO_PORT_VALUE);
  120. assert_string_equal (mssim_conf.host, "::1");
  121. assert_null (mssim_conf.path);
  122. }
  123. /*
  124. * The 'conf_str_to_host_port' function rejects ports over UINT16_MAX.
  125. */
  126. static void
  127. conf_str_to_host_port_invalid_port_large_test (void **state)
  128. {
  129. TSS2_RC rc;
  130. char conf[] = "host=127.0.0.1,port=99999";
  131. mssim_conf_t mssim_conf = { 0 };
  132. rc = parse_key_value_string (conf, mssim_kv_callback, &mssim_conf);
  133. assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
  134. }
  135. /* The 'conf_str_to_host_port' function rejects URIs with port == 0 */
  136. static void
  137. conf_str_to_host_port_invalid_port_0_test (void **state)
  138. {
  139. TSS2_RC rc;
  140. char conf[] = "host=127.0.0.1,port=0";
  141. mssim_conf_t mssim_conf = { 0 };
  142. rc = parse_key_value_string (conf, mssim_kv_callback, &mssim_conf);
  143. assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
  144. }
  145. /*
  146. * This tests our ability to handle conf strings that have a path
  147. * component. In this case the 'conf_str_to_host_port' function
  148. * should set the 'path' parameter and so we check to be sure it's
  149. * set. (And that 'host' is unset.)
  150. */
  151. static void
  152. conf_str_to_path_success_test (void **state)
  153. {
  154. TSS2_RC rc;
  155. char conf[] = "path=/some/path";
  156. char unusedhost[] = NO_HOST_VALUE;
  157. mssim_conf_t mssim_conf = {
  158. .host = unusedhost
  159. };
  160. rc = parse_key_value_string (conf, mssim_kv_callback, &mssim_conf);
  161. assert_int_equal (rc, TSS2_RC_SUCCESS);
  162. assert_string_equal (mssim_conf.path, "/some/path");
  163. assert_null (mssim_conf.host);
  164. }
  165. /* When passed all NULL values ensure that we get back the expected RC. */
  166. static void
  167. tcti_socket_init_all_null_test (void **state)
  168. {
  169. TSS2_RC rc;
  170. rc = Tss2_Tcti_Mssim_Init (NULL, NULL, NULL);
  171. assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
  172. }
  173. /*
  174. * Determine the size of a TCTI context structure. Requires calling the
  175. * initialization function for the device TCTI with the first parameter
  176. * (the TCTI context) NULL.
  177. */
  178. static void
  179. tcti_socket_init_size_test (void **state)
  180. {
  181. size_t tcti_size = 0;
  182. TSS2_RC ret = TSS2_RC_SUCCESS;
  183. ret = Tss2_Tcti_Mssim_Init (NULL, &tcti_size, NULL);
  184. assert_int_equal (ret, TSS2_RC_SUCCESS);
  185. assert_int_equal (tcti_size, sizeof (TSS2_TCTI_MSSIM_CONTEXT));
  186. }
  187. /*
  188. * Wrap the 'connect' system call. The mock queue for this function must have
  189. * an integer to return as a response.
  190. */
  191. int
  192. __wrap_connect (int sockfd,
  193. const struct sockaddr *addr,
  194. socklen_t addrlen)
  195. {
  196. return mock_type (int);
  197. }
  198. /*
  199. * Wrap the 'recv' system call. The mock queue for this function must have an
  200. * integer return value (the number of byts recv'd), as well as a pointer to
  201. * a buffer to copy data from to return to the caller.
  202. */
  203. ssize_t
  204. __wrap_read (int sockfd,
  205. void *buf,
  206. size_t len)
  207. {
  208. ssize_t ret = mock_type (ssize_t);
  209. uint8_t *buf_in = mock_ptr_type (uint8_t*);
  210. memcpy (buf, buf_in, ret);
  211. return ret;
  212. }
  213. /*
  214. * Wrap the 'send' system call. The mock queue for this function must have an
  215. * integer to return as a response.
  216. */
  217. ssize_t
  218. __wrap_write (int sockfd,
  219. const void *buf,
  220. size_t len)
  221. {
  222. return mock_type (TSS2_RC);
  223. }
  224. /*
  225. * Wrap the 'poll' system call.
  226. */
  227. int
  228. __wrap_poll (struct pollfd *fds, nfds_t nfds, int timeout)
  229. {
  230. int ret = mock_type (int);
  231. fds->revents = fds->events;
  232. return ret;
  233. }
  234. /*
  235. * This is a utility function used by other tests to setup a TCTI context. It
  236. * effectively wraps the init / allocate / init pattern as well as priming the
  237. * mock functions necessary for a the successful call to
  238. * 'Tss2_Tcti_Mssim_Init'.
  239. */
  240. static TSS2_TCTI_CONTEXT*
  241. tcti_socket_init_from_conf (const char *conf)
  242. {
  243. size_t tcti_size = 0;
  244. uint8_t recv_buf[4] = { 0 };
  245. TSS2_RC ret = TSS2_RC_SUCCESS;
  246. TSS2_TCTI_CONTEXT *ctx = NULL;
  247. printf ("%s: before first init\n", __func__);
  248. ret = Tss2_Tcti_Mssim_Init (NULL, &tcti_size, NULL);
  249. assert_true (ret == TSS2_RC_SUCCESS);
  250. ctx = calloc (1, tcti_size);
  251. assert_non_null (ctx);
  252. /*
  253. * two calls to connect, one for the data socket, one for the command
  254. * socket
  255. */
  256. will_return (__wrap_connect, 0);
  257. will_return (__wrap_connect, 0);
  258. /*
  259. * two 'platform commands are sent on initialization, 4 bytes sent for
  260. * each, 4 byte response received (all 0's) for each.
  261. */
  262. will_return (__wrap_write, 4);
  263. will_return (__wrap_read, 4);
  264. will_return (__wrap_read, recv_buf);
  265. will_return (__wrap_write, 4);
  266. will_return (__wrap_read, 4);
  267. will_return (__wrap_read, recv_buf);
  268. printf ("%s: before second_init\n", __func__);
  269. ret = Tss2_Tcti_Mssim_Init (ctx, &tcti_size, conf);
  270. printf ("%s: after second init\n", __func__);
  271. assert_int_equal (ret, TSS2_RC_SUCCESS);
  272. return ctx;
  273. }
  274. /*
  275. * This is a utility function to setup the "default" TCTI context.
  276. */
  277. static int
  278. tcti_socket_setup (void **state)
  279. {
  280. printf ("%s: before tcti_socket_init_from_conf\n", __func__);
  281. *state = tcti_socket_init_from_conf ("host=127.0.0.1,port=666");
  282. printf ("%s: done\n", __func__);
  283. return 0;
  284. }
  285. #ifndef _WIN32
  286. /* variant of tcti_socket_setup() for unix domain sockets. */
  287. static int
  288. tcti_socket_setup_unix (void **state)
  289. {
  290. printf ("%s: before tcti_socket_init_from_conf\n", __func__);
  291. *state = tcti_socket_init_from_conf ("path=/notarealdirectory/notarealfile");
  292. printf ("%s: done\n", __func__);
  293. return 0;
  294. }
  295. #endif
  296. static void
  297. tcti_socket_init_null_conf_test (void **state)
  298. {
  299. TSS2_TCTI_CONTEXT *ctx = tcti_socket_init_from_conf (NULL);
  300. assert_non_null (ctx);
  301. free (ctx);
  302. }
  303. /*
  304. * This is a utility function to teardown a TCTI context allocated by the
  305. * tcti_socket_setup function.
  306. */
  307. static int
  308. tcti_socket_teardown (void **state)
  309. {
  310. TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
  311. Tss2_Tcti_Finalize (ctx);
  312. free (ctx);
  313. return 0;
  314. }
  315. /*
  316. * This test ensures that the GetPollHandles function in the device TCTI
  317. * returns the expected value. Since this TCTI does not support async I/O
  318. * on account of limitations in the kernel it just returns the
  319. * NOT_IMPLEMENTED response code.
  320. */
  321. static void
  322. tcti_mssim_get_poll_handles_test (void **state)
  323. {
  324. TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
  325. size_t num_handles = 5;
  326. TSS2_TCTI_POLL_HANDLE handles [5] = { 0 };
  327. TSS2_RC rc = Tss2_Tcti_GetPollHandles (ctx, handles, &num_handles);
  328. TSS2_TCTI_MSSIM_CONTEXT *mssim_ctx = (TSS2_TCTI_MSSIM_CONTEXT*)ctx;
  329. assert_int_equal (rc, TSS2_RC_SUCCESS);
  330. assert_int_equal (num_handles, 1);
  331. assert_int_equal (handles[0].fd, mssim_ctx->tpm_sock);
  332. }
  333. /*
  334. */
  335. static void
  336. tcti_socket_receive_null_size_test (void **state)
  337. {
  338. TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
  339. TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_common_context_cast (ctx);
  340. TSS2_RC rc;
  341. /* Keep state machine check in `receive` from returning error. */
  342. tcti_common->state = TCTI_STATE_RECEIVE;
  343. rc = Tss2_Tcti_Receive (ctx,
  344. NULL, /* NULL 'size' parameter */
  345. NULL,
  346. TSS2_TCTI_TIMEOUT_BLOCK);
  347. assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
  348. rc = Tss2_Tcti_Receive (ctx,
  349. NULL, /* NULL 'size' parameter */
  350. (uint8_t*)1, /* non-NULL buffer */
  351. TSS2_TCTI_TIMEOUT_BLOCK);
  352. assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
  353. }
  354. /*
  355. * This test exercises the successful code path through the receive function.
  356. */
  357. static void
  358. tcti_socket_receive_success_test (void **state)
  359. {
  360. TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
  361. TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_common_context_cast (ctx);
  362. TSS2_RC rc = TSS2_RC_SUCCESS;
  363. size_t response_size = 0xc;
  364. uint8_t response_in [] = { 0x80, 0x02,
  365. 0x00, 0x00, 0x00, 0x0c,
  366. 0x00, 0x00, 0x00, 0x00,
  367. 0x01, 0x02,
  368. /* simulator appends 4 bytes of 0's to every response */
  369. 0x00, 0x00, 0x00, 0x00 };
  370. uint8_t response_out [12] = { 0 };
  371. /* Keep state machine check in `receive` from returning error. */
  372. tcti_common->state = TCTI_STATE_RECEIVE;
  373. /* receive response size */
  374. will_return (__wrap_poll, 1);
  375. will_return (__wrap_read, 4);
  376. will_return (__wrap_read, &response_in [2]);
  377. /* receive tag */
  378. will_return (__wrap_poll, 1);
  379. will_return (__wrap_read, 2);
  380. will_return (__wrap_read, response_in);
  381. /* receive size (again) */
  382. will_return (__wrap_poll, 1);
  383. will_return (__wrap_read, 4);
  384. will_return (__wrap_read, &response_in [2]);
  385. /* receive the rest of the command */
  386. will_return (__wrap_read, 0xc - sizeof (TPM2_ST) - sizeof (UINT32));
  387. will_return (__wrap_read, &response_in [6]);
  388. /* receive the 4 bytes of 0's appended by the simulator */
  389. will_return (__wrap_read, 4);
  390. will_return (__wrap_read, &response_in [12]);
  391. rc = Tss2_Tcti_Receive (ctx, &response_size, response_out, TSS2_TCTI_TIMEOUT_BLOCK);
  392. assert_int_equal (rc, TSS2_RC_SUCCESS);
  393. assert_memory_equal (response_in, response_out, response_size);
  394. }
  395. /*
  396. */
  397. static void
  398. tcti_socket_receive_size_success_test (void **state)
  399. {
  400. TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
  401. TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_common_context_cast (ctx);
  402. TSS2_RC rc = TSS2_RC_SUCCESS;
  403. size_t response_size = 0;
  404. uint8_t response_in [] = { 0x80, 0x02,
  405. 0x00, 0x00, 0x00, 0x0c,
  406. 0x00, 0x00, 0x00, 0x00,
  407. 0x01, 0x02,
  408. /* simulator appends 4 bytes of 0's to every response */
  409. 0x00, 0x00, 0x00, 0x00 };
  410. uint8_t response_out [12] = { 0 };
  411. /* Keep state machine check in `receive` from returning error. */
  412. tcti_common->state = TCTI_STATE_RECEIVE;
  413. /* receive response size */
  414. will_return (__wrap_poll, 1);
  415. will_return (__wrap_read, 4);
  416. will_return (__wrap_read, &response_in [2]);
  417. rc = Tss2_Tcti_Receive (ctx, &response_size, NULL, TSS2_TCTI_TIMEOUT_BLOCK);
  418. assert_int_equal (rc, TSS2_RC_SUCCESS);
  419. assert_int_equal (response_size, 0xc);
  420. /* receive tag */
  421. will_return (__wrap_poll, 1);
  422. will_return (__wrap_read, 2);
  423. will_return (__wrap_read, response_in);
  424. /* receive size (again) */
  425. will_return (__wrap_poll, 1);
  426. will_return (__wrap_read, 4);
  427. will_return (__wrap_read, &response_in [2]);
  428. /* receive the rest of the command */
  429. will_return (__wrap_read, 0xc - sizeof (TPM2_ST) - sizeof (UINT32));
  430. will_return (__wrap_read, &response_in [6]);
  431. /* receive the 4 bytes of 0's appended by the simulator */
  432. will_return (__wrap_read, 4);
  433. will_return (__wrap_read, &response_in [12]);
  434. rc = Tss2_Tcti_Receive (ctx, &response_size, response_out, TSS2_TCTI_TIMEOUT_BLOCK);
  435. assert_int_equal (rc, TSS2_RC_SUCCESS);
  436. assert_memory_equal (response_in, response_out, response_size);
  437. }
  438. /*
  439. * This test causes the underlying 'read' call to return 0 / EOF when we
  440. * call the TCTI 'receive' function. In this case the TCTI should return an
  441. * IO error.
  442. */
  443. static void
  444. tcti_mssim_receive_eof_first_read_test (void **state)
  445. {
  446. TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
  447. TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_common_context_cast (ctx);
  448. TSS2_RC rc;
  449. /* output buffer for response */
  450. uint8_t buf [TPM_HEADER_SIZE] = { 0 };
  451. size_t size = sizeof (buf);
  452. /* Keep state machine check in `receive` from returning error. */
  453. tcti_common->state = TCTI_STATE_RECEIVE;
  454. will_return (__wrap_poll, 1);
  455. will_return (__wrap_read, 0);
  456. will_return (__wrap_read, buf);
  457. rc = Tss2_Tcti_Receive (ctx,
  458. &size,
  459. buf,
  460. TSS2_TCTI_TIMEOUT_BLOCK);
  461. assert_true (rc == TSS2_TCTI_RC_IO_ERROR);
  462. }
  463. /*
  464. * This test causes the underlying 'read' call to return EOF but only after
  465. * a successful read that gets us the response size. This results in the
  466. * an IO_ERROR RC being returned.
  467. */
  468. static void
  469. tcti_mssim_receive_eof_second_read_test (void **state)
  470. {
  471. TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
  472. TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_common_context_cast (ctx);
  473. TSS2_RC rc;
  474. /* input response buffer */
  475. uint8_t response_in [] = { 0x80, 0x02,
  476. 0x00, 0x00, 0x00, 0x0c,
  477. 0x00, 0x00, 0x00, 0x00,
  478. 0x01, 0x02,
  479. /* simulator appends 4 bytes of 0's to every response */
  480. 0x00, 0x00, 0x00, 0x00 };
  481. /* output response buffer */
  482. uint8_t response_out [12] = { 0 };
  483. size_t size = sizeof (response_out);
  484. /* Keep state machine check in `receive` from returning error. */
  485. tcti_common->state = TCTI_STATE_RECEIVE;
  486. /* setup response size for first read */
  487. will_return (__wrap_poll, 1);
  488. will_return (__wrap_read, 4);
  489. will_return (__wrap_read, &response_in [2]);
  490. /* setup 0 for EOF on second read */
  491. will_return (__wrap_poll, 1);
  492. will_return (__wrap_read, 0);
  493. will_return (__wrap_read, response_in);
  494. rc = Tss2_Tcti_Receive (ctx,
  495. &size,
  496. response_out,
  497. TSS2_TCTI_TIMEOUT_BLOCK);
  498. assert_true (rc == TSS2_TCTI_RC_IO_ERROR);
  499. }
  500. /*
  501. * This test exercises the successful code path through the transmit function.
  502. */
  503. static void
  504. tcti_socket_transmit_success_test (void **state)
  505. {
  506. TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
  507. TSS2_RC rc = TSS2_RC_SUCCESS;
  508. uint8_t command [] = { 0x80, 0x02,
  509. 0x00, 0x00, 0x00, 0x0c,
  510. 0x00, 0x00, 0x00, 0x00,
  511. 0x01, 0x02 };
  512. size_t command_size = sizeof (command);
  513. /* send the TPM2_SEND_COMMAND code */
  514. will_return (__wrap_write, 4);
  515. /* send the locality for the command */
  516. will_return (__wrap_write, 1);
  517. /* send the number of bytes in command */
  518. will_return (__wrap_write, 4);
  519. /* send the command buffer */
  520. will_return (__wrap_write, 0xc);
  521. rc = Tss2_Tcti_Transmit (ctx, command_size, command);
  522. assert_int_equal (rc, TSS2_RC_SUCCESS);
  523. }
  524. int
  525. main (int argc,
  526. char *argv[])
  527. {
  528. const struct CMUnitTest tests[] = {
  529. cmocka_unit_test (conf_str_to_host_port_success_test),
  530. cmocka_unit_test (conf_str_to_host_port_no_port_test),
  531. cmocka_unit_test (conf_str_to_host_ipv6_port_success_test),
  532. cmocka_unit_test (conf_str_to_host_ipv6_port_no_port_test),
  533. cmocka_unit_test (conf_str_to_host_port_invalid_port_large_test),
  534. cmocka_unit_test (conf_str_to_host_port_invalid_port_0_test),
  535. cmocka_unit_test (conf_str_to_path_success_test),
  536. cmocka_unit_test (tcti_socket_init_all_null_test),
  537. cmocka_unit_test (tcti_socket_init_size_test),
  538. cmocka_unit_test (tcti_socket_init_null_conf_test),
  539. cmocka_unit_test_setup_teardown (tcti_mssim_get_poll_handles_test,
  540. tcti_socket_setup,
  541. tcti_socket_teardown),
  542. cmocka_unit_test_setup_teardown (tcti_socket_receive_null_size_test,
  543. tcti_socket_setup,
  544. tcti_socket_teardown),
  545. cmocka_unit_test_setup_teardown (tcti_socket_receive_success_test,
  546. tcti_socket_setup,
  547. tcti_socket_teardown),
  548. cmocka_unit_test_setup_teardown (tcti_socket_receive_size_success_test,
  549. tcti_socket_setup,
  550. tcti_socket_teardown),
  551. cmocka_unit_test_setup_teardown (tcti_mssim_receive_eof_first_read_test,
  552. tcti_socket_setup,
  553. tcti_socket_teardown),
  554. cmocka_unit_test_setup_teardown (tcti_mssim_receive_eof_second_read_test,
  555. tcti_socket_setup,
  556. tcti_socket_teardown),
  557. cmocka_unit_test_setup_teardown (tcti_socket_transmit_success_test,
  558. tcti_socket_setup,
  559. tcti_socket_teardown),
  560. #ifndef _WIN32
  561. cmocka_unit_test_setup_teardown (tcti_socket_receive_success_test,
  562. tcti_socket_setup_unix,
  563. tcti_socket_teardown),
  564. #endif
  565. };
  566. return cmocka_run_group_tests (tests, NULL, NULL);
  567. }