[go: nahoru, domu]

blob: 084a712db9565abc9ac81118b4109445eb9b6f26 [file] [log] [blame]
Philip Triccab258ab92019-06-28 13:52:14 -07001/* 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
8#ifdef HAVE_CONFIG_H
9#include <config.h>
10#endif
11
12#include <stdio.h>
13#include <stddef.h>
14#include <stdlib.h>
15
16#include <dlfcn.h>
17
18#include <setjmp.h>
19#include <cmocka.h>
20
21#include "tss2_tcti.h"
22
23#include "tss2-tcti/tctildr-interface.h"
24#include "tss2-tcti/tctildr-dl.h"
Philip Tricca06389062019-06-21 14:19:51 -070025#include "tss2-tcti/tctildr.h"
Philip Triccab258ab92019-06-28 13:52:14 -070026#define LOGMODULE test
27#include "util/log.h"
28
Philip Triccabe796452019-07-09 16:13:19 -070029/* global TCTI object, use to return reference from */
30static TSS2_TCTI_CONTEXT_COMMON_V2 tcti_instance = { 0, };
31
Philip Tricca06389062019-06-21 14:19:51 -070032char *
33__wrap_dlerror(void)
34{
35 return (char*)__func__;
36}
37
Philip Triccab258ab92019-06-28 13:52:14 -070038void *
39__wrap_dlopen(const char *filename, int flags)
40{
41 LOG_TRACE("Called with filename %s and flags %x", filename, flags);
42 check_expected_ptr(filename);
43 check_expected(flags);
44 return mock_type(void *);
45}
46
47int
48__wrap_dlclose(void *handle)
49{
50 LOG_TRACE("Called with handle %p", handle);
51 check_expected_ptr(handle);
52 return mock_type(int);
53}
54
55void *
56__wrap_dlsym(void *handle, const char *symbol)
57{
58 LOG_TRACE("Called with handle %p and symbol %s", handle, symbol);
59 check_expected_ptr(handle);
60 check_expected_ptr(symbol);
61 return mock_type(void *);
62}
63
64TSS2_TCTI_INFO *
65__wrap_Tss2_Tcti_Fake_Info(void)
66{
67 LOG_TRACE("Called.");
68 return mock_type(TSS2_TCTI_INFO *);
69}
70
71TSS2_RC
72__wrap_tcti_from_init(TSS2_TCTI_INIT_FUNC init,
73 const char* conf,
74 TSS2_TCTI_CONTEXT **tcti)
75{
76 printf("%s", __func__);
77 return mock_type (TSS2_RC);
78}
79TSS2_RC
80__wrap_tcti_from_info(TSS2_TCTI_INFO_FUNC infof,
81 const char* conf,
82 TSS2_TCTI_CONTEXT **tcti)
83{
84 check_expected (infof);
85 check_expected (conf);
86 check_expected (tcti);
87 if (tcti != NULL)
88 *tcti = mock_type (TSS2_TCTI_CONTEXT*);
89 return mock_type (TSS2_RC);
90}
91
Philip Tricca24014ec2019-06-21 19:27:18 -070092#define TEST_HANDLE (void*)0xade0
93static void
94test_info_from_handle_null (void **state)
95{
96 const TSS2_TCTI_INFO* info = info_from_handle (NULL);
97 assert_null (info);
98}
99static void
100test_info_from_handle_dlsym_fail (void **state)
101{
102 expect_value(__wrap_dlsym, handle, TEST_HANDLE);
103 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
104 will_return(__wrap_dlsym, NULL);
105
106 const TSS2_TCTI_INFO* info = info_from_handle (TEST_HANDLE);
107 assert_null (info);
108}
109static void
110test_info_from_handle_success (void **state)
111{
112 TSS2_TCTI_INFO info_instance = { 0, };
113 const TSS2_TCTI_INFO *info = { 0, };
114
115 expect_value(__wrap_dlsym, handle, TEST_HANDLE);
116 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
117 will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
118
119 will_return(__wrap_Tss2_Tcti_Fake_Info, &info_instance);
120
121 info = info_from_handle (TEST_HANDLE);
122 assert_ptr_equal (info, &info_instance);
123}
124
Philip Triccab258ab92019-06-28 13:52:14 -0700125static void
126test_fail_null(void **state)
127{
128 TSS2_RC r = tctildr_get_default(NULL, NULL);
129 assert_int_equal(r, TSS2_TCTI_RC_BAD_REFERENCE);
130}
131
Philip Tricca06389062019-06-21 14:19:51 -0700132static void
133test_handle_from_name_null_handle (void **state)
134{
135 TSS2_RC rc = handle_from_name (NULL, NULL);
136 assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
137}
Philip Triccab258ab92019-06-28 13:52:14 -0700138#define TEST_TCTI_NAME "test-tcti"
139#define TEST_TCTI_CONF "test-conf"
140static void
Philip Tricca06389062019-06-21 14:19:51 -0700141test_handle_from_name_first_dlopen_success (void **state)
Philip Triccab258ab92019-06-28 13:52:14 -0700142{
143 TSS2_RC rc;
Philip Tricca06389062019-06-21 14:19:51 -0700144 void *handle = NULL;
145
146 expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME);
147 expect_value(__wrap_dlopen, flags, RTLD_NOW);
148 will_return(__wrap_dlopen, TEST_HANDLE);
149
150 rc = handle_from_name (TEST_TCTI_NAME, &handle);
151 assert_int_equal (rc, TSS2_RC_SUCCESS);
152 assert_int_equal (handle, TEST_HANDLE);
153}
154
155#define TEST_TCTI_NAME_SO_0 TCTI_PREFIX"-"TEST_TCTI_NAME""TCTI_SUFFIX_0
156static void
157test_handle_from_name_second_dlopen_success (void **state)
158{
159 TSS2_RC rc;
Philip Triccab258ab92019-06-28 13:52:14 -0700160 void *handle = NULL;
161
162 expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME);
163 expect_value(__wrap_dlopen, flags, RTLD_NOW);
164 will_return(__wrap_dlopen, NULL);
165
Philip Tricca06389062019-06-21 14:19:51 -0700166 expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME_SO_0);
167 expect_value(__wrap_dlopen, flags, RTLD_NOW);
168 will_return(__wrap_dlopen, TEST_HANDLE);
169
170 rc = handle_from_name (TEST_TCTI_NAME, &handle);
171 assert_int_equal (rc, TSS2_RC_SUCCESS);
172 assert_int_equal (handle, TEST_HANDLE);
173}
174#define TEST_TCTI_NAME_SO TCTI_PREFIX"-"TEST_TCTI_NAME""TCTI_SUFFIX
175static void
176test_handle_from_name_third_dlopen_success (void **state)
177{
178 TSS2_RC rc;
179 void *handle = NULL;
180
181 expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME);
182 expect_value(__wrap_dlopen, flags, RTLD_NOW);
183 will_return(__wrap_dlopen, NULL);
184
185 expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME_SO_0);
186 expect_value(__wrap_dlopen, flags, RTLD_NOW);
187 will_return(__wrap_dlopen, NULL);
188
189 expect_string(__wrap_dlopen, filename, TEST_TCTI_NAME_SO);
190 expect_value(__wrap_dlopen, flags, RTLD_NOW);
191 will_return(__wrap_dlopen, TEST_HANDLE);
192
193 rc = handle_from_name (TEST_TCTI_NAME, &handle);
194 assert_int_equal (rc, TSS2_RC_SUCCESS);
195 assert_int_equal (handle, TEST_HANDLE);
196}
197
198static void
199test_tcti_from_file_null_tcti (void **state)
200{
201 TSS2_RC rc = tcti_from_file (NULL, NULL, NULL, NULL);
202 assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
Philip Triccab258ab92019-06-28 13:52:14 -0700203}
204
Philip Triccaf7afc9c2019-07-14 22:20:25 -0700205#define HANDLE (void *)123321
Philip Triccab258ab92019-06-28 13:52:14 -0700206#ifndef ESYS_TCTI_DEFAULT_MODULE
Philip Tricca24014ec2019-06-21 19:27:18 -0700207static void
208test_get_info_default_null (void **state)
209{
210 TSS2_RC rc = get_info_default (NULL, NULL);
211 assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
212}
213static void
214test_get_info_default_success (void **state)
215{
216 const TSS2_TCTI_INFO info_instance = { 0, };
217 TSS2_TCTI_INFO *info = { 0, };
218 void *handle;
Philip Triccab258ab92019-06-28 13:52:14 -0700219
Philip Tricca24014ec2019-06-21 19:27:18 -0700220 expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
221 expect_value(__wrap_dlopen, flags, RTLD_NOW);
222 will_return(__wrap_dlopen, NULL);
223
224 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so.0");
225 expect_value(__wrap_dlopen, flags, RTLD_NOW);
226 will_return(__wrap_dlopen, NULL);
227
228 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so");
229 expect_value(__wrap_dlopen, flags, RTLD_NOW);
230 will_return(__wrap_dlopen, NULL);
231
232 expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
233 expect_value(__wrap_dlopen, flags, RTLD_NOW);
234 will_return(__wrap_dlopen, HANDLE);
235
236 expect_value(__wrap_dlsym, handle, HANDLE);
237 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
238 will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
239
240 will_return(__wrap_Tss2_Tcti_Fake_Info, &info_instance);
241
242 TSS2_RC rc = get_info_default (&info, &handle);
243 assert_int_equal (rc, TSS2_RC_SUCCESS);
244 assert_ptr_equal (info, &info_instance);
245}
246static void
247test_get_info_default_info_fail (void **state)
248{
249 TSS2_TCTI_INFO *info = { 0, };
250 void *handle;
251
252 expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
253 expect_value(__wrap_dlopen, flags, RTLD_NOW);
254 will_return(__wrap_dlopen, NULL);
255
256 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so.0");
257 expect_value(__wrap_dlopen, flags, RTLD_NOW);
258 will_return(__wrap_dlopen, NULL);
259
260 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so");
261 expect_value(__wrap_dlopen, flags, RTLD_NOW);
262 will_return(__wrap_dlopen, NULL);
263
264 expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
265 expect_value(__wrap_dlopen, flags, RTLD_NOW);
266 will_return(__wrap_dlopen, HANDLE);
267
268 expect_value(__wrap_dlsym, handle, HANDLE);
269 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
270 will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
271
272 will_return(__wrap_Tss2_Tcti_Fake_Info, NULL);
273
274 expect_value(__wrap_dlclose, handle, HANDLE);
275 will_return(__wrap_dlclose, 0);
276
277 TSS2_RC rc = get_info_default (&info, &handle);
278 assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
279 assert_ptr_equal (info, NULL);
280}
Philip Triccab258ab92019-06-28 13:52:14 -0700281/** Test for tcti
282 * { "libtss2-tcti-default.so", NULL, "", "Access libtss2-tcti-default.so" }
283 */
284static void
285test_tcti_default(void **state)
286{
Philip Triccab258ab92019-06-28 13:52:14 -0700287 TSS2_TCTI_CONTEXT *tcti;
288
289 expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
290 expect_value(__wrap_dlopen, flags, RTLD_NOW);
291 will_return(__wrap_dlopen, HANDLE);
292
293 expect_value(__wrap_dlsym, handle, HANDLE);
294 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
295 will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
296
297 expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
298 expect_value(__wrap_tcti_from_info, conf, NULL);
299 expect_value(__wrap_tcti_from_info, tcti, &tcti);
300 will_return(__wrap_tcti_from_info, &tcti_instance);
301 will_return(__wrap_tcti_from_info, TSS2_RC_SUCCESS);
302
303 TSS2_RC r;
304 void *handle = NULL;
305 r = tctildr_get_default(&tcti, &handle);
306 assert_int_equal(r, TSS2_RC_SUCCESS);
307}
308
309/** Test for failure on tcti
310 * { "libtss2-tcti-default.so", NULL, "", "Access libtss2-tcti-default.so" }
311 */
312static void
313test_tcti_default_fail_sym(void **state)
314{
315 TSS2_TCTI_CONTEXT *tcti;
316#define HANDLE (void *)123321
317
318 expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
319 expect_value(__wrap_dlopen, flags, RTLD_NOW);
320 will_return(__wrap_dlopen, HANDLE);
321
322 expect_value(__wrap_dlsym, handle, HANDLE);
323 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
324 will_return(__wrap_dlsym, NULL);
325
326 expect_value(__wrap_dlclose, handle, HANDLE);
327 will_return(__wrap_dlclose, 0);
328
329 /** Now test
330 *{ "libtss2-tcti-tabrmd.so", NULL, "", "Access libtss2-tcti-tabrmd.so"},
331 */
Philip Tricca06389062019-06-21 14:19:51 -0700332 expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
Philip Triccab258ab92019-06-28 13:52:14 -0700333 expect_value(__wrap_dlopen, flags, RTLD_NOW);
334 will_return(__wrap_dlopen, HANDLE);
335
336 expect_value(__wrap_dlsym, handle, HANDLE);
337 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
338 will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
339
340 expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
341 expect_value(__wrap_tcti_from_info, conf, NULL);
342 expect_value(__wrap_tcti_from_info, tcti, &tcti);
343 will_return(__wrap_tcti_from_info, &tcti_instance);
344 will_return(__wrap_tcti_from_info, TSS2_RC_SUCCESS);
345
346 TSS2_RC r;
347 r = tctildr_get_default(&tcti, NULL);
348 assert_int_equal(r, TSS2_RC_SUCCESS);
349}
350
351/** Test for failure on tcti
352 * { "libtss2-tcti-default.so", NULL, "", "Access libtss2-tcti-default.so" }
353 */
354static void
355test_tcti_default_fail_info(void **state)
356{
357 TSS2_TCTI_CONTEXT *tcti;
358#define HANDLE (void *)123321
359#define TEST_RC 0x55687
360
361 /** Test for failure on tcti
362 * { "libtss2-tcti-default.so", NULL, "", "Access libtss2-tcti-default.so" }
363 */
364 expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
365 expect_value(__wrap_dlopen, flags, RTLD_NOW);
366 will_return(__wrap_dlopen, HANDLE);
367
368 expect_value(__wrap_dlsym, handle, HANDLE);
369 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
370 will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
371
372 expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
373 expect_value(__wrap_tcti_from_info, conf, NULL);
374 expect_value(__wrap_tcti_from_info, tcti, &tcti);
375 will_return(__wrap_tcti_from_info, &tcti_instance);
376 will_return(__wrap_tcti_from_info, TEST_RC);
377
378 expect_value(__wrap_dlclose, handle, HANDLE);
379 will_return(__wrap_dlclose, 0);
380
381 /** Now test
382 *{ "libtss2-tcti-tabrmd.so", NULL, "", "Access libtss2-tcti-tabrmd.so"},
383 */
Philip Tricca06389062019-06-21 14:19:51 -0700384 expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
Philip Triccab258ab92019-06-28 13:52:14 -0700385 expect_value(__wrap_dlopen, flags, RTLD_NOW);
386 will_return(__wrap_dlopen, HANDLE);
387
388 expect_value(__wrap_dlsym, handle, HANDLE);
389 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
390 will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
391
392 expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
393 expect_value(__wrap_tcti_from_info, conf, NULL);
394 expect_value(__wrap_tcti_from_info, tcti, &tcti);
395 will_return(__wrap_tcti_from_info, &tcti_instance);
396 will_return(__wrap_tcti_from_info, TSS2_RC_SUCCESS);
397
398 TSS2_RC r;
399 r = tctildr_get_default(&tcti, NULL);
400 assert_int_equal(r, TSS2_RC_SUCCESS);
401}
402
403static void
404test_tcti_fail_all (void **state)
405{
406 /* skip over libtss2-tcti-default.so */
407 expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
408 expect_value(__wrap_dlopen, flags, RTLD_NOW);
409 will_return(__wrap_dlopen, NULL);
Philip Tricca06389062019-06-21 14:19:51 -0700410 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so.0");
411 expect_value(__wrap_dlopen, flags, RTLD_NOW);
412 will_return(__wrap_dlopen, NULL);
413 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-default.so.so");
414 expect_value(__wrap_dlopen, flags, RTLD_NOW);
415 will_return(__wrap_dlopen, NULL);
Philip Triccab258ab92019-06-28 13:52:14 -0700416
417 /* Skip over libtss2-tcti-tabrmd.so */
Philip Tricca06389062019-06-21 14:19:51 -0700418 expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
419 expect_value(__wrap_dlopen, flags, RTLD_NOW);
420 will_return(__wrap_dlopen, NULL);
421 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-tabrmd.so.0.so.0");
422 expect_value(__wrap_dlopen, flags, RTLD_NOW);
423 will_return(__wrap_dlopen, NULL);
424 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-tabrmd.so.0.so");
Philip Triccab258ab92019-06-28 13:52:14 -0700425 expect_value(__wrap_dlopen, flags, RTLD_NOW);
426 will_return(__wrap_dlopen, NULL);
427
428 /* Skip over libtss2-tcti-device.so, /dev/tpmrm0 */
Philip Tricca06389062019-06-21 14:19:51 -0700429 expect_string(__wrap_dlopen, filename, "libtss2-tcti-device.so.0");
430 expect_value(__wrap_dlopen, flags, RTLD_NOW);
431 will_return(__wrap_dlopen, NULL);
432 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-device.so.0.so.0");
433 expect_value(__wrap_dlopen, flags, RTLD_NOW);
434 will_return(__wrap_dlopen, NULL);
435 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-device.so.0.so");
Philip Triccab258ab92019-06-28 13:52:14 -0700436 expect_value(__wrap_dlopen, flags, RTLD_NOW);
437 will_return(__wrap_dlopen, NULL);
438
439 /* Skip over libtss2-tcti-device.so, /dev/tpm0 */
Philip Tricca06389062019-06-21 14:19:51 -0700440 expect_string(__wrap_dlopen, filename, "libtss2-tcti-device.so.0");
Philip Triccab258ab92019-06-28 13:52:14 -0700441 expect_value(__wrap_dlopen, flags, RTLD_NOW);
442 will_return(__wrap_dlopen, NULL);
Philip Tricca06389062019-06-21 14:19:51 -0700443 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-device.so.0.so.0");
444 expect_value(__wrap_dlopen, flags, RTLD_NOW);
445 will_return(__wrap_dlopen, NULL);
446 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-device.so.0.so");
447 expect_value(__wrap_dlopen, flags, RTLD_NOW);
448 will_return(__wrap_dlopen, NULL);
449
Philip Triccab258ab92019-06-28 13:52:14 -0700450 /* Skip over libtss2-tcti-mssim.so */
Philip Tricca06389062019-06-21 14:19:51 -0700451 expect_string(__wrap_dlopen, filename, "libtss2-tcti-mssim.so.0");
452 expect_value(__wrap_dlopen, flags, RTLD_NOW);
453 will_return(__wrap_dlopen, NULL);
454 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-mssim.so.0.so.0");
455 expect_value(__wrap_dlopen, flags, RTLD_NOW);
456 will_return(__wrap_dlopen, NULL);
457 expect_string(__wrap_dlopen, filename, "libtss2-tcti-libtss2-tcti-mssim.so.0.so");
Philip Triccab258ab92019-06-28 13:52:14 -0700458 expect_value(__wrap_dlopen, flags, RTLD_NOW);
459 will_return(__wrap_dlopen, NULL);
460
461 TSS2_RC r;
462 TSS2_TCTI_CONTEXT *tcti;
463 r = tctildr_get_default(&tcti, NULL);
464 assert_int_equal(r, TSS2_TCTI_RC_IO_ERROR);
465}
466#endif
Philip Triccabe796452019-07-09 16:13:19 -0700467void
Philip Tricca24014ec2019-06-21 19:27:18 -0700468test_info_from_name_null (void **state)
469{
470 TSS2_RC rc = info_from_name (NULL, NULL, NULL);
471 assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
472}
473void
474test_info_from_name_handle_fail (void **state)
475{
476 const TSS2_TCTI_INFO *info;
477 void *data;
478
479 expect_string(__wrap_dlopen, filename, "foo");
480 expect_value(__wrap_dlopen, flags, RTLD_NOW);
481 will_return(__wrap_dlopen, NULL);
482 expect_string(__wrap_dlopen, filename, "libtss2-tcti-foo.so.0");
483 expect_value(__wrap_dlopen, flags, RTLD_NOW);
484 will_return(__wrap_dlopen, NULL);
485 expect_string(__wrap_dlopen, filename, "libtss2-tcti-foo.so");
486 expect_value(__wrap_dlopen, flags, RTLD_NOW);
487 will_return(__wrap_dlopen, NULL);
488
489 TSS2_RC rc = info_from_name ("foo", &info, &data);
490 assert_int_equal (rc, TSS2_TCTI_RC_NOT_SUPPORTED);
491}
492void
493test_info_from_name_info_fail (void **state)
494{
495 const TSS2_TCTI_INFO *info = { 0, };
496 void *data = HANDLE;
497
498 expect_string(__wrap_dlopen, filename, "foo");
499 expect_value(__wrap_dlopen, flags, RTLD_NOW);
500 will_return(__wrap_dlopen, HANDLE);
501
502 expect_value(__wrap_dlsym, handle, HANDLE);
503 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
504 will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
505
506 will_return(__wrap_Tss2_Tcti_Fake_Info, NULL);
507
508 expect_value(__wrap_dlclose, handle, HANDLE);
509 will_return(__wrap_dlclose, 0);
510
511 TSS2_RC rc = info_from_name ("foo", &info, &data);
512 assert_int_equal (rc, TSS2_TCTI_RC_IO_ERROR);
513}
514void
515test_info_from_name_success (void **state)
516{
517 const TSS2_TCTI_INFO *info = { 0, };
518 TSS2_TCTI_INFO info_instance = { 0, };
519 void *data;
520
521 expect_string(__wrap_dlopen, filename, "foo");
522 expect_value(__wrap_dlopen, flags, RTLD_NOW);
523 will_return(__wrap_dlopen, HANDLE);
524
525 expect_value(__wrap_dlsym, handle, HANDLE);
526 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
527 will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
528
529 will_return(__wrap_Tss2_Tcti_Fake_Info, &info_instance);
530
531 TSS2_RC rc = info_from_name ("foo", &info, &data);
532 assert_int_equal (rc, TSS2_RC_SUCCESS);
533 assert_ptr_equal (info, &info_instance);
534 assert_ptr_equal (data, HANDLE);
535}
536void
Philip Triccabe796452019-07-09 16:13:19 -0700537test_get_tcti_null (void **state)
538{
539 TSS2_RC rc = tctildr_get_tcti (NULL, NULL, NULL, NULL);
540 assert_int_equal(rc, TSS2_TCTI_RC_BAD_REFERENCE);
541}
542void
543test_get_tcti_default (void **state)
544{
545 TSS2_TCTI_CONTEXT *tcti;
546
547 expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
548 expect_value(__wrap_dlopen, flags, RTLD_NOW);
549 will_return(__wrap_dlopen, HANDLE);
550
551 expect_value(__wrap_dlsym, handle, HANDLE);
552 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
553 will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
554
555 expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
556 expect_value(__wrap_tcti_from_info, conf, NULL);
557 expect_value(__wrap_tcti_from_info, tcti, &tcti);
558 will_return(__wrap_tcti_from_info, &tcti_instance);
559 will_return(__wrap_tcti_from_info, TSS2_RC_SUCCESS);
560
561 void *data;
562 TSS2_RC rc = tctildr_get_tcti (NULL, NULL, &tcti, &data);
563 assert_int_equal(rc, TSS2_RC_SUCCESS);
564}
565void
566test_get_tcti_from_name (void **state)
567{
568 TSS2_TCTI_CONTEXT *tcti;
569
570 expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
571 expect_value(__wrap_dlopen, flags, RTLD_NOW);
572 will_return(__wrap_dlopen, HANDLE);
573
574 expect_value(__wrap_dlsym, handle, HANDLE);
575 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
576 will_return(__wrap_dlsym, &__wrap_Tss2_Tcti_Fake_Info);
577
578 expect_value(__wrap_tcti_from_info, infof, __wrap_Tss2_Tcti_Fake_Info);
579 expect_value(__wrap_tcti_from_info, conf, NULL);
580 expect_value(__wrap_tcti_from_info, tcti, &tcti);
581 will_return(__wrap_tcti_from_info, &tcti_instance);
582 will_return(__wrap_tcti_from_info, TSS2_RC_SUCCESS);
583
584 void *data;
585 TSS2_RC rc = tctildr_get_tcti ("libtss2-tcti-default.so", NULL, &tcti, &data);
586 assert_int_equal(rc, TSS2_RC_SUCCESS);
587}
Philip Triccab258ab92019-06-28 13:52:14 -0700588
589void
Philip Tricca24014ec2019-06-21 19:27:18 -0700590test_tctildr_get_info_from_name (void **state)
591{
592 const TSS2_TCTI_INFO *info;
593 void *data;
594
595 expect_string(__wrap_dlopen, filename, "foo");
596 expect_value(__wrap_dlopen, flags, RTLD_NOW);
597 will_return(__wrap_dlopen, NULL);
598 expect_string(__wrap_dlopen, filename, "libtss2-tcti-foo.so.0");
599 expect_value(__wrap_dlopen, flags, RTLD_NOW);
600 will_return(__wrap_dlopen, NULL);
601 expect_string(__wrap_dlopen, filename, "libtss2-tcti-foo.so");
602 expect_value(__wrap_dlopen, flags, RTLD_NOW);
603 will_return(__wrap_dlopen, NULL);
604
605 TSS2_RC rc = tctildr_get_info ("foo", &info, &data);
606 assert_int_equal (rc, TSS2_TCTI_RC_NOT_SUPPORTED);
607}
608void
609test_tctildr_get_info_default (void **state)
610{
611 const TSS2_TCTI_INFO *info;
612 void *data;
613
614 expect_string(__wrap_dlopen, filename, "libtss2-tcti-default.so");
615 expect_value(__wrap_dlopen, flags, RTLD_NOW);
616 will_return(__wrap_dlopen, HANDLE);
617
618 expect_value(__wrap_dlsym, handle, HANDLE);
619 expect_string(__wrap_dlsym, symbol, TSS2_TCTI_INFO_SYMBOL);
620 will_return(__wrap_dlsym, NULL);
621
622 expect_value(__wrap_dlclose, handle, HANDLE);
623 will_return(__wrap_dlclose, 0);
624
625 TSS2_RC rc = tctildr_get_info (NULL, &info, &data);
626 assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
627}
628
629void
Philip Triccab258ab92019-06-28 13:52:14 -0700630test_finalize_data (void **state)
631{
632 void *data = HANDLE;
633
634 expect_value(__wrap_dlclose, handle, data);
635 will_return(__wrap_dlclose, 0);
636 tctildr_finalize_data (&data);
637 assert_null (data);
638}
639
640int
641main(void)
642{
643 const struct CMUnitTest tests[] = {
Philip Tricca24014ec2019-06-21 19:27:18 -0700644 cmocka_unit_test(test_info_from_handle_null),
645 cmocka_unit_test(test_info_from_handle_dlsym_fail),
646 cmocka_unit_test(test_info_from_handle_success),
Philip Tricca06389062019-06-21 14:19:51 -0700647 cmocka_unit_test(test_handle_from_name_null_handle),
648 cmocka_unit_test(test_handle_from_name_first_dlopen_success),
649 cmocka_unit_test(test_handle_from_name_second_dlopen_success),
650 cmocka_unit_test(test_handle_from_name_third_dlopen_success),
Philip Triccab258ab92019-06-28 13:52:14 -0700651 cmocka_unit_test(test_fail_null),
Philip Tricca06389062019-06-21 14:19:51 -0700652 cmocka_unit_test(test_tcti_from_file_null_tcti),
Philip Triccab258ab92019-06-28 13:52:14 -0700653#ifndef ESYS_TCTI_DEFAULT_MODULE
Philip Tricca24014ec2019-06-21 19:27:18 -0700654 cmocka_unit_test(test_get_info_default_null),
655 cmocka_unit_test(test_get_info_default_success),
656 cmocka_unit_test(test_get_info_default_info_fail),
Philip Triccab258ab92019-06-28 13:52:14 -0700657 cmocka_unit_test(test_tcti_default),
658 cmocka_unit_test(test_tcti_default_fail_sym),
659 cmocka_unit_test(test_tcti_default_fail_info),
660 cmocka_unit_test(test_tcti_fail_all),
Philip Triccabe796452019-07-09 16:13:19 -0700661 cmocka_unit_test(test_get_tcti_null),
662 cmocka_unit_test(test_get_tcti_default),
663 cmocka_unit_test(test_get_tcti_from_name),
Philip Tricca24014ec2019-06-21 19:27:18 -0700664 cmocka_unit_test(test_tctildr_get_info_from_name),
665 cmocka_unit_test(test_tctildr_get_info_default),
Philip Triccab258ab92019-06-28 13:52:14 -0700666#endif
Philip Tricca24014ec2019-06-21 19:27:18 -0700667 cmocka_unit_test(test_info_from_name_null),
668 cmocka_unit_test(test_info_from_name_handle_fail),
669 cmocka_unit_test(test_info_from_name_info_fail),
670 cmocka_unit_test(test_info_from_name_success),
Philip Triccab258ab92019-06-28 13:52:14 -0700671 cmocka_unit_test(test_finalize_data),
672 };
673 return cmocka_run_group_tests (tests, NULL, NULL);
674}