• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  * http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 
13 #include <gtest/gtest.h>
14 #include <test_defines.h>
15 #include <common_test.h>
16 #include <securec.h>
17 #include <tee_client_api.h>
18 #include <tee_client_type.h>
19 #include <test_log.h>
20 #include <test_tcf_cmdid.h>
21 
22 using namespace testing::ext;
23 
24 TEEC_Context TeeTCF1Test::context = { 0 };
25 TEEC_Session TeeTCF1Test::session = { 0 };
26 
SetUp()27 void TeeTCF1Test::SetUp()
28 {
29     TEEC_Operation operation = { 0 };
30     TEEC_Result ret = TEEC_InitializeContext(NULL, &context);
31     if (ret != TEEC_SUCCESS) {
32         TEST_PRINT_ERROR("TEEC_InitializeContext failed\n");
33         return;
34     }
35     operation.started = 1;
36     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_NONE);
37     TEEC_UUID uuid = TCF_API_UUID_1;
38     char str[64] = { 0 };
39     sprintf(str,"/data/local/tmp/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.sec", uuid.timeLow, uuid.timeMid,
40         uuid.timeHiAndVersion, uuid.clockSeqAndNode[0], uuid.clockSeqAndNode[1], uuid.clockSeqAndNode[2],
41         uuid.clockSeqAndNode[3], uuid.clockSeqAndNode[4], uuid.clockSeqAndNode[5], uuid.clockSeqAndNode[6],
42         uuid.clockSeqAndNode[7]);
43     context.ta_path = (uint8_t *)str;
44     ret = TEEC_OpenSession(&context, &session, &uuid, TEEC_LOGIN_IDENTIFY, NULL, &operation, NULL);
45     if (ret != TEEC_SUCCESS) {
46         TEEC_FinalizeContext(&context);
47         TEST_PRINT_ERROR("TEEC_OpenSession failed\n");
48         return;
49     }
50 }
51 
TearDown()52 void TeeTCF1Test::TearDown()
53 {
54     TEEC_CloseSession(&session);
55     TEEC_FinalizeContext(&context);
56 }
57 
58 TEEC_Context TeeTCF2Test::context = { 0 };
59 TEEC_Session TeeTCF2Test::session = { 0 };
60 
SetUp()61 void TeeTCF2Test::SetUp()
62 {
63     TEEC_Operation operation = { 0 };
64 
65     TEEC_Result ret = TEEC_InitializeContext(NULL, &context);
66     if (ret != TEEC_SUCCESS) {
67         TEST_PRINT_ERROR("TEEC_InitializeContext failed\n");
68         return;
69     }
70 
71     operation.started = 1;
72     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_NONE);
73 
74     TEEC_UUID uuid = TCF_API_UUID_2;
75     char str[64] = { 0 };
76     sprintf(str,"/data/local/tmp/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.sec", uuid.timeLow, uuid.timeMid,
77         uuid.timeHiAndVersion, uuid.clockSeqAndNode[0], uuid.clockSeqAndNode[1], uuid.clockSeqAndNode[2],
78         uuid.clockSeqAndNode[3], uuid.clockSeqAndNode[4], uuid.clockSeqAndNode[5], uuid.clockSeqAndNode[6],
79         uuid.clockSeqAndNode[7]);
80     context.ta_path = (uint8_t *)str;
81     ret = TEEC_OpenSession(&context, &session, &uuid, TEEC_LOGIN_IDENTIFY, NULL, &operation, NULL);
82     if (ret != TEEC_SUCCESS) {
83         TEEC_FinalizeContext(&context);
84         TEST_PRINT_ERROR("TEEC_OpenSession failed\n");
85         return;
86     }
87 }
88 
TearDown()89 void TeeTCF2Test::TearDown()
90 {
91     TEEC_CloseSession(&session);
92     TEEC_FinalizeContext(&context);
93 }
94 
95 TEEC_Context TeeTCF2TA2TATest::context = { 0 };
96 TEEC_Session TeeTCF2TA2TATest::session = { 0 };
97 TEEC_Session TeeTCF2TA2TATest::session2 = { 0 };
98 
SetUp()99 void TeeTCF2TA2TATest::SetUp()
100 {
101     TEEC_Operation operation = { 0 };
102 
103     TEEC_Result ret = TEEC_InitializeContext(NULL, &context);
104     if (ret != TEEC_SUCCESS) {
105         TEST_PRINT_ERROR("TEEC_InitializeContext failed\n");
106         return;
107     }
108 
109     operation.started = 1;
110     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_NONE);
111 
112     TEEC_UUID uuid2 = TCF_API_UUID_1; // this is TA2 UUID
113     char str[64] = { 0 };
114     sprintf(str,"/data/local/tmp/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.sec", uuid2.timeLow, uuid2.timeMid,
115         uuid2.timeHiAndVersion, uuid2.clockSeqAndNode[0], uuid2.clockSeqAndNode[1], uuid2.clockSeqAndNode[2],
116         uuid2.clockSeqAndNode[3], uuid2.clockSeqAndNode[4], uuid2.clockSeqAndNode[5], uuid2.clockSeqAndNode[6],
117         uuid2.clockSeqAndNode[7]);
118     context.ta_path = (uint8_t *)str;
119     ret = TEEC_OpenSession(&context, &session2, &uuid2, TEEC_LOGIN_IDENTIFY, NULL, &operation, NULL);
120     if (ret != TEEC_SUCCESS) {
121         TEST_PRINT_ERROR("TEEC_OpenSession ta2 failed\n");
122         return;
123     }
124 
125     TEEC_UUID uuid = TCF_API_UUID_2; // this is TA1 UUID
126     memset(str, 0, 64);
127     sprintf(str,"/data/local/tmp/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.sec", uuid.timeLow, uuid.timeMid,
128         uuid.timeHiAndVersion, uuid.clockSeqAndNode[0], uuid.clockSeqAndNode[1], uuid.clockSeqAndNode[2],
129         uuid.clockSeqAndNode[3], uuid.clockSeqAndNode[4], uuid.clockSeqAndNode[5], uuid.clockSeqAndNode[6],
130         uuid.clockSeqAndNode[7]);
131     context.ta_path = (uint8_t *)str;
132     ret = TEEC_OpenSession(&context, &session, &uuid, TEEC_LOGIN_IDENTIFY, NULL, &operation, NULL);
133     if (ret != TEEC_SUCCESS) {
134         TEEC_FinalizeContext(&context);
135         TEST_PRINT_ERROR("TEEC_OpenSession ta1 failed\n");
136         return;
137     }
138 }
139 
TearDown()140 void TeeTCF2TA2TATest::TearDown()
141 {
142     TEEC_CloseSession(&session);
143     TEEC_CloseSession(&session2);
144     TEEC_FinalizeContext(&context);
145 }
146 
147 TEEC_Context TeeTCF1EnumTest::context = { 0 };
148 TEEC_Session TeeTCF1EnumTest::session = { 0 };
149 
SetUpTestCase()150 void TeeTCF1EnumTest::SetUpTestCase()
151 {
152     TEEC_Operation operation = { 0 };
153 
154     TEEC_Result ret = TEEC_InitializeContext(NULL, &context);
155     if (ret != TEEC_SUCCESS) {
156         TEST_PRINT_ERROR("TEEC_InitializeContext failed\n");
157         return;
158     }
159 
160     operation.started = 1;
161     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_NONE);
162 
163     TEEC_UUID uuid = TCF_API_UUID_1;
164     char str[64] = { 0 };
165     sprintf(str,"/data/local/tmp/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.sec", uuid.timeLow, uuid.timeMid,
166         uuid.timeHiAndVersion, uuid.clockSeqAndNode[0], uuid.clockSeqAndNode[1], uuid.clockSeqAndNode[2],
167         uuid.clockSeqAndNode[3], uuid.clockSeqAndNode[4], uuid.clockSeqAndNode[5], uuid.clockSeqAndNode[6],
168         uuid.clockSeqAndNode[7]);
169     context.ta_path = (uint8_t *)str;
170     ret = TEEC_OpenSession(&context, &session, &uuid, TEEC_LOGIN_IDENTIFY, NULL, &operation, NULL);
171     if (ret != TEEC_SUCCESS) {
172         TEEC_FinalizeContext(&context);
173         TEST_PRINT_ERROR("TEEC_OpenSession failed\n");
174         return;
175     }
176 }
177 
TearDownTestCase()178 void TeeTCF1EnumTest::TearDownTestCase()
179 {
180     TEEC_CloseSession(&session);
181     TEEC_FinalizeContext(&context);
182 }
183 
SetUp()184 void TeeTCF1EnumTest::SetUp()
185 {
186     TEEC_Result ret;
187 
188     // alloc PropertyEnumerator
189     value.cmd = CMD_TEE_AllocatePropertyEnumerator;
190     ret = Invoke_AllocatePropertyEnumerator(GetSession(), &value);
191     ABORT_UNLESS(ret != TEEC_SUCCESS);
192 }
193 
TearDown()194 void TeeTCF1EnumTest::TearDown()
195 {
196     value.cmd = CMD_TEE_FreePropertyEnumerator;
197     Invoke_Operate_PropertyEnumerator(GetSession(), &value);
198 }
199 
Invoke_GetPropertyAsX(TEEC_Context * context,TEEC_Session * session,TestData * testData)200 TEEC_Result Invoke_GetPropertyAsX(TEEC_Context *context, TEEC_Session *session, TestData *testData)
201 {
202     if (session->session_id == 0)
203         return TEEC_ERROR_SESSION_NOT_EXIST;
204 
205     TEEC_Result result = TEEC_FAIL;
206     int rc;
207     TEEC_Operation operation = { 0 };
208     TEEC_SharedMemory shareMemInput, shareMemOutput;
209 
210     // allocate the share memorys
211     shareMemInput.size = BIG_SIZE;
212     shareMemInput.flags = TEEC_MEM_INOUT;
213     result = TEEC_AllocateSharedMemory(context, &shareMemInput);
214     if (result != TEEC_SUCCESS) {
215         TEST_PRINT_ERROR("alloc shareMemInput fail!\n");
216         return TEEC_FAIL;
217     }
218 
219     shareMemOutput.size = BIG_SIZE;
220     shareMemOutput.flags = TEEC_MEM_INOUT;
221     result = TEEC_AllocateSharedMemory(context, &shareMemOutput);
222     if (result != TEEC_SUCCESS) {
223         TEST_PRINT_ERROR("alloc shareMemOutput fail!\n");
224         TEEC_ReleaseSharedMemory(&shareMemInput);
225         return TEEC_FAIL;
226     }
227 
228     // Invoke command
229     operation.started = 1;
230     operation.paramTypes =
231         TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_PARTIAL_INPUT, TEEC_MEMREF_PARTIAL_OUTPUT, TEEC_NONE);
232     if (testData->enumerator != 0)
233         operation.params[0].value.a = testData->enumerator;
234     else
235         operation.params[0].value.a = testData->propSet;
236 
237     operation.params[0].value.b = testData->caseId;
238     (void)memset_s(shareMemInput.buffer, BIG_SIZE, 0, BIG_SIZE);
239     rc = memcpy_s(shareMemInput.buffer, shareMemInput.size, testData->inBuffer, testData->inBufferLen);
240     if (rc != TEEC_SUCCESS) {
241         TEST_PRINT_ERROR("memcpy_s inBuffer to shareMemInput fail!\n");
242         goto clean;
243     }
244 
245     operation.params[1].memref.parent = &shareMemInput;
246     operation.params[1].memref.size = shareMemInput.size;
247     operation.params[1].memref.offset = 0;
248     operation.params[2].memref.parent = &shareMemOutput;
249     operation.params[2].memref.size = shareMemOutput.size;
250     operation.params[2].memref.offset = 0;
251 
252     result = TEEC_InvokeCommand(session, testData->cmd, &operation, &testData->origin);
253     testData->outBufferLen = operation.params[2].memref.size;
254     rc = memcpy_s(testData->outBuffer, BIG_SIZE, shareMemOutput.buffer, testData->outBufferLen);
255     if (rc != TEEC_SUCCESS) {
256         TEST_PRINT_ERROR("memcpy_s shareMemOutput to outBuffer fail! rc = 0x%x\n", rc);
257     }
258 
259 clean:
260     TEEC_ReleaseSharedMemory(&shareMemInput);
261     TEEC_ReleaseSharedMemory(&shareMemOutput);
262     return result;
263 }
264 
Invoke_AllocatePropertyEnumerator(TEEC_Session * session,TestData * testData)265 TEEC_Result Invoke_AllocatePropertyEnumerator(TEEC_Session *session, TestData *testData)
266 {
267     if (session->session_id == 0)
268         return TEEC_ERROR_SESSION_NOT_EXIST;
269 
270     TEEC_Result result;
271     TEEC_Operation operation = { 0 };
272 
273     // Invoke command
274     operation.started = 1;
275     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
276     operation.params[0].value.a = 0;
277     operation.params[0].value.b = testData->caseId;
278     result = TEEC_InvokeCommand(session, testData->cmd, &operation, &testData->origin);
279 
280     testData->enumerator = operation.params[0].value.a;
281     return result;
282 }
283 
Invoke_Operate_PropertyEnumerator(TEEC_Session * session,TestData * testData)284 TEEC_Result Invoke_Operate_PropertyEnumerator(TEEC_Session *session, TestData *testData)
285 {
286     if (session->session_id == 0)
287         return TEEC_ERROR_SESSION_NOT_EXIST;
288 
289     TEEC_Result result;
290     TEEC_Operation operation = { 0 };
291     // Invoke command
292     operation.started = 1;
293     switch (testData->cmd) {
294         case CMD_TEE_FreePropertyEnumerator:
295         case CMD_TEE_ResetPropertyEnumerator:
296         case CMD_TEE_GetNextPropertyEnumerator:
297             operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
298             operation.params[0].value.a = testData->enumerator;
299             operation.params[0].value.b = testData->cmd;
300             break;
301         case CMD_TEE_StartPropertyEnumerator:
302             operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE);
303             operation.params[0].value.a = testData->enumerator;
304             operation.params[1].value.a = testData->propSet;
305             break;
306         case CMD_TEE_GetPropertyNameEnumerator:
307             operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE, TEEC_NONE);
308             operation.params[0].value.a = testData->enumerator;
309             operation.params[0].value.b = testData->caseId;
310             operation.params[1].tmpref.buffer = testData->outBuffer;
311             operation.params[1].tmpref.size = testData->outBufferLen;
312             break;
313         default:
314             TEST_PRINT_ERROR("not support this test command! cmdId: 0x%x", testData->cmd);
315             return TEEC_FAIL;
316     }
317 
318     result = TEEC_InvokeCommand(session, testData->cmd, &operation, &testData->origin);
319     if (testData->cmd == CMD_TEE_GetPropertyNameEnumerator)
320         testData->outBufferLen = operation.params[1].tmpref.size;
321 
322     return result;
323 }
324 
Invoke_Malloc(TEEC_Session * session,uint32_t commandID,TestMemData * testData,uint32_t * origin)325 TEEC_Result Invoke_Malloc(TEEC_Session *session, uint32_t commandID, TestMemData *testData, uint32_t *origin)
326 {
327     if (session->session_id == 0)
328         return TEEC_ERROR_SESSION_NOT_EXIST;
329 
330     TEEC_Result result;
331     TEEC_Operation operation = { 0 };
332 
333     // Invoke command
334     operation.started = 1;
335     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE, TEEC_NONE);
336     operation.params[0].value.a = testData->inMemSize;
337     operation.params[0].value.b = testData->inHint;
338     operation.params[1].tmpref.buffer = testData->testBuffer;
339     operation.params[1].tmpref.size = MAX_SHARE_SIZE;
340 
341     result = TEEC_InvokeCommand(session, commandID, &operation, origin);
342     return result;
343 }
344 
Invoke_Realloc(TEEC_Session * session,uint32_t commandID,TestMemData * testData,char * output)345 TEEC_Result Invoke_Realloc(TEEC_Session *session, uint32_t commandID, TestMemData *testData, char *output)
346 {
347     if (session->session_id == 0)
348         return TEEC_ERROR_SESSION_NOT_EXIST;
349 
350     TEEC_Result result;
351     TEEC_Operation operation = { 0 };
352     char *buffer = NULL;
353     uint32_t bufSize = testData->oldSize > testData->newSize ? testData->oldSize : testData->newSize;
354 
355     if (bufSize > MAX_SHARE_SIZE)
356         bufSize = TESTSIZE;
357 
358     buffer = reinterpret_cast<char *>(malloc(bufSize));
359     if (buffer == NULL) {
360         TEST_PRINT_ERROR("malloc buffer fail!\n");
361         return TEEC_FAIL;
362     }
363 
364     // Invoke command
365     operation.started = 1;
366     operation.paramTypes =
367         TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_OUTPUT, TEEC_VALUE_OUTPUT, TEEC_VALUE_INPUT);
368     operation.params[0].value.a = testData->oldSize;
369     operation.params[0].value.b = testData->newSize;
370     operation.params[1].tmpref.buffer = buffer;
371     operation.params[1].tmpref.size = bufSize;
372     operation.params[3].value.a = testData->caseId;
373 
374     result = TEEC_InvokeCommand(session, commandID, &operation, &testData->origin);
375     testData->oldAddr = operation.params[2].value.a;
376     testData->newAddr = operation.params[2].value.b;
377     int rc = memcpy_s(output, bufSize, buffer, operation.params[1].tmpref.size);
378     if (rc != 0) {
379         TEST_PRINT_ERROR("memcpy_s output failed, rc=0x%x\n", rc);
380         return TEEC_FAIL;
381     }
382 
383     free(buffer);
384     return result;
385 }
386 
Invoke_MemMove_Or_Fill(TEEC_Session * session,uint32_t commandID,TestMemData * testData,char * output)387 TEEC_Result Invoke_MemMove_Or_Fill(TEEC_Session *session, uint32_t commandID, TestMemData *testData, char *output)
388 {
389     if (session->session_id == 0)
390         return TEEC_ERROR_SESSION_NOT_EXIST;
391 
392     TEEC_Result result;
393     TEEC_Operation operation = { 0 };
394     char *buffer = NULL;
395     uint32_t bufSize = testData->oldSize;
396     buffer = reinterpret_cast<char*>(malloc(bufSize));
397     if (buffer == NULL) {
398         TEST_PRINT_ERROR("malloc buffer fail!\n");
399         return TEEC_FAIL;
400     }
401 
402     // Invoke command
403     operation.started = 1;
404     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE, TEEC_NONE);
405     operation.params[0].value.a = testData->oldSize;
406     operation.params[0].value.b = testData->caseId;
407     operation.params[1].tmpref.buffer = buffer;
408     operation.params[1].tmpref.size = bufSize;
409 
410     result = TEEC_InvokeCommand(session, commandID, &operation, &testData->origin);
411     int rc = memcpy_s(output, testData->oldSize, buffer, operation.params[1].tmpref.size);
412     if (rc != 0) {
413         TEST_PRINT_ERROR("memcpy_s uuid to tee failed, rc=0x%x\n", rc);
414         return TEEC_FAIL;
415     }
416     free(buffer);
417     return result;
418 }
419 
Invoke_Free(TEEC_Session * session,uint32_t commandID,uint32_t caseNum,uint32_t * origin)420 TEEC_Result Invoke_Free(TEEC_Session *session, uint32_t commandID, uint32_t caseNum, uint32_t *origin)
421 {
422     if (session->session_id == 0)
423         return TEEC_ERROR_SESSION_NOT_EXIST;
424 
425     TEEC_Result result;
426     TEEC_Operation operation = { 0 };
427 
428     // Invoke command
429     operation.started = 1;
430     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
431     operation.params[0].value.a = caseNum;
432 
433     result = TEEC_InvokeCommand(session, commandID, &operation, origin);
434     return result;
435 }
436 
Invoke_MemCompare(TEEC_Session * session,uint32_t commandID,TestMemData * testData,char * buffer1,char * buffer2)437 TEEC_Result Invoke_MemCompare(TEEC_Session *session, uint32_t commandID, TestMemData *testData, char *buffer1,
438     char *buffer2)
439 {
440     if (session->session_id == 0)
441         return TEEC_ERROR_SESSION_NOT_EXIST;
442 
443     TEEC_Result result = TEEC_SUCCESS;
444     TEEC_Operation operation = { 0 };
445 
446     // Invoke command
447     operation.started = 1;
448     operation.paramTypes =
449         TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_INPUT, TEEC_MEMREF_TEMP_INPUT, TEEC_NONE);
450     operation.params[0].value.a = testData->oldSize;
451     operation.params[0].value.b = testData->caseId;
452     operation.params[1].tmpref.buffer = buffer1;
453     operation.params[1].tmpref.size = testData->oldSize > 0 ? testData->oldSize : TESTSIZE;
454     operation.params[2].tmpref.buffer = buffer2;
455     operation.params[2].tmpref.size = testData->oldSize > 0 ? testData->oldSize : TESTSIZE;
456 
457     result = TEEC_InvokeCommand(session, commandID, &operation, &testData->origin);
458     return result;
459 }
460 
Invoke_CheckMemoryAccessRights(TEEC_Session * session,uint32_t commandID,TestMemData * testData)461 TEEC_Result Invoke_CheckMemoryAccessRights(TEEC_Session *session, uint32_t commandID, TestMemData *testData)
462 {
463     if (session->session_id == 0)
464         return TEEC_ERROR_SESSION_NOT_EXIST;
465 
466     TEEC_Result result;
467     TEEC_Operation operation = { 0 };
468     char *buffer = NULL;
469     uint32_t bufSize = testData->oldSize;
470     buffer = reinterpret_cast<char*>(malloc(bufSize));
471     if (buffer == NULL) {
472         TEST_PRINT_ERROR("malloc buffer fail!\n");
473         return TEEC_FAIL;
474     }
475     // Invoke command
476     operation.started = 1;
477     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_OUTPUT, TEEC_VALUE_INPUT, TEEC_NONE);
478     operation.params[0].value.a = testData->accessFlags;
479     operation.params[0].value.b = testData->oldSize;
480     operation.params[1].tmpref.buffer = buffer;
481     operation.params[1].tmpref.size = bufSize;
482     operation.params[2].value.a = testData->caseId;
483 
484     result = TEEC_InvokeCommand(session, commandID, &operation, &testData->origin);
485     free(buffer);
486     return result;
487 }
488 
Invoke_SetInstanceData(TEEC_Session * session,uint32_t commandID,char * buffer,uint32_t caseNum,uint32_t * origin)489 TEEC_Result Invoke_SetInstanceData(TEEC_Session *session, uint32_t commandID, char *buffer, uint32_t caseNum,
490     uint32_t *origin)
491 {
492     if (session->session_id == 0)
493         return TEEC_ERROR_SESSION_NOT_EXIST;
494 
495     TEEC_Result result = TEEC_SUCCESS;
496     TEEC_Operation operation = { 0 };
497 
498     // Invoke command
499     operation.started = 1;
500     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_INPUT, TEEC_NONE, TEEC_NONE);
501     operation.params[0].value.a = caseNum;
502     operation.params[1].tmpref.buffer = buffer;
503     operation.params[1].tmpref.size = strlen(buffer) + 1;
504 
505     result = TEEC_InvokeCommand(session, commandID, &operation, origin);
506     return result;
507 }
508 
Invoke_GetInstanceData(TEEC_Session * session,uint32_t commandID,char * buffer,uint32_t * bufSize,uint32_t * origin)509 TEEC_Result Invoke_GetInstanceData(TEEC_Session *session, uint32_t commandID, char *buffer, uint32_t *bufSize,
510     uint32_t *origin)
511 {
512     if (session->session_id == 0)
513         return TEEC_ERROR_SESSION_NOT_EXIST;
514 
515     TEEC_Result result = TEEC_SUCCESS;
516     TEEC_Operation operation = { 0 };
517 
518     // Invoke command
519     operation.started = 1;
520     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
521     operation.params[0].tmpref.buffer = buffer;
522     operation.params[0].tmpref.size = *bufSize;
523 
524     result = TEEC_InvokeCommand(session, commandID, &operation, origin);
525     *bufSize = operation.params[0].tmpref.size;
526     return result;
527 }
528 
retrieveUint32toBuffer(uint8_t * buffer,uint32_t i)529 static void retrieveUint32toBuffer(uint8_t *buffer, uint32_t i)
530 {
531     buffer[3] = i & 0xff;
532     buffer[2] = (i >> 8) & 0xff;
533     buffer[1] = (i >> 16) & 0xff;
534     buffer[0] = (i >> 24) & 0xff;
535 }
536 
retrieveUint16toBuffer(uint8_t * buffer,uint16_t i)537 static void retrieveUint16toBuffer(uint8_t *buffer, uint16_t i)
538 {
539     buffer[1] = i & 0xff;
540     buffer[0] = (i >> 8) & 0xff;
541 }
542 
Invoke_OpenTASession(TEEC_Session * session,uint32_t commandID,uint32_t * ta2taSession,TestData * testData,uint32_t * origin)543 TEEC_Result Invoke_OpenTASession(TEEC_Session *session, uint32_t commandID, uint32_t *ta2taSession, TestData *testData,
544     uint32_t *origin)
545 {
546     if (session->session_id == 0)
547         return TEEC_ERROR_SESSION_NOT_EXIST;
548 
549     TEEC_Result result = TEEC_SUCCESS;
550     TEEC_Operation operation = { 0 };
551     uint8_t tempBuffer[16];
552     int rc;
553 
554     retrieveUint32toBuffer(tempBuffer, testData->uuid.timeLow);
555     retrieveUint16toBuffer(tempBuffer + 4, testData->uuid.timeMid);
556     retrieveUint16toBuffer(tempBuffer + 6, testData->uuid.timeHiAndVersion);
557     rc = memcpy_s(tempBuffer + 8, 8, &(testData->uuid.clockSeqAndNode), 8);
558     if (rc != 0) {
559         TEST_PRINT_ERROR("memcpy_s uuid to tee failed, rc=0x%x\n", rc);
560         return TEEC_ERROR_GENERIC;
561     }
562 
563     // Invoke command
564     operation.started = 1;
565     operation.paramTypes =
566         TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_INPUT, TEEC_VALUE_OUTPUT, TEEC_MEMREF_TEMP_INOUT);
567     operation.params[0].value.a = testData->caseId;
568     operation.params[1].tmpref.buffer = tempBuffer;
569     operation.params[1].tmpref.size = sizeof(tempBuffer);
570     operation.params[3].tmpref.buffer = testData->inBuffer;
571     operation.params[3].tmpref.size = testData->inBufferLen;
572 
573     result = TEEC_InvokeCommand(session, commandID, &operation, origin);
574     *ta2taSession = operation.params[2].value.a;
575     testData->origin = operation.params[2].value.b;
576     testData->inBufferLen = operation.params[3].tmpref.size;
577 
578     return result;
579 }
580 
Invoke_CloseTASession(TEEC_Session * session,uint32_t commandID,uint32_t ta2taSession,uint32_t * origin)581 TEEC_Result Invoke_CloseTASession(TEEC_Session *session, uint32_t commandID, uint32_t ta2taSession, uint32_t *origin)
582 {
583     if (session->session_id == 0)
584         return TEEC_ERROR_SESSION_NOT_EXIST;
585 
586     TEEC_Result result = TEEC_SUCCESS;
587     TEEC_Operation operation = { 0 };
588 
589     // Invoke command
590     operation.started = 1;
591     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
592     operation.params[0].value.a = ta2taSession;
593 
594     result = TEEC_InvokeCommand(session, commandID, &operation, origin);
595     return result;
596 }
597 
Invoke_InvokeTACommand(TEEC_Session * session,uint32_t commandID,uint32_t ta2taSession,TestData * testData,uint32_t * origin)598 TEEC_Result Invoke_InvokeTACommand(TEEC_Session *session, uint32_t commandID, uint32_t ta2taSession, TestData *testData,
599     uint32_t *origin)
600 {
601     if (session->session_id == 0)
602         return TEEC_ERROR_SESSION_NOT_EXIST;
603 
604     TEEC_Result result = TEEC_SUCCESS;
605     TEEC_Operation operation = { 0 };
606 
607     // Invoke command
608     operation.started = 1;
609     operation.paramTypes =
610         TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_TEMP_INOUT, TEEC_VALUE_OUTPUT, TEEC_MEMREF_TEMP_OUTPUT);
611     operation.params[0].value.a = testData->caseId;
612     operation.params[0].value.b = ta2taSession;
613     operation.params[1].tmpref.buffer = testData->inBuffer;
614     operation.params[1].tmpref.size = testData->inBufferLen;
615     operation.params[3].tmpref.buffer = testData->outBuffer;
616     operation.params[3].tmpref.size = testData->outBufferLen;
617     result = TEEC_InvokeCommand(session, commandID, &operation, origin);
618     testData->origin = operation.params[2].value.a;
619     testData->inBufferLen = operation.params[1].tmpref.size;
620     testData->outBufferLen = operation.params[3].tmpref.size;
621     return result;
622 }
623 
get_ta_data_size(TEEC_Context * context,TEEC_Session * session)624 uint32_t get_ta_data_size(TEEC_Context *context, TEEC_Session *session)
625 {
626     if (session->session_id == 0)
627         return TEEC_ERROR_SESSION_NOT_EXIST;
628 
629     int rc;
630     TestData value = { 0 };
631     value.cmd = CMD_TEE_GetPropertyAsU32;
632     value.propSet = TEE_PROPSET_CURRENT_TA;
633     rc = memcpy_s(value.inBuffer, BIG_SIZE, GPD_TA_DATASIZE, sizeof(GPD_TA_DATASIZE));
634     if (rc != 0) {
635         TEST_PRINT_ERROR("memcpy_s for GPD_TA_DATASIZE fail,rc=0x%x\n", rc);
636         return -1;
637     }
638     value.inBufferLen = sizeof(GPD_TA_DATASIZE);
639     Invoke_GetPropertyAsX(context, session, &value);
640 
641     return atoi(value.outBuffer);
642 }
643 
get_ta_stack_size(TEEC_Context * context,TEEC_Session * session)644 uint32_t get_ta_stack_size(TEEC_Context *context, TEEC_Session *session)
645 {
646     if (session->session_id == 0)
647         return TEEC_ERROR_SESSION_NOT_EXIST;
648 
649     int rc;
650     TestData value = { 0 };
651     value.cmd = CMD_TEE_GetPropertyAsU32;
652     value.propSet = TEE_PROPSET_CURRENT_TA;
653     rc = memcpy_s(value.inBuffer, BIG_SIZE, GPD_TA_STACKSIZE, sizeof(GPD_TA_STACKSIZE));
654     if (rc != 0) {
655         TEST_PRINT_ERROR("memcpy_s for GPD_TA_STACKSIZE fail,rc=0x%x\n", rc);
656         return -1;
657     }
658     value.inBufferLen = sizeof(GPD_TA_STACKSIZE);
659 
660     Invoke_GetPropertyAsX(context, session, &value);
661     return atoi(value.outBuffer);
662 }
663 
Invoke_Panic(TEEC_Session * session,uint32_t commandID,TEEC_Result panicCode,uint32_t * origin)664 TEEC_Result Invoke_Panic(TEEC_Session *session, uint32_t commandID, TEEC_Result panicCode, uint32_t *origin)
665 {
666     if (session->session_id == 0)
667         return TEEC_ERROR_SESSION_NOT_EXIST;
668 
669     TEEC_Result result;
670     TEEC_Operation operation = { 0 };
671 
672     // Invoke command
673     operation.started = 1;
674     operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
675     operation.params[0].value.a = panicCode;
676 
677     result = TEEC_InvokeCommand(session, commandID, &operation, origin);
678     return result;
679 }