• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "command_poller.h"
18 #include "hook_manager.h"
19 #include "hook_service.h"
20 #include "hook_socket_client.h"
21 #include "parameters.h"
22 #include "socket_context.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::Developtools::NativeDaemon;
26 namespace {
27 class HookManagerTest : public ::testing::Test {
28 public:
SetUpTestCase()29     static void SetUpTestCase()
30     {
31         OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "1");
32 #ifdef COVERAGE_TEST
33         const int coverageSleepTime = 5; // sleep 5s
34         sleep(coverageSleepTime);
35 #else
36         sleep(1); // 睡眠1s确保hiprofilerd进程启动
37 #endif
38     }
TearDownTestCase()39     static void TearDownTestCase()
40     {
41         OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "0");
42     }
43 };
44 
45 /*
46  * @tc.name: RegisterPlugin
47  * @tc.desc: test HookManager::RegisterAgentPlugin with normal case.
48  * @tc.type: FUNC
49  */
50 HWTEST_F(HookManagerTest, RegisterPlugin, TestSize.Level1)
51 {
52     std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>();
53     ASSERT_TRUE(hookManager != nullptr);
54     std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager);
55     ASSERT_TRUE(commandPoller != nullptr);
56     EXPECT_TRUE(commandPoller->OnConnect());
57     hookManager->SetCommandPoller(commandPoller);
58     ASSERT_TRUE(hookManager->RegisterAgentPlugin("nativehook"));
59     ASSERT_TRUE(hookManager->UnregisterAgentPlugin("nativehook"));
60 }
61 
62 /*
63  * @tc.name: LoadPlugin
64  * @tc.desc: test HookManager::LoadPlugin with normal case.
65  * @tc.type: FUNC
66  */
67 HWTEST_F(HookManagerTest, LoadPlugin, TestSize.Level1)
68 {
69     std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>();
70     ASSERT_TRUE(hookManager != nullptr);
71     std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager);
72     ASSERT_TRUE(commandPoller != nullptr);
73     EXPECT_TRUE(commandPoller->OnConnect());
74     hookManager->SetCommandPoller(commandPoller);
75     ASSERT_TRUE(hookManager->RegisterAgentPlugin("nativehook"));
76     ASSERT_TRUE(hookManager->LoadPlugin("nativehook"));
77     ASSERT_TRUE(hookManager->UnloadPlugin("nativehook"));
78     ASSERT_TRUE(hookManager->UnregisterAgentPlugin("nativehook"));
79 }
80 
81 /*
82  * @tc.name: UnloadPlugin
83  * @tc.desc: test HookManager::UnloadPlugin with normal case.
84  * @tc.type: FUNC
85  */
86 HWTEST_F(HookManagerTest, UnloadPlugin, TestSize.Level1)
87 {
88     std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>();
89     ASSERT_TRUE(hookManager != nullptr);
90     std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager);
91     ASSERT_TRUE(commandPoller != nullptr);
92     EXPECT_TRUE(commandPoller->OnConnect());
93     hookManager->SetCommandPoller(commandPoller);
94     ASSERT_TRUE(hookManager->RegisterAgentPlugin("nativehook"));
95     ASSERT_TRUE(hookManager->LoadPlugin("nativehook"));
96     ASSERT_TRUE(hookManager->UnloadPlugin(commandPoller->GetRequestId()));
97     ASSERT_TRUE(hookManager->UnregisterAgentPlugin("nativehook"));
98 }
99 
100 /*
101  * @tc.name: PluginSession
102  * @tc.desc: test HookManager process with normal case.
103  * @tc.type: FUNC
104  */
105 HWTEST_F(HookManagerTest, PluginSession, TestSize.Level1)
106 {
107     std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>();
108     ASSERT_TRUE(hookManager != nullptr);
109     std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(hookManager);
110     ASSERT_TRUE(commandPoller != nullptr);
111     EXPECT_TRUE(commandPoller->OnConnect());
112     hookManager->SetCommandPoller(commandPoller);
113 
114     std::vector<uint32_t> pluginIds(1);
115     ProfilerPluginConfig config;
116     config.set_name("nativehook");
117     config.set_plugin_sha256("");
118     config.set_sample_interval(20);
119 
120     PluginResult result;
121     std::vector<ProfilerPluginConfig> configVec;
122     configVec.push_back(config);
123 
124     EXPECT_FALSE(hookManager->CreatePluginSession(configVec));
125     EXPECT_FALSE(hookManager->StartPluginSession(pluginIds, configVec, result));
126     EXPECT_TRUE(hookManager->CreateWriter("name", 0, 0, 0));
127     EXPECT_TRUE(hookManager->ResetWriter(0));
128     EXPECT_FALSE(hookManager->StopPluginSession(pluginIds));
129     EXPECT_TRUE(hookManager->DestroyPluginSession(pluginIds));
130 }
131 
132 /*
133  * @tc.name: CheckProcess
134  * @tc.desc: test CheckProcess with false case.
135  * @tc.type: FUNC
136  */
137 HWTEST_F(HookManagerTest, CheckProcess, TestSize.Level1)
138 {
139     HookManager hookManager;
140     NativeHookConfig nativeConfig;
141     nativeConfig.set_process_name("HookManagerTest");
142     hookManager.SetHookConfig(nativeConfig);
143     EXPECT_TRUE(hookManager.CheckProcess());
144 
145     nativeConfig.set_startup_mode(true);
146     hookManager.SetHookConfig(nativeConfig);
147     EXPECT_TRUE(hookManager.CheckProcess());
148     hookManager.ResetStartupParam();
149 
150     // native_daemon_ut as a testing process
151     nativeConfig.set_startup_mode(false);
152     nativeConfig.set_process_name("native_daemon_ut");
153     hookManager.SetHookConfig(nativeConfig);
154     EXPECT_TRUE(hookManager.CheckProcess());
155     EXPECT_TRUE(hookManager.CheckProcessName());
156 }
157 
158 } // namespace