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