• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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)95     virtual bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd)
96     {
97         if (bufferSize == 0) {
98             return false;
99         }
100         return true;
101     }
ResetWriter(uint32_t pluginId)102     virtual bool ResetWriter(uint32_t pluginId)
103     {
104         if (pluginId == 0) {
105             return false;
106         }
107         return true;
108     }
SetCommandPoller(const std::shared_ptr<CommandPoller> & p)109     virtual void SetCommandPoller(const std::shared_ptr<CommandPoller>& p)
110     {
111         this->commandPoller_ = p;
112     }
113 
ReportPluginBasicData(const std::vector<uint32_t> & pluginIds)114     virtual bool ReportPluginBasicData(const std::vector<uint32_t>& pluginIds)
115     {
116         return true;
117     }
118 private:
119     CommandPollerPtr commandPoller_;
120 };
121 
122 
123 class CommandPollerTest : public ::testing::Test {
124 protected:
SetUpTestCase()125     static void SetUpTestCase() {}
TearDownTestCase()126     static void TearDownTestCase() {}
127 };
128 
129 HWTEST_F(CommandPollerTest, CreateCmdTest, TestSize.Level1)
130 {
131     auto pluginManage = std::make_shared<PluginManagerStub>();
132     auto commandPoller = std::make_shared<CommandPoller>(pluginManage);
133     pluginManage->SetCommandPoller(commandPoller);
134 
135     CreateSessionCmd successCmd;
136     CreateSessionCmd failed1Cmd;
137     CreateSessionCmd failed2Cmd;
138     CreateSessionCmd failed3Cmd;
139     SocketContext ctx;
140 
141     successCmd.add_buffer_sizes(1024);
142     successCmd.add_plugin_configs()->set_name("existplugin");
143 
144     failed1Cmd.add_buffer_sizes(0);
145     failed1Cmd.add_plugin_configs()->set_name("existplugin");
146 
147     failed2Cmd.add_buffer_sizes(0);
148     failed2Cmd.add_plugin_configs()->set_name("noexistplugin");
149 
150     failed3Cmd.add_buffer_sizes(1);
151     failed3Cmd.add_plugin_configs()->set_name("noexistplugin");
152     EXPECT_TRUE(commandPoller->OnCreateSessionCmd(successCmd, ctx));
153     EXPECT_FALSE(commandPoller->OnCreateSessionCmd(failed1Cmd, ctx));
154     EXPECT_FALSE(commandPoller->OnCreateSessionCmd(failed2Cmd, ctx));
155     EXPECT_FALSE(commandPoller->OnCreateSessionCmd(failed3Cmd, ctx));
156 }
157 
158 HWTEST_F(CommandPollerTest, StartCmdTest, TestSize.Level1)
159 {
160     auto pluginManage = std::make_shared<PluginManagerStub>();
161     auto commandPoller = std::make_shared<CommandPoller>(pluginManage);
162     pluginManage->SetCommandPoller(commandPoller);
163 
164     StartSessionCmd successCmd;
165     successCmd.add_plugin_ids(1);
166     successCmd.add_plugin_configs()->set_name("existplugin");
167     StartSessionCmd failed1Cmd;
168 
169     failed1Cmd.add_plugin_ids(0);
170     failed1Cmd.add_plugin_configs()->set_name("existplugin");
171 
172     StartSessionCmd failed2Cmd;
173     failed2Cmd.add_plugin_ids(1);
174     failed2Cmd.add_plugin_configs()->set_name("noexistplugin");
175 
176     PluginResult result;
177     EXPECT_TRUE(commandPoller->OnStartSessionCmd(successCmd, result));
178     EXPECT_FALSE(commandPoller->OnStartSessionCmd(failed1Cmd, result));
179     EXPECT_FALSE(commandPoller->OnStartSessionCmd(failed2Cmd, result));
180 }
181 
182 HWTEST_F(CommandPollerTest, StopCmdTest, TestSize.Level1)
183 {
184     auto pluginManage = std::make_shared<PluginManagerStub>();
185     auto commandPoller = std::make_shared<CommandPoller>(pluginManage);
186     pluginManage->SetCommandPoller(commandPoller);
187 
188     StopSessionCmd successCmd;
189     successCmd.add_plugin_ids(1);
190     StopSessionCmd failedCmd;
191     failedCmd.add_plugin_ids(0);
192     EXPECT_TRUE(commandPoller->OnStopSessionCmd(successCmd));
193     EXPECT_FALSE(commandPoller->OnStopSessionCmd(failedCmd));
194 }
195 
196 HWTEST_F(CommandPollerTest, DestoryCmdTest, TestSize.Level1)
197 {
198     auto pluginManage = std::make_shared<PluginManagerStub>();
199     auto commandPoller = std::make_shared<CommandPoller>(pluginManage);
200     pluginManage->SetCommandPoller(commandPoller);
201     DestroySessionCmd successCmd;
202     DestroySessionCmd failed1Cmd;
203     DestroySessionCmd failed2Cmd;
204     DestroySessionCmd failed3Cmd;
205     successCmd.add_plugin_ids(1);
206     failed1Cmd.add_plugin_ids(0);
207     failed2Cmd.add_plugin_ids(2);
208     failed3Cmd.add_plugin_ids(3);
209     EXPECT_TRUE(commandPoller->OnDestroySessionCmd(successCmd));
210     EXPECT_FALSE(commandPoller->OnDestroySessionCmd(failed1Cmd));
211     EXPECT_FALSE(commandPoller->OnDestroySessionCmd(failed2Cmd));
212     EXPECT_FALSE(commandPoller->OnDestroySessionCmd(failed3Cmd));
213 }
214 }