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 "parameters.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 44 class PluginManagerTest : public ::testing::Test { 45 protected: 46 static constexpr auto TEMP_DELAY = std::chrono::milliseconds(20); SetUpTestCase()47 static void SetUpTestCase() 48 { 49 OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "1"); 50 #ifdef COVERAGE_TEST 51 const int coverageSleepTime = DEFAULT_SLEEP_TIME * 5; // sleep 5s 52 std::this_thread::sleep_for(std::chrono::milliseconds(coverageSleepTime)); 53 #else 54 std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_SLEEP_TIME)); 55 #endif 56 } 57 TearDownTestCase()58 static void TearDownTestCase() 59 { 60 OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "0"); 61 } 62 }; 63 64 /** 65 * @tc.name: plugin 66 * @tc.desc: Plug-in normal loading and removal process test. 67 * @tc.type: FUNC 68 */ 69 HWTEST_F(PluginManagerTest, SuccessPlugin, TestSize.Level1) 70 { 71 auto pluginManage = std::make_shared<PluginManager>(); 72 auto commandPoller = std::make_shared<CommandPoller>(pluginManage); 73 EXPECT_TRUE(commandPoller->OnConnect()); 74 pluginManage->SetCommandPoller(commandPoller); 75 76 const uint8_t configData[] = {0x30, 0x01, 0x38, 0x01, 0x42, 0x01, 0x01}; 77 std::string pluginName = "memory-plugin"; 78 ProfilerPluginConfig config; 79 const std::vector<uint32_t> pluginIdsVector = {2}; 80 config.set_name(pluginName); 81 config.set_config_data((const void*)configData, 7); 82 config.set_sample_interval(DEFAULT_SLEEP_TIME); 83 84 EXPECT_FALSE(pluginManage->LoadPlugin(pluginName)); 85 EXPECT_FALSE(pluginManage->UnloadPlugin(pluginName)); 86 EXPECT_TRUE(pluginManage->AddPlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME)); 87 EXPECT_FALSE(pluginManage->AddPlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME)); 88 EXPECT_TRUE(pluginManage->RemovePlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME)); 89 EXPECT_FALSE(pluginManage->RemovePlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME)); 90 EXPECT_TRUE(pluginManage->AddPlugin(g_testPluginDir + SUCCESS_PLUGIN_NAME)); 91 EXPECT_TRUE(pluginManage->LoadPlugin(pluginName)); 92 EXPECT_FALSE(pluginManage->LoadPlugin(pluginName)); 93 EXPECT_TRUE(pluginManage->UnloadPlugin(pluginName)); 94 95 EXPECT_TRUE(pluginManage->LoadPlugin(pluginName)); 96 97 std::vector<ProfilerPluginConfig> configVec; 98 PluginResult result; 99 configVec.push_back(config); 100 EXPECT_TRUE(pluginManage->CreatePluginSession(configVec)); 101 EXPECT_TRUE(pluginManage->StartPluginSession(pluginIdsVector, configVec, result)); 102 std::this_thread::sleep_for(TEMP_DELAY); 103 EXPECT_FALSE(pluginManage->ReportPluginBasicData(pluginIdsVector)); 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", "1.01", 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 162 /** 163 * @tc.name: plugin 164 * @tc.desc: network-profiler normal loading and StopPluginSession test. 165 * @tc.type: FUNC 166 */ 167 HWTEST_F(PluginManagerTest, networkprofilerPlugin, TestSize.Level1) 168 { 169 using namespace OHOS::Developtools::Profiler; 170 auto pluginManage = std::make_shared<PluginManager>(); 171 std::string pluginName = "network-profiler"; 172 ProfilerPluginConfig config; 173 const uint8_t configData[] = {0x55, 0xAA, 0x55, 0xAA}; 174 const std::vector<uint32_t> pluginIdsVector = {2}; 175 config.set_name(pluginName); 176 config.set_config_data((const void*)configData, 4); 177 config.set_sample_interval(DEFAULT_SLEEP_TIME); 178 std::shared_ptr<NetworkProfilerManager> networkProfilerMgr = std::make_shared<NetworkProfilerManager>(); 179 networkProfilerMgr->Init(); 180 pluginManage->AddNetworkProfilerManager(networkProfilerMgr); 181 182 std::vector<ProfilerPluginConfig> configVec; 183 PluginResult result; 184 configVec.push_back(config); 185 186 EXPECT_FALSE(pluginManage->LoadPlugin(pluginName)); 187 pluginManage->AddPlugin(g_testPluginDir + "libnetwork_profiler.z.so"); 188 EXPECT_FALSE(pluginManage->CreatePluginSession(configVec)); 189 EXPECT_FALSE(pluginManage->StartPluginSession(pluginIdsVector, configVec, result)); 190 std::this_thread::sleep_for(TEMP_DELAY); 191 networkProfilerMgr->StartNetworkProfiler(); 192 EXPECT_FALSE(pluginManage->ReportPluginBasicData(pluginIdsVector)); 193 EXPECT_FALSE(pluginManage->StopPluginSession(pluginIdsVector)); 194 } 195 } // namespace 196