1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <cstring> 17 #include <gtest/gtest.h> 18 #include <securec.h> 19 20 #include "code_sign_utils_in_c.h" 21 #include "errcode.h" 22 #include "log.h" 23 24 namespace OHOS { 25 namespace Security { 26 namespace CodeSign { 27 using namespace testing::ext; 28 using namespace std; 29 30 static constexpr int32_t ENTRYMAP_COUNT = 2; 31 static const std::string APP_BASE_PATH = "/data/app/el1/bundle/public/tmp"; 32 33 class CodeSignUtilsInCTest : public testing::Test { 34 public: CodeSignUtilsInCTest()35 CodeSignUtilsInCTest() {}; ~CodeSignUtilsInCTest()36 virtual ~CodeSignUtilsInCTest() {}; SetUpTestCase()37 static void SetUpTestCase() {}; TearDownTestCase()38 static void TearDownTestCase() {}; SetUp()39 void SetUp() {}; TearDown()40 void TearDown() {}; 41 }; 42 43 /** 44 * @tc.name: CodeSignUtilsInCTest_0001 45 * @tc.desc: enable code signature for app with the c interface 46 * @tc.type: Func 47 * @tc.require: 48 */ 49 HWTEST_F(CodeSignUtilsInCTest, CodeSignUtilsInCTest_0001, TestSize.Level0) 50 { 51 std::string hapRealPath = APP_BASE_PATH + "/demo_with_multi_lib/demo_with_code_sign_block.hap"; 52 std::string filePath1("libs/arm64-v8a/libc++_shared.so"); 53 std::string targetPath1 = APP_BASE_PATH + "/demo_with_multi_lib/libs/arm64-v8a/code_sign_block/libc++_shared.so"; 54 std::string filePath2("libs/arm64-v8a/libentry.so"); 55 std::string targetPath2 = APP_BASE_PATH + "/demo_with_multi_lib/libs/arm64-v8a/code_sign_block/libentry.so"; 56 57 EntryMapEntryData *entryMapEntryData = static_cast<EntryMapEntryData *>(malloc(sizeof(EntryMapEntryData))); 58 (void)memset_s(entryMapEntryData, sizeof(EntryMapEntryData), 0, sizeof(EntryMapEntryData)); 59 60 int32_t length = sizeof(EntryMapEntry) * ENTRYMAP_COUNT; 61 EntryMapEntry *entryMapEntry = static_cast<EntryMapEntry *>(malloc(length)); 62 (void)memset_s(entryMapEntry, length, 0, length); 63 64 entryMapEntry[0].key = const_cast<char *>(filePath1.c_str()); 65 entryMapEntry[0].value = const_cast<char *>(targetPath1.c_str()); 66 entryMapEntry[1].key = const_cast<char *>(filePath2.c_str()); 67 entryMapEntry[1].value = const_cast<char *>(targetPath2.c_str()); 68 69 entryMapEntryData->count = ENTRYMAP_COUNT; 70 entryMapEntryData->entries = entryMapEntry; 71 72 int32_t ret = EnforceCodeSignForApp(hapRealPath.c_str(), entryMapEntryData, FILE_ALL); 73 EXPECT_EQ(ret, CS_SUCCESS); 74 75 free(entryMapEntry); 76 free(entryMapEntryData); 77 entryMapEntry = nullptr; 78 entryMapEntryData = nullptr; 79 } 80 81 /** 82 * @tc.name: CodeSignUtilsInCTest_0002 83 * @tc.desc: enable code signature for app with the c interface, nullptr 84 * @tc.type: Func 85 * @tc.require: 86 */ 87 HWTEST_F(CodeSignUtilsInCTest, CodeSignUtilsInCTest_0002, TestSize.Level0) 88 { 89 std::string hapRealPath = APP_BASE_PATH + "/demo_with_multi_lib/demo_with_code_sign_block.hap"; 90 int32_t ret = EnforceCodeSignForApp(nullptr, nullptr, FILE_ALL); 91 EXPECT_EQ(ret, CS_ERR_PARAM_INVALID); 92 93 ret = EnforceCodeSignForApp(hapRealPath.c_str(), nullptr, FILE_ALL); 94 EXPECT_EQ(ret, CS_ERR_PARAM_INVALID); 95 } 96 97 /** 98 * @tc.name: CodeSignUtilsInCTest_0003 99 * @tc.desc: enable code signature for app with the c interface, entryMapEntry is nullptr 100 * @tc.type: Func 101 * @tc.require: 102 */ 103 HWTEST_F(CodeSignUtilsInCTest, CodeSignUtilsInCTest_0003, TestSize.Level0) 104 { 105 std::string hapRealPath = APP_BASE_PATH + "/demo_with_multi_lib/demo_with_code_sign_block.hap"; 106 std::string filePath1("libs/arm64-v8a/libc++_shared.so"); 107 std::string targetPath1 = APP_BASE_PATH + "/demo_with_multi_lib/libs/arm64-v8a/code_sign_block/libc++_shared.so"; 108 std::string filePath2("libs/arm64-v8a/libentry.so"); 109 std::string targetPath2 = APP_BASE_PATH + "/demo_with_multi_lib/libs/arm64-v8a/code_sign_block/libentry.so"; 110 111 EntryMapEntryData *entryMapEntryData = static_cast<EntryMapEntryData *>(malloc(sizeof(EntryMapEntryData))); 112 (void)memset_s(entryMapEntryData, sizeof(EntryMapEntryData), 0, sizeof(EntryMapEntryData)); 113 114 int32_t length = sizeof(EntryMapEntry) * ENTRYMAP_COUNT; 115 EntryMapEntry *entryMapEntry = static_cast<EntryMapEntry *>(malloc(length)); 116 (void)memset_s(entryMapEntry, length, 0, length); 117 118 entryMapEntry[0].key = nullptr; 119 entryMapEntry[0].value = nullptr; 120 entryMapEntry[1].key = nullptr; 121 entryMapEntry[1].value = nullptr; 122 123 entryMapEntryData->count = ENTRYMAP_COUNT; 124 entryMapEntryData->entries = entryMapEntry; 125 126 int32_t ret = EnforceCodeSignForApp(hapRealPath.c_str(), entryMapEntryData, FILE_ALL); 127 EXPECT_EQ(ret, CS_ERR_PARAM_INVALID); 128 129 entryMapEntry[0].key = const_cast<char *>(filePath1.c_str()); 130 entryMapEntryData->entries = entryMapEntry; 131 132 ret = EnforceCodeSignForApp(hapRealPath.c_str(), entryMapEntryData, FILE_ALL); 133 EXPECT_EQ(ret, CS_ERR_PARAM_INVALID); 134 135 entryMapEntry[0].value = const_cast<char *>(targetPath1.c_str()); 136 entryMapEntryData->entries = entryMapEntry; 137 138 ret = EnforceCodeSignForApp(hapRealPath.c_str(), entryMapEntryData, FILE_ALL); 139 EXPECT_EQ(ret, CS_ERR_PARAM_INVALID); 140 141 free(entryMapEntry); 142 free(entryMapEntryData); 143 entryMapEntry = nullptr; 144 entryMapEntryData = nullptr; 145 } 146 } // namespace CodeSign 147 } // namespace Security 148 } // namespace OHOS 149