1 /*
2 * Copyright (c) 2023-2024 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 "daudio_input_test.h"
17
18 #include "av_trans_message.h"
19 #include "av_trans_errno.h"
20 #include "softbus_channel_adapter.h"
21
22 namespace OHOS {
23 namespace DistributedHardware {
24 namespace {
25 const std::string META_DATA_TYPE = "meta_data_type";
26 const std::string META_TIMESTAMP = "meta_timestamp";
27 const std::string META_FRAME_NUMBER = "meta_frame_number";
28 const std::string META_EXT_TIMESTAMP = "meta_ext_timestamp";
29 const std::string META_EXT_FRAME_NUMBER = "meta_ext_frame_number";
30 const std::string KEY_OWNER_NAME = "ownerName";
31 const std::string KEY_PEER_DEVID = "peerDevId";
32 const std::string KEY_TYPE = "type";
33 const std::string KEY_CONTENT = "content";
34 const std::string PLUGINNAME = "daudio_input";
35 }
36
SetUpTestCase()37 void DaudioInputTest::SetUpTestCase() {}
38
TearDownTestCase()39 void DaudioInputTest::TearDownTestCase() {}
40
SetUp()41 void DaudioInputTest::SetUp() {}
42
TearDown()43 void DaudioInputTest::TearDown() {}
44
45 HWTEST_F(DaudioInputTest, Pause_001, TestSize.Level0)
46 {
47 auto plugin = std::make_shared<DaudioInputPlugin>(PLUGINNAME);
48 AVTransSharedMemory sharedMemory1 {1, 0, ""};
49 plugin->sharedMemory_ = sharedMemory1;
50 Status ret = plugin->Pause();
51 EXPECT_EQ(Status::OK, ret);
52
53 AVTransSharedMemory sharedMemory2 { 1, 1, "" };
54 plugin->sharedMemory_ = sharedMemory2;
55 ret = plugin->Pause();
56 EXPECT_EQ(Status::OK, ret);
57
58 AVTransSharedMemory sharedMemory3 { 1, 1, "name" };
59 plugin->sharedMemory_ = sharedMemory3;
60 ret = plugin->Pause();
61 EXPECT_EQ(Status::OK, ret);
62 }
63
64 HWTEST_F(DaudioInputTest, SetParameter_001, TestSize.Level0)
65 {
66 auto plugin = std::make_shared<DaudioInputPlugin>(PLUGINNAME);
67 std::string value = "dsoftbus_input_test";
68 Status ret = plugin->SetParameter(Tag::USER_SHARED_MEMORY_FD, value);
69 EXPECT_EQ(Status::OK, ret);
70 }
71
72 HWTEST_F(DaudioInputTest, GetParameter_001, TestSize.Level0)
73 {
74 auto plugin = std::make_shared<DaudioInputPlugin>(PLUGINNAME);
75 std::string value = "dsoftbus_input_test";
76 plugin->SetParameter(Tag::USER_SHARED_MEMORY_FD, value);
77
78 ValueType val;
79 Status ret = plugin->GetParameter(Tag::USER_SHARED_MEMORY_FD, val);
80 EXPECT_EQ(Status::OK, ret);
81
82 plugin->tagMap_.clear();
83 ret = plugin->GetParameter(Tag::USER_SHARED_MEMORY_FD, val);
84 EXPECT_EQ(Status::ERROR_NOT_EXISTED, ret);
85 }
86
87 HWTEST_F(DaudioInputTest, PushData_001, TestSize.Level0)
88 {
89 auto plugin = std::make_shared<DaudioInputPlugin>(PLUGINNAME);
90 std::shared_ptr<AVBuffer> buffer = nullptr;
91 Status ret = plugin->PushData("", buffer, 0);
92 EXPECT_EQ(Status::ERROR_NULL_POINTER, ret);
93
94 buffer = std::make_shared<AVBuffer>();
95 ret = plugin->PushData("", buffer, 0);
96 EXPECT_EQ(Status::ERROR_INVALID_PARAMETER, ret);
97
98 buffer->AllocMemory(nullptr, 10);
99 buffer->GetMemory()->Write((uint8_t*)"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 10);
100 ret = plugin->PushData("", buffer, 0);
101 EXPECT_EQ(Status::OK, ret);
102
103 plugin->sharedMemory_.fd = 1;
104 ret = plugin->PushData("", buffer, 0);
105 EXPECT_EQ(Status::OK, ret);
106
107 plugin->sharedMemory_.fd = 1;
108 plugin->sharedMemory_.size = 1;
109 ret = plugin->PushData("", buffer, 0);
110 EXPECT_EQ(Status::OK, ret);
111
112 plugin->sharedMemory_.fd = 0;
113 plugin->sharedMemory_.size = 0;
114 plugin->sharedMemory_.name = "sharedMemory_";
115 ret = plugin->PushData("", buffer, 0);
116 EXPECT_EQ(Status::OK, ret);
117
118 plugin->sharedMemory_.fd = 1;
119 plugin->sharedMemory_.size = 1;
120 plugin->sharedMemory_.name = "sharedMemory_";
121 ret = plugin->PushData("", buffer, 0);
122 EXPECT_EQ(Status::OK, ret);
123 }
124
125 HWTEST_F(DaudioInputTest, MarshalAudioMeta_001, TestSize.Level0)
126 {
127 auto meta = std::make_shared<AVTransAudioBufferMeta>();
128 meta->dataType_ = BufferDataType::AUDIO;
129 meta->pts_ = 10;
130 meta->frameNum_ = 10;
131 auto ret = meta->MarshalAudioMeta();
132 EXPECT_EQ(false, ret.empty());
133 }
134
135 HWTEST_F(DaudioInputTest, UnmarshalAudioMeta_001, TestSize.Level0)
136 {
137 auto meta = std::make_shared<AVTransAudioBufferMeta>();
138 cJSON *metaJson = cJSON_CreateObject();
139 cJSON_AddStringToObject(metaJson, META_DATA_TYPE.c_str(), "data_type_test");
140 cJSON_AddStringToObject(metaJson, META_TIMESTAMP.c_str(), "timestamp_test");
141 cJSON_AddStringToObject(metaJson, META_FRAME_NUMBER.c_str(), "frame_num_test");
142 char* cjson = cJSON_PrintUnformatted(metaJson);
143 std::string jsonStr(cjson);
144 auto ret = meta->UnmarshalAudioMeta(jsonStr);
145 EXPECT_EQ(false, ret);
146 cJSON_free(cjson);
147 cJSON_Delete(metaJson);
148
149 cJSON *metaJson1 = cJSON_CreateObject();
150 cJSON_AddNumberToObject(metaJson1, META_DATA_TYPE.c_str(), static_cast<uint32_t>(BufferDataType::AUDIO));
151 cJSON_AddStringToObject(metaJson1, META_TIMESTAMP.c_str(), "timestamp_test");
152 cJSON_AddStringToObject(metaJson1, META_FRAME_NUMBER.c_str(), "frame_num_test");
153 char* cjson1 = cJSON_PrintUnformatted(metaJson1);
154 std::string jsonStr1(cjson1);
155 ret = meta->UnmarshalAudioMeta(jsonStr1);
156 EXPECT_EQ(false, ret);
157 cJSON_free(cjson1);
158 cJSON_Delete(metaJson1);
159
160 cJSON *metaJson2 = cJSON_CreateObject();
161 cJSON_AddNumberToObject(metaJson2, META_DATA_TYPE.c_str(), static_cast<uint32_t>(BufferDataType::AUDIO));
162 cJSON_AddNumberToObject(metaJson2, META_TIMESTAMP.c_str(), 100);
163 cJSON_AddStringToObject(metaJson2, META_FRAME_NUMBER.c_str(), "frame_num_test");
164 char* cjson2 = cJSON_PrintUnformatted(metaJson2);
165 std::string jsonStr2(cjson2);
166 ret = meta->UnmarshalAudioMeta(jsonStr2);
167 EXPECT_EQ(false, ret);
168 cJSON_free(cjson2);
169 cJSON_Delete(metaJson2);
170 }
171
172 HWTEST_F(DaudioInputTest, UnmarshalAudioMeta_002, TestSize.Level0)
173 {
174 auto meta = std::make_shared<AVTransAudioBufferMeta>();
175 cJSON *metaJson3 = cJSON_CreateObject();
176 cJSON_AddNumberToObject(metaJson3, META_DATA_TYPE.c_str(), static_cast<uint32_t>(BufferDataType::AUDIO));
177 cJSON_AddStringToObject(metaJson3, META_TIMESTAMP.c_str(), "timestamp_test");
178 cJSON_AddNumberToObject(metaJson3, META_FRAME_NUMBER.c_str(), 10);
179 char* cjson3 = cJSON_PrintUnformatted(metaJson3);
180 std::string jsonStr3(cjson3);
181 auto ret = meta->UnmarshalAudioMeta(jsonStr3);
182 EXPECT_EQ(false, ret);
183 cJSON_free(cjson3);
184 cJSON_Delete(metaJson3);
185
186 cJSON *metaJson4 = cJSON_CreateObject();
187 cJSON_AddStringToObject(metaJson4, META_DATA_TYPE.c_str(), "data_type_test");
188 cJSON_AddNumberToObject(metaJson4, META_TIMESTAMP.c_str(), 100);
189 cJSON_AddNumberToObject(metaJson4, META_FRAME_NUMBER.c_str(), 10);
190 char* cjson4 = cJSON_PrintUnformatted(metaJson4);
191 std::string jsonStr4(cjson4);
192 ret = meta->UnmarshalAudioMeta(jsonStr4);
193 EXPECT_EQ(false, ret);
194 cJSON_free(cjson4);
195 cJSON_Delete(metaJson4);
196
197 cJSON *metaJson5 = cJSON_CreateObject();
198 cJSON_AddNumberToObject(metaJson5, META_DATA_TYPE.c_str(), static_cast<uint32_t>(BufferDataType::AUDIO));
199 cJSON_AddNumberToObject(metaJson5, META_TIMESTAMP.c_str(), 100);
200 cJSON_AddNumberToObject(metaJson5, META_FRAME_NUMBER.c_str(), 10);
201 char* cjson5 = cJSON_PrintUnformatted(metaJson5);
202 std::string jsonStr5(cjson5);
203 ret = meta->UnmarshalAudioMeta(jsonStr5);
204 EXPECT_EQ(true, ret);
205 cJSON_free(cjson5);
206 cJSON_Delete(metaJson5);
207 }
208
209 HWTEST_F(DaudioInputTest, MarshalVideoMeta_001, TestSize.Level0)
210 {
211 auto meta = std::make_shared<AVTransVideoBufferMeta>();
212 meta->dataType_ = BufferDataType::AUDIO;
213 meta->pts_ = 10;
214 meta->frameNum_ = 10;
215 meta->extPts_ = 1;
216 meta->extFrameNum_ = 1;
217 auto ret = meta->MarshalVideoMeta();
218 EXPECT_EQ(false, ret.empty());
219 }
220
221 HWTEST_F(DaudioInputTest, MarshalVideoMeta_002, TestSize.Level0)
222 {
223 auto meta = std::make_shared<AVTransVideoBufferMeta>();
224 meta->dataType_ = BufferDataType::AUDIO;
225 meta->pts_ = 10;
226 meta->frameNum_ = 10;
227 meta->extPts_ = 0;
228 meta->extFrameNum_ = 0;
229 auto ret = meta->MarshalVideoMeta();
230 EXPECT_EQ(false, ret.empty());
231 }
232
233 HWTEST_F(DaudioInputTest, UnmarshalVideoMeta_001, TestSize.Level0)
234 {
235 auto meta = std::make_shared<AVTransVideoBufferMeta>();
236 std::string str = "";
237 auto ret = meta->UnmarshalVideoMeta(str);
238 EXPECT_EQ(false, ret);
239
240 cJSON *metaJson = cJSON_CreateObject();
241 cJSON_AddStringToObject(metaJson, META_DATA_TYPE.c_str(), "data_type_test");
242 char* cjson = cJSON_PrintUnformatted(metaJson);
243 std::string jsonStr(cjson);
244 ret = meta->UnmarshalVideoMeta(jsonStr);
245 EXPECT_EQ(true, ret);
246 cJSON_free(cjson);
247 cJSON_Delete(metaJson);
248
249 cJSON *metaJson1 = cJSON_CreateObject();
250 cJSON_AddNumberToObject(metaJson1, META_DATA_TYPE.c_str(), static_cast<uint32_t>(BufferDataType::AUDIO));
251 cJSON_AddStringToObject(metaJson1, META_TIMESTAMP.c_str(), "timestamp_test");
252 char* cjson1 = cJSON_PrintUnformatted(metaJson1);
253 std::string jsonStr1(cjson1);
254 ret = meta->UnmarshalVideoMeta(jsonStr1);
255 EXPECT_EQ(true, ret);
256 cJSON_free(cjson1);
257 cJSON_Delete(metaJson1);
258
259 cJSON *metaJson2 = cJSON_CreateObject();
260 cJSON_AddNumberToObject(metaJson2, META_DATA_TYPE.c_str(), static_cast<uint32_t>(BufferDataType::AUDIO));
261 cJSON_AddNumberToObject(metaJson2, META_TIMESTAMP.c_str(), 100);
262 cJSON_AddStringToObject(metaJson2, META_FRAME_NUMBER.c_str(), "frame_num_test");
263 char* cjson2 = cJSON_PrintUnformatted(metaJson2);
264 std::string jsonStr2(cjson2);
265 ret = meta->UnmarshalVideoMeta(jsonStr2);
266 EXPECT_EQ(true, ret);
267 cJSON_free(cjson2);
268 cJSON_Delete(metaJson2);
269 }
270
271 HWTEST_F(DaudioInputTest, UnmarshalVideoMeta_002, TestSize.Level0)
272 {
273 auto meta = std::make_shared<AVTransVideoBufferMeta>();
274 cJSON *metaJson3 = cJSON_CreateObject();
275 cJSON_AddNumberToObject(metaJson3, META_DATA_TYPE.c_str(), static_cast<uint32_t>(BufferDataType::AUDIO));
276 cJSON_AddNumberToObject(metaJson3, META_TIMESTAMP.c_str(), 100);
277 cJSON_AddNumberToObject(metaJson3, META_FRAME_NUMBER.c_str(), 10);
278 cJSON_AddStringToObject(metaJson3, META_EXT_TIMESTAMP.c_str(), "ext_timestamp_test");
279 char* cjson3 = cJSON_PrintUnformatted(metaJson3);
280 std::string jsonStr3(cjson3);
281 auto ret = meta->UnmarshalVideoMeta(jsonStr3);
282 EXPECT_EQ(true, ret);
283 cJSON_free(cjson3);
284 cJSON_Delete(metaJson3);
285
286 cJSON *metaJson4 = cJSON_CreateObject();
287 cJSON_AddNumberToObject(metaJson4, META_DATA_TYPE.c_str(), static_cast<uint32_t>(BufferDataType::AUDIO));
288 cJSON_AddNumberToObject(metaJson4, META_TIMESTAMP.c_str(), 100);
289 cJSON_AddNumberToObject(metaJson4, META_FRAME_NUMBER.c_str(), 10);
290 cJSON_AddNumberToObject(metaJson4, META_EXT_TIMESTAMP.c_str(), 10);
291 cJSON_AddStringToObject(metaJson4, META_EXT_FRAME_NUMBER.c_str(), "ext_frame_num_test");
292 char* cjson4 = cJSON_PrintUnformatted(metaJson4);
293 std::string jsonStr4(cjson4);
294 ret = meta->UnmarshalVideoMeta(jsonStr4);
295 EXPECT_EQ(true, ret);
296 cJSON_free(cjson4);
297 cJSON_Delete(metaJson4);
298
299 cJSON *metaJson5 = cJSON_CreateObject();
300 cJSON_AddNumberToObject(metaJson5, META_DATA_TYPE.c_str(), static_cast<uint32_t>(BufferDataType::AUDIO));
301 cJSON_AddNumberToObject(metaJson5, META_TIMESTAMP.c_str(), 100);
302 cJSON_AddNumberToObject(metaJson5, META_FRAME_NUMBER.c_str(), 10);
303 cJSON_AddNumberToObject(metaJson5, META_EXT_TIMESTAMP.c_str(), 10);
304 cJSON_AddNumberToObject(metaJson5, META_EXT_FRAME_NUMBER.c_str(), 10);
305 char* cjson5 = cJSON_PrintUnformatted(metaJson5);
306 std::string jsonStr5(cjson5);
307 ret = meta->UnmarshalVideoMeta(jsonStr5);
308 EXPECT_EQ(true, ret);
309 cJSON_free(cjson5);
310 cJSON_Delete(metaJson5);
311 }
312
313 HWTEST_F(DaudioInputTest, ParseChannelDescription_001, TestSize.Level0)
314 {
315 std::string descJsonStr = "";
316 std::string ownerName = "";
317 std::string peerDevId = "";
318 ParseChannelDescription(descJsonStr, ownerName, peerDevId);
319 EXPECT_EQ(true, descJsonStr.empty());
320 }
321
322 HWTEST_F(DaudioInputTest, ParseChannelDescription_002, TestSize.Level0)
323 {
324 std::string ownerName = "";
325 std::string peerDevId = "";
326 cJSON *metaJson = cJSON_CreateObject();
327 cJSON_AddNumberToObject(metaJson, KEY_OWNER_NAME.c_str(), 1);
328 char* cjson = cJSON_PrintUnformatted(metaJson);
329 std::string descJsonStr(cjson);
330 ParseChannelDescription(descJsonStr, ownerName, peerDevId);
331 EXPECT_EQ(false, descJsonStr.empty());
332 cJSON_free(cjson);
333 cJSON_Delete(metaJson);
334
335
336 cJSON *metaJson1 = cJSON_CreateObject();
337 cJSON_AddStringToObject(metaJson1, KEY_OWNER_NAME.c_str(), "owner_name_test");
338 cJSON_AddNumberToObject(metaJson1, KEY_PEER_DEVID.c_str(), 1);
339 char* cjson1 = cJSON_PrintUnformatted(metaJson1);
340 std::string descJsonStr1(cjson1);
341 ParseChannelDescription(descJsonStr1, ownerName, peerDevId);
342 EXPECT_EQ(false, descJsonStr1.empty());
343 cJSON_free(cjson1);
344 cJSON_Delete(metaJson1);
345
346 cJSON *metaJson2 = cJSON_CreateObject();
347 cJSON_AddStringToObject(metaJson2, KEY_OWNER_NAME.c_str(), "owner_name_test");
348 cJSON_AddStringToObject(metaJson2, KEY_PEER_DEVID.c_str(), "peer_devid_test");
349 char* cjson2 = cJSON_PrintUnformatted(metaJson2);
350 std::string descJsonStr2(cjson2);
351 ParseChannelDescription(descJsonStr2, ownerName, peerDevId);
352 EXPECT_EQ(false, descJsonStr2.empty());
353 cJSON_free(cjson2);
354 cJSON_Delete(metaJson2);
355 }
356
357 HWTEST_F(DaudioInputTest, HiSBuffer2TransBuffer_001, TestSize.Level0)
358 {
359 std::shared_ptr<AVBuffer> hisBuffer = nullptr;
360 auto ret = HiSBuffer2TransBuffer(hisBuffer);
361 EXPECT_EQ(nullptr, ret);
362
363 hisBuffer = std::make_shared<AVBuffer>();
364 ret = HiSBuffer2TransBuffer(hisBuffer);
365 EXPECT_EQ(nullptr, ret);
366 }
367
368 HWTEST_F(DaudioInputTest, CastEventType_001, TestSize.Level0)
369 {
370 Plugin::PluginEventType type = Plugin::PluginEventType::EVENT_CHANNEL_OPENED;
371 bool isAbnormal = true;
372 auto ret = CastEventType(type, isAbnormal);
373 EXPECT_EQ(EventType::EVENT_START_SUCCESS, ret);
374
375 type = Plugin::PluginEventType::EVENT_CHANNEL_OPEN_FAIL;
376 ret = CastEventType(type, isAbnormal);
377 EXPECT_EQ(EventType::EVENT_START_FAIL, ret);
378
379 type = Plugin::PluginEventType::EVENT_CHANNEL_CLOSED;
380 ret = CastEventType(type, isAbnormal);
381 EXPECT_EQ(EventType::EVENT_ENGINE_ERROR, ret);
382
383 std::string fileName = "";
384 uint8_t *buffer = nullptr;
385 int32_t bufSize = 1;
386 DumpBufferToFile(fileName, buffer, bufSize);
387
388 fileName = "fileName_test";
389 DumpBufferToFile(fileName, buffer, bufSize);
390
391 unsigned char* adtsHeader = new unsigned char[1024];
392 uint32_t packetLen = 1;
393 uint32_t profile = 2;
394 uint32_t sampleRate = 1;
395 uint32_t channels = 1;
396 GenerateAdtsHeader(adtsHeader, packetLen, profile, sampleRate, channels);
397
398 profile = 0;
399 GenerateAdtsHeader(adtsHeader, packetLen, profile, sampleRate, channels);
400 delete [] adtsHeader;
401 adtsHeader = nullptr;
402 }
403
404 HWTEST_F(DaudioInputTest, GetBufferData_001, TestSize.Level0)
405 {
406 std::shared_ptr<AVTransBuffer> transBuffer = std::make_shared<AVTransBuffer>();
407 uint32_t index = 10;
408 auto ret = transBuffer->GetBufferData(index);
409 EXPECT_EQ(nullptr, ret);
410 }
411
412 HWTEST_F(DaudioInputTest, MarshalMessage_001, TestSize.Level0)
413 {
414 auto avMessage = std::make_shared<AVTransMessage>((uint32_t)AVTransTag::START_AV_SYNC,
415 "content_test", "dstDevId_test");
416 auto ret = avMessage->MarshalMessage();
417 EXPECT_EQ(false, ret.empty());
418 }
419
420 HWTEST_F(DaudioInputTest, UnmarshalMessage_001, TestSize.Level0)
421 {
422 auto avMessage = std::make_shared<AVTransMessage>();
423 std::string peerDevId = "peerDevId_test";
424 cJSON *msgJson = cJSON_CreateObject();
425 cJSON_AddStringToObject(msgJson, KEY_TYPE.c_str(), "type_test");
426 cJSON_AddNumberToObject(msgJson, KEY_CONTENT.c_str(), 1);
427 char* cjson = cJSON_PrintUnformatted(msgJson);
428 std::string jsonStr(cjson);
429 auto ret = avMessage->UnmarshalMessage(jsonStr, peerDevId);
430 EXPECT_EQ(false, ret);
431 cJSON_free(cjson);
432 cJSON_Delete(msgJson);
433
434 cJSON *msgJson1 = cJSON_CreateObject();
435 cJSON_AddNumberToObject(msgJson1, KEY_TYPE.c_str(), (uint32_t)AVTransTag::START_AV_SYNC);
436 cJSON_AddNumberToObject(msgJson1, KEY_CONTENT.c_str(), 1);
437 char* cjson1 = cJSON_PrintUnformatted(msgJson1);
438 std::string jsonStr1(cjson1);
439 ret = avMessage->UnmarshalMessage(jsonStr1, peerDevId);
440 EXPECT_EQ(false, ret);
441 cJSON_free(cjson1);
442 cJSON_Delete(msgJson1);
443
444 cJSON *msgJson2 = cJSON_CreateObject();
445 cJSON_AddStringToObject(msgJson2, KEY_TYPE.c_str(), "type_test");
446 cJSON_AddStringToObject(msgJson2, KEY_CONTENT.c_str(), "content_test");
447 char* cjson2 = cJSON_PrintUnformatted(msgJson2);
448 std::string jsonStr2(cjson2);
449 ret = avMessage->UnmarshalMessage(jsonStr2, peerDevId);
450 EXPECT_EQ(false, ret);
451 cJSON_free(cjson2);
452 cJSON_Delete(msgJson2);
453
454 cJSON *msgJson3 = cJSON_CreateObject();
455 cJSON_AddNumberToObject(msgJson3, KEY_TYPE.c_str(), (uint32_t)AVTransTag::START_AV_SYNC);
456 cJSON_AddStringToObject(msgJson3, KEY_CONTENT.c_str(), "content_test");
457 char* cjson3 = cJSON_PrintUnformatted(msgJson3);
458 std::string jsonStr3(cjson3);
459 ret = avMessage->UnmarshalMessage(jsonStr3, peerDevId);
460 EXPECT_EQ(true, ret);
461 cJSON_free(cjson3);
462 cJSON_Delete(msgJson3);
463 }
464
465 HWTEST_F(DaudioInputTest, CreateChannelServer_001, TestSize.Level0)
466 {
467 std::string pkgName = "pkgName_test";
468 std::string sessName = "sessName_avtrans.data";
469 int32_t socketId = 1;
470 SoftbusChannelAdapter::GetInstance().serverMap_.insert(std::make_pair(pkgName + "_" + sessName, socketId));
471 auto ret = SoftbusChannelAdapter::GetInstance().CreateChannelServer(pkgName, sessName);
472 EXPECT_EQ(DH_AVT_SUCCESS, ret);
473
474 SoftbusChannelAdapter::GetInstance().serverMap_.clear();
475 ret = SoftbusChannelAdapter::GetInstance().CreateChannelServer(pkgName, sessName);
476 EXPECT_EQ(ERR_DH_AVT_SESSION_ERROR, ret);
477 }
478
479 HWTEST_F(DaudioInputTest, RemoveChannelServer_001, TestSize.Level0)
480 {
481 SoftbusChannelAdapter::GetInstance().serverMap_.clear();
482 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.clear();
483 std::string pkgName = "pkgName_test";
484 std::string sessName = "sessName_avtrans.data";
485 int32_t socketId = 1;
486 auto ret = SoftbusChannelAdapter::GetInstance().RemoveChannelServer(pkgName, sessName);
487 EXPECT_EQ(DH_AVT_SUCCESS, ret);
488
489 SoftbusChannelAdapter::GetInstance().serverMap_.insert(std::make_pair(pkgName + "_" + sessName, socketId));
490 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.insert(std::make_pair(sessName, socketId));
491 ret = SoftbusChannelAdapter::GetInstance().RemoveChannelServer(pkgName, sessName);
492 EXPECT_EQ(DH_AVT_SUCCESS, ret);
493 }
494
495 HWTEST_F(DaudioInputTest, OpenSoftbusChannel_001, TestSize.Level0)
496 {
497 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.clear();
498 std::string mySessName = "mySessName_test";
499 std::string peerSessName = "peerSessName_test";
500 std::string peerDevId = "peerDevId_test";
501 int32_t socketId = 1;
502 std::string key = mySessName + "_" + peerDevId;
503 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.insert(std::make_pair(key, socketId));
504 auto ret = SoftbusChannelAdapter::GetInstance().OpenSoftbusChannel(mySessName, peerSessName, peerDevId);
505 EXPECT_EQ(ERR_DH_AVT_SESSION_HAS_OPENED, ret);
506
507 mySessName = "mySessName_avtrans.data";
508 ret = SoftbusChannelAdapter::GetInstance().OpenSoftbusChannel(mySessName, peerSessName, peerDevId);
509 EXPECT_EQ(ERR_DH_AVT_SESSION_ERROR, ret);
510 }
511
512 HWTEST_F(DaudioInputTest, SendBytesData_001, TestSize.Level0)
513 {
514 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.clear();
515 std::string sessName = "sessName_test";
516 std::string peerDevId = "peerDevId_test";
517 std::string data = "data_test";
518 int32_t socketId = 1;
519 auto ret = SoftbusChannelAdapter::GetInstance().SendBytesData(sessName, peerDevId, data);
520 EXPECT_EQ(ERR_DH_AVT_SEND_DATA_FAILED, ret);
521
522 std::string key = sessName + "_" + peerDevId;
523 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.insert(std::make_pair(key, socketId));
524 ret = SoftbusChannelAdapter::GetInstance().SendBytesData(sessName, peerDevId, data);
525 EXPECT_EQ(ERR_DH_AVT_SEND_DATA_FAILED, ret);
526 }
527
528 HWTEST_F(DaudioInputTest, GetSessionNameById_001, TestSize.Level0)
529 {
530 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.clear();
531 std::string sessName = "sessName_test";
532 std::string peerDevId = "peerDevId_test";
533 int32_t sessionId = 1;
534 ShutdownReason reason = ShutdownReason::SHUTDOWN_REASON_UNKNOWN;
535 uint32_t dataLen = 100;
536 char data[10] = "data_test";
537 SoftbusChannelAdapter::GetInstance().OnSoftbusChannelClosed(sessionId, reason);
538 SoftbusChannelAdapter::GetInstance().OnSoftbusBytesReceived(sessionId, data, dataLen);
539 auto ret = SoftbusChannelAdapter::GetInstance().GetSessionNameById(sessionId);
540 EXPECT_EQ(EMPTY_STRING, ret);
541
542 std::string key = sessName + "_" + peerDevId;
543 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.insert(std::make_pair(key, sessionId));
544 SoftbusChannelAdapter::GetInstance().OnSoftbusChannelClosed(sessionId, reason);
545 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.insert(std::make_pair(key, sessionId));
546 ret = SoftbusChannelAdapter::GetInstance().GetSessionNameById(sessionId);
547 EXPECT_EQ(key, ret);
548 }
549
550 HWTEST_F(DaudioInputTest, GetPeerDevIdBySessId_001, TestSize.Level0)
551 {
552 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.clear();
553 std::string peerDevId = "peerDevIdtest";
554 std::string sessName = "sessNametest";
555 int32_t sessionId = 1;
556 std::string key = sessName + peerDevId;
557 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.insert(std::make_pair(key, sessionId));
558 auto ret = SoftbusChannelAdapter::GetInstance().GetPeerDevIdBySessId(2);
559 EXPECT_EQ(EMPTY_STRING, ret);
560
561 ret = SoftbusChannelAdapter::GetInstance().GetPeerDevIdBySessId(sessionId);
562 EXPECT_EQ(EMPTY_STRING, ret);
563
564 key = sessName + "_" + peerDevId;
565 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.insert(std::make_pair(key, sessionId));
566 ret = SoftbusChannelAdapter::GetInstance().GetPeerDevIdBySessId(sessionId);
567 EXPECT_EQ(peerDevId, ret);
568
569 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.clear();
570 peerDevId = "av.trans.special.device.id";
571 key = sessName + "_" + peerDevId;
572 SoftbusChannelAdapter::GetInstance().devId2SessIdMap_.insert(std::make_pair(key, sessionId));
573 ret = SoftbusChannelAdapter::GetInstance().GetPeerDevIdBySessId(sessionId);
574 EXPECT_EQ(EMPTY_STRING, ret);
575 }
576 } // namespace DistributedHardware
577 } // namespace OHOS