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 <vector> 17 #include <gtest/gtest.h> 18 #include "configs/guard_name_cache.h" 19 #include "util/test_util.h" 20 21 using namespace testing::ext; 22 using namespace panda; 23 24 namespace { 25 const std::string NAME_CACHE_TEST_01_FILE = PANDA_GUARD_UNIT_TEST_DIR "configs/name_cache_test_01.json"; 26 const std::string NAME_CACHE_TEST_02_FILE = PANDA_GUARD_UNIT_TEST_DIR "configs/name_cache_test_02.json"; 27 const std::string NAME_CACHE_TEST_02_CONFIG_FILE = PANDA_GUARD_UNIT_TEST_DIR "configs/name_cache_test_02_config.json"; 28 const std::string NAME_CACHE_TEST_04_FILE = PANDA_GUARD_UNIT_TEST_DIR "configs/name_cache_test_04.json"; 29 } // namespace 30 31 /** 32 * @tc.name: guard_name_cache_test_001 33 * @tc.desc: test name cache json parse is right 34 * @tc.type: FUNC 35 * @tc.require: 36 */ 37 HWTEST(GuardNameCacheUnitTest, guard_name_cache_test_001, TestSize.Level4) 38 { 39 auto options = std::make_shared<guard::GuardOptions>(); 40 guard::NameCache nameCache(options); 41 nameCache.Load(NAME_CACHE_TEST_01_FILE); 42 43 std::set<std::string> expectList = {"/entry/src/a/b.ets", "5.0.0.1", "a", "b", "c", "d", "e", 44 "entry|1.0.0", "f", "g"}; 45 auto historyValues = nameCache.GetHistoryUsedNames(); 46 bool bEqual = std::equal(historyValues.begin(), historyValues.end(), expectList.begin(), expectList.end()); 47 EXPECT_EQ(bEqual, true); 48 49 auto fileMapping = nameCache.GetHistoryFileNameMapping(); 50 EXPECT_EQ(fileMapping["test1"], "a"); 51 EXPECT_EQ(fileMapping["test2"], "b"); 52 53 auto nameMapping = nameCache.GetHistoryNameMapping(); 54 EXPECT_EQ(nameMapping["test3"], "c"); 55 EXPECT_EQ(nameMapping["test4"], "d"); 56 EXPECT_EQ(nameMapping["test5"], "e"); 57 EXPECT_EQ(nameMapping["func"], "f"); 58 EXPECT_EQ(nameMapping["test7"], "g"); 59 } 60 61 /** 62 * @tc.name: guard_name_cache_test_002 63 * @tc.desc: test name cache merge and write is right 64 * @tc.type: FUNC 65 * @tc.require: 66 */ 67 HWTEST(GuardNameCacheUnitTest, guard_name_cache_test_002, TestSize.Level4) 68 { 69 auto options = std::make_shared<guard::GuardOptions>(); 70 options->Load(NAME_CACHE_TEST_02_CONFIG_FILE); 71 72 guard::NameCache nameCache(options); 73 nameCache.Load(NAME_CACHE_TEST_02_FILE); 74 75 std::string filePath = "/entry/src/test1/test2.ets"; 76 std::string obfFilePath = "/entry/src/a/b.ets"; 77 nameCache.AddObfName(filePath, obfFilePath); 78 nameCache.AddObfIdentifierName(filePath, "#haTest1", "a1"); 79 nameCache.AddObfMemberMethodName(filePath, "#haTest2:3:5", "b1"); 80 nameCache.AddObfPropertyName("haTest1", "a1"); 81 nameCache.AddObfPropertyName("haTest2", "b1"); 82 83 std::string newFilePath = "/entry/src/newTest1/newTest2.ets"; 84 std::string obfNewFilePath = "/entry/src/a2/b2.ets"; 85 nameCache.AddObfName(newFilePath, obfNewFilePath); 86 nameCache.AddNewNameCacheObfFileName("newTest1", "a2"); 87 nameCache.AddNewNameCacheObfFileName("newTest2", "b2"); 88 nameCache.AddObfIdentifierName(newFilePath, "#newTest3", "c2"); 89 nameCache.AddObfMemberMethodName(newFilePath, "#newTest4:3:5", "d2"); 90 nameCache.AddObfPropertyName("newTest3", "c2"); 91 nameCache.AddObfPropertyName("newTest4", "d2"); 92 93 nameCache.WriteCache(); 94 95 static const std::string NEW_NAME_CACHE_TEST_JSON_FILE = 96 PANDA_GUARD_UNIT_TEST_DIR "configs/new_name_cache_test.json"; 97 guard::NameCache newNameCache(options); 98 newNameCache.Load(NEW_NAME_CACHE_TEST_JSON_FILE); 99 100 std::set<std::string> expectList = {"/entry/src/a/b.ets", 101 "/entry/src/a2/b2.ets", 102 "5.0.0.2", 103 "a", 104 "a1", 105 "a2", 106 "b", 107 "b1", 108 "b2", 109 "c", 110 "c2", 111 "d", 112 "d2", 113 "e", 114 "entry|2.0.1", 115 "f", 116 "g"}; 117 auto newValues = newNameCache.GetHistoryUsedNames(); 118 bool bEqual = std::equal(newValues.begin(), newValues.end(), expectList.begin(), expectList.end()); 119 EXPECT_EQ(bEqual, true); 120 121 guard::TestUtil::RemoveFile(NEW_NAME_CACHE_TEST_JSON_FILE); 122 } 123 124 /** 125 * @tc.name: guard_name_cache_test_003 126 * @tc.desc: test name cache json parse with abnormal args 127 * @tc.type: FUNC 128 * @tc.require: 129 */ 130 HWTEST(GuardNameCacheUnitTest, guard_name_cache_test_003, TestSize.Level4) 131 { 132 auto options = std::make_shared<guard::GuardOptions>(); 133 guard::NameCache nameCache(options); 134 nameCache.Load(""); 135 136 std::string filePath = "/entry/src/common/calc.ets"; 137 std::string obfName = "c"; 138 std::string origin = "#class1"; 139 nameCache.AddObfIdentifierName("", origin, obfName); 140 nameCache.AddObfIdentifierName(filePath, "", obfName); 141 nameCache.AddObfIdentifierName(filePath, origin, ""); 142 143 nameCache.AddObfMemberMethodName("", origin, obfName); 144 nameCache.AddObfMemberMethodName(filePath, "", obfName); 145 nameCache.AddObfMemberMethodName(filePath, origin, ""); 146 147 std::string obfFilePath = "/entry/src/a/b.ets"; 148 nameCache.AddObfName("", obfFilePath); 149 nameCache.AddObfName(filePath, ""); 150 151 std::string fileName = "calc"; 152 std::string obfFileName = "b"; 153 nameCache.AddNewNameCacheObfFileName("", obfFileName); 154 nameCache.AddNewNameCacheObfFileName(fileName, ""); 155 156 EXPECT_DEATH(nameCache.WriteCache(), ""); 157 } 158 159 /** 160 * @tc.name: guard_name_cache_test_004 161 * @tc.desc: test name cache json parse with abnormal json data 162 * @tc.type: FUNC 163 * @tc.require: 164 */ 165 HWTEST(GuardNameCacheUnitTest, guard_name_cache_test_004, TestSize.Level4) 166 { 167 auto options = std::make_shared<guard::GuardOptions>(); 168 guard::NameCache nameCache(options); 169 EXPECT_DEATH(nameCache.Load(NAME_CACHE_TEST_04_FILE), ""); 170 }