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