• 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 <dlfcn.h>
17 #include <hwext/gtest-ext.h>
18 #include <hwext/gtest-tag.h>
19 #include <iomanip>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <ctime>
23 #include <unistd.h>
24 #include <vector>
25 
26 #include "logging.h"
27 #include "memory_data_plugin.h"
28 #include "plugin_module_api.h"
29 #include "sample_plugin.h"
30 #include "stream_plugin.h"
31 
32 using namespace testing::ext;
33 
34 namespace {
35 constexpr uint32_t MAX_BUFFER_SIZE = 4 * 1024 * 1024;
36 std::mutex g_taskMutex;
37 std::unique_ptr<MemoryDataPlugin> memoryPlugin = nullptr;
38 std::unique_ptr<StreamPlugin> streamPlugin = nullptr;
39 std::unique_ptr<SamplePlugin> samplePlugin = nullptr;
40 
41 class PluginModuleApiTest : public ::testing::Test {
42 public:
43     PluginModuleCallbacks* memoryCallbacks_;
44     PluginModuleCallbacks* sampleCallbacks_;
45     PluginModuleCallbacks* streamCallbacks_;
46 
47     PluginModuleStruct memoryModule_;
48     PluginModuleStruct sampleModule_;
49     PluginModuleStruct streamModule_;
50 
51     WriterStruct* writer_;
52 protected:
SetUpTestCase()53     static void SetUpTestCase() {}
TearDownTestCase()54     static void TearDownTestCase() {}
55 
SetUp()56     void SetUp()
57     {
58         memoryCallbacks_ = nullptr;
59         sampleCallbacks_ = nullptr;
60         streamCallbacks_ = nullptr;
61         memoryModule_ = {memoryCallbacks_, "memory-plugin", "1.01", MAX_BUFFER_SIZE};
62         sampleModule_ = {sampleCallbacks_, "sample-plugin", "1.01", MAX_BUFFER_SIZE};
63         streamModule_ = {streamCallbacks_, "stream-plugin", "1.01", MAX_BUFFER_SIZE};
64         writer_ = nullptr;
65     }
66 
67     bool PollPluginStub(MemoryDataPlugin& plugin, MemoryConfig& protoConfig, MemoryData& protoData);
68     bool StreamPluginStub(StreamPlugin& plugin, StreamConfig& protoConfig, StreamData& protoData);
69     // memory
70     static int MemDataPluginSessionStart(const uint8_t* configData, uint32_t configSize);
71     static int MemPluginReportResult(uint8_t* bufferData, uint32_t bufferSize);
72     static int MemPluginSessionStop();
73 
74     // stream
75     static int StreamPluginSessionStart(const uint8_t* configData, uint32_t configSize);
76     static int StreamPluginSessionStop();
77     static int StreamRegisterWriterStruct(WriterStruct* writer);
78     static long Write(WriterStruct* writer, const void* data, size_t size);
79     static bool Flush(WriterStruct* writer);
80 
81     // sample
82     static int SamplePluginSessionStart(const uint8_t* configData, uint32_t configSize);
83     static int SamplePluginReportResult(uint8_t* bufferData, uint32_t bufferSize);
84     static int SamplePluginSessionStop();
85     void SetSampleConfig(std::vector<uint8_t>& configData, uint32_t& configSize);
86     void SetMemoryConfig(std::vector<uint8_t>& configData, uint32_t& configSize);
87     void SetStreamConfig(std::vector<uint8_t>& configData, uint32_t& configSize);
88 };
89 
90 // memory
MemDataPluginSessionStart(const uint8_t * configData,uint32_t configSize)91 int PluginModuleApiTest::MemDataPluginSessionStart(const uint8_t* configData, uint32_t configSize)
92 {
93     std::lock_guard<std::mutex> guard(g_taskMutex);
94     memoryPlugin = std::make_unique<MemoryDataPlugin>();
95     return memoryPlugin->Start(configData, configSize);
96 }
97 
MemPluginReportResult(uint8_t * bufferData,uint32_t bufferSize)98 int PluginModuleApiTest::MemPluginReportResult(uint8_t* bufferData, uint32_t bufferSize)
99 {
100     std::lock_guard<std::mutex> guard(g_taskMutex);
101     return memoryPlugin->Report(bufferData, bufferSize);
102 }
103 
MemPluginSessionStop()104 int PluginModuleApiTest::MemPluginSessionStop()
105 {
106     std::lock_guard<std::mutex> guard(g_taskMutex);
107     CHECK_TRUE(memoryPlugin != nullptr, -1, "memoryPlugin is null!!!");
108     memoryPlugin->Stop();
109     memoryPlugin = nullptr;
110     return 0;
111 }
112 
113 // stream
StreamPluginSessionStart(const uint8_t * configData,uint32_t configSize)114 int PluginModuleApiTest::StreamPluginSessionStart(const uint8_t* configData, uint32_t configSize)
115 {
116     std::lock_guard<std::mutex> guard(g_taskMutex);
117     CHECK_TRUE(streamPlugin != nullptr, -1, "PluginStub::start plugin fail!!!");
118     return streamPlugin->Start(configData, configSize);
119 }
120 
StreamPluginSessionStop()121 int PluginModuleApiTest::StreamPluginSessionStop()
122 {
123     std::lock_guard<std::mutex> guard(g_taskMutex);
124     streamPlugin->Stop();
125     streamPlugin = nullptr;
126     return 0;
127 }
128 
StreamRegisterWriterStruct(WriterStruct * writer)129 int PluginModuleApiTest::StreamRegisterWriterStruct(WriterStruct* writer)
130 {
131     std::lock_guard<std::mutex> guard(g_taskMutex);
132     streamPlugin = std::make_unique<StreamPlugin>();
133     streamPlugin->SetWriter(writer);
134     return 0;
135 }
136 
137 // write
Write(WriterStruct * writer,const void * data,size_t size)138 long PluginModuleApiTest::Write(WriterStruct* writer, const void* data, size_t size)
139 {
140     HILOG_INFO(LOG_CORE, "PluginModuleApiTest::Write no implementation !");
141     return 0;
142 }
143 
Flush(WriterStruct * writer)144 bool PluginModuleApiTest::Flush(WriterStruct* writer)
145 {
146     HILOG_INFO(LOG_CORE, "PluginModuleApiTest::Write no implementation !");
147     return true;
148 }
149 
150 // sample
SamplePluginSessionStart(const uint8_t * configData,uint32_t configSize)151 int PluginModuleApiTest::SamplePluginSessionStart(const uint8_t* configData, uint32_t configSize)
152 {
153     std::lock_guard<std::mutex> guard(g_taskMutex);
154     samplePlugin = std::make_unique<SamplePlugin>();
155     return samplePlugin->Start(configData, configSize);
156 }
157 
SamplePluginReportResult(uint8_t * bufferData,uint32_t bufferSize)158 int PluginModuleApiTest::SamplePluginReportResult(uint8_t* bufferData, uint32_t bufferSize)
159 {
160     std::lock_guard<std::mutex> guard(g_taskMutex);
161     return samplePlugin->Report(bufferData, bufferSize);
162 }
163 
SamplePluginSessionStop()164 int PluginModuleApiTest::SamplePluginSessionStop()
165 {
166     std::lock_guard<std::mutex> guard(g_taskMutex);
167     samplePlugin->Stop();
168     samplePlugin = nullptr;
169     return 0;
170 }
171 
SetSampleConfig(std::vector<uint8_t> & configData,uint32_t & configSize)172 void PluginModuleApiTest::SetSampleConfig(std::vector<uint8_t>& configData, uint32_t& configSize)
173 {
174     SampleConfig protoConfig;
175     protoConfig.set_pid(1);
176     // serialize
177     protoConfig.SerializeToArray(configData.data(), configData.size());
178     configSize = protoConfig.ByteSizeLong();
179 }
180 
SetMemoryConfig(std::vector<uint8_t> & configData,uint32_t & configSize)181 void PluginModuleApiTest::SetMemoryConfig(std::vector<uint8_t>& configData, uint32_t& configSize)
182 {
183     MemoryConfig protoConfig;
184     protoConfig.set_report_process_mem_info(true);
185     protoConfig.add_pid(1);
186     // serialize
187     protoConfig.SerializeToArray(configData.data(), configSize);
188     configSize = protoConfig.ByteSizeLong();
189 }
190 
SetStreamConfig(std::vector<uint8_t> & configData,uint32_t & configSize)191 void PluginModuleApiTest::SetStreamConfig(std::vector<uint8_t>& configData, uint32_t& configSize)
192 {
193     StreamConfig protoConfig;
194     protoConfig.set_pid(1);
195     // serialize
196     protoConfig.SerializeToArray(configData.data(), configSize);
197     configSize = protoConfig.ByteSizeLong();
198 }
199 
200 // 流式调用写接口
201 HWTEST_F(PluginModuleApiTest, StreamPluginRegister1, TestSize.Level1)
202 {
203     PluginModuleCallbacks callbacks = {
204         StreamPluginSessionStart,
205         0,
206         StreamPluginSessionStop,
207         (RegisterWriterStructCallback)StreamRegisterWriterStruct,
208     };
209     streamCallbacks_ = &callbacks;
210 
211     EXPECT_NE(streamCallbacks_->onRegisterWriterStruct, nullptr);
212     EXPECT_EQ(streamCallbacks_->onRegisterWriterStruct(writer_), 0);
213     EXPECT_NE(streamPlugin, nullptr);
214 
215     EXPECT_EQ(streamPlugin->resultWriter_, nullptr);
216 }
217 
218 // 流式调用写接口
219 HWTEST_F(PluginModuleApiTest, StreamPluginRegister2, TestSize.Level1)
220 {
221     PluginModuleCallbacks callbacks = {
222         StreamPluginSessionStart,
223         0,
224         StreamPluginSessionStop,
225         (RegisterWriterStructCallback)StreamRegisterWriterStruct,
226     };
227     streamCallbacks_ = &callbacks;
228 
229     EXPECT_NE(streamCallbacks_->onRegisterWriterStruct, nullptr);
230     WriterStruct writer = {
231         Write,
232         Flush,
233     };
234     writer_ = &writer;
235     EXPECT_EQ(streamCallbacks_->onRegisterWriterStruct(writer_), 0);
236     EXPECT_NE(streamPlugin, nullptr);
237 
238     EXPECT_NE(streamPlugin->resultWriter_, nullptr);
239     EXPECT_NE(streamPlugin->resultWriter_->write, nullptr);
240 }
241 
242 // 流式调用写接口
243 HWTEST_F(PluginModuleApiTest, StreamPluginRegister3, TestSize.Level1)
244 {
245     PluginModuleCallbacks callbacks = {
246         StreamPluginSessionStart,
247         0,
248         StreamPluginSessionStop,
249         (RegisterWriterStructCallback)StreamRegisterWriterStruct,
250     };
251     streamCallbacks_ = &callbacks;
252 
253     EXPECT_NE(streamCallbacks_->onRegisterWriterStruct, nullptr);
254     WriterStruct writer = {
255         Write,
256         Flush,
257     };
258     writer_ = &writer;
259     EXPECT_EQ(streamCallbacks_->onRegisterWriterStruct(writer_), 0);
260     EXPECT_NE(streamPlugin, nullptr);
261 
262     EXPECT_NE(streamPlugin->resultWriter_, nullptr);
263     EXPECT_NE(streamPlugin->resultWriter_->write, nullptr);
264     EXPECT_NE(streamPlugin->resultWriter_->flush, nullptr);
265 }
266 
267 // 轮询式调用写接口
268 HWTEST_F(PluginModuleApiTest, SamplePluginRegister, TestSize.Level1)
269 {
270     PluginModuleCallbacks callbacks = {
271         SamplePluginSessionStart,
272         SamplePluginReportResult,
273         SamplePluginSessionStop,
274         0,
275     };
276     sampleCallbacks_ = &callbacks;
277 
278     EXPECT_EQ(sampleCallbacks_->onRegisterWriterStruct, nullptr);
279     EXPECT_EQ(samplePlugin, nullptr);
280 }
281 
282 HWTEST_F(PluginModuleApiTest, MemPluginRegister, TestSize.Level1)
283 {
284     PluginModuleCallbacks callbacks = {
285         MemDataPluginSessionStart,
286         MemPluginReportResult,
287         MemPluginSessionStop,
288         0,
289     };
290     memoryCallbacks_ = &callbacks;
291 
292     EXPECT_EQ(memoryCallbacks_->onRegisterWriterStruct, nullptr);
293     EXPECT_EQ(memoryPlugin, nullptr);
294 }
295 
296 // 流式插件正常启动
297 HWTEST_F(PluginModuleApiTest, StreamPluginStartSucc, TestSize.Level1)
298 {
299     PluginModuleCallbacks callbacks = {
300         StreamPluginSessionStart,
301         0,
302         StreamPluginSessionStop,
303         (RegisterWriterStructCallback)StreamRegisterWriterStruct,
304     };
305     streamCallbacks_ = &callbacks;
306 
307     EXPECT_NE(streamCallbacks_->onRegisterWriterStruct, nullptr);
308     WriterStruct writer = {
309         Write,
310         Flush,
311     };
312     writer_ = &writer;
313     streamCallbacks_->onRegisterWriterStruct(writer_);
314     EXPECT_NE(streamPlugin, nullptr);
315 
316     EXPECT_NE(streamCallbacks_->onPluginSessionStart, nullptr);
317     uint32_t configLen = MAX_BUFFER_SIZE;
318     std::vector<uint8_t> configData(configLen);
319     SetStreamConfig(configData, configLen);
320     EXPECT_EQ(streamCallbacks_->onPluginSessionStart(configData.data(), configLen), 0);
321 
322     streamCallbacks_->onPluginSessionStop();
323 }
324 
325 // 流式异常启动
326 HWTEST_F(PluginModuleApiTest, StreamPluginStartFail, TestSize.Level1)
327 {
328     PluginModuleCallbacks callbacks = {
329         StreamPluginSessionStart,
330         0,
331         StreamPluginSessionStop,
332         (RegisterWriterStructCallback)StreamRegisterWriterStruct,
333     };
334     streamCallbacks_ = &callbacks;
335 
336     EXPECT_NE(streamCallbacks_->onRegisterWriterStruct, nullptr);
337     WriterStruct writer;
338     writer_ = &writer;
339     streamCallbacks_->onRegisterWriterStruct(writer_);
340     EXPECT_NE(streamPlugin, nullptr);
341 
342     EXPECT_NE(streamCallbacks_->onPluginSessionStart, nullptr);
343     std::vector<uint8_t> configData(MAX_BUFFER_SIZE);
344     EXPECT_EQ(streamCallbacks_->onPluginSessionStart(configData.data(), configData.size()), -1);
345 }
346 
347 // 轮询式异常启动
348 HWTEST_F(PluginModuleApiTest, SamplePluginStartFail, TestSize.Level1)
349 {
350     PluginModuleCallbacks callbacks = {
351         SamplePluginSessionStart,
352         SamplePluginReportResult,
353         SamplePluginSessionStop,
354         0,
355     };
356     sampleCallbacks_ = &callbacks;
357 
358     EXPECT_NE(sampleCallbacks_->onPluginSessionStart, nullptr);
359     EXPECT_EQ(sampleCallbacks_->onPluginSessionStart(nullptr, 0), -1);
360     EXPECT_NE(samplePlugin, nullptr);
361 }
362 
363 // 轮询式插件正常启动
364 HWTEST_F(PluginModuleApiTest, SamplePluginStartSucc, TestSize.Level1)
365 {
366     PluginModuleCallbacks callbacks = {
367         SamplePluginSessionStart,
368         SamplePluginReportResult,
369         SamplePluginSessionStop,
370         0,
371     };
372     sampleCallbacks_ = &callbacks;
373 
374     EXPECT_NE(sampleCallbacks_->onPluginSessionStart, nullptr);
375     uint32_t configLen = MAX_BUFFER_SIZE;
376     std::vector<uint8_t> configData(configLen); // config
377     SetSampleConfig(configData, configLen);
378     EXPECT_EQ(sampleCallbacks_->onPluginSessionStart(configData.data(), configLen), 0);
379     EXPECT_NE(samplePlugin, nullptr);
380 
381     sampleCallbacks_->onPluginSessionStop();
382 }
383 
384 // 多个插件启动
385 HWTEST_F(PluginModuleApiTest, MultPluginStart, TestSize.Level1)
386 {
387     PluginModuleCallbacks callbacks = {
388         SamplePluginSessionStart,
389         SamplePluginReportResult,
390         SamplePluginSessionStop,
391         0,
392     };
393     PluginModuleCallbacks memCallbacks = {
394         MemDataPluginSessionStart,
395         MemPluginReportResult,
396         MemPluginSessionStop,
397         0,
398     };
399     sampleCallbacks_ = &callbacks;
400     memoryCallbacks_ = &memCallbacks;
401     uint32_t configLen = MAX_BUFFER_SIZE;
402 
403     EXPECT_NE(sampleCallbacks_->onPluginSessionStart, nullptr);
404     std::vector<uint8_t> sampleConfigData(configLen);
405     SetSampleConfig(sampleConfigData, configLen);
406     EXPECT_EQ(sampleCallbacks_->onPluginSessionStart(sampleConfigData.data(), configLen), 0);
407     EXPECT_NE(samplePlugin, nullptr);
408     sampleCallbacks_->onPluginSessionStop();
409 
410     EXPECT_NE(memoryCallbacks_->onPluginSessionStart, nullptr);
411     configLen = MAX_BUFFER_SIZE;
412     std::vector<uint8_t> memoryConfigData(configLen);
413     SetMemoryConfig(memoryConfigData, configLen);
414     EXPECT_EQ(memoryCallbacks_->onPluginSessionStart(memoryConfigData.data(), configLen), 0);
415     EXPECT_NE(memoryPlugin, nullptr);
416     memoryCallbacks_->onPluginSessionStop();
417 }
418 
419 // 流式上报
420 HWTEST_F(PluginModuleApiTest, StreamPluginReport, TestSize.Level1)
421 {
422     PluginModuleCallbacks callbacks = {
423         StreamPluginSessionStart,
424         0,
425         StreamPluginSessionStop,
426         (RegisterWriterStructCallback)StreamRegisterWriterStruct,
427     };
428     streamCallbacks_ = &callbacks;
429 
430     EXPECT_EQ(streamCallbacks_->onPluginReportResult, nullptr);
431 }
432 
433 // 轮询式上报
434 HWTEST_F(PluginModuleApiTest, SamplePluginReport, TestSize.Level1)
435 {
436     PluginModuleCallbacks callbacks = {
437         SamplePluginSessionStart,
438         SamplePluginReportResult,
439         SamplePluginSessionStop,
440         0,
441     };
442     sampleCallbacks_ = &callbacks;
443 
444     EXPECT_NE(sampleCallbacks_->onPluginSessionStart, nullptr);
445     uint32_t configLen = MAX_BUFFER_SIZE;
446     std::vector<uint8_t> configData(configLen);
447     SetSampleConfig(configData, configLen);
448     EXPECT_EQ(sampleCallbacks_->onPluginSessionStart(configData.data(), configLen), 0);
449     EXPECT_NE(samplePlugin, nullptr);
450 
451     EXPECT_NE(sampleCallbacks_->onPluginReportResult, nullptr);
452     std::vector<uint8_t> buffer(MAX_BUFFER_SIZE);
453     EXPECT_NE(sampleCallbacks_->onPluginReportResult(buffer.data(), buffer.size()), 0);
454     sampleCallbacks_->onPluginSessionStop();
455 }
456 
457 HWTEST_F(PluginModuleApiTest, MemoryPluginReport, TestSize.Level1)
458 {
459     PluginModuleCallbacks callbacks = {
460         MemDataPluginSessionStart,
461         MemPluginReportResult,
462         MemPluginSessionStop,
463         0,
464     };
465     memoryCallbacks_ = &callbacks;
466 
467     EXPECT_NE(memoryCallbacks_->onPluginSessionStart, nullptr);
468     uint32_t configLen = MAX_BUFFER_SIZE;
469     std::vector<uint8_t> configData(configLen);
470     SetMemoryConfig(configData, configLen);
471     EXPECT_EQ(memoryCallbacks_->onPluginSessionStart(configData.data(), configLen), 0);
472     EXPECT_NE(memoryPlugin, nullptr);
473 
474     EXPECT_NE(memoryCallbacks_->onPluginReportResult, nullptr);
475     std::vector<uint8_t> buffer(MAX_BUFFER_SIZE);
476     EXPECT_NE(memoryCallbacks_->onPluginReportResult(buffer.data(), buffer.size()), 0);
477     memoryCallbacks_->onPluginSessionStop();
478 }
479 
480 // 多个插件上报
481 HWTEST_F(PluginModuleApiTest, MultPluginReport1, TestSize.Level1)
482 {
483     PluginModuleCallbacks callbacks = {
484         SamplePluginSessionStart,
485         SamplePluginReportResult,
486         SamplePluginSessionStop,
487         0,
488     };
489     sampleCallbacks_ = &callbacks;
490     uint32_t configLen = MAX_BUFFER_SIZE;
491 
492     EXPECT_NE(sampleCallbacks_->onPluginSessionStart, nullptr);
493     std::vector<uint8_t> sampleConfigData(configLen);
494     SetSampleConfig(sampleConfigData, configLen);
495     EXPECT_EQ(sampleCallbacks_->onPluginSessionStart(sampleConfigData.data(), configLen), 0);
496     EXPECT_NE(samplePlugin, nullptr);
497     EXPECT_NE(sampleCallbacks_->onPluginReportResult, nullptr);
498     std::vector<uint8_t> sampleBuffer(MAX_BUFFER_SIZE);
499     EXPECT_NE(sampleCallbacks_->onPluginReportResult(sampleBuffer.data(), sampleBuffer.size()), 0);
500 
501     PluginModuleCallbacks memCallbacks = {
502         MemDataPluginSessionStart,
503         MemPluginReportResult,
504         MemPluginSessionStop,
505         0,
506     };
507     memoryCallbacks_ = &memCallbacks;
508 
509     configLen = MAX_BUFFER_SIZE;
510     EXPECT_NE(memoryCallbacks_->onPluginSessionStart, nullptr);
511     std::vector<uint8_t> memoryConfigData(configLen);
512     SetMemoryConfig(memoryConfigData, configLen);
513     EXPECT_EQ(memoryCallbacks_->onPluginSessionStart(memoryConfigData.data(), configLen), 0);
514     EXPECT_NE(memoryPlugin, nullptr);
515     EXPECT_NE(memoryCallbacks_->onPluginReportResult, nullptr);
516     std::vector<uint8_t> memoryBuffer(MAX_BUFFER_SIZE);
517     EXPECT_NE(memoryCallbacks_->onPluginReportResult(memoryBuffer.data(), memoryBuffer.size()), 0);
518 
519     sampleCallbacks_->onPluginSessionStop();
520     memoryCallbacks_->onPluginSessionStop();
521 }
522 
523 HWTEST_F(PluginModuleApiTest, MultPluginReport2, TestSize.Level1)
524 {
525     PluginModuleCallbacks callbacks = {
526         SamplePluginSessionStart,
527         SamplePluginReportResult,
528         SamplePluginSessionStop,
529         0,
530     };
531     sampleCallbacks_ = &callbacks;
532     uint32_t configLen = MAX_BUFFER_SIZE;
533 
534     EXPECT_NE(sampleCallbacks_->onPluginSessionStart, nullptr);
535     std::vector<uint8_t> sampleConfigData(configLen);
536     SetSampleConfig(sampleConfigData, configLen);
537     EXPECT_EQ(sampleCallbacks_->onPluginSessionStart(sampleConfigData.data(), configLen), 0);
538     EXPECT_NE(samplePlugin, nullptr);
539     EXPECT_NE(sampleCallbacks_->onPluginReportResult, nullptr);
540     std::vector<uint8_t> sampleBuffer(MAX_BUFFER_SIZE);
541     EXPECT_NE(sampleCallbacks_->onPluginReportResult(sampleBuffer.data(), sampleBuffer.size()), 0);
542 
543     PluginModuleCallbacks streamCallbacks = {
544         StreamPluginSessionStart,
545         0,
546         StreamPluginSessionStop,
547         (RegisterWriterStructCallback)StreamRegisterWriterStruct,
548     };
549     streamCallbacks_ = &streamCallbacks;
550 
551     EXPECT_NE(streamCallbacks_->onRegisterWriterStruct, nullptr);
552     WriterStruct writer = {
553         Write,
554         Flush,
555     };
556     writer_ = &writer;
557     streamCallbacks_->onRegisterWriterStruct(writer_);
558     EXPECT_NE(streamPlugin, nullptr);
559     EXPECT_NE(streamPlugin->resultWriter_, nullptr);
560     EXPECT_NE(streamPlugin->resultWriter_->write, nullptr);
561     EXPECT_NE(streamPlugin->resultWriter_->flush, nullptr);
562 
563     EXPECT_NE(streamCallbacks_->onPluginSessionStart, nullptr);
564     configLen = MAX_BUFFER_SIZE;
565     std::vector<uint8_t> configData(configLen);
566     SetStreamConfig(configData, configLen);
567     EXPECT_EQ(streamCallbacks_->onPluginSessionStart(configData.data(), configLen), 0);
568 
569     sampleCallbacks_->onPluginSessionStop();
570     streamCallbacks_->onPluginSessionStop();
571 }
572 
573 // 流式插件停止
574 HWTEST_F(PluginModuleApiTest, StreamPluginStop, TestSize.Level1)
575 {
576     PluginModuleCallbacks callbacks = {
577         StreamPluginSessionStart,
578         0,
579         StreamPluginSessionStop,
580         (RegisterWriterStructCallback)StreamRegisterWriterStruct,
581     };
582     streamCallbacks_ = &callbacks;
583 
584     EXPECT_NE(streamCallbacks_->onRegisterWriterStruct, nullptr);
585     streamCallbacks_->onRegisterWriterStruct(writer_);
586     EXPECT_NE(streamPlugin, nullptr);
587 
588     EXPECT_NE(streamCallbacks_->onPluginSessionStart, nullptr);
589     std::vector<uint8_t> configData(1);
590     streamCallbacks_->onPluginSessionStart(configData.data(), configData.size());
591 
592     EXPECT_NE(streamCallbacks_->onPluginSessionStop, nullptr);
593     EXPECT_EQ(streamCallbacks_->onPluginSessionStop(), 0);
594 }
595 
596 // 轮询式插件停止
597 HWTEST_F(PluginModuleApiTest, SamplePluginStop, TestSize.Level1)
598 {
599     PluginModuleCallbacks callbacks = {
600         SamplePluginSessionStart,
601         SamplePluginReportResult,
602         SamplePluginSessionStop,
603         0,
604     };
605     sampleCallbacks_ = &callbacks;
606 
607     EXPECT_NE(sampleCallbacks_->onPluginSessionStart, nullptr);
608     uint32_t configLen = MAX_BUFFER_SIZE;
609     std::vector<uint8_t> configData(configLen);
610     SetSampleConfig(configData, configLen);
611     EXPECT_EQ(sampleCallbacks_->onPluginSessionStart(configData.data(), configLen), 0);
612     EXPECT_NE(samplePlugin, nullptr);
613 
614     EXPECT_NE(sampleCallbacks_->onPluginSessionStop, nullptr);
615     EXPECT_EQ(sampleCallbacks_->onPluginSessionStop(), 0);
616 }
617 
618 HWTEST_F(PluginModuleApiTest, MemoryPluginStop, TestSize.Level1)
619 {
620     PluginModuleCallbacks callbacks = {
621         MemDataPluginSessionStart,
622         MemPluginReportResult,
623         MemPluginSessionStop,
624         0,
625     };
626     memoryCallbacks_ = &callbacks;
627 
628     EXPECT_NE(memoryCallbacks_->onPluginSessionStart, nullptr);
629     uint32_t configLen = MAX_BUFFER_SIZE;
630     std::vector<uint8_t> memoryConfigData(configLen);
631     SetMemoryConfig(memoryConfigData, configLen);
632     EXPECT_EQ(memoryCallbacks_->onPluginSessionStart(memoryConfigData.data(), configLen), 0);
633     EXPECT_NE(memoryPlugin, nullptr);
634 
635     EXPECT_NE(memoryCallbacks_->onPluginSessionStop, nullptr);
636     EXPECT_EQ(memoryCallbacks_->onPluginSessionStop(), 0);
637 }
638 
639 // 多个插件停止
640 HWTEST_F(PluginModuleApiTest, MultPluginStop, TestSize.Level1)
641 {
642     PluginModuleCallbacks callbacks = {
643         SamplePluginSessionStart,
644         SamplePluginReportResult,
645         SamplePluginSessionStop,
646         0,
647     };
648     PluginModuleCallbacks memCallbacks = {
649         MemDataPluginSessionStart,
650         MemPluginReportResult,
651         MemPluginSessionStop,
652         0,
653     };
654     sampleCallbacks_ = &callbacks;
655     memoryCallbacks_ = &memCallbacks;
656     uint32_t configLen = MAX_BUFFER_SIZE;
657 
658     EXPECT_NE(sampleCallbacks_->onPluginSessionStart, nullptr);
659     std::vector<uint8_t> sampleConfigData(configLen);
660     SetSampleConfig(sampleConfigData, configLen);
661     EXPECT_EQ(sampleCallbacks_->onPluginSessionStart(sampleConfigData.data(), configLen), 0);
662     EXPECT_NE(samplePlugin, nullptr);
663 
664     EXPECT_NE(memoryCallbacks_->onPluginSessionStart, nullptr);
665     configLen = MAX_BUFFER_SIZE;
666     std::vector<uint8_t> memoryConfigData(configLen);
667     SetMemoryConfig(memoryConfigData, configLen);
668     EXPECT_EQ(memoryCallbacks_->onPluginSessionStart(memoryConfigData.data(), configLen), 0);
669     EXPECT_NE(memoryPlugin, nullptr);
670 
671     EXPECT_EQ(sampleCallbacks_->onPluginSessionStop(), 0);
672     EXPECT_EQ(memoryCallbacks_->onPluginSessionStop(), 0);
673 }
674 
675 // 异常停止
676 HWTEST_F(PluginModuleApiTest, PluginAbnormalStop, TestSize.Level1)
677 {
678     PluginModuleCallbacks callbacks = {
679         MemDataPluginSessionStart,
680         MemPluginReportResult,
681         MemPluginSessionStop,
682         0,
683     };
684     memoryCallbacks_ = &callbacks;
685 
686     EXPECT_NE(memoryCallbacks_->onPluginSessionStop, nullptr);
687     EXPECT_EQ(memoryCallbacks_->onPluginSessionStop(), -1);
688 }
689 } // namespace