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
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"
25 #include "tss2-tcti/tctildr.h"
26 #define LOGMODULE test
27 #include "util/log.h"
28
29 /* global TCTI object, use to return reference from */
30 static TSS2_TCTI_CONTEXT_COMMON_V2 tcti_instance = { 0, };
31
32 char *
__wrap_dlerror(void)33 __wrap_dlerror(void)
34 {
35 return (char*)__func__;
36 }
37
38 void *
__wrap_dlopen(const char * filename,int flags)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
47 int
__wrap_dlclose(void * handle)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
55 void *
__wrap_dlsym(void * handle,const char * symbol)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
64 TSS2_TCTI_INFO *
__wrap_Tss2_Tcti_Fake_Info(void)65 __wrap_Tss2_Tcti_Fake_Info(void)
66 {
67 LOG_TRACE("Called.");
68 return mock_type(TSS2_TCTI_INFO *);
69 }
70
71 TSS2_RC
__wrap_tcti_from_init(TSS2_TCTI_INIT_FUNC init,const char * conf,TSS2_TCTI_CONTEXT ** tcti)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 }
79 TSS2_RC
__wrap_tcti_from_info(TSS2_TCTI_INFO_FUNC infof,const char * conf,TSS2_TCTI_CONTEXT ** tcti)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
92 #define TEST_HANDLE (void*)0xade0
93 static void
test_info_from_handle_null(void ** state)94 test_info_from_handle_null (void **state)
95 {
96 const TSS2_TCTI_INFO* info = info_from_handle (NULL);
97 assert_null (info);
98 }
99 static void
test_info_from_handle_dlsym_fail(void ** state)100 test_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 }
109 static void
test_info_from_handle_success(void ** state)110 test_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
125 static void
test_fail_null(void ** state)126 test_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
132 static void
test_handle_from_name_null_handle(void ** state)133 test_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 }
138 #define TEST_TCTI_NAME "test-tcti"
139 #define TEST_TCTI_CONF "test-conf"
140 static void
test_handle_from_name_first_dlopen_success(void ** state)141 test_handle_from_name_first_dlopen_success (void **state)
142 {
143 TSS2_RC rc;
144 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
156 static void
test_handle_from_name_second_dlopen_success(void ** state)157 test_handle_from_name_second_dlopen_success (void **state)
158 {
159 TSS2_RC rc;
160 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
166 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
175 static void
test_handle_from_name_third_dlopen_success(void ** state)176 test_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
198 static void
test_tcti_from_file_null_tcti(void ** state)199 test_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);
203 }
204
205 #define HANDLE (void *)123321
206 #ifndef ESYS_TCTI_DEFAULT_MODULE
207 static void
test_get_info_default_null(void ** state)208 test_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 }
213 static void
test_get_info_default_success(void ** state)214 test_get_info_default_success (void **state)
215 {
216 const TSS2_TCTI_INFO info_instance = { 0, };
217 TSS2_TCTI_INFO *info = { 0, };
218 void *handle;
219
220 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 }
246 static void
test_get_info_default_info_fail(void ** state)247 test_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 }
281 /** Test for tcti
282 * { "libtss2-tcti-default.so", NULL, "", "Access libtss2-tcti-default.so" }
283 */
284 static void
test_tcti_default(void ** state)285 test_tcti_default(void **state)
286 {
287 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 */
312 static void
test_tcti_default_fail_sym(void ** state)313 test_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 */
332 expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
333 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 */
354 static void
test_tcti_default_fail_info(void ** state)355 test_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 */
384 expect_string(__wrap_dlopen, filename, "libtss2-tcti-tabrmd.so.0");
385 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
403 static void
test_tcti_fail_all(void ** state)404 test_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);
410 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);
416
417 /* Skip over libtss2-tcti-tabrmd.so */
418 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");
425 expect_value(__wrap_dlopen, flags, RTLD_NOW);
426 will_return(__wrap_dlopen, NULL);
427
428 /* Skip over libtss2-tcti-device.so, /dev/tpmrm0 */
429 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");
436 expect_value(__wrap_dlopen, flags, RTLD_NOW);
437 will_return(__wrap_dlopen, NULL);
438
439 /* Skip over libtss2-tcti-device.so, /dev/tpm0 */
440 expect_string(__wrap_dlopen, filename, "libtss2-tcti-device.so.0");
441 expect_value(__wrap_dlopen, flags, RTLD_NOW);
442 will_return(__wrap_dlopen, NULL);
443 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
450 /* Skip over libtss2-tcti-mssim.so */
451 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");
458 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
467 void
test_info_from_name_null(void ** state)468 test_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 }
473 void
test_info_from_name_handle_fail(void ** state)474 test_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 }
492 void
test_info_from_name_info_fail(void ** state)493 test_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 }
514 void
test_info_from_name_success(void ** state)515 test_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 }
536 void
test_get_tcti_null(void ** state)537 test_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 }
542 void
test_get_tcti_default(void ** state)543 test_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 }
565 void
test_get_tcti_from_name(void ** state)566 test_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 }
588
589 void
test_tctildr_get_info_from_name(void ** state)590 test_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 }
608 void
test_tctildr_get_info_default(void ** state)609 test_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
629 void
test_finalize_data(void ** state)630 test_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
640 int
main(void)641 main(void)
642 {
643 const struct CMUnitTest tests[] = {
644 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),
647 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),
651 cmocka_unit_test(test_fail_null),
652 cmocka_unit_test(test_tcti_from_file_null_tcti),
653 #ifndef ESYS_TCTI_DEFAULT_MODULE
654 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),
657 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),
661 cmocka_unit_test(test_get_tcti_null),
662 cmocka_unit_test(test_get_tcti_default),
663 cmocka_unit_test(test_get_tcti_from_name),
664 cmocka_unit_test(test_tctildr_get_info_from_name),
665 cmocka_unit_test(test_tctildr_get_info_default),
666 #endif
667 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),
671 cmocka_unit_test(test_finalize_data),
672 };
673 return cmocka_run_group_tests (tests, NULL, NULL);
674 }
675