• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gmock/gmock.h>
17 #include <hwext/gtest-ext.h>
18 #include <hwext/gtest-tag.h>
19 
20 #include "command_poller.h"
21 
22 #include "plugin_manager.h"
23 #include "plugin_service.ipc.h"
24 #include "socket_context.h"
25 
26 using namespace testing::ext;
27 
28 namespace {
29 class PluginManagerStub final : public ManagerInterface {
30 public:
LoadPlugin(const std::string & pluginPath)31     bool LoadPlugin(const std::string& pluginPath) override
32     {
33         if (pluginPath == "existplugin") {
34             return true;
35         } else if (pluginPath == "noexistplugin") {
36             return false;
37         }
38         return true;
39     }
UnloadPlugin(const std::string & pluginPath)40     virtual bool UnloadPlugin(const std::string& pluginPath)
41     {
42         if (pluginPath == "existplugin") {
43             return true;
44         } else if (pluginPath == "noexistplugin") {
45             return false;
46         }
47         return true;
48     }
UnloadPlugin(const uint32_t pluginId)49     virtual bool UnloadPlugin(const uint32_t pluginId)
50     {
51         if (pluginId == 0) {
52             return false;
53         }
54         return true;
55     }
56 
CreatePluginSession(const std::vector<ProfilerPluginConfig> & config)57     virtual bool CreatePluginSession(const std::vector<ProfilerPluginConfig>& config)
58     {
59         if (config[0].name() == "existplugin") {
60             return true;
61         } else if (config[0].name() == "noexistplugin") {
62             return false;
63         }
64         return true;
65     }
DestroyPluginSession(const std::vector<uint32_t> & pluginIds)66     virtual bool DestroyPluginSession(const std::vector<uint32_t>& pluginIds)
67     {
68         if (pluginIds[0] != 1) {
69             return false;
70         }
71         return true;
72     }
StartPluginSession(const std::vector<uint32_t> & pluginIds,const std::vector<ProfilerPluginConfig> & config,PluginResult & result)73     virtual bool StartPluginSession(const std::vector<uint32_t>& pluginIds,
74                                         const std::vector<ProfilerPluginConfig>& config, PluginResult& result)
75     {
76         if (pluginIds[0] == 0) {
77             return false;
78         }
79 
80         if (config[0].name() == "existplugin") {
81             return true;
82         } else if (config[0].name() == "noexistplugin") {
83             return false;
84         }
85         return true;
86     }
StopPluginSession(const std::vector<uint32_t> & pluginIds)87     virtual bool StopPluginSession(const std::vector<uint32_t>& pluginIds)
88     {
89         if (pluginIds[0] == 0) {
90             return false;
91         }
92         return true;
93     }
94 
CreateWriter(std::string pluginName,uint32_t bufferSize,int smbFd,int eventFd,bool isProtobufSerialize=true)95     virtual bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd,
96                               bool isProtobufSerialize = true)
97     {
98         if (bufferSize == 0) {
99             return false;
100         }
101         return true;
102     }
ResetWriter(uint32_t pluginId)103     virtual bool ResetWriter(uint32_t pluginId)
104     {
105         if (pluginId == 0) {
106             return false;
107         }
108         return true;
109     }
SetCommandPoller(const std::shared_ptr<CommandPoller> & p)110     virtual void SetCommandPoller(const std::shared_ptr<CommandPoller>& p)
111     {
112         this->commandPoller_ = p;
113     }
114 
ReportPluginBasicData(const std::vector<uint32_t> & pluginIds)115     virtual bool ReportPluginBasicData(const std::vector<uint32_t>& pluginIds)
116     {
117         return true;
118     }
119 private:
120     CommandPollerPtr commandPoller_;
121 };
122 
123 
124 class CommandPollerTest : public ::testing::Test {
125 protected:
SetUpTestCase()126     static void SetUpTestCase() {}
TearDownTestCase()127     static void TearDownTestCase() {}
128 };
129 
130 HWTEST_F(CommandPollerTest, CreateCmdTest, TestSize.Level1)
131 {
132     auto pluginManage = std::make_shared<PluginManagerStub>();
133     auto commandPoller = std::make_shared<CommandPoller>(pluginManage);
134     pluginManage->SetCommandPoller(commandPoller);
135 
136     CreateSessionCmd successCmd;
137     CreateSessionCmd failed1Cmd;
138     CreateSessionCmd failed2Cmd;
139     CreateSessionCmd failed3Cmd;
140     SocketContext ctx;
141 
142     successCmd.add_buffer_sizes(1024);
143     successCmd.add_plugin_configs()->set_name("existplugin");
144 
145     failed1Cmd.add_buffer_sizes(0);
146     failed1Cmd.add_plugin_configs()->set_name("existplugin");
147 
148     failed2Cmd.add_buffer_sizes(0);
149     failed2Cmd.add_plugin_configs()->set_name("noexistplugin");
150 
151     failed3Cmd.add_buffer_sizes(1);
152     failed3Cmd.add_plugin_configs()->set_name("noexistplugin");
153     EXPECT_TRUE(commandPoller->OnCreateSessionCmd(successCmd, ctx));
154     EXPECT_FALSE(commandPoller->OnCreateSessionCmd(failed1Cmd, ctx));
155     EXPECT_FALSE(commandPoller->OnCreateSessionCmd(failed2Cmd, ctx));
156     EXPECT_FALSE(commandPoller->OnCreateSessionCmd(failed3Cmd, ctx));
157 }
158 
159 HWTEST_F(CommandPollerTest, StartCmdTest, TestSize.Level1)
160 {
161     auto pluginManage = std::make_shared<PluginManagerStub>();
162     auto commandPoller = std::make_shared<CommandPoller>(pluginManage);
163     pluginManage->SetCommandPoller(commandPoller);
164 
165     StartSessionCmd successCmd;
166     successCmd.add_plugin_ids(1);
167     successCmd.add_plugin_configs()->set_name("existplugin");
168     StartSessionCmd failed1Cmd;
169 
170     failed1Cmd.add_plugin_ids(0);
171     failed1Cmd.add_plugin_configs()->set_name("existplugin");
172 
173     StartSessionCmd failed2Cmd;
174     failed2Cmd.add_plugin_ids(1);
175     failed2Cmd.add_plugin_configs()->set_name("noexistplugin");
176 
177     PluginResult result;
178     EXPECT_TRUE(commandPoller->OnStartSessionCmd(successCmd, result));
179     EXPECT_FALSE(commandPoller->OnStartSessionCmd(failed1Cmd, result));
180     EXPECT_FALSE(commandPoller->OnStartSessionCmd(failed2Cmd, result));
181 }
182 
183 HWTEST_F(CommandPollerTest, StopCmdTest, TestSize.Level1)
184 {
185     auto pluginManage = std::make_shared<PluginManagerStub>();
186     auto commandPoller = std::make_shared<CommandPoller>(pluginManage);
187     pluginManage->SetCommandPoller(commandPoller);
188 
189     StopSessionCmd successCmd;
190     successCmd.add_plugin_ids(1);
191     StopSessionCmd failedCmd;
192     failedCmd.add_plugin_ids(0);
193     EXPECT_TRUE(commandPoller->OnStopSessionCmd(successCmd));
194     EXPECT_FALSE(commandPoller->OnStopSessionCmd(failedCmd));
195 }
196 
197 HWTEST_F(CommandPollerTest, DestoryCmdTest, TestSize.Level1)
198 {
199     auto pluginManage = std::make_shared<PluginManagerStub>();
200     auto commandPoller = std::make_shared<CommandPoller>(pluginManage);
201     pluginManage->SetCommandPoller(commandPoller);
202     DestroySessionCmd successCmd;
203     DestroySessionCmd failed1Cmd;
204     DestroySessionCmd failed2Cmd;
205     DestroySessionCmd failed3Cmd;
206     successCmd.add_plugin_ids(1);
207     failed1Cmd.add_plugin_ids(0);
208     failed2Cmd.add_plugin_ids(2);
209     failed3Cmd.add_plugin_ids(3);
210     EXPECT_TRUE(commandPoller->OnDestroySessionCmd(successCmd));
211     EXPECT_FALSE(commandPoller->OnDestroySessionCmd(failed1Cmd));
212     EXPECT_FALSE(commandPoller->OnDestroySessionCmd(failed2Cmd));
213     EXPECT_FALSE(commandPoller->OnDestroySessionCmd(failed3Cmd));
214 }
215 }