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