106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci 1606f6ba60Sopenharmony_ci#include <gtest/gtest.h> 1706f6ba60Sopenharmony_ci#include <thread> 1806f6ba60Sopenharmony_ci 1906f6ba60Sopenharmony_ci#include "common.h" 2006f6ba60Sopenharmony_ci#include "plugin_module.h" 2106f6ba60Sopenharmony_ci#include "common_types.pb.h" 2206f6ba60Sopenharmony_ci 2306f6ba60Sopenharmony_ciusing namespace testing::ext; 2406f6ba60Sopenharmony_ci 2506f6ba60Sopenharmony_cinamespace { 2606f6ba60Sopenharmony_ciconst std::string SUCCESS_PLUGIN_NAME = "libmemdataplugin.z.so"; 2706f6ba60Sopenharmony_ciconstexpr size_t READ_BUFFER_SIZE = 4 * 1024 * 1024; 2806f6ba60Sopenharmony_ci#if defined(__LP64__) 2906f6ba60Sopenharmony_cistd::string g_testPluginDir("/system/lib64/"); 3006f6ba60Sopenharmony_ci#else 3106f6ba60Sopenharmony_cistd::string g_testPluginDir("/system/lib/"); 3206f6ba60Sopenharmony_ci#endif 3306f6ba60Sopenharmony_ci 3406f6ba60Sopenharmony_ciclass PluginModuleTest : public ::testing::Test { 3506f6ba60Sopenharmony_ciprotected: 3606f6ba60Sopenharmony_ci static constexpr auto TEMP_DELAY = std::chrono::milliseconds(20); 3706f6ba60Sopenharmony_ci static void SetUpTestCase() 3806f6ba60Sopenharmony_ci { 3906f6ba60Sopenharmony_ci#if defined(__i386__) || defined(__x86_64__) 4006f6ba60Sopenharmony_ci char pluginDir[PATH_MAX + 1] = {0}; 4106f6ba60Sopenharmony_ci if (readlink("/proc/self/exe", pluginDir, PATH_MAX) > 0) { 4206f6ba60Sopenharmony_ci char* pos = strrchr(pluginDir, '/'); 4306f6ba60Sopenharmony_ci if (pos != nullptr) { 4406f6ba60Sopenharmony_ci *(pos++) = '\0'; 4506f6ba60Sopenharmony_ci g_testPluginDir = pluginDir; 4606f6ba60Sopenharmony_ci } 4706f6ba60Sopenharmony_ci } 4806f6ba60Sopenharmony_ci#endif 4906f6ba60Sopenharmony_ci 5006f6ba60Sopenharmony_ci std::this_thread::sleep_for(TEMP_DELAY); 5106f6ba60Sopenharmony_ci } 5206f6ba60Sopenharmony_ci 5306f6ba60Sopenharmony_ci static void TearDownTestCase() {} 5406f6ba60Sopenharmony_ci}; 5506f6ba60Sopenharmony_ci 5606f6ba60Sopenharmony_ci/** 5706f6ba60Sopenharmony_ci * @tc.name: plugin 5806f6ba60Sopenharmony_ci * @tc.desc: pluginmodule normal test. 5906f6ba60Sopenharmony_ci * @tc.type: FUNC 6006f6ba60Sopenharmony_ci */ 6106f6ba60Sopenharmony_ciHWTEST_F(PluginModuleTest, PluginModuleNormal, TestSize.Level1) 6206f6ba60Sopenharmony_ci{ 6306f6ba60Sopenharmony_ci std::string pluginPath = g_testPluginDir + "/" + SUCCESS_PLUGIN_NAME; 6406f6ba60Sopenharmony_ci PluginModuleInfo info; 6506f6ba60Sopenharmony_ci auto plugin = std::make_shared<PluginModule>(pluginPath); 6606f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->Load()); 6706f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->BindFunctions()); 6806f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->GetInfo(info)); 6906f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->IsLoaded()); 7006f6ba60Sopenharmony_ci 7106f6ba60Sopenharmony_ci uint32_t size = 0; 7206f6ba60Sopenharmony_ci plugin->GetBufferSizeHint(size); 7306f6ba60Sopenharmony_ci EXPECT_EQ(size, READ_BUFFER_SIZE); 7406f6ba60Sopenharmony_ci std::string name; 7506f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->GetPluginName(name)); 7606f6ba60Sopenharmony_ci EXPECT_STREQ(name.c_str(), "memory-plugin"); 7706f6ba60Sopenharmony_ci 7806f6ba60Sopenharmony_ci const uint8_t configData[] = {74, 1, 10}; 7906f6ba60Sopenharmony_ci ProfilerPluginConfig config; 8006f6ba60Sopenharmony_ci config.set_name(g_testPluginDir + "/" + SUCCESS_PLUGIN_NAME); 8106f6ba60Sopenharmony_ci config.set_config_data((const void*)configData, 3); 8206f6ba60Sopenharmony_ci config.set_sample_interval(1000); 8306f6ba60Sopenharmony_ci plugin->SetConfigData(config.config_data()); 8406f6ba60Sopenharmony_ci 8506f6ba60Sopenharmony_ci std::string cfgData = plugin->GetConfigData(); 8606f6ba60Sopenharmony_ci EXPECT_EQ(cfgData.c_str()[0], 74); 8706f6ba60Sopenharmony_ci EXPECT_EQ(cfgData.c_str()[1], 1); 8806f6ba60Sopenharmony_ci EXPECT_EQ(cfgData.c_str()[2], 10); 8906f6ba60Sopenharmony_ci 9006f6ba60Sopenharmony_ci std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(size); 9106f6ba60Sopenharmony_ci EXPECT_NE(buffer, nullptr); 9206f6ba60Sopenharmony_ci plugin->SetClockId(COMMON::GetClockId("realtime")); 9306f6ba60Sopenharmony_ci (void)plugin->GetClockId(); 9406f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->GetPath().empty()); 9506f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->StartSession(reinterpret_cast<const uint8_t*>(cfgData.c_str()), cfgData.size())); 9606f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->GetStandaloneFileData()); 9706f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->IsRunning()); 9806f6ba60Sopenharmony_ci EXPECT_NE(plugin->ReportResult(buffer.get(), size), 0); 9906f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->StopSession()); 10006f6ba60Sopenharmony_ci 10106f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->StartSession(nullptr, 0)); 10206f6ba60Sopenharmony_ci EXPECT_EQ(plugin->ReportResult(buffer.get(), size), 0); 10306f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->StopSession()); 10406f6ba60Sopenharmony_ci 10506f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->StartSession(reinterpret_cast<const uint8_t*>(cfgData.c_str()), cfgData.size())); 10606f6ba60Sopenharmony_ci EXPECT_NE(plugin->ReportResult(nullptr, 0), 0); 10706f6ba60Sopenharmony_ci EXPECT_NE(plugin->ReportResult(nullptr, 0), -1); 10806f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->StopSession()); 10906f6ba60Sopenharmony_ci 11006f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->StartSession(nullptr, 0)); 11106f6ba60Sopenharmony_ci EXPECT_EQ(plugin->ReportResult(nullptr, 0), 0); 11206f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->StopSession()); 11306f6ba60Sopenharmony_ci 11406f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->Unload()); 11506f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->IsLoaded()); 11606f6ba60Sopenharmony_ci} 11706f6ba60Sopenharmony_ci 11806f6ba60Sopenharmony_ci/** 11906f6ba60Sopenharmony_ci * @tc.name: plugin 12006f6ba60Sopenharmony_ci * @tc.desc: pluginmodule abnormal test. 12106f6ba60Sopenharmony_ci * @tc.type: FUNC 12206f6ba60Sopenharmony_ci */ 12306f6ba60Sopenharmony_ciHWTEST_F(PluginModuleTest, PluginModuleAbnormal, TestSize.Level1) 12406f6ba60Sopenharmony_ci{ 12506f6ba60Sopenharmony_ci std::string pluginPath = "invalid.z.so"; 12606f6ba60Sopenharmony_ci PluginModuleInfo info; 12706f6ba60Sopenharmony_ci auto plugin = std::make_shared<PluginModule>(pluginPath); 12806f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->Load()); 12906f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->BindFunctions()); 13006f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->GetInfo(info)); 13106f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->IsLoaded()); 13206f6ba60Sopenharmony_ci 13306f6ba60Sopenharmony_ci uint32_t size = 0; 13406f6ba60Sopenharmony_ci plugin->GetBufferSizeHint(size); 13506f6ba60Sopenharmony_ci EXPECT_EQ(size, 0); 13606f6ba60Sopenharmony_ci 13706f6ba60Sopenharmony_ci std::string name; 13806f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->GetPluginName(name)); 13906f6ba60Sopenharmony_ci EXPECT_STREQ(name.c_str(), ""); 14006f6ba60Sopenharmony_ci 14106f6ba60Sopenharmony_ci std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(size); 14206f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->StartSession(nullptr, 0)); 14306f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->GetStandaloneFileData()); 14406f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->IsRunning()); 14506f6ba60Sopenharmony_ci EXPECT_EQ(plugin->ReportResult(buffer.get(), size), -1); 14606f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->StopSession()); 14706f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->Unload()); 14806f6ba60Sopenharmony_ci} 14906f6ba60Sopenharmony_ci 15006f6ba60Sopenharmony_ci/** 15106f6ba60Sopenharmony_ci * @tc.name: plugin 15206f6ba60Sopenharmony_ci * @tc.desc: pluginmodule test. 15306f6ba60Sopenharmony_ci * @tc.type: FUNC 15406f6ba60Sopenharmony_ci */ 15506f6ba60Sopenharmony_ciHWTEST_F(PluginModuleTest, PluginModuleTest, TestSize.Level1) 15606f6ba60Sopenharmony_ci{ 15706f6ba60Sopenharmony_ci std::string pluginPath = g_testPluginDir + "/" + SUCCESS_PLUGIN_NAME; 15806f6ba60Sopenharmony_ci PluginModuleInfo info; 15906f6ba60Sopenharmony_ci auto plugin = std::make_shared<PluginModule>(pluginPath); 16006f6ba60Sopenharmony_ci 16106f6ba60Sopenharmony_ci std::string outFileName; 16206f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->GetOutFileName(outFileName)); 16306f6ba60Sopenharmony_ci std::string pluginVersion; 16406f6ba60Sopenharmony_ci EXPECT_FALSE(plugin->GetPluginVersion(pluginVersion)); 16506f6ba60Sopenharmony_ci EXPECT_EQ(plugin->GetWriter(), nullptr); 16606f6ba60Sopenharmony_ci 16706f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->Load()); 16806f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->BindFunctions()); 16906f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->GetInfo(info)); 17006f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->IsLoaded()); 17106f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->GetOutFileName(outFileName)); 17206f6ba60Sopenharmony_ci EXPECT_EQ(outFileName, ""); 17306f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->GetPluginVersion(pluginVersion)); 17406f6ba60Sopenharmony_ci EXPECT_EQ(pluginVersion, "1.02"); 17506f6ba60Sopenharmony_ci BufferWriterPtr writer; 17606f6ba60Sopenharmony_ci EXPECT_TRUE(plugin->RegisterWriter(writer)); 17706f6ba60Sopenharmony_ci} 17806f6ba60Sopenharmony_ci} // namespace 179