blob: b1bc68357954f5ce74b92f5ce3a2463da45a1ac9 [file] [log] [blame]
Philip Tricca8e5afda2017-03-14 09:29:54 -07001#include <inttypes.h>
Philip Tricca09a5bcd2016-03-21 13:20:19 -07002#include <stdbool.h>
Philip Triccaa7d2e2c2016-11-17 14:47:21 -08003#include <stdio.h>
Philip Tricca910f17c2018-03-15 12:38:37 -07004#include <string.h>
Philip Triccaa7d2e2c2016-11-17 14:47:21 -08005
Philip Tricca09a5bcd2016-03-21 13:20:19 -07006#include <setjmp.h>
7#include <cmocka.h>
Philip Triccab194be92016-03-15 13:27:09 -07008
Philip Tricca8ffd3c42018-03-09 16:27:24 -08009#include "tss2_mu.h"
10#include "tss2_tcti_device.h"
Philip Triccab8584ac2018-03-10 17:02:30 -080011#include "tss2-tcti/tcti.h"
Philip Triccaa7d2e2c2016-11-17 14:47:21 -080012
Philip Tricca0b870442018-03-26 14:27:28 -070013/*
14 * Size of the TPM2 buffer used in these tests. In some cases this will be
15 * the command sent (transmit tests) and in others it's used as the response
16 * buffer returned by the TCTI. The only field used by the TCTI is the size
17 * field.
18 */
19#define BUF_SIZE 20
20static uint8_t tpm2_buf [BUF_SIZE] = {
21 0x80, 0x02, /* TAG */
22 0x00, 0x00, 0x00, 0x14, /* size (BUF_SIZE) */
23 0x00, 0x00, 0x00, 0x00, /* rc (success) */
24 0xde, 0xad, 0xbe, 0xef, /* junk data */
25 0xca, 0xfe, 0xba, 0xbe,
26 0xfe, 0xef
27};
Philip Tricca467d63b2016-11-17 18:56:41 -080028/**
29 * When passed all NULL values ensure that we get back the expected RC
30 * indicating bad values.
31 */
32static void
33tcti_device_init_all_null_test (void **state)
34{
35 TSS2_RC rc;
36
Philip Tricca1ae4cff2018-01-19 14:41:57 -080037 rc = Tss2_Tcti_Device_Init (NULL, NULL, NULL);
Philip Tricca467d63b2016-11-17 18:56:41 -080038 assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
39}
Philip Triccab194be92016-03-15 13:27:09 -070040/* Determine the size of a TCTI context structure. Requires calling the
41 * initialization function for the device TCTI with the first parameter
42 * (the TCTI context) NULL.
43 */
Philip Triccaae884f52016-11-17 14:54:56 -080044static void
45tcti_device_init_size_test (void **state)
Philip Triccab194be92016-03-15 13:27:09 -070046{
Philip Triccab194be92016-03-15 13:27:09 -070047 size_t tcti_size = 0;
48 TSS2_RC ret = TSS2_RC_SUCCESS;
49
Philip Tricca1ae4cff2018-01-19 14:41:57 -080050 ret = Tss2_Tcti_Device_Init (NULL, &tcti_size, NULL);
Philip Triccaae884f52016-11-17 14:54:56 -080051 assert_int_equal (ret, TSS2_RC_SUCCESS);
Philip Triccab194be92016-03-15 13:27:09 -070052}
Philip Tricca8e5afda2017-03-14 09:29:54 -070053/* wrap functions for read & write required to test receive / transmit */
54ssize_t
Philip Tricca7a10ee32018-03-06 12:48:39 -080055__wrap_read (int fd, void *buf, size_t count)
Philip Tricca8e5afda2017-03-14 09:29:54 -070056{
Philip Tricca7a10ee32018-03-06 12:48:39 -080057 ssize_t ret = mock_type (ssize_t);
58 uint8_t *buf_in = mock_type (uint8_t*);
59
60 memcpy (buf, buf_in, ret);
61 return ret;
Philip Tricca8e5afda2017-03-14 09:29:54 -070062}
63ssize_t
64__wrap_write (int fd, const void *buffer, size_t buffer_size)
65{
Philip Tricca0b870442018-03-26 14:27:28 -070066 ssize_t ret = mock_type (ssize_t);
67 uint8_t *buf_out = mock_type (uint8_t*);
68
69 memcpy (buf_out, buffer, ret);
70 return ret;
Philip Tricca8e5afda2017-03-14 09:29:54 -070071}
72
73typedef struct {
74 TSS2_TCTI_CONTEXT *ctx;
Philip Tricca8e5afda2017-03-14 09:29:54 -070075 size_t buffer_size;
76 size_t data_size;
Philip Tricca0b870442018-03-26 14:27:28 -070077 uint8_t buffer [TPM_HEADER_SIZE * 2];
Philip Tricca8e5afda2017-03-14 09:29:54 -070078} data_t;
79/* Setup functions to create the context for the device TCTI */
Philip Tricca25789b22018-03-07 15:23:47 -080080static int
Philip Tricca8e5afda2017-03-14 09:29:54 -070081tcti_device_setup (void **state)
82{
83 size_t tcti_size = 0;
84 TSS2_RC ret = TSS2_RC_SUCCESS;
85 TSS2_TCTI_CONTEXT *ctx = NULL;
Philip Tricca8e5afda2017-03-14 09:29:54 -070086
Philip Tricca1ae4cff2018-01-19 14:41:57 -080087 ret = Tss2_Tcti_Device_Init (NULL, &tcti_size, NULL);
Philip Tricca8e5afda2017-03-14 09:29:54 -070088 assert_true (ret == TSS2_RC_SUCCESS);
89 ctx = calloc (1, tcti_size);
90 assert_non_null (ctx);
Philip Tricca1ae4cff2018-01-19 14:41:57 -080091 ret = Tss2_Tcti_Device_Init (ctx, 0, "/dev/null");
Philip Tricca8e5afda2017-03-14 09:29:54 -070092 assert_true (ret == TSS2_RC_SUCCESS);
Philip Tricca25789b22018-03-07 15:23:47 -080093
Philip Tricca8e5afda2017-03-14 09:29:54 -070094 *state = ctx;
Philip Tricca25789b22018-03-07 15:23:47 -080095 return 0;
Philip Tricca8e5afda2017-03-14 09:29:54 -070096}
97
Tadeusz Struk2147c492017-08-09 13:40:31 -070098static int
Philip Tricca8e5afda2017-03-14 09:29:54 -070099tcti_device_teardown (void **state)
100{
Philip Tricca25789b22018-03-07 15:23:47 -0800101 TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
Philip Tricca8e5afda2017-03-14 09:29:54 -0700102
Andreas Fuchs930b5c12017-11-20 11:28:57 +0100103 Tss2_Tcti_Finalize (ctx);
Philip Tricca8e5afda2017-03-14 09:29:54 -0700104 free (ctx);
Philip Tricca25789b22018-03-07 15:23:47 -0800105
Tadeusz Struk2147c492017-08-09 13:40:31 -0700106 return 0;
Philip Tricca25789b22018-03-07 15:23:47 -0800107
108}
Philip Tricca25789b22018-03-07 15:23:47 -0800109/*
110 */
111static void
112tcti_device_receive_null_size_test (void **state)
113{
114 TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
115 TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (ctx);
116 TSS2_RC rc;
117
118 /* Keep state machine check in `receive` from returning error. */
119 tcti_intel->state = TCTI_STATE_RECEIVE;
120 rc = Tss2_Tcti_Receive (ctx,
121 NULL, /* NULL 'size' parameter */
122 NULL,
123 TSS2_TCTI_TIMEOUT_BLOCK);
124 assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
125 rc = Tss2_Tcti_Receive (ctx,
126 NULL, /* NULL 'size' parameter */
127 (uint8_t*)1, /* non-NULL buffer */
128 TSS2_TCTI_TIMEOUT_BLOCK);
129 assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
Philip Tricca8e5afda2017-03-14 09:29:54 -0700130}
131/*
132 * A test case for a successful call to the receive function. This requires
133 * that the context and the command buffer be valid (including the size
134 * field being set appropriately). The result should be an RC indicating
135 * success and the size parameter be updated to reflect the size of the
136 * data received.
137 */
138static void
Philip Tricca7a10ee32018-03-06 12:48:39 -0800139tcti_device_receive_one_call_success (void **state)
Philip Tricca8e5afda2017-03-14 09:29:54 -0700140{
Philip Tricca0b870442018-03-26 14:27:28 -0700141 TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
142 TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (ctx);
Philip Tricca8e5afda2017-03-14 09:29:54 -0700143 TSS2_RC rc;
Philip Tricca0b870442018-03-26 14:27:28 -0700144 /* output buffer for response */
145 uint8_t buf_out [BUF_SIZE + 5] = { 0 };
146 size_t size = BUF_SIZE + 5;
Philip Tricca8e5afda2017-03-14 09:29:54 -0700147
Philip Triccafa14abe2018-03-05 21:14:54 -0800148 /* Keep state machine check in `receive` from returning error. */
149 tcti_intel->state = TCTI_STATE_RECEIVE;
Philip Tricca7a10ee32018-03-06 12:48:39 -0800150 will_return (__wrap_read, TPM_HEADER_SIZE);
Philip Tricca0b870442018-03-26 14:27:28 -0700151 will_return (__wrap_read, tpm2_buf);
152 will_return (__wrap_read, BUF_SIZE - TPM_HEADER_SIZE);
153 will_return (__wrap_read, &tpm2_buf [TPM_HEADER_SIZE]);
154 rc = Tss2_Tcti_Receive (ctx,
155 &size,
156 buf_out,
Philip Tricca8e5afda2017-03-14 09:29:54 -0700157 TSS2_TCTI_TIMEOUT_BLOCK);
158 assert_true (rc == TSS2_RC_SUCCESS);
Philip Tricca0b870442018-03-26 14:27:28 -0700159 assert_int_equal (BUF_SIZE, size);
160 assert_memory_equal (tpm2_buf, buf_out, size);
Philip Tricca8e5afda2017-03-14 09:29:54 -0700161}
Philip Tricca7a10ee32018-03-06 12:48:39 -0800162static void
163tcti_device_receive_two_call_success (void **state)
164{
Philip Tricca0b870442018-03-26 14:27:28 -0700165 TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
166 TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (ctx);
Philip Tricca7a10ee32018-03-06 12:48:39 -0800167 TSS2_RC rc;
Philip Tricca0b870442018-03-26 14:27:28 -0700168 /* output buffer for response */
169 uint8_t buf_out [BUF_SIZE + 5] = { 0 };
Philip Tricca7a10ee32018-03-06 12:48:39 -0800170 size_t size = 0;
171
172 /* Keep state machine check in `receive` from returning error. */
173 tcti_intel->state = TCTI_STATE_RECEIVE;
174 will_return (__wrap_read, TPM_HEADER_SIZE);
Philip Tricca0b870442018-03-26 14:27:28 -0700175 will_return (__wrap_read, tpm2_buf);
176 will_return (__wrap_read, BUF_SIZE - TPM_HEADER_SIZE);
177 will_return (__wrap_read, &tpm2_buf [TPM_HEADER_SIZE]);
178 rc = Tss2_Tcti_Receive (ctx,
Philip Tricca7a10ee32018-03-06 12:48:39 -0800179 &size,
180 NULL,
181 TSS2_TCTI_TIMEOUT_BLOCK);
Philip Tricca7a10ee32018-03-06 12:48:39 -0800182 assert_int_equal (rc, TSS2_RC_SUCCESS);
Philip Tricca0b870442018-03-26 14:27:28 -0700183 assert_int_equal (size, BUF_SIZE);
184 assert_true (size < BUF_SIZE + 5);
185 rc = Tss2_Tcti_Receive (ctx,
186 &size,
187 buf_out,
Philip Tricca7a10ee32018-03-06 12:48:39 -0800188 TSS2_TCTI_TIMEOUT_BLOCK);
189 assert_true (rc == TSS2_RC_SUCCESS);
Philip Tricca0b870442018-03-26 14:27:28 -0700190 assert_int_equal (size, BUF_SIZE);
191 assert_memory_equal (tpm2_buf, buf_out, size);
Philip Tricca7a10ee32018-03-06 12:48:39 -0800192}
Philip Tricca8e5afda2017-03-14 09:29:54 -0700193/*
194 * A test case for a successful call to the transmit function. This requires
195 * that the context and the cmmand buffer be valid. The only indication of
196 * success is the RC.
197 */
198static void
199tcti_device_transmit_success (void **state)
200{
Philip Tricca0b870442018-03-26 14:27:28 -0700201 TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
Philip Tricca8e5afda2017-03-14 09:29:54 -0700202 TSS2_RC rc;
Philip Tricca0b870442018-03-26 14:27:28 -0700203 /* output buffer for response */
204 uint8_t buf_out [BUF_SIZE] = { 0 };
Philip Tricca8e5afda2017-03-14 09:29:54 -0700205
Philip Tricca0b870442018-03-26 14:27:28 -0700206 will_return (__wrap_write, BUF_SIZE);
207 will_return (__wrap_write, buf_out);
208 rc = Tss2_Tcti_Transmit (ctx,
209 BUF_SIZE,
210 tpm2_buf);
Philip Tricca8e5afda2017-03-14 09:29:54 -0700211 assert_true (rc == TSS2_RC_SUCCESS);
Philip Tricca0b870442018-03-26 14:27:28 -0700212 assert_memory_equal (tpm2_buf, buf_out, BUF_SIZE);
Philip Tricca8e5afda2017-03-14 09:29:54 -0700213}
Philip Triccaa7d2e2c2016-11-17 14:47:21 -0800214
Philip Triccaa7d2e2c2016-11-17 14:47:21 -0800215int
216main(int argc, char* argv[])
217{
Tadeusz Struk2147c492017-08-09 13:40:31 -0700218 const struct CMUnitTest tests[] = {
219 cmocka_unit_test (tcti_device_init_all_null_test),
220 cmocka_unit_test(tcti_device_init_size_test),
Philip Tricca25789b22018-03-07 15:23:47 -0800221 cmocka_unit_test_setup_teardown (tcti_device_receive_null_size_test,
222 tcti_device_setup,
223 tcti_device_teardown),
Philip Tricca7a10ee32018-03-06 12:48:39 -0800224 cmocka_unit_test_setup_teardown (tcti_device_receive_one_call_success,
Philip Tricca0b870442018-03-26 14:27:28 -0700225 tcti_device_setup,
226 tcti_device_teardown),
Philip Tricca7a10ee32018-03-06 12:48:39 -0800227 cmocka_unit_test_setup_teardown (tcti_device_receive_two_call_success,
Philip Tricca0b870442018-03-26 14:27:28 -0700228 tcti_device_setup,
229 tcti_device_teardown),
Tadeusz Struk2147c492017-08-09 13:40:31 -0700230 cmocka_unit_test_setup_teardown (tcti_device_transmit_success,
Philip Tricca0b870442018-03-26 14:27:28 -0700231 tcti_device_setup,
232 tcti_device_teardown),
Philip Triccaa7d2e2c2016-11-17 14:47:21 -0800233 };
Tadeusz Struk2147c492017-08-09 13:40:31 -0700234 return cmocka_run_group_tests (tests, NULL, NULL);
Philip Triccaa7d2e2c2016-11-17 14:47:21 -0800235}