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