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 "command_poller.h" 1706f6ba60Sopenharmony_ci 1806f6ba60Sopenharmony_ci#include <gtest/gtest.h> 1906f6ba60Sopenharmony_ci 2006f6ba60Sopenharmony_ci#include "plugin_manager.h" 2106f6ba60Sopenharmony_ci#include "plugin_service.ipc.h" 2206f6ba60Sopenharmony_ci#include "socket_context.h" 2306f6ba60Sopenharmony_ci 2406f6ba60Sopenharmony_ciusing namespace testing::ext; 2506f6ba60Sopenharmony_ci 2606f6ba60Sopenharmony_cinamespace { 2706f6ba60Sopenharmony_ciclass PluginManagerStub final : public ManagerInterface { 2806f6ba60Sopenharmony_cipublic: 2906f6ba60Sopenharmony_ci bool LoadPlugin(const std::string& pluginPath) override 3006f6ba60Sopenharmony_ci { 3106f6ba60Sopenharmony_ci if (pluginPath == "existplugin") { 3206f6ba60Sopenharmony_ci return true; 3306f6ba60Sopenharmony_ci } else if (pluginPath == "noexistplugin") { 3406f6ba60Sopenharmony_ci return false; 3506f6ba60Sopenharmony_ci } 3606f6ba60Sopenharmony_ci return true; 3706f6ba60Sopenharmony_ci } 3806f6ba60Sopenharmony_ci bool UnloadPlugin(const std::string& pluginPath) override 3906f6ba60Sopenharmony_ci { 4006f6ba60Sopenharmony_ci if (pluginPath == "existplugin") { 4106f6ba60Sopenharmony_ci return true; 4206f6ba60Sopenharmony_ci } else if (pluginPath == "noexistplugin") { 4306f6ba60Sopenharmony_ci return false; 4406f6ba60Sopenharmony_ci } 4506f6ba60Sopenharmony_ci return true; 4606f6ba60Sopenharmony_ci } 4706f6ba60Sopenharmony_ci bool UnloadPlugin(const uint32_t pluginId) override 4806f6ba60Sopenharmony_ci { 4906f6ba60Sopenharmony_ci if (pluginId == 0) { 5006f6ba60Sopenharmony_ci return false; 5106f6ba60Sopenharmony_ci } 5206f6ba60Sopenharmony_ci return true; 5306f6ba60Sopenharmony_ci } 5406f6ba60Sopenharmony_ci 5506f6ba60Sopenharmony_ci bool CreatePluginSession(const std::vector<ProfilerPluginConfig>& config) override 5606f6ba60Sopenharmony_ci { 5706f6ba60Sopenharmony_ci if (config[0].name() == "existplugin") { 5806f6ba60Sopenharmony_ci return true; 5906f6ba60Sopenharmony_ci } else if (config[0].name() == "noexistplugin") { 6006f6ba60Sopenharmony_ci return false; 6106f6ba60Sopenharmony_ci } 6206f6ba60Sopenharmony_ci return true; 6306f6ba60Sopenharmony_ci } 6406f6ba60Sopenharmony_ci bool DestroyPluginSession(const std::vector<uint32_t>& pluginIds) override 6506f6ba60Sopenharmony_ci { 6606f6ba60Sopenharmony_ci if (pluginIds[0] != 1) { 6706f6ba60Sopenharmony_ci return false; 6806f6ba60Sopenharmony_ci } 6906f6ba60Sopenharmony_ci return true; 7006f6ba60Sopenharmony_ci } 7106f6ba60Sopenharmony_ci bool StartPluginSession(const std::vector<uint32_t>& pluginIds, const std::vector<ProfilerPluginConfig>& config, 7206f6ba60Sopenharmony_ci PluginResult& result) override 7306f6ba60Sopenharmony_ci { 7406f6ba60Sopenharmony_ci if (pluginIds[0] == 0) { 7506f6ba60Sopenharmony_ci return false; 7606f6ba60Sopenharmony_ci } 7706f6ba60Sopenharmony_ci 7806f6ba60Sopenharmony_ci if (config[0].name() == "existplugin") { 7906f6ba60Sopenharmony_ci return true; 8006f6ba60Sopenharmony_ci } else if (config[0].name() == "noexistplugin") { 8106f6ba60Sopenharmony_ci return false; 8206f6ba60Sopenharmony_ci } 8306f6ba60Sopenharmony_ci return true; 8406f6ba60Sopenharmony_ci } 8506f6ba60Sopenharmony_ci bool StopPluginSession(const std::vector<uint32_t>& pluginIds) override 8606f6ba60Sopenharmony_ci { 8706f6ba60Sopenharmony_ci if (pluginIds[0] == 0) { 8806f6ba60Sopenharmony_ci return false; 8906f6ba60Sopenharmony_ci } 9006f6ba60Sopenharmony_ci return true; 9106f6ba60Sopenharmony_ci } 9206f6ba60Sopenharmony_ci 9306f6ba60Sopenharmony_ci bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd, 9406f6ba60Sopenharmony_ci bool isProtobufSerialize = true) override 9506f6ba60Sopenharmony_ci { 9606f6ba60Sopenharmony_ci if (bufferSize == 0) { 9706f6ba60Sopenharmony_ci return false; 9806f6ba60Sopenharmony_ci } 9906f6ba60Sopenharmony_ci return true; 10006f6ba60Sopenharmony_ci } 10106f6ba60Sopenharmony_ci bool ResetWriter(uint32_t pluginId) override 10206f6ba60Sopenharmony_ci { 10306f6ba60Sopenharmony_ci if (pluginId == 0) { 10406f6ba60Sopenharmony_ci return false; 10506f6ba60Sopenharmony_ci } 10606f6ba60Sopenharmony_ci return true; 10706f6ba60Sopenharmony_ci } 10806f6ba60Sopenharmony_ci void SetCommandPoller(const std::shared_ptr<CommandPoller>& p) override 10906f6ba60Sopenharmony_ci { 11006f6ba60Sopenharmony_ci this->commandPoller_ = p; 11106f6ba60Sopenharmony_ci } 11206f6ba60Sopenharmony_ci 11306f6ba60Sopenharmony_ci bool ReportPluginBasicData(const std::vector<uint32_t>& pluginIds) override 11406f6ba60Sopenharmony_ci { 11506f6ba60Sopenharmony_ci return true; 11606f6ba60Sopenharmony_ci } 11706f6ba60Sopenharmony_ci 11806f6ba60Sopenharmony_ciprivate: 11906f6ba60Sopenharmony_ci CommandPollerPtr commandPoller_; 12006f6ba60Sopenharmony_ci}; 12106f6ba60Sopenharmony_ci 12206f6ba60Sopenharmony_ciclass CommandPollerTest : public ::testing::Test { 12306f6ba60Sopenharmony_ciprotected: 12406f6ba60Sopenharmony_ci static void SetUpTestCase() {} 12506f6ba60Sopenharmony_ci static void TearDownTestCase() {} 12606f6ba60Sopenharmony_ci}; 12706f6ba60Sopenharmony_ci 12806f6ba60Sopenharmony_ciHWTEST_F(CommandPollerTest, CreateCmdTest, TestSize.Level1) 12906f6ba60Sopenharmony_ci{ 13006f6ba60Sopenharmony_ci auto pluginManage = std::make_shared<PluginManagerStub>(); 13106f6ba60Sopenharmony_ci auto commandPoller = std::make_shared<CommandPoller>(pluginManage); 13206f6ba60Sopenharmony_ci pluginManage->SetCommandPoller(commandPoller); 13306f6ba60Sopenharmony_ci 13406f6ba60Sopenharmony_ci CreateSessionCmd successCmd; 13506f6ba60Sopenharmony_ci CreateSessionCmd failed1Cmd; 13606f6ba60Sopenharmony_ci CreateSessionCmd failed2Cmd; 13706f6ba60Sopenharmony_ci CreateSessionCmd failed3Cmd; 13806f6ba60Sopenharmony_ci SocketContext ctx; 13906f6ba60Sopenharmony_ci 14006f6ba60Sopenharmony_ci successCmd.add_buffer_sizes(1024); 14106f6ba60Sopenharmony_ci successCmd.add_plugin_configs()->set_name("existplugin"); 14206f6ba60Sopenharmony_ci 14306f6ba60Sopenharmony_ci failed1Cmd.add_buffer_sizes(0); 14406f6ba60Sopenharmony_ci failed1Cmd.add_plugin_configs()->set_name("existplugin"); 14506f6ba60Sopenharmony_ci 14606f6ba60Sopenharmony_ci failed2Cmd.add_buffer_sizes(0); 14706f6ba60Sopenharmony_ci failed2Cmd.add_plugin_configs()->set_name("noexistplugin"); 14806f6ba60Sopenharmony_ci 14906f6ba60Sopenharmony_ci failed3Cmd.add_buffer_sizes(1); 15006f6ba60Sopenharmony_ci failed3Cmd.add_plugin_configs()->set_name("noexistplugin"); 15106f6ba60Sopenharmony_ci EXPECT_TRUE(commandPoller->OnCreateSessionCmd(successCmd, ctx)); 15206f6ba60Sopenharmony_ci EXPECT_FALSE(commandPoller->OnCreateSessionCmd(failed1Cmd, ctx)); 15306f6ba60Sopenharmony_ci EXPECT_FALSE(commandPoller->OnCreateSessionCmd(failed2Cmd, ctx)); 15406f6ba60Sopenharmony_ci EXPECT_FALSE(commandPoller->OnCreateSessionCmd(failed3Cmd, ctx)); 15506f6ba60Sopenharmony_ci} 15606f6ba60Sopenharmony_ci 15706f6ba60Sopenharmony_ciHWTEST_F(CommandPollerTest, StartCmdTest, TestSize.Level1) 15806f6ba60Sopenharmony_ci{ 15906f6ba60Sopenharmony_ci auto pluginManage = std::make_shared<PluginManagerStub>(); 16006f6ba60Sopenharmony_ci auto commandPoller = std::make_shared<CommandPoller>(pluginManage); 16106f6ba60Sopenharmony_ci pluginManage->SetCommandPoller(commandPoller); 16206f6ba60Sopenharmony_ci 16306f6ba60Sopenharmony_ci StartSessionCmd successCmd; 16406f6ba60Sopenharmony_ci successCmd.add_plugin_ids(1); 16506f6ba60Sopenharmony_ci successCmd.add_plugin_configs()->set_name("existplugin"); 16606f6ba60Sopenharmony_ci StartSessionCmd failed1Cmd; 16706f6ba60Sopenharmony_ci 16806f6ba60Sopenharmony_ci failed1Cmd.add_plugin_ids(0); 16906f6ba60Sopenharmony_ci failed1Cmd.add_plugin_configs()->set_name("existplugin"); 17006f6ba60Sopenharmony_ci 17106f6ba60Sopenharmony_ci StartSessionCmd failed2Cmd; 17206f6ba60Sopenharmony_ci failed2Cmd.add_plugin_ids(1); 17306f6ba60Sopenharmony_ci failed2Cmd.add_plugin_configs()->set_name("noexistplugin"); 17406f6ba60Sopenharmony_ci 17506f6ba60Sopenharmony_ci PluginResult result; 17606f6ba60Sopenharmony_ci EXPECT_TRUE(commandPoller->OnStartSessionCmd(successCmd, result)); 17706f6ba60Sopenharmony_ci EXPECT_FALSE(commandPoller->OnStartSessionCmd(failed1Cmd, result)); 17806f6ba60Sopenharmony_ci EXPECT_FALSE(commandPoller->OnStartSessionCmd(failed2Cmd, result)); 17906f6ba60Sopenharmony_ci} 18006f6ba60Sopenharmony_ci 18106f6ba60Sopenharmony_ciHWTEST_F(CommandPollerTest, StopCmdTest, TestSize.Level1) 18206f6ba60Sopenharmony_ci{ 18306f6ba60Sopenharmony_ci auto pluginManage = std::make_shared<PluginManagerStub>(); 18406f6ba60Sopenharmony_ci auto commandPoller = std::make_shared<CommandPoller>(pluginManage); 18506f6ba60Sopenharmony_ci pluginManage->SetCommandPoller(commandPoller); 18606f6ba60Sopenharmony_ci 18706f6ba60Sopenharmony_ci StopSessionCmd successCmd; 18806f6ba60Sopenharmony_ci successCmd.add_plugin_ids(1); 18906f6ba60Sopenharmony_ci StopSessionCmd failedCmd; 19006f6ba60Sopenharmony_ci failedCmd.add_plugin_ids(0); 19106f6ba60Sopenharmony_ci EXPECT_TRUE(commandPoller->OnStopSessionCmd(successCmd)); 19206f6ba60Sopenharmony_ci EXPECT_FALSE(commandPoller->OnStopSessionCmd(failedCmd)); 19306f6ba60Sopenharmony_ci} 19406f6ba60Sopenharmony_ci 19506f6ba60Sopenharmony_ciHWTEST_F(CommandPollerTest, DestoryCmdTest, TestSize.Level1) 19606f6ba60Sopenharmony_ci{ 19706f6ba60Sopenharmony_ci auto pluginManage = std::make_shared<PluginManagerStub>(); 19806f6ba60Sopenharmony_ci auto commandPoller = std::make_shared<CommandPoller>(pluginManage); 19906f6ba60Sopenharmony_ci pluginManage->SetCommandPoller(commandPoller); 20006f6ba60Sopenharmony_ci DestroySessionCmd successCmd; 20106f6ba60Sopenharmony_ci DestroySessionCmd failed1Cmd; 20206f6ba60Sopenharmony_ci DestroySessionCmd failed2Cmd; 20306f6ba60Sopenharmony_ci DestroySessionCmd failed3Cmd; 20406f6ba60Sopenharmony_ci successCmd.add_plugin_ids(1); 20506f6ba60Sopenharmony_ci failed1Cmd.add_plugin_ids(0); 20606f6ba60Sopenharmony_ci failed2Cmd.add_plugin_ids(2); 20706f6ba60Sopenharmony_ci failed3Cmd.add_plugin_ids(3); 20806f6ba60Sopenharmony_ci EXPECT_TRUE(commandPoller->OnDestroySessionCmd(successCmd)); 20906f6ba60Sopenharmony_ci EXPECT_FALSE(commandPoller->OnDestroySessionCmd(failed1Cmd)); 21006f6ba60Sopenharmony_ci EXPECT_FALSE(commandPoller->OnDestroySessionCmd(failed2Cmd)); 21106f6ba60Sopenharmony_ci EXPECT_FALSE(commandPoller->OnDestroySessionCmd(failed3Cmd)); 21206f6ba60Sopenharmony_ci} 21306f6ba60Sopenharmony_ci} // namespace