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