• 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 
15 #include <vector>
16 #include <iostream>
17 
18 #include <securec.h>
19 #include <test_defines.h>
20 
21 #include <common_test.h>
22 #include <empty_test.h>
23 #include <public_test.h>
24 #include <session_mgr/client_session_mgr.h>
25 #include <tee_client_api.h>
26 #include <tee_client_type.h>
27 
28 using namespace std;
29 using namespace testing::ext;
30 
31 /**
32  * @testcase.name      : ReleaseSharedMemory_WithAllocatedMem
33  * @testcase.desc      : call TEEC_ReleaseSharedMemory WithAllocatedMem,
34  * @testcase.expect    : sharedMem.buffer has released
35  */
36 TEE_TEST(TeeBasicTestFramWithInitContext, ReleaseSharedMemory_WithAllocatedMem, Function | MediumTest | Level0)
37 {
38     TEEC_Result ret;
39     TEEC_SharedMemory sharedMem;
40     sharedMem.size = TEST_STR_LEN;
41     sharedMem.flags = TEEC_MEM_INOUT;
42     ret = TEEC_AllocateSharedMemory(GetContext(), &sharedMem);
43     EXPECT_EQ(ret, TEEC_SUCCESS);
44 
45     TEEC_ReleaseSharedMemory(&sharedMem);
46     ASSERT_STREQ(reinterpret_cast<char *>(sharedMem.buffer), NULL);
47     ASSERT_STREQ(reinterpret_cast<char *>(sharedMem.context), NULL);
48 }
49 
50 /**
51  * @testcase.name      : ReleaseSharedMemory_WithRegisterMem
52  * @testcase.desc      : call TEEC_ReleaseSharedMemory WithRegisterMem,
53  * @testcase.expect    : sharedMem.buffer has released
54  */
55 TEE_TEST(TeeBasicTestFramWithInitContext, ReleaseSharedMemory_WithRegisterMem, Function | MediumTest | Level0)
56 {
57     TEEC_Result ret;
58     TEEC_SharedMemory sharedMem;
59     char* testData0 = reinterpret_cast<char*>(malloc(TEST_STR_LEN));
60     ASSERT_STRNE(testData0, NULL);
61     (void)memset_s(testData0, TEST_STR_LEN, 0x0, TEST_STR_LEN);
62 
63     sharedMem.size = TEST_STR_LEN;
64     sharedMem.flags = TEEC_MEM_INOUT;
65     sharedMem.buffer = testData0;
66     ret = TEEC_RegisterSharedMemory(GetContext(), &sharedMem);
67     EXPECT_EQ(ret, TEEC_SUCCESS);
68 
69     TEEC_ReleaseSharedMemory(&sharedMem);
70     ASSERT_STREQ(reinterpret_cast<char *>(sharedMem.buffer), NULL);
71     ASSERT_STREQ(reinterpret_cast<char *>(sharedMem.context), NULL);
72     free(testData0);
73 }
74 
75 /**
76  * @testcase.name      : ReleaseSharedMemory_WithoutSharedMem
77  * @testcase.desc      : call TEEC_ReleaseSharedMemory WithoutSharedMem,
78  * @testcase.expect    : sharedMem.buffer has not released
79  */
80 TEE_TEST(TeeBasicTestFramWithInitContext, ReleaseSharedMemory_WithoutSharedMem, Function | MediumTest | Level0)
81 {
82     TEEC_Result ret;
83     TEEC_SharedMemory sharedMem;
84     sharedMem.size = TEST_STR_LEN;
85     sharedMem.flags = TEEC_MEM_INOUT;
86     ret = TEEC_AllocateSharedMemory(GetContext(), &sharedMem);
87     EXPECT_EQ(ret, TEEC_SUCCESS);
88 
89     TEEC_ReleaseSharedMemory(NULL);
90     ASSERT_STRNE(reinterpret_cast<char*>(sharedMem.buffer), NULL);
91     ASSERT_STRNE(reinterpret_cast<char*>(sharedMem.context), NULL);
92 }
93 
94 /**
95  * @testcase.name      : ReleaseSharedMemory_WithNotAllocatedSharedMem
96  * @testcase.desc      : call TEEC_ReleaseSharedMemory With SharedMem Not Allocated,
97  * @testcase.expect    : sharedMem.buffer is null, no error occur
98  */
99 TEE_TEST(TeeBasicTestFramWithInitContext, ReleaseSharedMemory_WithNotAllocatedSharedMem, Function | MediumTest | Level0)
100 {
101     TEEC_SharedMemory sharedMem = { 0 };
102     sharedMem.size = TEST_STR_LEN;
103     sharedMem.flags = TEEC_MEM_INOUT;
104 
105     TEEC_ReleaseSharedMemory(&sharedMem);
106     ASSERT_STREQ(reinterpret_cast<char *>(sharedMem.buffer), NULL);
107     ASSERT_STREQ(reinterpret_cast<char *>(sharedMem.context), NULL);
108 }