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 <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 std::string g_testPluginDir("/system/lib/"); 39 int g_hiprofilerProcessNum = -1; 40 const std::string DEFAULT_HIPROFILERD_PATH("/system/bin/hiprofilerd"); 41 42 class PluginManagerTest : public ::testing::Test { 43 protected: 44 static constexpr auto TEMP_DELAY = std::chrono::milliseconds(20); SetUpTestCase()45 static void SetUpTestCase() 46 { 47 int processNum = fork(); 48 if (processNum == 0) { 49 // start running hiprofilerd 50 execl(DEFAULT_HIPROFILERD_PATH.c_str(), nullptr, nullptr); 51 _exit(1); 52 } else { 53 g_hiprofilerProcessNum = processNum; 54 } 55 56 std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_SLEEP_TIME)); 57 } 58 TearDownTestCase()59 static void TearDownTestCase() 60 { 61 std::string stopCmd = "kill " + std::to_string(g_hiprofilerProcessNum); 62 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(stopCmd.c_str(), "r"), pclose); 63 } 64 }; 65 66 /** 67 * @tc.name: plugin 68 * @tc.desc: Plug-in normal loading and removal process test. 69 * @tc.type: FUNC 70 */ 71 HWTEST_F(PluginManagerTest, SuccessPlugin, TestSize.Level1) 72 { 73 auto pluginManage = std::make_shared<PluginManager>(); 74 auto commandPoller = std::make_shared<CommandPoller>(pluginManage); 75 EXPECT_TRUE(commandPoller->OnConnect()); 76 pluginManage->SetCommandPoller(commandPoller); 77 78 const uint8_t configData[] = {0x30, 0x01, 0x38, 0x01, 0x42, 0x01, 0x01}; 79 std::string pluginName = "memory-plugin"; 80 ProfilerPluginConfig config; 81 const std::vector<uint32_t> pluginIdsVector = {2}; 82 config.set_name(pluginName); 83 config.set_config_data((const void*)configData, 7); 84 config.set_sample_interval(DEFAULT_SLEEP_TIME); 85 86 EXPECT_FALSE(pluginManage->LoadPlugin(pluginName)); 87 EXPECT_FALSE(pluginManage->UnloadPlugin(pluginName)); 88 EXPECT_TRUE(pluginManage->AddPlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME)); 89 EXPECT_FALSE(pluginManage->AddPlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME)); 90 EXPECT_TRUE(pluginManage->RemovePlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME)); 91 EXPECT_FALSE(pluginManage->RemovePlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME)); 92 EXPECT_TRUE(pluginManage->AddPlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME)); 93 EXPECT_TRUE(pluginManage->LoadPlugin(pluginName)); 94 EXPECT_FALSE(pluginManage->LoadPlugin(pluginName)); 95 EXPECT_TRUE(pluginManage->UnloadPlugin(pluginName)); 96 97 EXPECT_TRUE(pluginManage->LoadPlugin(pluginName)); 98 99 std::vector<ProfilerPluginConfig> configVec; 100 configVec.push_back(config); 101 EXPECT_TRUE(pluginManage->CreatePluginSession(configVec)); 102 EXPECT_TRUE(pluginManage->StartPluginSession(pluginIdsVector, configVec)); 103 std::this_thread::sleep_for(TEMP_DELAY); 104 EXPECT_TRUE(pluginManage->StopPluginSession(pluginIdsVector)); 105 EXPECT_TRUE(pluginManage->DestroyPluginSession(pluginIdsVector)); 106 } 107 108 /** 109 * @tc.name: plugin 110 * @tc.desc: get sample Mode. 111 * @tc.type: FUNC 112 */ 113 HWTEST_F(PluginManagerTest, GetSampleMode, TestSize.Level1) 114 { 115 PluginModule pluginModule; 116 if (pluginModule.structPtr_ && pluginModule.structPtr_->callbacks) { 117 if (pluginModule.structPtr_->callbacks->onPluginReportResult != nullptr) { 118 EXPECT_EQ(pluginModule.GetSampleMode(), PluginModule::SampleMode::POLLING); 119 } else if (pluginModule.structPtr_->callbacks->onRegisterWriterStruct != nullptr) { 120 EXPECT_EQ(pluginModule.GetSampleMode(), PluginModule::SampleMode::STREAMING); 121 } 122 } 123 EXPECT_EQ(pluginModule.GetSampleMode(), PluginModule::SampleMode::UNKNOWN); 124 } 125 126 /** 127 * @tc.name: plugin 128 * @tc.desc: Plug-in data acquisition process test. 129 * @tc.type: FUNC 130 */ 131 HWTEST_F(PluginManagerTest, PluginManager, TestSize.Level1) 132 { 133 PluginManager pluginManager; 134 PluginModuleInfo info; 135 EXPECT_FALSE(pluginManager.UnloadPlugin(0)); 136 PluginResult pluginResult; 137 EXPECT_FALSE(pluginManager.SubmitResult(pluginResult)); 138 EXPECT_FALSE(pluginManager.PullResult(0)); 139 EXPECT_FALSE(pluginManager.CreateWriter("", 0, -1, -1)); 140 EXPECT_FALSE(pluginManager.ResetWriter(-1)); 141 142 PluginModule pluginModule; 143 EXPECT_EQ(pluginModule.ComputeSha256(), ""); 144 EXPECT_FALSE(pluginModule.Unload()); 145 EXPECT_FALSE(pluginModule.GetInfo(info)); 146 std::string str("memory-plugin"); 147 EXPECT_FALSE(pluginModule.GetPluginName(str)); 148 uint32_t num = 0; 149 EXPECT_FALSE(pluginModule.GetBufferSizeHint(num)); 150 EXPECT_FALSE(pluginModule.IsLoaded()); 151 152 BufferWriter bufferWriter("test", DEFAULT_BUFFER_SIZE, -1, -1, 0); 153 154 EXPECT_EQ(bufferWriter.shareMemoryBlock_, nullptr); 155 EXPECT_FALSE(bufferWriter.Write(str.data(), str.size())); 156 bufferWriter.shareMemoryBlock_ = 157 ShareMemoryAllocator::GetInstance().CreateMemoryBlockLocal("test", DEFAULT_BUFFER_SIZE); 158 EXPECT_TRUE(bufferWriter.Write(str.data(), str.size())); 159 EXPECT_TRUE(bufferWriter.Flush()); 160 } 161 } // namespace 162