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