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 18 #include <gtest/gtest.h> 19 20 #include "generator/name_mapping.h" 21 #include "generator/order_name_generator.h" 22 23 using namespace testing::ext; 24 using namespace panda::guard; 25 26 /** 27 * @tc.name: name_mapping_get_name_test_001 28 * @tc.desc: test name mapping get name is order 29 * @tc.type: FUNC 30 * @tc.require: 31 */ 32 HWTEST(NameGeneratotUnitTest, name_mapping_get_name_test_001, TestSize.Level4) 33 { 34 auto options = std::make_shared<GuardOptions>(); 35 auto nameCache = std::make_shared<NameCache>(options); 36 auto nameMapping = std::make_shared<NameMapping>(nameCache); 37 38 vector<std::string> expectList = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", 39 "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a1", "b1"}; 40 for (size_t idx = 0; idx < expectList.size(); idx++) { 41 std::string origin = "test" + std::to_string(idx); 42 std::string obfName = nameMapping->GetName(origin); 43 EXPECT_EQ(obfName, expectList[idx]); 44 45 obfName = nameMapping->GetFileName(origin); 46 EXPECT_EQ(obfName, expectList[idx]); 47 } 48 49 std::string oriNum = "123"; 50 std::string obfNum = nameMapping->GetName(oriNum); 51 EXPECT_EQ(oriNum, obfNum); 52 } 53 54 /** 55 * @tc.name: name_mapping_get_name_test_002 56 * @tc.desc: test name mapping get name is order 57 * @tc.type: FUNC 58 * @tc.require: 59 */ 60 HWTEST(NameGeneratotUnitTest, name_mapping_get_name_test_002, TestSize.Level4) 61 { 62 auto options = std::make_shared<GuardOptions>(); 63 auto nameCache = std::make_shared<NameCache>(options); 64 auto nameMapping = std::make_shared<NameMapping>(nameCache); 65 66 std::string obfName = nameMapping->GetName("test0"); 67 EXPECT_EQ(obfName, "a"); 68 69 obfName = nameMapping->GetFileName("fileName_test0"); 70 EXPECT_EQ(obfName, "a"); 71 72 std::set<std::string> newReservedNames = {"b", "c"}; 73 nameMapping->AddReservedNames(newReservedNames); 74 75 obfName = nameMapping->GetName("test1"); 76 EXPECT_EQ(obfName, "d"); 77 78 obfName = nameMapping->GetFileName("fileName_test1"); 79 EXPECT_EQ(obfName, "d"); 80 81 obfName = nameMapping->GetName("test0"); 82 EXPECT_EQ(obfName, "a"); 83 84 obfName = nameMapping->GetFileName("fileName_test0"); 85 EXPECT_EQ(obfName, "a"); 86 } 87 88 /** 89 * @tc.name: name_mapping_get_name_test_003 90 * @tc.desc: test name mapping get multiple method name is same 91 * @tc.type: FUNC 92 * @tc.require: 93 */ 94 HWTEST(NameGeneratotUnitTest, name_mapping_get_name_test_003, TestSize.Level4) 95 { 96 auto options = std::make_shared<GuardOptions>(); 97 auto nameCache = std::make_shared<NameCache>(options); 98 auto nameMapping = std::make_shared<NameMapping>(nameCache); 99 100 std::string obfName = nameMapping->GetName("foo"); 101 EXPECT_EQ(obfName, "a"); 102 103 obfName = nameMapping->GetName("foo^1"); 104 EXPECT_EQ(obfName, "a^1"); 105 106 obfName = nameMapping->GetName("foo^a"); 107 EXPECT_EQ(obfName, "a^a"); 108 109 obfName = nameMapping->GetName("foo^1a"); 110 EXPECT_EQ(obfName, "a^1a"); 111 } 112