1 /* 2 * Copyright (c) 2021 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 <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 sleep(1); // 睡眠1s确保hiprofilerd进程启动 38 } TearDownTestCase()39 static void TearDownTestCase() 40 { 41 StopServerStub(g_hiprofilerdProcessNum); 42 } 43 StartServerStub(std::string name)44 static void StartServerStub(std::string name) 45 { 46 int processNum = fork(); 47 HILOG_INFO(LOG_CORE, "processNum : %d", processNum); 48 if (processNum == 0) { 49 if (DEFAULT_HIPROFILERD_PATH == name) { 50 // start running hiprofilerd 51 execl(name.c_str(), nullptr, nullptr); 52 } 53 _exit(1); 54 } else if (DEFAULT_HIPROFILERD_PATH == name) { 55 g_hiprofilerdProcessNum = processNum; 56 } 57 } 58 StopServerStub(int processNum)59 static void StopServerStub(int processNum) 60 { 61 std::string stopCmd = "kill " + std::to_string(processNum); 62 HILOG_INFO(LOG_CORE, "stop command : %s", stopCmd.c_str()); 63 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(stopCmd.c_str(), "r"), pclose); 64 } 65 }; 66 67 /* 68 * @tc.name: RegisterPlugin 69 * @tc.desc: test HookManager::RegisterAgentPlugin with normal case. 70 * @tc.type: FUNC 71 */ 72 HWTEST_F(HookManagerTest, RegisterPlugin, TestSize.Level1) 73 { 74 std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>(); 75 ASSERT_TRUE(hookManager != nullptr); 76 std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager); 77 ASSERT_TRUE(commandPoller != nullptr); 78 EXPECT_TRUE(commandPoller->OnConnect()); 79 hookManager->SetCommandPoller(commandPoller); 80 ASSERT_TRUE(hookManager->RegisterAgentPlugin("nativehook")); 81 ASSERT_TRUE(hookManager->UnregisterAgentPlugin("nativehook")); 82 } 83 84 /* 85 * @tc.name: LoadPlugin 86 * @tc.desc: test HookManager::LoadPlugin with normal case. 87 * @tc.type: FUNC 88 */ 89 HWTEST_F(HookManagerTest, LoadPlugin, TestSize.Level1) 90 { 91 std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>(); 92 ASSERT_TRUE(hookManager != nullptr); 93 std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager); 94 ASSERT_TRUE(commandPoller != nullptr); 95 EXPECT_TRUE(commandPoller->OnConnect()); 96 hookManager->SetCommandPoller(commandPoller); 97 ASSERT_TRUE(hookManager->RegisterAgentPlugin("nativehook")); 98 ASSERT_TRUE(hookManager->LoadPlugin("nativehook")); 99 ASSERT_TRUE(hookManager->UnloadPlugin("nativehook")); 100 ASSERT_TRUE(hookManager->UnregisterAgentPlugin("nativehook")); 101 } 102 103 /* 104 * @tc.name: UnloadPlugin 105 * @tc.desc: test HookManager::UnloadPlugin with normal case. 106 * @tc.type: FUNC 107 */ 108 HWTEST_F(HookManagerTest, UnloadPlugin, TestSize.Level1) 109 { 110 std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>(); 111 ASSERT_TRUE(hookManager != nullptr); 112 std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager); 113 ASSERT_TRUE(commandPoller != nullptr); 114 EXPECT_TRUE(commandPoller->OnConnect()); 115 hookManager->SetCommandPoller(commandPoller); 116 ASSERT_TRUE(hookManager->RegisterAgentPlugin("nativehook")); 117 ASSERT_TRUE(hookManager->LoadPlugin("nativehook")); 118 ASSERT_TRUE(hookManager->UnloadPlugin(commandPoller->GetRequestId())); 119 ASSERT_TRUE(hookManager->UnregisterAgentPlugin("nativehook")); 120 } 121 122 /* 123 * @tc.name: PluginSession 124 * @tc.desc: test HookManager process with normal case. 125 * @tc.type: FUNC 126 */ 127 HWTEST_F(HookManagerTest, PluginSession, TestSize.Level1) 128 { 129 std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>(); 130 ASSERT_TRUE(hookManager != nullptr); 131 std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager); 132 ASSERT_TRUE(commandPoller != nullptr); 133 EXPECT_TRUE(commandPoller->OnConnect()); 134 hookManager->SetCommandPoller(commandPoller); 135 136 std::vector<uint32_t> pluginIds(1); 137 ProfilerPluginConfig config; 138 config.set_name("nativehook"); 139 config.set_plugin_sha256(""); 140 config.set_sample_interval(20); 141 142 PluginResult result; 143 std::vector<ProfilerPluginConfig> configVec; 144 configVec.push_back(config); 145 146 EXPECT_FALSE(hookManager->CreatePluginSession(configVec)); 147 EXPECT_FALSE(hookManager->StartPluginSession(pluginIds, configVec, result)); 148 EXPECT_TRUE(hookManager->CreateWriter("name", 0, 0, 0)); 149 EXPECT_TRUE(hookManager->ResetWriter(0)); 150 EXPECT_FALSE(hookManager->StopPluginSession(pluginIds)); 151 EXPECT_TRUE(hookManager->DestroyPluginSession(pluginIds)); 152 } 153 } // namespace 154