• 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 <google/protobuf/message.h>
17 #include <grpcpp/health_check_service_interface.h>
18 #include <gtest/gtest.h>
19 #include <thread>
20 
21 #include "command_poller.h"
22 #include "grpc/impl/codegen/log.h"
23 #include "logging.h"
24 #include "plugin_manager.h"
25 #include "plugin_service.h"
26 #include "plugin_service.ipc.h"
27 #include "profiler_service.h"
28 #include "socket_context.h"
29 
30 using google::protobuf::Message;
31 using namespace testing::ext;
32 
33 namespace {
34 constexpr int DEFAULT_BUFFER_SIZE = 4096;
35 constexpr int DEFAULT_SLEEP_TIME = 1000;
36 const std::string SUCCESS_PLUGIN_NAME = "libmemdataplugin.z.so";
37 #if defined(__LP64__)
38 std::string g_testPluginDir("/system/lib64/");
39 #else
40 std::string g_testPluginDir("/system/lib/");
41 #endif
42 int g_hiprofilerProcessNum = -1;
43 const std::string DEFAULT_HIPROFILERD_PATH("/system/bin/hiprofilerd");
44 
45 class PluginManagerTest : public ::testing::Test {
46 protected:
47     static constexpr auto TEMP_DELAY = std::chrono::milliseconds(20);
SetUpTestCase()48     static void SetUpTestCase()
49     {
50         if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
51             return;
52         }
53         int processNum = fork();
54         if (processNum == 0) {
55             // start running hiprofilerd
56             execl(DEFAULT_HIPROFILERD_PATH.c_str(), nullptr, nullptr);
57             _exit(1);
58         } else {
59             g_hiprofilerProcessNum = processNum;
60         }
61 
62 #ifdef COVERAGE_TEST
63         const int coverageSleepTime = DEFAULT_SLEEP_TIME * 5; // sleep 5s
64         std::this_thread::sleep_for(std::chrono::milliseconds(coverageSleepTime));
65 #else
66         std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_SLEEP_TIME));
67 #endif
68     }
69 
TearDownTestCase()70     static void TearDownTestCase()
71     {
72         std::string stopCmd = "kill " + std::to_string(g_hiprofilerProcessNum);
73         std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(stopCmd.c_str(), "r"), pclose);
74     }
75 };
76 
77 /**
78  * @tc.name: plugin
79  * @tc.desc: Plug-in normal loading and removal process test.
80  * @tc.type: FUNC
81  */
82 HWTEST_F(PluginManagerTest, SuccessPlugin, TestSize.Level1)
83 {
84     auto pluginManage = std::make_shared<PluginManager>();
85     auto commandPoller = std::make_shared<CommandPoller>(pluginManage);
86     EXPECT_TRUE(commandPoller->OnConnect());
87     pluginManage->SetCommandPoller(commandPoller);
88 
89     const uint8_t configData[] = {0x30, 0x01, 0x38, 0x01, 0x42, 0x01, 0x01};
90     std::string pluginName = "memory-plugin";
91     ProfilerPluginConfig config;
92     const std::vector<uint32_t> pluginIdsVector = {2};
93     config.set_name(pluginName);
94     config.set_config_data((const void*)configData, 7);
95     config.set_sample_interval(DEFAULT_SLEEP_TIME);
96 
97     EXPECT_FALSE(pluginManage->LoadPlugin(pluginName));
98     EXPECT_FALSE(pluginManage->UnloadPlugin(pluginName));
99     EXPECT_TRUE(pluginManage->AddPlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME));
100     EXPECT_FALSE(pluginManage->AddPlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME));
101     EXPECT_TRUE(pluginManage->RemovePlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME));
102     EXPECT_FALSE(pluginManage->RemovePlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME));
103     EXPECT_TRUE(pluginManage->AddPlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME));
104     EXPECT_TRUE(pluginManage->LoadPlugin(pluginName));
105     EXPECT_FALSE(pluginManage->LoadPlugin(pluginName));
106     EXPECT_TRUE(pluginManage->UnloadPlugin(pluginName));
107 
108     EXPECT_TRUE(pluginManage->LoadPlugin(pluginName));
109 
110     std::vector<ProfilerPluginConfig> configVec;
111     PluginResult result;
112     configVec.push_back(config);
113     EXPECT_TRUE(pluginManage->CreatePluginSession(configVec));
114     EXPECT_TRUE(pluginManage->StartPluginSession(pluginIdsVector, configVec, result));
115     std::this_thread::sleep_for(TEMP_DELAY);
116     EXPECT_FALSE(pluginManage->ReportPluginBasicData(pluginIdsVector));
117     EXPECT_TRUE(pluginManage->StopPluginSession(pluginIdsVector));
118     EXPECT_TRUE(pluginManage->DestroyPluginSession(pluginIdsVector));
119 }
120 
121 /**
122  * @tc.name: plugin
123  * @tc.desc: get sample Mode.
124  * @tc.type: FUNC
125  */
126 HWTEST_F(PluginManagerTest, GetSampleMode, TestSize.Level1)
127 {
128     PluginModule pluginModule;
129     if (pluginModule.structPtr_ && pluginModule.structPtr_->callbacks) {
130         if (pluginModule.structPtr_->callbacks->onPluginReportResult != nullptr) {
131             EXPECT_EQ(pluginModule.GetSampleMode(), PluginModule::SampleMode::POLLING);
132         } else if (pluginModule.structPtr_->callbacks->onRegisterWriterStruct != nullptr) {
133             EXPECT_EQ(pluginModule.GetSampleMode(), PluginModule::SampleMode::STREAMING);
134         }
135     }
136     EXPECT_EQ(pluginModule.GetSampleMode(), PluginModule::SampleMode::UNKNOWN);
137 }
138 
139 /**
140  * @tc.name: plugin
141  * @tc.desc: Plug-in data acquisition process test.
142  * @tc.type: FUNC
143  */
144 HWTEST_F(PluginManagerTest, PluginManager, TestSize.Level1)
145 {
146     PluginManager pluginManager;
147     PluginModuleInfo info;
148     EXPECT_FALSE(pluginManager.UnloadPlugin(0));
149     PluginResult pluginResult;
150     EXPECT_FALSE(pluginManager.SubmitResult(pluginResult));
151     EXPECT_FALSE(pluginManager.PullResult(0));
152     EXPECT_FALSE(pluginManager.CreateWriter("", 0, -1, -1));
153     EXPECT_FALSE(pluginManager.ResetWriter(-1));
154 
155     PluginModule pluginModule;
156     EXPECT_EQ(pluginModule.ComputeSha256(), "");
157     EXPECT_FALSE(pluginModule.Unload());
158     EXPECT_FALSE(pluginModule.GetInfo(info));
159     std::string str("memory-plugin");
160     EXPECT_FALSE(pluginModule.GetPluginName(str));
161     uint32_t num = 0;
162     EXPECT_FALSE(pluginModule.GetBufferSizeHint(num));
163     EXPECT_FALSE(pluginModule.IsLoaded());
164 
165     BufferWriter bufferWriter("test", "1.01", DEFAULT_BUFFER_SIZE, -1, -1, 0);
166 
167     EXPECT_EQ(bufferWriter.shareMemoryBlock_, nullptr);
168     EXPECT_FALSE(bufferWriter.Write(str.data(), str.size()));
169     bufferWriter.shareMemoryBlock_ =
170         ShareMemoryAllocator::GetInstance().CreateMemoryBlockLocal("test", DEFAULT_BUFFER_SIZE);
171     EXPECT_TRUE(bufferWriter.Write(str.data(), str.size()));
172     EXPECT_TRUE(bufferWriter.Flush());
173 }
174 } // namespace
175