• 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 "plugin_service.h"
17 #include <gtest/gtest.h>
18 #include "common_types.pb.h"
19 #include "plugin_command_builder.h"
20 #include "plugin_service.ipc.h"
21 #include "plugin_service_impl.h"
22 #include "plugin_session_manager.h"
23 #include "profiler_data_repeater.h"
24 #include "profiler_service_types.pb.h"
25 #include "socket_context.h"
26 
27 using namespace testing::ext;
28 
29 namespace {
30 constexpr uint32_t BUFFER_SIZE = 4096;
31 constexpr size_t QUEUE_MAX_SIZE = 4096;
32 constexpr uint32_t SAMPLE_INTERVAL = 20;
33 
34 class PluginClientTest final : public IPluginServiceClient {
35 public:
OnGetCommandResponse(SocketContext & context,::GetCommandResponse & response)36     bool OnGetCommandResponse(SocketContext& context, ::GetCommandResponse& response) override
37     {
38         return true;
39     }
40 };
41 
42 class UnitTestPluginService : public testing::Test {
43 public:
SetUpTestCase()44     static void SetUpTestCase() {}
TearDownTestCase()45     static void TearDownTestCase() {}
46 
SetUp()47     void SetUp()
48     {
49         pluginService_ = std::make_shared<PluginService>();
50         usleep(100000); // sleep for 100000 us.
51         pluginClient_ = std::make_unique<PluginClientTest>();
52         pluginServiceImp_ = std::make_unique<PluginServiceImpl>(*pluginService_);
53         commandBuilder_ = std::make_unique<PluginCommandBuilder>();
54         pluginSessionMgr_ = std::make_shared<PluginSessionManager>(pluginService_);
55         pluginClient_->Connect(DEFAULT_UNIX_SOCKET_FULL_PATH);
56         pluginService_->SetPluginSessionManager(pluginSessionMgr_);
57     }
58 
TearDown()59     void TearDown() {}
60 
61 public:
62     std::shared_ptr<PluginService> pluginService_ = nullptr;
63     std::unique_ptr<PluginClientTest> pluginClient_ = nullptr;
64     std::unique_ptr<PluginServiceImpl> pluginServiceImp_ = nullptr;
65     std::unique_ptr<PluginCommandBuilder> commandBuilder_ = nullptr;
66     std::shared_ptr<PluginSessionManager> pluginSessionMgr_ = nullptr;
67     uint32_t pluginId_;
68 };
69 
70 /**
71  * @tc.name: Service
72  * @tc.desc: Set plugin registration information.
73  * @tc.type: FUNC
74  */
75 HWTEST_F(UnitTestPluginService, AddPluginInfo, TestSize.Level1)
76 {
77     RegisterPluginRequest request;
78     RegisterPluginResponse response;
79     PluginInfo plugin;
80 
81     request.set_request_id(1);
82     request.set_path("abc.so");
83     request.set_sha256("asdfasdfasdfasfd");
84     request.set_name("abc.so");
85     ASSERT_TRUE(response.status() != ResponseStatus::OK);
86     pluginId_ = response.plugin_id();
87 
88     plugin.name = "abc.so";
89     plugin.bufferSizeHint = 0;
90     plugin.sha256 = "";
91     ASSERT_TRUE(pluginService_->AddPluginInfo(plugin));
92 }
93 
94 /**
95  * @tc.name: Service
96  * @tc.desc: Set plugin configuration information.
97  * @tc.type: FUNC
98  */
99 HWTEST_F(UnitTestPluginService, CreatePluginSession1, TestSize.Level1)
100 {
101     ProfilerPluginConfig ppc;
102     ppc.set_name("abc1.so");
103     ppc.set_plugin_sha256("ASDFAADSF");
104     ppc.set_sample_interval(SAMPLE_INTERVAL);
105     ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
106     PluginInfo plugin;
107     plugin.name = "abc1.so";
108     plugin.bufferSizeHint = 0;
109     plugin.sha256 = "ASDFAADSF";
110     plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get();
111     EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
112 }
113 
114 /**
115  * @tc.name: Service
116  * @tc.desc: Set session configuration.
117  * @tc.type: FUNC
118  */
119 HWTEST_F(UnitTestPluginService, CreatePluginSession2, TestSize.Level1)
120 {
121     ProfilerPluginConfig ppc;
122     ppc.set_name("abc2.so");
123     ppc.set_plugin_sha256("ASDFAADSF");
124     ppc.set_sample_interval(SAMPLE_INTERVAL);
125     ProfilerSessionConfig::BufferConfig bc;
126     bc.set_pages(1);
127     bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
128     ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
129     PluginInfo plugin;
130     plugin.name = "abc2.so";
131     plugin.bufferSizeHint = 0;
132     plugin.sha256 = "ASDFAADSF";
133     plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get();
134     pluginService_->AddPluginInfo(plugin);
135     ProfilerSessionConfig psc;
136     psc.set_session_mode(ProfilerSessionConfig::OFFLINE);
137     std::vector<string> names = {"abc2.so"};
138     pluginService_->SetProfilerSessionConfig(std::make_shared<ProfilerSessionConfig>(psc), names);
139     TraceFileWriterPtr traceWriter = std::make_shared<TraceFileWriter>("/data/local/tmp/tracewrite.trace");
140     pluginService_->SetTraceWriter(traceWriter);
141     EXPECT_TRUE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
142     uint32_t pluginId = pluginService_->GetPluginIdByName("abc2.so");
143     PluginContextPtr pluginCtx = pluginService_->GetPluginContextById(pluginId);
144     pluginService_->ReadShareMemoryOffline(*pluginCtx);
145     EXPECT_TRUE(pluginService_->DestroyPluginSession("abc2.so"));
146     EXPECT_TRUE(pluginService_->RemovePluginSessionCtx("abc2.so"));
147     pluginService_->RemovePluginInfo(plugin);
148 }
149 
150 /**
151  * @tc.name: Service
152  * @tc.desc: Start plugin session.
153  * @tc.type: FUNC
154  */
155 HWTEST_F(UnitTestPluginService, StartPluginSession, TestSize.Level1)
156 {
157     ProfilerPluginConfig ppc;
158     ppc.set_name("abc3.so");
159     ppc.set_plugin_sha256("ASDFAADSF");
160     ppc.set_sample_interval(SAMPLE_INTERVAL);
161     ASSERT_FALSE(pluginService_->StartPluginSession(ppc));
162     PluginInfo plugin;
163     plugin.name = "abc3.so";
164     plugin.bufferSizeHint = 0;
165     plugin.sha256 = "ASDFAADSF";
166     plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get();
167     pluginService_->AddPluginInfo(plugin);
168     EXPECT_TRUE(pluginService_->StartPluginSession(ppc));
169 }
170 
171 /**
172  * @tc.name: Service
173  * @tc.desc: Stop plugin session.
174  * @tc.type: FUNC
175  */
176 HWTEST_F(UnitTestPluginService, StopPluginSession, TestSize.Level1)
177 {
178     ASSERT_FALSE(pluginService_->StopPluginSession("abc.so"));
179     PluginInfo plugin;
180     SocketContext ctx;
181     plugin.name = "abc4.so";
182     plugin.bufferSizeHint = 0;
183     plugin.sha256 = "ASDFAADSF";
184     plugin.context = &ctx;
185     pluginService_->AddPluginInfo(plugin);
186     EXPECT_FALSE(pluginService_->StopPluginSession("abc4.so"));
187 }
188 
189 /**
190  * @tc.name: Service
191  * @tc.desc: Destroy receiving test.
192  * @tc.type: FUNC
193  */
194 HWTEST_F(UnitTestPluginService, DestroyPluginSession, TestSize.Level1)
195 {
196     ASSERT_FALSE(pluginService_->DestroyPluginSession("abc.so"));
197     PluginInfo plugin;
198     plugin.name = "abc5.so";
199     plugin.bufferSizeHint = 0;
200     plugin.sha256 = "ASDFAADSF";
201     plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get();
202     pluginService_->AddPluginInfo(plugin);
203     EXPECT_TRUE(pluginService_->DestroyPluginSession("abc5.so"));
204 }
205 /**
206  * @tc.name: Service
207  * @tc.desc: RefreshPluginSession  test.
208  * @tc.type: FUNC
209  */
210 HWTEST_F(UnitTestPluginService, RefreshPluginSession, TestSize.Level1)
211 {
212     ASSERT_FALSE(pluginService_->RefreshPluginSession("abc.so"));
213     PluginInfo plugin;
214     SocketContext ctx;
215     plugin.name = "abc6.so";
216     plugin.bufferSizeHint = 0;
217     plugin.sha256 = "ASDFAADSF";
218     plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get();
219     pluginService_->AddPluginInfo(plugin);
220     EXPECT_TRUE(pluginService_->RefreshPluginSession("abc6.so"));
221 }
222 /**
223  * @tc.name: Service
224  * @tc.desc: GetPluginInfo  test.
225  * @tc.type: FUNC
226  */
227 HWTEST_F(UnitTestPluginService, GetPluginInfo, TestSize.Level1)
228 {
229     PluginInfo plugin;
230     EXPECT_FALSE(pluginService_->GetPluginInfo("edf.so", plugin));
231     SocketContext ctx;
232     plugin.name = "abc7.so";
233     plugin.bufferSizeHint = 0;
234     plugin.sha256 = "ASDFAADSF";
235     plugin.context = &ctx;
236     pluginService_->AddPluginInfo(plugin);
237     EXPECT_TRUE(pluginService_->GetPluginInfo("abc7.so", plugin));
238 }
239 
240 /**
241  * @tc.name: Service
242  * @tc.desc: RemovePluginSessionCtx  test.
243  * @tc.type: FUNC
244  */
245 HWTEST_F(UnitTestPluginService, RemovePluginSessionCtx, TestSize.Level1)
246 {
247     PluginInfo plugin;
248     SocketContext ctx;
249     plugin.name = "abc8.so";
250     plugin.bufferSizeHint = 0;
251     plugin.sha256 = "ASDFAADSF";
252     plugin.context = &ctx;
253     pluginService_->AddPluginInfo(plugin);
254     EXPECT_TRUE(pluginService_->RemovePluginSessionCtx("abc8.so"));
255 }
256 
257 /**
258  * @tc.name: Service
259  * @tc.desc: Remove the specified plugin.
260  * @tc.type: FUNC
261  */
262 HWTEST_F(UnitTestPluginService, RemovePluginInfo, TestSize.Level1)
263 {
264     UnregisterPluginRequest request;
265     PluginInfo pi;
266     pi.id = 0;
267     pi.name = "abc9.so";
268     pi.path = "abc9.so";
269     pi.sha256 = "asdfasdf";
270     ASSERT_FALSE(pluginService_->RemovePluginInfo(pi));
271     pluginService_->AddPluginInfo(pi);
272     pi.id = pluginService_->GetPluginIdByName("abc9.so");
273     EXPECT_TRUE(pluginService_->RemovePluginInfo(pi));
274 }
275 
276 /**
277  * @tc.name: Service
278  * @tc.desc: Setting report results.
279  * @tc.type: FUNC
280  */
281 HWTEST_F(UnitTestPluginService, AppendResult, TestSize.Level1)
282 {
283     NotifyResultRequest nrr;
284     nrr.set_request_id(1);
285     nrr.set_command_id(1);
286     ASSERT_TRUE(pluginService_->AppendResult(nrr));
287 }
288 
289 /**
290  * @tc.name: Service
291  * @tc.desc: Get plugin status.
292  * @tc.type: FUNC
293  */
294 HWTEST_F(UnitTestPluginService, GetPluginStatus, TestSize.Level1)
295 {
296     auto status = pluginService_->GetPluginStatus();
297     ASSERT_TRUE(status.size() == 0);
298 }
299 
300 /**
301  * @tc.name: Service
302  * @tc.desc: Gets the plugin with the specified name.
303  * @tc.type: FUNC
304  */
305 HWTEST_F(UnitTestPluginService, GetPluginIdByName, TestSize.Level1)
306 {
307     ASSERT_TRUE(pluginService_->GetPluginIdByName("abc.so") == 0);
308 }
309 
310 /**
311  * @tc.name: Service
312  * @tc.desc: Set plugin registration information.
313  * @tc.type: FUNC
314  */
315 HWTEST_F(UnitTestPluginService, AddPluginInfo2, TestSize.Level1)
316 {
317     RegisterPluginRequest request;
318     RegisterPluginResponse response;
319     PluginInfo plugin;
320 
321     request.set_request_id(2);
322     request.set_path("mem.so");
323     request.set_sha256("asdfasdfasdfasfd");
324     request.set_name("mem.so");
325     ASSERT_TRUE(response.status() != ResponseStatus::OK);
326     pluginId_ = response.plugin_id();
327 
328     plugin.name = "mem.so";
329     plugin.bufferSizeHint = 0;
330     plugin.sha256 = "";
331     EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
332 }
333 
334 /**
335  * @tc.name: Service
336  * @tc.desc: Set plugin configuration information.
337  * @tc.type: FUNC
338  */
339 HWTEST_F(UnitTestPluginService, CreatePluginSession12, TestSize.Level1)
340 {
341     ProfilerPluginConfig ppc;
342     ppc.set_name("mem.so");
343     ppc.set_plugin_sha256("ASDFAADSF");
344     ppc.set_sample_interval(SAMPLE_INTERVAL);
345 
346     ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
347 }
348 
349 /**
350  * @tc.name: Service
351  * @tc.desc: Set session configuration.
352  * @tc.type: FUNC
353  */
354 HWTEST_F(UnitTestPluginService, CreatePluginSession22, TestSize.Level1)
355 {
356     ProfilerPluginConfig ppc;
357     ppc.set_name("mem.so");
358     ppc.set_plugin_sha256("ASDFAADSF");
359     ppc.set_sample_interval(SAMPLE_INTERVAL);
360 
361     ProfilerSessionConfig::BufferConfig bc;
362     bc.set_pages(1);
363     bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
364 
365     ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
366 }
367 
368 /**
369  * @tc.name: Service
370  * @tc.desc: Start plugin session.
371  * @tc.type: FUNC
372  */
373 HWTEST_F(UnitTestPluginService, StartPluginSession2, TestSize.Level1)
374 {
375     ProfilerPluginConfig ppc;
376     ppc.set_name("mem.so");
377     ppc.set_plugin_sha256("ASDFAADSF");
378     ppc.set_sample_interval(SAMPLE_INTERVAL);
379 
380     ASSERT_FALSE(pluginService_->StartPluginSession(ppc));
381 }
382 
383 /**
384  * @tc.name: Service
385  * @tc.desc: Stop plugin session.
386  * @tc.type: FUNC
387  */
388 HWTEST_F(UnitTestPluginService, StopPluginSession2, TestSize.Level1)
389 {
390     ASSERT_FALSE(pluginService_->StopPluginSession("mem.so"));
391 }
392 
393 /**
394  * @tc.name: Service
395  * @tc.desc: Destroy receiving test.
396  * @tc.type: FUNC
397  */
398 HWTEST_F(UnitTestPluginService, DestroyPluginSession2, TestSize.Level1)
399 {
400     ASSERT_FALSE(pluginService_->DestroyPluginSession("mem.so"));
401 }
402 
403 /**
404  * @tc.name: Service
405  * @tc.desc: Remove the specified plugin.
406  * @tc.type: FUNC
407  */
408 HWTEST_F(UnitTestPluginService, RemovePluginInfo2, TestSize.Level1)
409 {
410     UnregisterPluginRequest request;
411     PluginInfo pi;
412     pi.id = 0;
413     pi.name = "mem.so";
414     pi.path = "mem.so";
415     pi.sha256 = "asdfasdf";
416     ASSERT_FALSE(pluginService_->RemovePluginInfo(pi));
417 }
418 
419 /**
420  * @tc.name: Service
421  * @tc.desc: Setting report results.
422  * @tc.type: FUNC
423  */
424 HWTEST_F(UnitTestPluginService, AppendResult2, TestSize.Level1)
425 {
426     NotifyResultRequest nrr;
427     nrr.set_request_id(2);
428     nrr.set_command_id(2);
429     ASSERT_TRUE(pluginService_->AppendResult(nrr));
430 }
431 
432 /**
433  * @tc.name: Service
434  * @tc.desc: Get plugin status.
435  * @tc.type: FUNC
436  */
437 HWTEST_F(UnitTestPluginService, GetPluginStatus2, TestSize.Level1)
438 {
439     auto status = pluginService_->GetPluginStatus();
440     ASSERT_TRUE(status.size() == 0);
441 }
442 
443 /**
444  * @tc.name: Service
445  * @tc.desc: Gets the plugin with the specified name.
446  * @tc.type: FUNC
447  */
448 HWTEST_F(UnitTestPluginService, GetPluginIdByName2, TestSize.Level1)
449 {
450     ASSERT_TRUE(pluginService_->GetPluginIdByName("mem.so") == 0);
451 }
452 
453 /**
454  * @tc.name: Service
455  * @tc.desc: Set plugin registration information.
456  * @tc.type: FUNC
457  */
458 HWTEST_F(UnitTestPluginService, AddPluginInfo3, TestSize.Level1)
459 {
460     RegisterPluginRequest request;
461     RegisterPluginResponse response;
462     PluginInfo plugin;
463 
464     request.set_request_id(3);
465     request.set_path("cpu.so");
466     request.set_sha256("asdfasdfasdfasfd");
467     request.set_name("cpu.so");
468     ASSERT_TRUE(response.status() != ResponseStatus::OK);
469     pluginId_ = response.plugin_id();
470 
471     plugin.name = "cpu.so";
472     plugin.bufferSizeHint = 0;
473     plugin.sha256 = "";
474     EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
475 }
476 
477 /**
478  * @tc.name: Service
479  * @tc.desc: Set plugin configuration information.
480  * @tc.type: FUNC
481  */
482 HWTEST_F(UnitTestPluginService, CreatePluginSession13, TestSize.Level1)
483 {
484     ProfilerPluginConfig ppc;
485     ppc.set_name("cpu.so");
486     ppc.set_plugin_sha256("ASDFAADSF");
487     ppc.set_sample_interval(SAMPLE_INTERVAL);
488 
489     ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
490 }
491 
492 /**
493  * @tc.name: Service
494  * @tc.desc: Set session configuration.
495  * @tc.type: FUNC
496  */
497 HWTEST_F(UnitTestPluginService, CreatePluginSession23, TestSize.Level1)
498 {
499     ProfilerPluginConfig ppc;
500     ppc.set_name("cpu.so");
501     ppc.set_plugin_sha256("ASDFAADSF");
502     ppc.set_sample_interval(SAMPLE_INTERVAL);
503 
504     ProfilerSessionConfig::BufferConfig bc;
505     bc.set_pages(1);
506     bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
507 
508     ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
509 }
510 
511 /**
512  * @tc.name: Service
513  * @tc.desc: Start plugin session.
514  * @tc.type: FUNC
515  */
516 HWTEST_F(UnitTestPluginService, StartPluginSession3, TestSize.Level1)
517 {
518     ProfilerPluginConfig ppc;
519     ppc.set_name("cpu.so");
520     ppc.set_plugin_sha256("ASDFAADSF");
521     ppc.set_sample_interval(SAMPLE_INTERVAL);
522 
523     ASSERT_FALSE(pluginService_->StartPluginSession(ppc));
524 }
525 
526 /**
527  * @tc.name: Service
528  * @tc.desc: Stop plugin session.
529  * @tc.type: FUNC
530  */
531 HWTEST_F(UnitTestPluginService, StopPluginSession3, TestSize.Level1)
532 {
533     ASSERT_FALSE(pluginService_->StopPluginSession("cpu.so"));
534 }
535 
536 /**
537  * @tc.name: Service
538  * @tc.desc: Destroy receiving test.
539  * @tc.type: FUNC
540  */
541 HWTEST_F(UnitTestPluginService, DestroyPluginSession3, TestSize.Level1)
542 {
543     ASSERT_FALSE(pluginService_->DestroyPluginSession("cpu.so"));
544 }
545 
546 /**
547  * @tc.name: Service
548  * @tc.desc: Remove the specified plugin.
549  * @tc.type: FUNC
550  */
551 HWTEST_F(UnitTestPluginService, RemovePluginInfo3, TestSize.Level1)
552 {
553     UnregisterPluginRequest request;
554     PluginInfo pi;
555     pi.id = 0;
556     pi.name = "cpu.so";
557     pi.path = "cpu.so";
558     pi.sha256 = "asdfasdf";
559     ASSERT_FALSE(pluginService_->RemovePluginInfo(pi));
560 }
561 
562 /**
563  * @tc.name: Service
564  * @tc.desc: Setting report results.
565  * @tc.type: FUNC
566  */
567 HWTEST_F(UnitTestPluginService, AppendResult3, TestSize.Level1)
568 {
569     NotifyResultRequest nrr;
570     nrr.set_request_id(3);
571     nrr.set_command_id(3);
572     ASSERT_TRUE(pluginService_->AppendResult(nrr));
573 }
574 
575 /**
576  * @tc.name: Service
577  * @tc.desc: Get plugin status.
578  * @tc.type: FUNC
579  */
580 HWTEST_F(UnitTestPluginService, GetPluginStatus3, TestSize.Level1)
581 {
582     auto status = pluginService_->GetPluginStatus();
583     ASSERT_TRUE(status.size() == 0);
584 }
585 
586 /**
587  * @tc.name: Service
588  * @tc.desc: Gets the plugin with the specified name.
589  * @tc.type: FUNC
590  */
591 HWTEST_F(UnitTestPluginService, GetPluginIdByName3, TestSize.Level1)
592 {
593     ASSERT_TRUE(pluginService_->GetPluginIdByName("cpu.so") == 0);
594 }
595 
596 /**
597  * @tc.name: Service
598  * @tc.desc: Set plugin registration information.
599  * @tc.type: FUNC
600  */
601 HWTEST_F(UnitTestPluginService, AddPluginInfo4, TestSize.Level1)
602 {
603     RegisterPluginRequest request;
604     RegisterPluginResponse response;
605     PluginInfo plugin;
606 
607     request.set_request_id(4);
608     request.set_path("stream.so");
609     request.set_sha256("asdfasdfasdfasfd");
610     request.set_name("stream.so");
611     ASSERT_TRUE(response.status() != ResponseStatus::OK);
612     pluginId_ = response.plugin_id();
613 
614     plugin.name = "stream.so";
615     plugin.bufferSizeHint = 0;
616     plugin.sha256 = "";
617     EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
618     plugin.bufferSizeHint = 10;
619     plugin.isStandaloneFileData = true;
620     plugin.outFileName = "test-file.bin";
621     plugin.pluginVersion = "1.02";
622     EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
623 }
624 
625 /**
626  * @tc.name: Service
627  * @tc.desc: Set plugin configuration information.
628  * @tc.type: FUNC
629  */
630 HWTEST_F(UnitTestPluginService, CreatePluginSession14, TestSize.Level1)
631 {
632     ProfilerPluginConfig ppc;
633     ppc.set_name("stream.so");
634     ppc.set_plugin_sha256("ASDFAADSF");
635     ppc.set_sample_interval(SAMPLE_INTERVAL);
636 
637     ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
638 }
639 
640 /**
641  * @tc.name: Service
642  * @tc.desc: Set session configuration.
643  * @tc.type: FUNC
644  */
645 HWTEST_F(UnitTestPluginService, CreatePluginSession24, TestSize.Level1)
646 {
647     ProfilerPluginConfig ppc;
648     ppc.set_name("stream.so");
649     ppc.set_plugin_sha256("ASDFAADSF");
650     ppc.set_sample_interval(SAMPLE_INTERVAL);
651 
652     ProfilerSessionConfig::BufferConfig bc;
653     bc.set_pages(1);
654     bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
655 
656     ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
657 }
658 
659 /**
660  * @tc.name: Service
661  * @tc.desc: Start plugin session.
662  * @tc.type: FUNC
663  */
664 HWTEST_F(UnitTestPluginService, StartPluginSession4, TestSize.Level1)
665 {
666     ProfilerPluginConfig ppc;
667     ppc.set_name("stream.so");
668     ppc.set_plugin_sha256("ASDFAADSF");
669     ppc.set_sample_interval(SAMPLE_INTERVAL);
670 
671     ASSERT_FALSE(pluginService_->StartPluginSession(ppc));
672 }
673 
674 /**
675  * @tc.name: Service
676  * @tc.desc: Stop plugin session.
677  * @tc.type: FUNC
678  */
679 HWTEST_F(UnitTestPluginService, StopPluginSession4, TestSize.Level1)
680 {
681     ASSERT_FALSE(pluginService_->StopPluginSession("stream.so"));
682 }
683 
684 /**
685  * @tc.name: Service
686  * @tc.desc: Destroy receiving test.
687  * @tc.type: FUNC
688  */
689 HWTEST_F(UnitTestPluginService, DestroyPluginSession4, TestSize.Level1)
690 {
691     ASSERT_FALSE(pluginService_->DestroyPluginSession("stream.so"));
692 }
693 
694 /**
695  * @tc.name: Service
696  * @tc.desc: Remove the specified plugin.
697  * @tc.type: FUNC
698  */
699 HWTEST_F(UnitTestPluginService, RemovePluginInfo4, TestSize.Level1)
700 {
701     UnregisterPluginRequest request;
702     PluginInfo pi;
703     pi.id = 0;
704     pi.name = "stream.so";
705     pi.path = "stream.so";
706     pi.sha256 = "asdfasdf";
707     ASSERT_FALSE(pluginService_->RemovePluginInfo(pi));
708 }
709 
710 /**
711  * @tc.name: Service
712  * @tc.desc: Setting report results.
713  * @tc.type: FUNC
714  */
715 HWTEST_F(UnitTestPluginService, AppendResult4, TestSize.Level1)
716 {
717     NotifyResultRequest nrr;
718     nrr.set_request_id(4);
719     nrr.set_command_id(4);
720     ASSERT_TRUE(pluginService_->AppendResult(nrr));
721 }
722 
723 /**
724  * @tc.name: Service
725  * @tc.desc: Get plugin status.
726  * @tc.type: FUNC
727  */
728 HWTEST_F(UnitTestPluginService, GetPluginStatus4, TestSize.Level1)
729 {
730     auto status = pluginService_->GetPluginStatus();
731     ASSERT_TRUE(status.size() == 0);
732 }
733 
734 /**
735  * @tc.name: Service
736  * @tc.desc: Gets the plugin with the specified name.
737  * @tc.type: FUNC
738  */
739 HWTEST_F(UnitTestPluginService, GetPluginIdByName4, TestSize.Level1)
740 {
741     ASSERT_TRUE(pluginService_->GetPluginIdByName("stream.so") == 0);
742 }
743 
744 /**
745  * @tc.name: Service
746  * @tc.desc: Set plugin registration information.
747  * @tc.type: FUNC
748  */
749 HWTEST_F(UnitTestPluginService, AddPluginInfo5, TestSize.Level1)
750 {
751     RegisterPluginRequest request;
752     RegisterPluginResponse response;
753     PluginInfo plugin;
754 
755     request.set_request_id(5);
756     request.set_path("sample.so");
757     request.set_sha256("asdfasdfasdfasfd");
758     request.set_name("sample.so");
759     ASSERT_TRUE(response.status() != ResponseStatus::OK);
760     pluginId_ = response.plugin_id();
761 
762     plugin.name = "sample.so";
763     plugin.bufferSizeHint = 0;
764     plugin.sha256 = "";
765     EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
766 }
767 
768 /**
769  * @tc.name: Service
770  * @tc.desc: Set plugin configuration information.
771  * @tc.type: FUNC
772  */
773 HWTEST_F(UnitTestPluginService, CreatePluginSession15, TestSize.Level1)
774 {
775     ProfilerPluginConfig ppc;
776     ppc.set_name("sample.so");
777     ppc.set_plugin_sha256("ASDFAADSF");
778     ppc.set_sample_interval(SAMPLE_INTERVAL);
779 
780     ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
781 }
782 
783 /**
784  * @tc.name: Service
785  * @tc.desc: Set session configuration.
786  * @tc.type: FUNC
787  */
788 HWTEST_F(UnitTestPluginService, CreatePluginSession25, TestSize.Level1)
789 {
790     ProfilerPluginConfig ppc;
791     ppc.set_name("sample.so");
792     ppc.set_plugin_sha256("ASDFAADSF");
793     ppc.set_sample_interval(SAMPLE_INTERVAL);
794 
795     ProfilerSessionConfig::BufferConfig bc;
796     bc.set_pages(1);
797     bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
798 
799     ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
800 }
801 
802 /**
803  * @tc.name: Service
804  * @tc.desc: Start plugin session.
805  * @tc.type: FUNC
806  */
807 HWTEST_F(UnitTestPluginService, StartPluginSession5, TestSize.Level1)
808 {
809     ProfilerPluginConfig ppc;
810     ppc.set_name("sample.so");
811     ppc.set_plugin_sha256("ASDFAADSF");
812     ppc.set_sample_interval(SAMPLE_INTERVAL);
813 
814     ASSERT_FALSE(pluginService_->StartPluginSession(ppc));
815 }
816 
817 /**
818  * @tc.name: Service
819  * @tc.desc: Stop plugin session.
820  * @tc.type: FUNC
821  */
822 HWTEST_F(UnitTestPluginService, StopPluginSession5, TestSize.Level1)
823 {
824     ASSERT_FALSE(pluginService_->StopPluginSession("sample.so"));
825 }
826 
827 /**
828  * @tc.name: Service
829  * @tc.desc: Destroy receiving test.
830  * @tc.type: FUNC
831  */
832 HWTEST_F(UnitTestPluginService, DestroyPluginSession5, TestSize.Level1)
833 {
834     ASSERT_FALSE(pluginService_->DestroyPluginSession("sample.so"));
835 }
836 
837 /**
838  * @tc.name: Service
839  * @tc.desc: Remove the specified plugin.
840  * @tc.type: FUNC
841  */
842 HWTEST_F(UnitTestPluginService, RemovePluginInfo5, TestSize.Level1)
843 {
844     UnregisterPluginRequest request;
845     PluginInfo pi;
846     pi.id = 0;
847     pi.name = "sample.so";
848     pi.path = "sample.so";
849     pi.sha256 = "asdfasdf";
850     ASSERT_FALSE(pluginService_->RemovePluginInfo(pi));
851 }
852 
853 /**
854  * @tc.name: Service
855  * @tc.desc: Setting report results.
856  * @tc.type: FUNC
857  */
858 HWTEST_F(UnitTestPluginService, AppendResult5, TestSize.Level1)
859 {
860     NotifyResultRequest nrr;
861     nrr.set_request_id(5);
862     nrr.set_command_id(5);
863     ASSERT_TRUE(pluginService_->AppendResult(nrr));
864 }
865 
866 /**
867  * @tc.name: Service
868  * @tc.desc: Get plugin status.
869  * @tc.type: FUNC
870  */
871 HWTEST_F(UnitTestPluginService, GetPluginStatus5, TestSize.Level1)
872 {
873     auto status = pluginService_->GetPluginStatus();
874     ASSERT_TRUE(status.size() == 0);
875 }
876 
877 /**
878  * @tc.name: Service
879  * @tc.desc: Gets the plugin with the specified name.
880  * @tc.type: FUNC
881  */
882 HWTEST_F(UnitTestPluginService, GetPluginIdByName5, TestSize.Level1)
883 {
884     ASSERT_TRUE(pluginService_->GetPluginIdByName("sample.so") == 0);
885 }
886 
887 /**
888  * @tc.name: Service
889  * @tc.desc: test function RegisterPlugin.
890  * @tc.type: FUNC
891  */
892 HWTEST_F(UnitTestPluginService, PluginServiceImpl_RegisterPlugin, TestSize.Level1)
893 {
894     RegisterPluginRequest request;
895     RegisterPluginResponse response;
896     SocketContext ctx;
897 
898     request.set_request_id(5);
899     request.set_path("sample2.so");
900     request.set_sha256("");
901     request.set_name("sample2.so");
902     request.set_buffer_size_hint(0);
903     EXPECT_TRUE(pluginServiceImp_->RegisterPlugin(ctx, request, response));
904     request.set_sha256("abcdsfdsfad");
905     EXPECT_FALSE(pluginServiceImp_->RegisterPlugin(ctx, request, response));
906     UnregisterPluginRequest unRequest;
907     UnregisterPluginResponse unResponse;
908     uint32_t plugId = pluginService_->GetPluginIdByName("sample2.so");
909     unRequest.set_plugin_id(plugId);
910     EXPECT_TRUE(pluginServiceImp_->UnregisterPlugin(ctx, unRequest, unResponse));
911 }
912 
913 /**
914  * @tc.name: Service
915  * @tc.desc: test function UnregisterPlugin
916  * @tc.type: FUNC
917  */
918 HWTEST_F(UnitTestPluginService, PluginServiceImpl_UnregisterPlugin, TestSize.Level1)
919 {
920     UnregisterPluginRequest request;
921     UnregisterPluginResponse response;
922     SocketContext ctx;
923     request.set_plugin_id(1);
924     EXPECT_FALSE(pluginServiceImp_->UnregisterPlugin(ctx, request, response));
925 
926     GetCommandRequest gcRequest;
927     GetCommandResponse gcResponse;
928     EXPECT_FALSE(pluginServiceImp_->GetCommand(ctx, gcRequest, gcResponse));
929 
930     NotifyResultRequest nResRequest;
931     NotifyResultResponse nResResponse;
932     EXPECT_TRUE(pluginServiceImp_->NotifyResult(ctx, nResRequest, nResResponse));
933 }
934 /**
935  * @tc.name: BuildCreateSessionCmd
936  * @tc.desc: build create session command
937  * @tc.type: FUNC
938  */
939 HWTEST_F(UnitTestPluginService, pluginCommandBuilder, TestSize.Level1)
940 {
941     ProfilerPluginConfig ppc;
942     ppc.set_name("abc.so");
943     ppc.set_plugin_sha256("ASDFAADSF");
944     ppc.set_sample_interval(SAMPLE_INTERVAL);
945     const uint32_t pluginId = 1;
946     EXPECT_TRUE(commandBuilder_->BuildCreateSessionCmd(ppc, BUFFER_SIZE) != nullptr);
947     EXPECT_TRUE(commandBuilder_->BuildStartSessionCmd(ppc, pluginId) != nullptr);
948     EXPECT_TRUE(commandBuilder_->BuildRefreshSessionCmd(pluginId) != nullptr);
949     EXPECT_TRUE(commandBuilder_->BuildStopSessionCmd(pluginId) != nullptr);
950     EXPECT_TRUE(commandBuilder_->BuildDestroySessionCmd(pluginId) != nullptr);
951     EXPECT_FALSE(commandBuilder_->GetedCommandResponse(0));
952     EXPECT_TRUE(commandBuilder_->GetedCommandResponse(1));
953 }
954 
955 /**
956  * @tc.name: PluginSessionManager
957  * @tc.desc: test plugin session manager checkBufferConfig
958  * @tc.type: FUNC
959  */
960 HWTEST_F(UnitTestPluginService, PluginSessionManager_Check, TestSize.Level1)
961 {
962     PluginInfo plugin;
963     SocketContext ctx;
964     plugin.name = "abc_check.so";
965     plugin.bufferSizeHint = 0;
966     plugin.sha256 = "asdfasdfasdfasfd";
967     plugin.context = &ctx;
968     EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
969     ProfilerSessionConfig::BufferConfig bc;
970     bc.set_pages(1);
971     bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
972     EXPECT_TRUE(pluginSessionMgr_->CheckBufferConfig(bc));
973     ProfilerPluginConfig ppc;
974     ppc.set_name("abc_check.so");
975     ppc.set_plugin_sha256("asdfasdfasdfasfd");
976     EXPECT_TRUE(pluginSessionMgr_->CheckPluginSha256(ppc));
977 }
978 /**
979  * @tc.name: PluginSessionManager
980  * @tc.desc: test plugin session manager by creating sesssion
981  * @tc.type: FUNC
982  */
983 HWTEST_F(UnitTestPluginService, PluginSessionManager_CreateSession, TestSize.Level1)
984 {
985     PluginInfo plugin;
986     plugin.name = "testsample.so";
987     plugin.bufferSizeHint = 0;
988     plugin.sha256 = "ASDFAADSF";
989     plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get();
990     EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
991     ProfilerPluginConfig ppc;
992     ppc.set_name("testsample.so");
993     ppc.set_plugin_sha256("ASDFAADSF");
994     ppc.set_sample_interval(SAMPLE_INTERVAL);
995 
996     ProfilerSessionConfig::BufferConfig bc;
997     bc.set_pages(1);
998     bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
999     ProfilerSessionConfig psc;
1000     psc.set_session_mode(ProfilerSessionConfig::OFFLINE);
1001     std::vector<string> names = {"testsample.so"};
1002     pluginService_->SetProfilerSessionConfig(std::make_shared<ProfilerSessionConfig>(psc), names);
1003     EXPECT_TRUE(pluginSessionMgr_->CreatePluginSession(
1004                     ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)) != nullptr);
1005 }
1006 /**
1007  * @tc.name: PluginSessionManager
1008  * @tc.desc: test plugin session manager by creating a lot of sesssions
1009  * @tc.type: FUNC
1010  */
1011 HWTEST_F(UnitTestPluginService, PluginSessionManager_CreateSessions, TestSize.Level1)
1012 {
1013     PluginInfo plugin;
1014     plugin.name = "testsample1.so";
1015     plugin.bufferSizeHint = 0;
1016     plugin.sha256 = "ASDFAADSF";
1017     plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get();
1018     EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
1019     ProfilerPluginConfig ppc;
1020     ppc.set_name("testsample1.so");
1021     ppc.set_plugin_sha256("ASDFAADSF");
1022     ppc.set_sample_interval(SAMPLE_INTERVAL);
1023 
1024     ProfilerSessionConfig::BufferConfig bc;
1025     bc.set_pages(1);
1026     bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
1027     std::vector<ProfilerPluginConfig> vecConfig;
1028     std::vector<ProfilerSessionConfig::BufferConfig> vecBuf;
1029     vecConfig.push_back(ppc);
1030     vecBuf.push_back(bc);
1031     ProfilerSessionConfig psc;
1032     psc.set_session_mode(ProfilerSessionConfig::OFFLINE);
1033     std::vector<string> names = {"testsample1.so"};
1034     pluginService_->SetProfilerSessionConfig(std::make_shared<ProfilerSessionConfig>(psc), names);
1035     EXPECT_TRUE(pluginSessionMgr_->CreatePluginSessions(vecConfig, vecBuf,
1036                                                         std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
1037     std::vector<std::string> nameList{"testsample.so", "testsample1.so"};
1038     EXPECT_TRUE(pluginSessionMgr_->InvalidatePluginSessions(nameList));
1039     std::vector<PluginSession::State> vecRet = pluginSessionMgr_->GetStatus(nameList);
1040     EXPECT_TRUE(vecRet.size() != 0);
1041     EXPECT_TRUE(pluginSessionMgr_->RefreshPluginSession());
1042 }
1043 
1044 /**
1045  * @tc.name: plugin service
1046  * @tc.desc: test createpluginsession online
1047  * @tc.type: FUNC
1048  */
1049 HWTEST_F(UnitTestPluginService, PluginService_CreatePluginSession_Online, TestSize.Level1)
1050 {
1051     ProfilerPluginConfig ppc;
1052     ppc.set_name("abconline.so");
1053     ppc.set_plugin_sha256("ASDFAADSF");
1054     ppc.set_sample_interval(SAMPLE_INTERVAL);
1055     ProfilerSessionConfig::BufferConfig bc;
1056     bc.set_pages(1);
1057     bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
1058 
1059     PluginInfo plugin;
1060     plugin.name = "abconline.so";
1061     plugin.bufferSizeHint = 0;
1062     plugin.sha256 = "ASDFAADSF";
1063     plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get();
1064     EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
1065     ProfilerSessionConfig psc;
1066     psc.set_session_mode(ProfilerSessionConfig::ONLINE);
1067     std::vector<string> names = {"abconline.so"};
1068     pluginService_->SetProfilerSessionConfig(std::make_shared<ProfilerSessionConfig>(psc), names);
1069     EXPECT_TRUE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
1070     uint32_t pluginId = pluginService_->GetPluginIdByName("abconline.so");
1071     PluginContextPtr pluginCtx = pluginService_->GetPluginContextById(pluginId);
1072     pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
1073     pluginService_->ReadShareMemoryOnline(*pluginCtx);
1074     NotifyResultRequest nrr;
1075     nrr.set_request_id(5);
1076     nrr.set_command_id(5);
1077     PluginResult* result = nrr.add_result();
1078     result->set_plugin_id(pluginId);
1079     result->set_out_file_name("/data/local/tmp/testonline.trace");
1080     result->set_data("test plugin result!");
1081     EXPECT_TRUE(pluginService_->AppendResult(nrr));
1082     plugin.id = pluginId;
1083     EXPECT_TRUE(pluginService_->RemovePluginInfo(plugin));
1084 }
1085 } // namespace