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 <common_test.h> 14 #include <gtest/gtest.h> 15 #include <securec.h> 16 #include <tee_client_api.h> 17 #include <tee_client_type.h> 18 #include <test_defines.h> 19 #include <test_log.h> 20 #include <test_tcf_cmdid.h> 21 22 using namespace testing::ext; 23 24 /** 25 * @testcase.name : TEE_Realloc_With_NewSizeIsZero 26 * @testcase.desc : test TA call TEE_Realloc to alloc buffer with newsize is 0 27 * @testcase.expect : return TEEC_ERROR_OUT_OF_MEMORY 28 */ 29 TEE_TEST(TeeTCF2Test, TEE_Realloc_With_NewSizeIsZero, Function | MediumTest | Level0) 30 { 31 TEEC_Result ret; 32 TestMemData value = { 0 }; 33 char outBuf[TESTSIZE] = { 0 }; 34 35 value.oldSize = TESTSIZE; 36 value.newSize = 0; 37 ret = Invoke_Realloc(GetSession(), CMD_TEE_Realloc, &value, outBuf); 38 ASSERT_EQ(ret, TEEC_ERROR_OUT_OF_MEMORY); 39 ASSERT_EQ(value.origin, TEEC_ORIGIN_TRUSTED_APP); 40 } 41 42 /** 43 * @testcase.name : TEE_Realloc_With_SizeExceedHeapLimit 44 * @testcase.desc : test TA call TEE_Realloc to alloc buffer with newsize exceed heaplimit 45 * @testcase.expect : return TEEC_ERROR_OUT_OF_MEMORY 46 */ 47 TEE_TEST(TeeTCF2Test, TEE_Realloc_With_SizeExceedHeapLimit, Function | MediumTest | Level0) 48 { 49 TEEC_Result ret; 50 TestMemData value = { 0 }; 51 char outBuf[TESTSIZE] = { 0 }; 52 53 uint32_t dateSize = get_ta_data_size(GetContext(), GetSession()); 54 ASSERT_GT(dateSize, 0); 55 56 uint32_t stackSize = get_ta_stack_size(GetContext(), GetSession()); 57 ASSERT_GT(stackSize, 0); 58 59 value.oldSize = TESTSIZE; 60 value.newSize = dateSize + stackSize; 61 ret = Invoke_Realloc(GetSession(), CMD_TEE_Realloc, &value, outBuf); 62 ASSERT_EQ(ret, TEEC_ERROR_OUT_OF_MEMORY); 63 ASSERT_EQ(value.origin, TEEC_ORIGIN_TRUSTED_APP); 64 } 65 66 /** 67 * @testcase.name : TEE_Realloc_With_BufferIsNull 68 * @testcase.desc : test TA call TEE_Realloc to alloc buffer with buffer is null 69 * @testcase.expect : return TEEC_SUCCESS 70 */ 71 TEE_TEST(TeeTCF2Test, TEE_Realloc_With_BufferIsNull, Function | MediumTest | Level0) 72 { 73 TEEC_Result ret; 74 TestMemData value = { 0 }; 75 char outBuf[TESTSIZE + 1] = { 0 }; 76 77 value.oldSize = TESTSIZE; 78 value.newSize = TESTSIZE; 79 value.caseId = INPUT_ISNULL; 80 ret = Invoke_Realloc(GetSession(), CMD_TEE_Realloc, &value, outBuf); 81 ASSERT_EQ(ret, TEEC_SUCCESS); 82 ASSERT_EQ(value.origin, TEEC_ORIGIN_TRUSTED_APP); 83 ASSERT_NE(value.oldAddr, value.newAddr); 84 ASSERT_STREQ(outBuf, EXPECTBUFFER_A); 85 } 86 87 /** 88 * @testcase.name : TEE_Realloc_With_SameSize 89 * @testcase.desc : test TA call TEE_Realloc to alloc buffer with newsize is same as oldsize 90 * @testcase.expect : return TEEC_SUCCESS 91 */ 92 TEE_TEST(TeeTCF2Test, TEE_Realloc_With_SameSize, Function | MediumTest | Level0) 93 { 94 TEEC_Result ret; 95 TestMemData value = { 0 }; 96 char outBuf[TESTSIZE + 1] = { 0 }; 97 98 value.oldSize = TESTSIZE; 99 value.newSize = TESTSIZE; 100 ret = Invoke_Realloc(GetSession(), CMD_TEE_Realloc, &value, outBuf); 101 ASSERT_EQ(ret, TEEC_SUCCESS); 102 ASSERT_EQ(value.origin, TEEC_ORIGIN_TRUSTED_APP); 103 ASSERT_EQ(value.oldAddr, value.newAddr); 104 ASSERT_STREQ(outBuf, EXPECTBUFFER_A); 105 } 106 107 /** 108 * @testcase.name : TEE_Realloc_With_LessSize 109 * @testcase.desc : test TA call TEE_Realloc to alloc buffer with newsize is less than oldsize 110 * @testcase.expect : return TEEC_SUCCESS 111 */ 112 TEE_TEST(TeeTCF2Test, TEE_Realloc_With_LessSize, Function | MediumTest | Level0) 113 { 114 TEEC_Result ret; 115 TestMemData value = { 0 }; 116 char outBuf[TESTSIZE] = { 0 }; 117 118 value.oldSize = TESTSIZE; 119 value.newSize = TESTSIZE - 1; 120 ret = Invoke_Realloc(GetSession(), CMD_TEE_Realloc, &value, outBuf); 121 ASSERT_EQ(ret, TEEC_SUCCESS); 122 ASSERT_EQ(value.origin, TEEC_ORIGIN_TRUSTED_APP); 123 ASSERT_EQ(value.oldAddr, value.newAddr); 124 ASSERT_STREQ(outBuf, EXPECTBUFFER_A_LESS); 125 } 126 127 /** 128 * @testcase.name : TEE_Realloc_With_GreaterSize 129 * @testcase.desc : test TA call TEE_Realloc to alloc buffer with newsize is greater than oldsize 130 * @testcase.expect : return TEEC_SUCCESS 131 */ 132 TEE_TEST(TeeTCF2Test, TEE_Realloc_With_GreaterSize, Function | MediumTest | Level0) 133 { 134 TEEC_Result ret; 135 TestMemData value = { 0 }; 136 char outBuf[BIG_SIZE] = { 0 }; 137 uint32_t i; 138 139 value.oldSize = TESTSIZE; 140 value.newSize = BIG_SIZE; 141 ret = Invoke_Realloc(GetSession(), CMD_TEE_Realloc, &value, outBuf); 142 ASSERT_EQ(ret, TEEC_SUCCESS); 143 ASSERT_EQ(value.origin, TEEC_ORIGIN_TRUSTED_APP); 144 ASSERT_STREQ(outBuf, EXPECTBUFFER_A); 145 146 for (i = TESTSIZE; i < BIG_SIZE; i++) { 147 ASSERT_EQ(outBuf[i], 0); 148 } 149 } 150