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 <gtest/gtest.h> 17 18 #include "configs/guard_context.h" 19 20 using namespace testing::ext; 21 using namespace panda; 22 23 namespace { 24 const std::string CONTEXT_TEST_01_CONFIG_FILE = PANDA_GUARD_UNIT_TEST_DIR "configs/context_test_01_config.json"; 25 } 26 27 /** 28 * @tc.name: guard_context_test_001 29 * @tc.desc: test context init is right 30 * @tc.type: FUNC 31 * @tc.require: 32 */ 33 HWTEST(GuardContextUnitTest, guard_context_test_001, TestSize.Level4) 34 { 35 int argc = 3; 36 char *argv[3]; 37 argv[0] = const_cast<char *>("xxx"); 38 argv[1] = const_cast<char *>("--debug"); 39 argv[2] = const_cast<char *>(CONTEXT_TEST_01_CONFIG_FILE.c_str()); 40 41 auto context = guard::GuardContext::GetInstance(); 42 context->Init(argc, const_cast<const char **>(argv)); 43 EXPECT_EQ(context->IsDebugMode(), true); 44 45 auto nameMapping = context->GetNameMapping(); 46 std::string obfName = nameMapping->GetFileName("context_test"); 47 EXPECT_EQ(obfName, "h"); 48 49 obfName = nameMapping->GetFileName("test1"); 50 EXPECT_EQ(obfName, "a"); 51 52 obfName = nameMapping->GetFileName("xxx"); 53 EXPECT_EQ(obfName, "xxx"); 54 } 55 56 /** 57 * @tc.name: guard_context_test_002 58 * @tc.desc: test context init with invalid config 59 * @tc.type: FUNC 60 * @tc.require: 61 */ 62 HWTEST(GuardContextUnitTest, guard_context_test_002, TestSize.Level4) 63 { 64 int argc = 2; 65 char *argv[2]; 66 argv[0] = const_cast<char *>("xxx"); 67 argv[1] = const_cast<char *>("xxx"); 68 69 auto context = guard::GuardContext::GetInstance(); 70 EXPECT_DEATH(context->Init(argc, const_cast<const char **>(argv)), ""); 71 }