1 /*
2 * SPDX-License-Identifier: BSD-2-Clause
3 * Copyright 2019, Intel Corporation
4 */
5
6 #include <inttypes.h>
7 #include <limits.h>
8 #include <stdarg.h>
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13
14 #include <setjmp.h>
15 #include <cmocka.h>
16
17 #include "tss2_tcti.h"
18 #include "tss2_tctildr.h"
19
20 #include "tss2-tcti/tctildr.h"
21 #include "tss2-tcti/tcti-common.h"
22
23 static char* name_dup = "name_dup";
24 static char* description_dup = "description_dup";
25 static char* config_help_dup = "config_help_dup";
26
27 static TSS2_TCTI_INFO info_src = {
28 .version = 1,
29 .name = "name",
30 .description = "description",
31 .config_help = "config_help",
32 };
33 static TSS2_TCTI_INFO info_dst = { 0, };
34
35 char*
36 __real_strndup (const char *s, size_t n);
37 char*
__wrap_strndup(const char * s,size_t n)38 __wrap_strndup (const char *s,
39 size_t n)
40 {
41 if (s == info_src.name || \
42 s == info_src.description || \
43 s == info_src.config_help)
44 {
45 printf("%s: got known string\n", __func__);
46 errno = mock_type (int);
47 return mock_type (char*);
48 } else {
49 return __real_strndup (s, n);
50 }
51 }
52
53 void __real_free (void *ptr);
54 void
__wrap_free(void * ptr)55 __wrap_free (void *ptr)
56 {
57 printf("%s: %p\n", __func__, ptr);
58 if (ptr == name_dup || \
59 ptr == description_dup || \
60 ptr == config_help_dup || \
61 ptr == &info_dst)
62 {
63 printf("%s: got known memory\n", __func__);
64 return;
65 } else {
66 printf("%s: real free\n", __func__);
67 return __real_free (ptr);
68 }
69 }
70 TSS2_RC
__wrap_tctildr_get_info(const char * name,const TSS2_TCTI_INFO ** info,void ** data)71 __wrap_tctildr_get_info (const char *name,
72 const TSS2_TCTI_INFO **info,
73 void **data)
74 {
75 TSS2_RC rc = mock_type (TSS2_RC);
76 if (rc == TSS2_RC_SUCCESS) {
77 *info = mock_type (TSS2_TCTI_INFO*);
78 *data = mock_type (void*);
79 }
80 return rc;
81 }
82
83 TSS2_RC
__wrap_tctildr_get_tcti(const char * name,const char * conf,TSS2_TCTI_CONTEXT ** tcti,void ** data)84 __wrap_tctildr_get_tcti (const char *name,
85 const char* conf,
86 TSS2_TCTI_CONTEXT **tcti,
87 void **data)
88 {
89 TSS2_RC rc = mock_type (TSS2_RC);
90 if (rc == TSS2_RC_SUCCESS) {
91 *tcti= mock_type (TSS2_TCTI_CONTEXT*);
92 *data = mock_type (void*);
93 }
94 return rc;
95 }
__wrap_tctildr_finalize_data(void ** data)96 void __wrap_tctildr_finalize_data (void **data) {}
97
98 static void
copyinfo_null_params(void ** state)99 copyinfo_null_params (void **state)
100 {
101 TSS2_RC rc = copy_info (NULL, NULL);
102 assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
103 }
104 static void
copyinfo_strndup1_fail(void ** state)105 copyinfo_strndup1_fail (void **state)
106 {
107 TSS2_TCTI_INFO info_dst = { 0, };
108 will_return (__wrap_strndup, ENOMEM);
109 will_return (__wrap_strndup, NULL);
110 TSS2_RC rc = copy_info (&info_src, &info_dst);
111 assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
112 }
113 static void
copyinfo_strndup2_fail(void ** state)114 copyinfo_strndup2_fail (void **state)
115 {
116 TSS2_TCTI_INFO info_dst = { 0, };
117 will_return (__wrap_strndup, 0);
118 will_return (__wrap_strndup, name_dup);
119 will_return (__wrap_strndup, ENOMEM);
120 will_return (__wrap_strndup, NULL);
121 TSS2_RC rc = copy_info (&info_src, &info_dst);
122 assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
123 }
124 static void
copyinfo_strndup3_fail(void ** state)125 copyinfo_strndup3_fail (void **state)
126 {
127 TSS2_TCTI_INFO info_dst = { 0, };
128 will_return (__wrap_strndup, 0);
129 will_return (__wrap_strndup, name_dup);
130 will_return (__wrap_strndup, 0);
131 will_return (__wrap_strndup, description_dup);
132 will_return (__wrap_strndup, ENOMEM);
133 will_return (__wrap_strndup, NULL);
134 TSS2_RC rc = copy_info (&info_src, &info_dst);
135 assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
136 }
137 static void
copyinfo_success(void ** state)138 copyinfo_success (void **state)
139 {
140 TSS2_TCTI_INFO info_dst = { 0, };
141 will_return (__wrap_strndup, 0);
142 will_return (__wrap_strndup, name_dup);
143 will_return (__wrap_strndup, 0);
144 will_return (__wrap_strndup, description_dup);
145 will_return (__wrap_strndup, 0);
146 will_return (__wrap_strndup, config_help_dup);
147 TSS2_RC rc = copy_info (&info_src, &info_dst);
148 assert_int_equal (rc, TSS2_RC_SUCCESS);
149 assert_string_equal (info_dst.name, name_dup);
150 assert_string_equal (info_dst.description, description_dup);
151 assert_string_equal (info_dst.config_help, config_help_dup);
152 }
153 static void
getinfo_null_info(void ** state)154 getinfo_null_info (void **state)
155 {
156 TSS2_RC rc = Tss2_TctiLdr_GetInfo (NULL, NULL);
157 assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE);
158 }
159 TSS2_RC
__wrap_handle_from_name(const char * file,void ** handle)160 __wrap_handle_from_name(const char *file,
161 void **handle)
162 {
163 printf ("%s\n", __func__);
164 *handle = mock_type (void*);
165 return mock_type (TSS2_RC);
166 }
167 const TSS2_TCTI_INFO*
__wrap_info_from_handle(void * dlhandle)168 __wrap_info_from_handle (void *dlhandle)
169 {
170 printf ("%s\n", __func__);
171 return mock_type (const TSS2_TCTI_INFO*);
172 }
173 TSS2_RC
__wrap_get_tcti_default(TSS2_TCTI_CONTEXT ** tcticontext,void ** dlhandle)174 __wrap_get_tcti_default(TSS2_TCTI_CONTEXT ** tcticontext, void **dlhandle)
175 {
176 printf ("%s\n", __func__);
177 return TSS2_RC_SUCCESS;
178 }
179 TSS2_RC
__wrap_tcti_from_file(const char * file,const char * conf,TSS2_TCTI_CONTEXT ** tcti,void ** dlhandle)180 __wrap_tcti_from_file(const char *file,
181 const char* conf,
182 TSS2_TCTI_CONTEXT **tcti,
183 void **dlhandle)
184 {
185 printf ("%s\n", __func__);
186 return TSS2_RC_SUCCESS;
187 }
188 #define TEST_HANDLE (void*)666
189 #define HANDLE_FROM_NAME_FAIL_RC (TSS2_RC)574567392
190 void
__wrap_dlclose(void * handle)191 __wrap_dlclose (void *handle)
192 {
193 if (handle == TEST_HANDLE) {
194 printf ("%s got TEST_HANDLE\n", __func__);
195 return;
196 } else {
197 printf ("%s got unexpected handle\n", __func__);
198 return;
199 }
200 }
201 TSS2_RC
__wrap_get_info_default(TSS2_TCTI_INFO ** info,void ** dlhandle)202 __wrap_get_info_default (TSS2_TCTI_INFO **info,
203 void **dlhandle)
204 {
205 printf ("%s\n", __func__);
206 if (*info == &info_src) {
207 *dlhandle = mock_type (void*);
208 return mock_type (TSS2_RC);
209 } else {
210 printf ("%s: this shouldn't happen", __func__);
211 assert_true(1);
212 return 1;
213 }
214 }
215 #define TCTI_NAME "foo"
216 static void
getinfo_get_info_fail(void ** state)217 getinfo_get_info_fail (void **state)
218 {
219 TSS2_TCTI_INFO *info = NULL;
220 will_return (__wrap_tctildr_get_info, HANDLE_FROM_NAME_FAIL_RC);
221 TSS2_RC rc = Tss2_TctiLdr_GetInfo (TCTI_NAME, &info);
222 assert_int_equal (rc, HANDLE_FROM_NAME_FAIL_RC);
223 }
224 void*
__wrap_calloc(size_t nmemb,size_t size)225 __wrap_calloc(size_t nmemb, size_t size)
226 {
227 printf("%s\n", __func__);
228 return mock_type (void*);
229 }
230 #define TEST_DATA (void*)0xabcdef999
231 static void
getinfo_calloc_fail(void ** state)232 getinfo_calloc_fail (void **state)
233 {
234 TSS2_TCTI_INFO *info = NULL;
235 will_return (__wrap_tctildr_get_info, TSS2_RC_SUCCESS);
236 will_return (__wrap_tctildr_get_info, &info_src);
237 will_return (__wrap_tctildr_get_info, TEST_DATA);
238 will_return (__wrap_calloc, NULL);
239 TSS2_RC rc = Tss2_TctiLdr_GetInfo (TCTI_NAME, &info);
240 assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
241 }
242 static void
getinfo_copy_info_fail(void ** state)243 getinfo_copy_info_fail (void **state)
244 {
245 TSS2_TCTI_INFO *info = NULL;
246 printf ("%s: pointer %p\n", __func__, &info_dst);
247 will_return (__wrap_tctildr_get_info, TSS2_RC_SUCCESS);
248 will_return (__wrap_tctildr_get_info, &info_src);
249 will_return (__wrap_tctildr_get_info, TEST_DATA);
250 will_return (__wrap_calloc, &info_dst);
251 will_return (__wrap_strndup, ENOMEM);
252 will_return (__wrap_strndup, NULL);
253 TSS2_RC rc = Tss2_TctiLdr_GetInfo (TCTI_NAME, &info);
254 assert_int_equal (rc, TSS2_TCTI_RC_GENERAL_FAILURE);
255 }
256 static void
getinfo_success(void ** state)257 getinfo_success (void **state)
258 {
259 TSS2_TCTI_INFO *info = NULL;
260 will_return (__wrap_tctildr_get_info, TSS2_RC_SUCCESS);
261 will_return (__wrap_tctildr_get_info, &info_src);
262 will_return (__wrap_tctildr_get_info, TEST_DATA);
263 will_return (__wrap_calloc, &info_dst);
264 will_return (__wrap_strndup, 0);
265 will_return (__wrap_strndup, name_dup);
266 will_return (__wrap_strndup, 0);
267 will_return (__wrap_strndup, description_dup);
268 will_return (__wrap_strndup, 0);
269 will_return (__wrap_strndup, config_help_dup);
270 TSS2_RC rc = Tss2_TctiLdr_GetInfo (TCTI_NAME, &info);
271 assert_int_equal (rc, TSS2_RC_SUCCESS);
272 }
273 static void
freeinfo_null(void ** state)274 freeinfo_null (void **state)
275 {
276 TSS2_TCTI_INFO *info = NULL;
277 Tss2_TctiLdr_FreeInfo (&info);
278 assert_null (info);
279 }
280 static void
freeinfo_success(void ** state)281 freeinfo_success (void **state)
282 {
283 TSS2_TCTI_INFO *info = &info_dst;
284 Tss2_TctiLdr_FreeInfo (&info);
285 assert_null (info);
286 }
287 int
main(int argc,char * arvg[])288 main (int argc, char* arvg[])
289 {
290 const struct CMUnitTest tests[] = {
291 cmocka_unit_test (copyinfo_null_params),
292 cmocka_unit_test (copyinfo_strndup1_fail),
293 cmocka_unit_test (copyinfo_strndup2_fail),
294 cmocka_unit_test (copyinfo_strndup3_fail),
295 cmocka_unit_test (copyinfo_success),
296 cmocka_unit_test (getinfo_null_info),
297 cmocka_unit_test (getinfo_get_info_fail),
298 cmocka_unit_test (getinfo_calloc_fail),
299 cmocka_unit_test (getinfo_copy_info_fail),
300 cmocka_unit_test (getinfo_success),
301 cmocka_unit_test (freeinfo_null),
302 cmocka_unit_test (freeinfo_success),
303 };
304 return cmocka_run_group_tests (tests, NULL, NULL);
305 }
306