1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 <hwext/gtest-ext.h> 17 #include <hwext/gtest-tag.h> 18 19 #include "hook_manager.h" 20 #include "hook_service.h" 21 #include "hook_socket_client.h" 22 #include "socket_context.h" 23 #include "command_poller.h" 24 25 using namespace testing::ext; 26 using namespace OHOS::Developtools::NativeDaemon; 27 28 namespace { 29 const std::string DEFAULT_HIPROFILERD_PATH("/system/bin/hiprofilerd"); 30 int g_hiprofilerdProcessNum = -1; 31 32 class HookManagerTest : public ::testing::Test { 33 public: SetUpTestCase()34 static void SetUpTestCase() 35 { 36 StartServerStub(DEFAULT_HIPROFILERD_PATH); 37 #ifdef COVERAGE_TEST 38 const int coverageSleepTime = 5; // sleep 5s 39 sleep(coverageSleepTime); 40 #else 41 sleep(1); // 睡眠1s确保hiprofilerd进程启动 42 #endif 43 } TearDownTestCase()44 static void TearDownTestCase() 45 { 46 StopServerStub(g_hiprofilerdProcessNum); 47 } 48 StartServerStub(std::string name)49 static void StartServerStub(std::string name) 50 { 51 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { 52 return; 53 } 54 int processNum = fork(); 55 PROFILER_LOG_INFO(LOG_CORE, "processNum : %d", processNum); 56 if (processNum == 0) { 57 if (DEFAULT_HIPROFILERD_PATH == name) { 58 // start running hiprofilerd 59 execl(name.c_str(), nullptr, nullptr); 60 } 61 _exit(1); 62 } else if (DEFAULT_HIPROFILERD_PATH == name) { 63 g_hiprofilerdProcessNum = processNum; 64 } 65 } 66 StopServerStub(int processNum)67 static void StopServerStub(int processNum) 68 { 69 std::string stopCmd = "kill " + std::to_string(processNum); 70 PROFILER_LOG_INFO(LOG_CORE, "stop command : %s", stopCmd.c_str()); 71 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(stopCmd.c_str(), "r"), pclose); 72 } 73 }; 74 75 /* 76 * @tc.name: RegisterPlugin 77 * @tc.desc: test HookManager::RegisterAgentPlugin with normal case. 78 * @tc.type: FUNC 79 */ 80 HWTEST_F(HookManagerTest, RegisterPlugin, TestSize.Level1) 81 { 82 std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>(); 83 ASSERT_TRUE(hookManager != nullptr); 84 std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager); 85 ASSERT_TRUE(commandPoller != nullptr); 86 EXPECT_TRUE(commandPoller->OnConnect()); 87 hookManager->SetCommandPoller(commandPoller); 88 ASSERT_TRUE(hookManager->RegisterAgentPlugin("nativehook")); 89 ASSERT_TRUE(hookManager->UnregisterAgentPlugin("nativehook")); 90 } 91 92 /* 93 * @tc.name: LoadPlugin 94 * @tc.desc: test HookManager::LoadPlugin with normal case. 95 * @tc.type: FUNC 96 */ 97 HWTEST_F(HookManagerTest, LoadPlugin, TestSize.Level1) 98 { 99 std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>(); 100 ASSERT_TRUE(hookManager != nullptr); 101 std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager); 102 ASSERT_TRUE(commandPoller != nullptr); 103 EXPECT_TRUE(commandPoller->OnConnect()); 104 hookManager->SetCommandPoller(commandPoller); 105 ASSERT_TRUE(hookManager->RegisterAgentPlugin("nativehook")); 106 ASSERT_TRUE(hookManager->LoadPlugin("nativehook")); 107 ASSERT_TRUE(hookManager->UnloadPlugin("nativehook")); 108 ASSERT_TRUE(hookManager->UnregisterAgentPlugin("nativehook")); 109 } 110 111 /* 112 * @tc.name: UnloadPlugin 113 * @tc.desc: test HookManager::UnloadPlugin with normal case. 114 * @tc.type: FUNC 115 */ 116 HWTEST_F(HookManagerTest, UnloadPlugin, TestSize.Level1) 117 { 118 std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>(); 119 ASSERT_TRUE(hookManager != nullptr); 120 std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager); 121 ASSERT_TRUE(commandPoller != nullptr); 122 EXPECT_TRUE(commandPoller->OnConnect()); 123 hookManager->SetCommandPoller(commandPoller); 124 ASSERT_TRUE(hookManager->RegisterAgentPlugin("nativehook")); 125 ASSERT_TRUE(hookManager->LoadPlugin("nativehook")); 126 ASSERT_TRUE(hookManager->UnloadPlugin(commandPoller->GetRequestId())); 127 ASSERT_TRUE(hookManager->UnregisterAgentPlugin("nativehook")); 128 } 129 130 /* 131 * @tc.name: PluginSession 132 * @tc.desc: test HookManager process with normal case. 133 * @tc.type: FUNC 134 */ 135 HWTEST_F(HookManagerTest, PluginSession, TestSize.Level1) 136 { 137 std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>(); 138 ASSERT_TRUE(hookManager != nullptr); 139 std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager); 140 ASSERT_TRUE(commandPoller != nullptr); 141 EXPECT_TRUE(commandPoller->OnConnect()); 142 hookManager->SetCommandPoller(commandPoller); 143 144 std::vector<uint32_t> pluginIds(1); 145 ProfilerPluginConfig config; 146 config.set_name("nativehook"); 147 config.set_plugin_sha256(""); 148 config.set_sample_interval(20); 149 150 PluginResult result; 151 std::vector<ProfilerPluginConfig> configVec; 152 configVec.push_back(config); 153 154 EXPECT_FALSE(hookManager->CreatePluginSession(configVec)); 155 EXPECT_FALSE(hookManager->StartPluginSession(pluginIds, configVec, result)); 156 EXPECT_TRUE(hookManager->CreateWriter("name", 0, 0, 0)); 157 EXPECT_TRUE(hookManager->ResetWriter(0)); 158 EXPECT_FALSE(hookManager->StopPluginSession(pluginIds)); 159 EXPECT_TRUE(hookManager->DestroyPluginSession(pluginIds)); 160 } 161 162 /* 163 * @tc.name: CheckProcess 164 * @tc.desc: test CheckProcess with false case. 165 * @tc.type: FUNC 166 */ 167 HWTEST_F(HookManagerTest, CheckProcess, TestSize.Level1) 168 { 169 HookManager hookManager; 170 NativeHookConfig nativeConfig; 171 nativeConfig.set_process_name("HookManagerTest"); 172 nativeConfig.set_startup_mode(true); 173 hookManager.SetHookConfig(nativeConfig); 174 EXPECT_TRUE(hookManager.CheckProcess()); 175 hookManager.ResetStartupParam(); 176 177 // native_daemon_ut as a testing process 178 nativeConfig.set_startup_mode(false); 179 nativeConfig.set_process_name("native_daemon_ut"); 180 hookManager.SetHookConfig(nativeConfig); 181 EXPECT_TRUE(hookManager.CheckProcess()); 182 EXPECT_TRUE(hookManager.CheckProcessName()); 183 } 184 } // namespace