1 /*
2 * SPDX-License-Identifier: BSD-2-Clause
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
11 #include <stdio.h>
12 #include <stddef.h>
13 #include <stdlib.h>
14
15 #include <dlfcn.h>
16
17 #include <setjmp.h>
18 #include <cmocka.h>
19
20 #include "tss2_tcti.h"
21
22 #include "tss2-tcti/tctildr-interface.h"
23 #include "tss2-tcti/tctildr-nodl.h"
24 #define LOGMODULE test
25 #include "util/log.h"
26
27 TSS2_RC
__wrap_Tss2_Tcti_Device_Init(TSS2_TCTI_CONTEXT * tctiContext,size_t * size,const char * config)28 __wrap_Tss2_Tcti_Device_Init(TSS2_TCTI_CONTEXT *tctiContext, size_t *size,
29 const char *config)
30 {
31 return TSS2_RC_SUCCESS;
32 }
33
34 TSS2_RC
__wrap_Tss2_Tcti_Mssim_Init(TSS2_TCTI_CONTEXT * tctiContext,size_t * size,const char * config)35 __wrap_Tss2_Tcti_Mssim_Init(TSS2_TCTI_CONTEXT *tctiContext, size_t *size,
36 const char *config)
37 {
38 return TSS2_RC_SUCCESS;
39 }
40
41 TSS2_RC
__wrap_tcti_from_init(TSS2_TCTI_INIT_FUNC init,const char * conf,TSS2_TCTI_CONTEXT ** tcti)42 __wrap_tcti_from_init(TSS2_TCTI_INIT_FUNC init,
43 const char* conf,
44 TSS2_TCTI_CONTEXT **tcti)
45 {
46 if (tcti != NULL)
47 *tcti = mock_type (TSS2_TCTI_CONTEXT*);
48 return mock_type (TSS2_RC);
49 }
50 void
test_tctildr_get_default_null_param(void ** state)51 test_tctildr_get_default_null_param (void **state)
52 {
53 TSS2_RC rc;
54
55 rc = tctildr_get_default (NULL, NULL);
56 assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
57 }
58 void
test_tctildr_get_default_all_fail(void ** state)59 test_tctildr_get_default_all_fail (void **state)
60 {
61 TSS2_RC rc;
62 TSS2_TCTI_CONTEXT *tcti_ctx = NULL;
63
64 #define TEST_RC 0x65203563
65 will_return (__wrap_tcti_from_init, tcti_ctx);
66 will_return (__wrap_tcti_from_init, TEST_RC);
67 will_return (__wrap_tcti_from_init, tcti_ctx);
68 will_return (__wrap_tcti_from_init, TEST_RC);
69 #ifdef TCTI_MSSIM
70 will_return (__wrap_tcti_from_init, tcti_ctx);
71 will_return (__wrap_tcti_from_init, TEST_RC);
72 #endif
73 rc = tctildr_get_default (&tcti_ctx, NULL);
74 assert_int_equal (rc, TSS2_TCTI_RC_IO_ERROR);
75
76 }
77 static TSS2_TCTI_CONTEXT_COMMON_V2 test_ctx = { 0, };
78 void
test_get_tcti_null_tcti(void ** state)79 test_get_tcti_null_tcti (void **state)
80 {
81 TSS2_RC rc = tctildr_get_tcti (NULL, NULL, NULL, NULL);
82 assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
83 }
84
85 void
test_get_tcti_default_success(void ** state)86 test_get_tcti_default_success (void **state)
87 {
88 TSS2_RC rc;
89 TSS2_TCTI_CONTEXT *tcti_ctx = NULL;
90
91 will_return (__wrap_tcti_from_init, &test_ctx);
92 will_return (__wrap_tcti_from_init, TSS2_RC_SUCCESS);
93 rc = tctildr_get_tcti (NULL, NULL, &tcti_ctx, NULL);
94 assert_int_equal (rc, TSS2_RC_SUCCESS);
95 assert_ptr_equal (tcti_ctx, &test_ctx);
96 }
97 void
test_get_tcti_match_second(void ** state)98 test_get_tcti_match_second (void **state)
99 {
100 TSS2_RC rc;
101 TSS2_TCTI_CONTEXT *tcti_ctx = NULL;
102
103 will_return (__wrap_tcti_from_init, &test_ctx);
104 will_return (__wrap_tcti_from_init, TSS2_RC_SUCCESS);
105 rc = tctildr_get_tcti ("libtss2-tcti-device.so", NULL, &tcti_ctx, NULL);
106 assert_int_equal (rc, TSS2_RC_SUCCESS);
107 assert_ptr_equal (tcti_ctx, &test_ctx);
108 }
109 void
test_get_tcti_match_none(void ** state)110 test_get_tcti_match_none (void **state)
111 {
112 TSS2_RC rc;
113 TSS2_TCTI_CONTEXT *tcti_ctx = NULL;
114
115 rc = tctildr_get_tcti ("foo", NULL, &tcti_ctx, NULL);
116 assert_int_equal (rc, TSS2_TCTI_RC_IO_ERROR);
117 }
118 void
test_finalize_data(void ** state)119 test_finalize_data (void **state)
120 {
121 tctildr_finalize_data (NULL);
122 }
123 void
test_get_info(void ** state)124 test_get_info (void **state)
125 {
126 TSS2_RC rc = tctildr_get_info (NULL, NULL, NULL);
127 assert_int_equal (rc, TSS2_TCTI_RC_NOT_SUPPORTED);
128 }
129 int
main(void)130 main(void)
131 {
132 const struct CMUnitTest tests[] = {
133 cmocka_unit_test (test_tctildr_get_default_null_param),
134 cmocka_unit_test (test_tctildr_get_default_all_fail),
135 cmocka_unit_test (test_get_tcti_null_tcti),
136 cmocka_unit_test (test_get_tcti_default_success),
137 cmocka_unit_test (test_get_tcti_match_second),
138 cmocka_unit_test (test_get_tcti_match_none),
139 cmocka_unit_test (test_finalize_data),
140 cmocka_unit_test (test_get_info),
141 };
142 return cmocka_run_group_tests (tests, NULL, NULL);
143 }
144