1 /*
2 * Copyright (c) 2021-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 "component_loader_test.h"
17
18 #include "config_policy_utils.h"
19 #include "component_loader.h"
20 #include "distributed_hardware_log.h"
21 #include "hitrace_meter.h"
22 #include "hidump_helper.h"
23 #include "kvstore_observer.h"
24 #include "cJSON.h"
25 #include "versionmanager/version_manager.h"
26
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace DistributedHardware {
SetUpTestCase(void)31 void ComponentLoaderTest::SetUpTestCase(void) {}
32
TearDownTestCase(void)33 void ComponentLoaderTest::TearDownTestCase(void) {}
34
SetUp()35 void ComponentLoaderTest::SetUp()
36 {
37 }
38
TearDown()39 void ComponentLoaderTest::TearDown()
40 {
41 }
42
43 HWTEST_F(ComponentLoaderTest, CheckComponentEnable_001, TestSize.Level0)
44 {
45 CompConfig config = {
46 .name = "name",
47 .type = DHType::UNKNOWN,
48 .compSourceSaId = 4801,
49 .compSinkSaId = 4802
50 };
51 auto ret = ComponentLoader::GetInstance().CheckComponentEnable(config);
52 EXPECT_EQ(true, ret);
53
54 CompConfig config1 = {
55 .name = "name",
56 .type = DHType::INPUT,
57 .compSourceSaId = 4801,
58 .compSinkSaId = 4802
59 };
60 ret = ComponentLoader::GetInstance().CheckComponentEnable(config1);
61 EXPECT_EQ(false, ret);
62 }
63
64 /**
65 * @tc.name: GetLocalDHVersion_001
66 * @tc.desc: Verify the GetLocalDHVersion function.
67 * @tc.type: FUNC
68 * @tc.require: AR000GHSK3
69 */
70 HWTEST_F(ComponentLoaderTest, GetLocalDHVersion_001, TestSize.Level0)
71 {
72 DHVersion dhVersion;
73 ComponentLoader::GetInstance().isLocalVersionInit_.store(false);
74 ComponentLoader::GetInstance().StoreLocalDHVersionInDB();
75 auto ret = ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion);
76 EXPECT_EQ(ERR_DH_FWK_LOADER_GET_LOCAL_VERSION_FAIL, ret);
77
78 ComponentLoader::GetInstance().isLocalVersionInit_.store(true);
79 ret = ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion);
80 EXPECT_EQ(DH_FWK_SUCCESS, ret);
81 }
82
83 /**
84 * @tc.name: GetHardwareHandler_001
85 * @tc.desc: Verify the GetHardwareHandler function.
86 * @tc.type: FUNC
87 * @tc.require: AR000GHSK3
88 */
89 HWTEST_F(ComponentLoaderTest, GetHardwareHandler_001, TestSize.Level0)
90 {
91 IHardwareHandler *hardwareHandlerPtr = nullptr;
92 auto ret = ComponentLoader::GetInstance().GetHardwareHandler(DHType::UNKNOWN, hardwareHandlerPtr);
93 EXPECT_EQ(ERR_DH_FWK_LOADER_HANDLER_IS_NULL, ret);
94
95 CompHandler comHandler;
96 comHandler.hardwareHandler = nullptr;
97 ComponentLoader::GetInstance().compHandlerMap_[DHType::AUDIO] = comHandler;
98 ret = ComponentLoader::GetInstance().GetHardwareHandler(DHType::AUDIO, hardwareHandlerPtr);
99 EXPECT_EQ(ERR_DH_FWK_LOADER_HANDLER_IS_NULL, ret);
100 ComponentLoader::GetInstance().compHandlerMap_.clear();
101 }
102
103 /**
104 * @tc.name: GetSource_001
105 * @tc.desc: Verify the GetSource function.
106 * @tc.type: FUNC
107 * @tc.require: AR000GHSK3
108 */
109 HWTEST_F(ComponentLoaderTest, GetSource_001, TestSize.Level0)
110 {
111 IDistributedHardwareSource *sourcePtr = nullptr;
112 auto ret = ComponentLoader::GetInstance().GetSource(DHType::UNKNOWN, sourcePtr);
113 EXPECT_EQ(ERR_DH_FWK_LOADER_HANDLER_IS_NULL, ret);
114
115 CompHandler comHandler;
116 comHandler.sourceHandler = nullptr;
117 ComponentLoader::GetInstance().compHandlerMap_[DHType::AUDIO] = comHandler;
118 ret = ComponentLoader::GetInstance().GetSource(DHType::AUDIO, sourcePtr);
119 EXPECT_EQ(ERR_DH_FWK_LOADER_HANDLER_IS_NULL, ret);
120 }
121
122 /**
123 * @tc.name: GetSink_001
124 * @tc.desc: Verify the GetSink function.
125 * @tc.type: FUNC
126 * @tc.require: AR000GHSK3
127 */
128 HWTEST_F(ComponentLoaderTest, GetSink_001, TestSize.Level0)
129 {
130 IDistributedHardwareSink *sinkPtr = nullptr;
131 auto ret = ComponentLoader::GetInstance().GetSink(DHType::UNKNOWN, sinkPtr);
132 EXPECT_EQ(ERR_DH_FWK_LOADER_HANDLER_IS_NULL, ret);
133
134 CompHandler comHandler;
135 comHandler.sinkHandler = nullptr;
136 ComponentLoader::GetInstance().compHandlerMap_[DHType::AUDIO] = comHandler;
137 ret = ComponentLoader::GetInstance().GetSink(DHType::AUDIO, sinkPtr);
138 EXPECT_EQ(ERR_DH_FWK_LOADER_HANDLER_IS_NULL, ret);
139 }
140
141 /**
142 * @tc.name: Readfile_001
143 * @tc.desc: Verify the Readfile function.
144 * @tc.type: FUNC
145 * @tc.require: AR000GHSK3
146 */
147 HWTEST_F(ComponentLoaderTest, Readfile_001, TestSize.Level0)
148 {
149 std::string filePath = "";
150 auto ret = ComponentLoader::GetInstance().Readfile(filePath);
151 EXPECT_EQ("", ret);
152 }
153
154 /**
155 * @tc.name: GetHandler_001
156 * @tc.desc: Verify the GetHandler function.
157 * @tc.type: FUNC
158 * @tc.require: AR000GHSK3
159 */
160 HWTEST_F(ComponentLoaderTest, GetHandler_001, TestSize.Level0)
161 {
162 std::string soNameEmpty = "";
163 auto handler = ComponentLoader::GetInstance().GetHandler(soNameEmpty);
164 EXPECT_EQ(nullptr, handler);
165 }
166
167 /**
168 * @tc.name: component_loader_test_017
169 * @tc.desc: Verify the GetCompPathAndVersion function.
170 * @tc.type: FUNC
171 * @tc.require: AR000GHSK3
172 */
173 HWTEST_F(ComponentLoaderTest, component_loader_test_017, TestSize.Level0)
174 {
175 std::string jsonStr = "";
176 std::map<DHType, CompConfig> dhtypeMap;
177 int32_t ret = ComponentLoader::GetInstance().GetCompPathAndVersion(jsonStr, dhtypeMap);
178 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
179 }
180
181 /**
182 * @tc.name: component_loader_test_018
183 * @tc.desc: Verify the GetCompPathAndVersion function.
184 * @tc.type: FUNC
185 * @tc.require: AR000GHSK3
186 */
187 HWTEST_F(ComponentLoaderTest, component_loader_test_018, TestSize.Level0)
188 {
189 const char *NAME = "NAME";
190 const char *TYPE = "TYPE";
191 const char *PATH = "PATH";
192 cJSON* json0bject = cJSON_CreateObject();
193 if (json0bject == nullptr) {
194 return;
195 }
196 cJSON* compVers = cJSON_CreateObject();
197 if (compVers == nullptr) {
198 cJSON_Delete(json0bject);
199 return;
200 }
201 cJSON_AddStringToObject(compVers, NAME, "name");
202 cJSON_AddNumberToObject(compVers, TYPE, 1111);
203 cJSON_AddItemToObject(json0bject, PATH, compVers);
204 char* cjson = cJSON_PrintUnformatted(json0bject);
205 if (cjson == nullptr) {
206 cJSON_Delete(json0bject);
207 return;
208 }
209 std::string jsonStr(cjson);
210 std::map<DHType, CompConfig> dhtypeMap;
211 int32_t ret = ComponentLoader::GetInstance().GetCompPathAndVersion(jsonStr, dhtypeMap);
212 cJSON_free(cjson);
213 cJSON_Delete(json0bject);
214 EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret);
215 }
216
217 /**
218 * @tc.name: IsDHTypeExist_001
219 * @tc.desc: Verify the IsDHTypeExist function.
220 * @tc.type: FUNC
221 * @tc.require: AR000GHSK3
222 */
223 HWTEST_F(ComponentLoaderTest, IsDHTypeExist_001, TestSize.Level0)
224 {
225 CompHandler comHandler;
226 ComponentLoader::GetInstance().compHandlerMap_[DHType::AUDIO] = comHandler;
227 bool ret = ComponentLoader::GetInstance().IsDHTypeExist(DHType::AUDIO);
228 EXPECT_EQ(true, ret);
229 ComponentLoader::GetInstance().compHandlerMap_.clear();
230 }
231
232 /**
233 * @tc.name: GetSourceSaId_001
234 * @tc.desc: Verify the GetSourceSaId function.
235 * @tc.type: FUNC
236 * @tc.require: AR000GHSK3
237 */
238 HWTEST_F(ComponentLoaderTest, GetSourceSaId_001, TestSize.Level0)
239 {
240 CompHandler comHandler;
241 ComponentLoader::GetInstance().compHandlerMap_[DHType::AUDIO] = comHandler;
242 int32_t ret = ComponentLoader::GetInstance().GetSourceSaId(DHType::UNKNOWN);
243 EXPECT_EQ(DEFAULT_SA_ID, ret);
244
245 comHandler.sourceSaId = 1;
246 ComponentLoader::GetInstance().compHandlerMap_[DHType::AUDIO] = comHandler;
247 ret = ComponentLoader::GetInstance().GetSourceSaId(DHType::AUDIO);
248 EXPECT_EQ(1, ret);
249 }
250
251 /**
252 * @tc.name: component_loader_test_023
253 * @tc.desc: Verify the ReleaseHandler function.
254 * @tc.type: FUNC
255 * @tc.require: AR000GHSK3
256 */
257 HWTEST_F(ComponentLoaderTest, component_loader_test_023, TestSize.Level0)
258 {
259 void *handler = nullptr;
260 int32_t ret = ComponentLoader::GetInstance().ReleaseHandler(handler);
261 EXPECT_EQ(ERR_DH_FWK_LOADER_HANDLER_IS_NULL, ret);
262 }
263
264 /**
265 * @tc.name: component_loader_test_024
266 * @tc.desc: Verify the ReleaseHardwareHandler function.
267 * @tc.type: FUNC
268 * @tc.require: AR000GHSK3
269 */
270 HWTEST_F(ComponentLoaderTest, component_loader_test_024, TestSize.Level0)
271 {
272 DHType dhType = DHType::GPS;
273 int32_t ret = ComponentLoader::GetInstance().ReleaseHardwareHandler(dhType);
274 EXPECT_EQ(ERR_DH_FWK_TYPE_NOT_EXIST, ret);
275 }
276
277 /**
278 * @tc.name: component_loader_test_025
279 * @tc.desc: Verify the ReleaseSource function.
280 * @tc.type: FUNC
281 * @tc.require: AR000GHSK3
282 */
283 HWTEST_F(ComponentLoaderTest, component_loader_test_025, TestSize.Level0)
284 {
285 DHType dhType = DHType::GPS;
286 int32_t ret = ComponentLoader::GetInstance().ReleaseSource(dhType);
287 EXPECT_EQ(ERR_DH_FWK_TYPE_NOT_EXIST, ret);
288 }
289
290 /**
291 * @tc.name: component_loader_test_026
292 * @tc.desc: Verify the ReleaseSink function.
293 * @tc.type: FUNC
294 * @tc.require: AR000GHSK3
295 */
296 HWTEST_F(ComponentLoaderTest, component_loader_test_026, TestSize.Level0)
297 {
298 DHType dhType = DHType::GPS;
299 int32_t ret = ComponentLoader::GetInstance().ReleaseSink(dhType);
300 EXPECT_EQ(ERR_DH_FWK_TYPE_NOT_EXIST, ret);
301 }
302
303 /**
304 * @tc.name: component_loader_test_027
305 * @tc.desc: Verify the ReleaseSink function.
306 * @tc.type: FUNC
307 * @tc.require: AR000GHSK3
308 */
309 HWTEST_F(ComponentLoaderTest, component_loader_test_027, TestSize.Level0)
310 {
311 DHType dhType = DHType::GPS;
312 bool ret = ComponentLoader::GetInstance().IsDHTypeExist(dhType);
313 EXPECT_EQ(false, ret);
314 }
315
316 /**
317 * @tc.name: component_loader_test_028
318 * @tc.desc: Verify the GetDHTypeBySrcSaId function.
319 * @tc.type: FUNC
320 * @tc.require: AR000GHSK3
321 */
322 HWTEST_F(ComponentLoaderTest, component_loader_test_028, TestSize.Level0)
323 {
324 int32_t saId = 4801;
325 DHType dhType = ComponentLoader::GetInstance().GetDHTypeBySrcSaId(saId);
326 EXPECT_EQ(dhType, DHType::UNKNOWN);
327 }
328
329 /**
330 * @tc.name: component_loader_test_029
331 * @tc.desc: Verify the from_json function.
332 * @tc.type: FUNC
333 * @tc.require: AR000GHSK3
334 */
335 HWTEST_F(ComponentLoaderTest, component_loader_test_029, TestSize.Level0)
336 {
337 CompConfig cfg;
338 cJSON *json = cJSON_CreateObject();
339 if (json == nullptr) {
340 return;
341 }
342 cJSON_AddNumberToObject(json, COMP_NAME.c_str(), 4801);
343
344 from_json(json, cfg);
345 cJSON_Delete(json);
346 EXPECT_EQ(true, cfg.name.empty());
347 }
348
349 /**
350 * @tc.name: component_loader_test_030
351 * @tc.desc: Verify the from_json function.
352 * @tc.type: FUNC
353 * @tc.require: AR000GHSK3
354 */
355 HWTEST_F(ComponentLoaderTest, component_loader_test_030, TestSize.Level0)
356 {
357 CompConfig cfg;
358 cJSON *json = cJSON_CreateObject();
359 if (json == nullptr) {
360 return;
361 }
362 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "name");
363 cJSON_AddNumberToObject(json, COMP_TYPE.c_str(), 0x02);
364
365 from_json(json, cfg);
366 cJSON_Delete(json);
367 EXPECT_NE(DHType::AUDIO, cfg.type);
368 }
369
370 /**
371 * @tc.name: component_loader_test_031
372 * @tc.desc: Verify the from_json function.
373 * @tc.type: FUNC
374 * @tc.require: AR000GHSK3
375 */
376 HWTEST_F(ComponentLoaderTest, component_loader_test_031, TestSize.Level0)
377 {
378 CompConfig cfg;
379 cJSON *json = cJSON_CreateObject();
380 if (json == nullptr) {
381 return;
382 }
383 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "name");
384 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "DHType::AUDIO");
385 cJSON_AddNumberToObject(json, COMP_HANDLER_LOC.c_str(), 4801);
386
387 from_json(json, cfg);
388 cJSON_Delete(json);
389 EXPECT_EQ(true, cfg.compHandlerLoc.empty());
390 }
391
392 /**
393 * @tc.name: component_loader_test_032
394 * @tc.desc: Verify the from_json function.
395 * @tc.type: FUNC
396 * @tc.require: AR000GHSK3
397 */
398 HWTEST_F(ComponentLoaderTest, component_loader_test_032, TestSize.Level0)
399 {
400 CompConfig cfg;
401 cJSON *json = cJSON_CreateObject();
402 if (json == nullptr) {
403 return;
404 }
405 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "name");
406 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "DHType::AUDIO");
407 cJSON_AddStringToObject(json, COMP_HANDLER_LOC.c_str(), "comp_handler_loc");
408 cJSON_AddNumberToObject(json, COMP_HANDLER_VERSION.c_str(), 4801);
409
410 from_json(json, cfg);
411 cJSON_Delete(json);
412 EXPECT_EQ(true, cfg.compHandlerVersion.empty());
413 }
414
415 /**
416 * @tc.name: component_loader_test_033
417 * @tc.desc: Verify the from_json function.
418 * @tc.type: FUNC
419 * @tc.require: AR000GHSK3
420 */
421 HWTEST_F(ComponentLoaderTest, component_loader_test_033, TestSize.Level0)
422 {
423 CompConfig cfg;
424 cJSON *json = cJSON_CreateObject();
425 if (json == nullptr) {
426 return;
427 }
428 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "name");
429 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "DHType::AUDIO");
430 cJSON_AddStringToObject(json, COMP_HANDLER_LOC.c_str(), "comp_handler_loc");
431 cJSON_AddStringToObject(json, COMP_HANDLER_VERSION.c_str(), "comp_handler_version");
432 cJSON_AddNumberToObject(json, COMP_SOURCE_LOC.c_str(), 4801);
433
434 from_json(json, cfg);
435 cJSON_Delete(json);
436 EXPECT_EQ(true, cfg.compSourceLoc.empty());
437 }
438
439 /**
440 * @tc.name: component_loader_test_034
441 * @tc.desc: Verify the from_json function.
442 * @tc.type: FUNC
443 * @tc.require: AR000GHSK3
444 */
445 HWTEST_F(ComponentLoaderTest, component_loader_test_034, TestSize.Level0)
446 {
447 CompConfig cfg;
448 cJSON *json = cJSON_CreateObject();
449 if (json == nullptr) {
450 return;
451 }
452 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "name");
453 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "DHType::AUDIO");
454 cJSON_AddStringToObject(json, COMP_HANDLER_LOC.c_str(), "comp_handler_loc");
455 cJSON_AddStringToObject(json, COMP_HANDLER_VERSION.c_str(), "comp_handler_version");
456 cJSON_AddStringToObject(json, COMP_SOURCE_LOC.c_str(), "comp_source_loc");
457 cJSON_AddNumberToObject(json, COMP_SOURCE_VERSION.c_str(), 4801);
458
459 from_json(json, cfg);
460 cJSON_Delete(json);
461 EXPECT_EQ(true, cfg.compSourceVersion.empty());
462 }
463
464 /**
465 * @tc.name: component_loader_test_035
466 * @tc.desc: Verify the from_json function.
467 * @tc.type: FUNC
468 * @tc.require: AR000GHSK3
469 */
470 HWTEST_F(ComponentLoaderTest, component_loader_test_035, TestSize.Level0)
471 {
472 CompConfig cfg;
473 cJSON *json = cJSON_CreateObject();
474 if (json == nullptr) {
475 return;
476 }
477 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "name");
478 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "DHType::AUDIO");
479 cJSON_AddStringToObject(json, COMP_HANDLER_LOC.c_str(), "comp_handler_loc");
480 cJSON_AddStringToObject(json, COMP_HANDLER_VERSION.c_str(), "comp_handler_version");
481 cJSON_AddStringToObject(json, COMP_SOURCE_LOC.c_str(), "comp_source_loc");
482 cJSON_AddStringToObject(json, COMP_SOURCE_VERSION.c_str(), "comp_source_version");
483 cJSON_AddStringToObject(json, COMP_SOURCE_SA_ID.c_str(), "comp_source_sa_id");
484
485 from_json(json, cfg);
486 cJSON_Delete(json);
487 EXPECT_NE(4801, cfg.compSourceSaId);
488 }
489
490 /**
491 * @tc.name: component_loader_test_036
492 * @tc.desc: Verify the from_json function.
493 * @tc.type: FUNC
494 * @tc.require: AR000GHSK3
495 */
496 HWTEST_F(ComponentLoaderTest, component_loader_test_036, TestSize.Level0)
497 {
498 CompConfig cfg;
499 cJSON *json = cJSON_CreateObject();
500 if (json == nullptr) {
501 return;
502 }
503 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "name");
504 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "DHType::AUDIO");
505 cJSON_AddStringToObject(json, COMP_HANDLER_LOC.c_str(), "comp_handler_loc");
506 cJSON_AddStringToObject(json, COMP_HANDLER_VERSION.c_str(), "comp_handler_version");
507 cJSON_AddStringToObject(json, COMP_SOURCE_LOC.c_str(), "comp_source_loc");
508 cJSON_AddStringToObject(json, COMP_SOURCE_VERSION.c_str(), "comp_source_version");
509 cJSON_AddStringToObject(json, COMP_SOURCE_SA_ID.c_str(), "4801");
510 cJSON_AddNumberToObject(json, COMP_SINK_LOC.c_str(), 4802);
511
512 from_json(json, cfg);
513 cJSON_Delete(json);
514 EXPECT_EQ(true, cfg.compSinkLoc.empty());
515 }
516
517 /**
518 * @tc.name: component_loader_test_037
519 * @tc.desc: Verify the from_json function.
520 * @tc.type: FUNC
521 * @tc.require: AR000GHSK3
522 */
523 HWTEST_F(ComponentLoaderTest, component_loader_test_037, TestSize.Level0)
524 {
525 CompConfig cfg;
526 cJSON *json = cJSON_CreateObject();
527 if (json == nullptr) {
528 return;
529 }
530 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "name");
531 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "DHType::AUDIO");
532 cJSON_AddStringToObject(json, COMP_HANDLER_LOC.c_str(), "comp_handler_loc");
533 cJSON_AddStringToObject(json, COMP_HANDLER_VERSION.c_str(), "comp_handler_version");
534 cJSON_AddStringToObject(json, COMP_SOURCE_LOC.c_str(), "comp_source_loc");
535 cJSON_AddStringToObject(json, COMP_SOURCE_VERSION.c_str(), "comp_source_version");
536 cJSON_AddNumberToObject(json, COMP_SOURCE_SA_ID.c_str(), 4801);
537 cJSON_AddStringToObject(json, COMP_SINK_LOC.c_str(), "comp_sink_loc");
538 cJSON_AddNumberToObject(json, COMP_SINK_VERSION.c_str(), 4802);
539
540 from_json(json, cfg);
541 cJSON_Delete(json);
542 EXPECT_EQ(true, cfg.compSinkVersion.empty());
543 }
544
545 /**
546 * @tc.name: component_loader_test_038
547 * @tc.desc: Verify the from_json function.
548 * @tc.type: FUNC
549 * @tc.require: AR000GHSK3
550 */
551 HWTEST_F(ComponentLoaderTest, component_loader_test_038, TestSize.Level0)
552 {
553 CompConfig cfg;
554 cJSON *json = cJSON_CreateObject();
555 if (json == nullptr) {
556 return;
557 }
558 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "name");
559 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "DHType::AUDIO");
560 cJSON_AddStringToObject(json, COMP_HANDLER_LOC.c_str(), "comp_handler_loc");
561 cJSON_AddStringToObject(json, COMP_HANDLER_VERSION.c_str(), "comp_handler_version");
562 cJSON_AddStringToObject(json, COMP_SOURCE_LOC.c_str(), "comp_source_loc");
563 cJSON_AddStringToObject(json, COMP_SOURCE_VERSION.c_str(), "comp_source_version");
564 cJSON_AddNumberToObject(json, COMP_SOURCE_SA_ID.c_str(), 4801);
565 cJSON_AddStringToObject(json, COMP_SINK_LOC.c_str(), "comp_sink_loc");
566 cJSON_AddStringToObject(json, COMP_SINK_VERSION.c_str(), "comp_sink_version");
567 cJSON_AddStringToObject(json, COMP_SINK_SA_ID.c_str(), "4802");
568
569 from_json(json, cfg);
570 cJSON_Delete(json);
571 EXPECT_NE(4802, cfg.compSinkSaId);
572 }
573
574 /**
575 * @tc.name: component_loader_test_039
576 * @tc.desc: Verify the from_json function.
577 * @tc.type: FUNC
578 * @tc.require: AR000GHSK3
579 */
580 HWTEST_F(ComponentLoaderTest, component_loader_test_039, TestSize.Level0)
581 {
582 CompConfig cfg;
583 cJSON *json = cJSON_CreateObject();
584 if (json == nullptr) {
585 return;
586 }
587 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "name");
588 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "DHType::AUDIO");
589 cJSON_AddStringToObject(json, COMP_HANDLER_LOC.c_str(), "comp_handler_loc");
590 cJSON_AddStringToObject(json, COMP_HANDLER_VERSION.c_str(), "comp_handler_version");
591 cJSON_AddStringToObject(json, COMP_SOURCE_LOC.c_str(), "comp_source_loc");
592 cJSON_AddStringToObject(json, COMP_SOURCE_VERSION.c_str(), "comp_source_version");
593 cJSON_AddNumberToObject(json, COMP_SOURCE_SA_ID.c_str(), 4801);
594 cJSON_AddStringToObject(json, COMP_SINK_LOC.c_str(), "comp_sink_loc");
595 cJSON_AddStringToObject(json, COMP_SINK_VERSION.c_str(), "comp_sink_version");
596 cJSON_AddNumberToObject(json, COMP_SINK_SA_ID.c_str(), 4802);
597
598 from_json(json, cfg);
599 cJSON_Delete(json);
600 EXPECT_EQ(4802, cfg.compSinkSaId);
601 }
602
603 HWTEST_F(ComponentLoaderTest, ParseSink_001, TestSize.Level0)
604 {
605 CompConfig cfg;
606 cJSON *json = cJSON_CreateObject();
607 if (json == nullptr) {
608 return;
609 }
610 cJSON_AddNumberToObject(json, COMP_SINK_LOC.c_str(), 100);
611 auto ret = ParseSink(json, cfg);
612 cJSON_Delete(json);
613 EXPECT_EQ(ret, ERR_DH_FWK_JSON_PARSE_FAILED);
614 }
615
616 HWTEST_F(ComponentLoaderTest, ParseSink_002, TestSize.Level0)
617 {
618 CompConfig cfg;
619 cJSON *json = cJSON_CreateObject();
620 if (json == nullptr) {
621 return;
622 }
623 cJSON_AddStringToObject(json, COMP_SINK_LOC.c_str(), "comp_sink_loc_test");
624 cJSON_AddNumberToObject(json, COMP_SINK_VERSION.c_str(), 100);
625 auto ret = ParseSink(json, cfg);
626 cJSON_Delete(json);
627 EXPECT_EQ(ret, ERR_DH_FWK_JSON_PARSE_FAILED);
628 }
629
630 HWTEST_F(ComponentLoaderTest, ParseSink_003, TestSize.Level0)
631 {
632 CompConfig cfg;
633 cJSON *json = cJSON_CreateObject();
634 if (json == nullptr) {
635 return;
636 }
637 cJSON_AddStringToObject(json, COMP_SINK_LOC.c_str(), "comp_sink_loc_test");
638 cJSON_AddStringToObject(json, COMP_SINK_VERSION.c_str(), "1.0");
639 cJSON_AddStringToObject(json, COMP_SINK_SA_ID.c_str(), "comp_sink_sa_id_test");
640 auto ret = ParseSink(json, cfg);
641 cJSON_Delete(json);
642 EXPECT_EQ(ret, ERR_DH_FWK_JSON_PARSE_FAILED);
643 }
644
645 HWTEST_F(ComponentLoaderTest, ParseSink_004, TestSize.Level0)
646 {
647 CompConfig cfg;
648 cJSON *json = cJSON_CreateObject();
649 if (json == nullptr) {
650 return;
651 }
652 cJSON_AddStringToObject(json, COMP_SINK_LOC.c_str(), "comp_sink_loc_test");
653 cJSON_AddStringToObject(json, COMP_SINK_VERSION.c_str(), "1.0");
654 cJSON_AddNumberToObject(json, COMP_SINK_SA_ID.c_str(), 4801);
655 auto ret = ParseSink(json, cfg);
656 cJSON_Delete(json);
657 EXPECT_EQ(ret, DH_FWK_SUCCESS);
658 }
659
660 HWTEST_F(ComponentLoaderTest, ParseResourceDesc_001, TestSize.Level0)
661 {
662 CompConfig cfg;
663 cJSON *array = cJSON_CreateArray();
664 if (array == nullptr) {
665 return;
666 }
667 cJSON *obj = cJSON_CreateObject();
668 if (obj == nullptr) {
669 cJSON_Delete(array);
670 return;
671 }
672 cJSON_AddStringToObject(obj, COMP_SUBTYPE.c_str(), "comp_subtype");
673 cJSON_AddBoolToObject(obj, COMP_SENSITIVE.c_str(), true);
674 cJSON_AddItemToArray(array, obj);
675 cJSON *json = cJSON_CreateObject();
676 if (json == nullptr) {
677 cJSON_Delete(array);
678 return;
679 }
680 cJSON_AddItemToObject(json, COMP_RESOURCE_DESC.c_str(), array);
681 auto ret = ParseResourceDesc(json, cfg);
682 cJSON_Delete(json);
683
684 CompConfig config;
685 cJSON *component = cJSON_CreateObject();
686 if (component == nullptr) {
687 return;
688 }
689 cJSON_AddNumberToObject(component, COMP_NAME.c_str(), 1);
690 cJSON_AddNumberToObject(component, COMP_TYPE.c_str(), 1);
691 cJSON_AddNumberToObject(component, COMP_HANDLER_LOC.c_str(), 1);
692 cJSON_AddNumberToObject(component, COMP_HANDLER_VERSION.c_str(), 1.0);
693 cJSON_AddNumberToObject(component, COMP_SOURCE_LOC.c_str(), 1);
694 cJSON_AddNumberToObject(component, COMP_SOURCE_VERSION.c_str(), 1.0);
695 cJSON_AddStringToObject(component, COMP_SOURCE_SA_ID.c_str(), "4801");
696 cJSON_AddNumberToObject(component, COMP_SINK_LOC.c_str(), 1);
697 cJSON_AddNumberToObject(component, COMP_SINK_VERSION.c_str(), 1.0);
698 cJSON_AddStringToObject(component, COMP_SINK_SA_ID.c_str(), "4802");
699 cJSON_AddStringToObject(component, COMP_RESOURCE_DESC.c_str(), "comp_resource_desc");
700 ComponentLoader::GetInstance().ParseCompConfigFromJson(component, config);
701 cJSON_Delete(component);
702 EXPECT_EQ(ret, DH_FWK_SUCCESS);
703 }
704
705 HWTEST_F(ComponentLoaderTest, ParseResourceDesc_002, TestSize.Level0)
706 {
707 CompConfig cfg;
708 cJSON *json = cJSON_CreateObject();
709 if (json == nullptr) {
710 return;
711 }
712 cJSON_AddNumberToObject(json, COMP_NAME.c_str(), 100);
713 auto ret = ParseComponent(json, cfg);
714 cJSON_Delete(json);
715 EXPECT_EQ(ret, ERR_DH_FWK_JSON_PARSE_FAILED);
716
717 CompConfig config1;
718 cJSON *component1 = cJSON_CreateObject();
719 if (component1 == nullptr) {
720 return;
721 }
722 cJSON_AddStringToObject(component1, COMP_NAME.c_str(), "comp_name_test");
723 cJSON_AddStringToObject(component1, COMP_TYPE.c_str(), "comp_type_test");
724 cJSON_AddStringToObject(component1, COMP_HANDLER_LOC.c_str(), "comp_handler_loc_test");
725 cJSON_AddStringToObject(component1, COMP_HANDLER_VERSION.c_str(), "comp_handler_version_test");
726 cJSON_AddStringToObject(component1, COMP_SOURCE_LOC.c_str(), "comp_source_loc_test");
727 cJSON_AddStringToObject(component1, COMP_SOURCE_VERSION.c_str(), "comp_source_verison_test");
728 cJSON_AddNumberToObject(component1, COMP_SOURCE_SA_ID.c_str(), 4801);
729 cJSON_AddStringToObject(component1, COMP_SINK_LOC.c_str(), "comp_sink_loc_test");
730 cJSON_AddStringToObject(component1, COMP_SINK_VERSION.c_str(), "com_sink_version_test");
731 cJSON_AddNumberToObject(component1, COMP_SINK_SA_ID.c_str(), 4802);
732 cJSON_AddStringToObject(component1, COMP_RESOURCE_DESC.c_str(), "comp_resource_desc");
733 ComponentLoader::GetInstance().ParseCompConfigFromJson(component1, config1);
734 cJSON_Delete(component1);
735 }
736
737 HWTEST_F(ComponentLoaderTest, from_json_001, TestSize.Level0)
738 {
739 CompConfig cfg;
740 cJSON *json = cJSON_CreateObject();
741 if (json == nullptr) {
742 return;
743 }
744 cJSON_AddNumberToObject(json, COMP_NAME.c_str(), 100);
745 from_json(json, cfg);
746 cJSON_Delete(json);
747
748 cJSON *Json1 = cJSON_CreateObject();
749 if (Json1 == nullptr) {
750 return;
751 }
752 cJSON_AddStringToObject(Json1, COMP_NAME.c_str(), "comp_name_test");
753 cJSON_AddStringToObject(Json1, COMP_TYPE.c_str(), "comp_type_test");
754 cJSON_AddStringToObject(Json1, COMP_HANDLER_LOC.c_str(), "comp_handler_loc_test");
755 cJSON_AddStringToObject(Json1, COMP_HANDLER_VERSION.c_str(), "1.0");
756 cJSON_AddNumberToObject(Json1, COMP_SOURCE_LOC.c_str(), 100);
757 from_json(Json1, cfg);
758 cJSON_Delete(Json1);
759
760 cJSON *Json2 = cJSON_CreateObject();
761 if (Json2 == nullptr) {
762 return;
763 }
764 cJSON_AddStringToObject(Json2, COMP_NAME.c_str(), "comp_name_test");
765 cJSON_AddStringToObject(Json2, COMP_TYPE.c_str(), "comp_type_test");
766 cJSON_AddStringToObject(Json2, COMP_HANDLER_LOC.c_str(), "comp_handler_loc_test");
767 cJSON_AddStringToObject(Json2, COMP_HANDLER_VERSION.c_str(), "1.0");
768 cJSON_AddStringToObject(Json2, COMP_SOURCE_LOC.c_str(), "comp_source_loc_test");
769 cJSON_AddStringToObject(Json2, COMP_SOURCE_VERSION.c_str(), "1.0");
770 cJSON_AddNumberToObject(Json2, COMP_SOURCE_SA_ID.c_str(), 4801);
771 cJSON_AddNumberToObject(Json2, COMP_SINK_LOC.c_str(), 100);
772 from_json(Json2, cfg);
773 EXPECT_EQ(4801, static_cast<int32_t>(cJSON_GetObjectItem(Json2, COMP_SOURCE_SA_ID.c_str())->valuedouble));
774 cJSON_Delete(Json2);
775 }
776
777 HWTEST_F(ComponentLoaderTest, ParseComponent_001, TestSize.Level0)
778 {
779 CompConfig cfg;
780 cJSON *json = cJSON_CreateObject();
781 if (json == nullptr) {
782 return;
783 }
784 cJSON_AddNumberToObject(json, COMP_NAME.c_str(), 100);
785 auto ret = ParseComponent(json, cfg);
786 cJSON_Delete(json);
787 EXPECT_EQ(ret, ERR_DH_FWK_JSON_PARSE_FAILED);
788 }
789
790 HWTEST_F(ComponentLoaderTest, ParseComponent_002, TestSize.Level0)
791 {
792 CompConfig cfg;
793 cJSON *json = cJSON_CreateObject();
794 if (json == nullptr) {
795 return;
796 }
797 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "comp_name_test");
798 cJSON_AddNumberToObject(json, COMP_TYPE.c_str(), 100);
799 auto ret = ParseComponent(json, cfg);
800 cJSON_Delete(json);
801 EXPECT_EQ(ret, ERR_DH_FWK_JSON_PARSE_FAILED);
802 }
803
804 HWTEST_F(ComponentLoaderTest, ParseComponent_003, TestSize.Level0)
805 {
806 CompConfig cfg;
807 cJSON *json = cJSON_CreateObject();
808 if (json == nullptr) {
809 return;
810 }
811 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "comp_name_test");
812 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "comp_type_test");
813 cJSON_AddNumberToObject(json, COMP_HANDLER_LOC.c_str(), 100);
814 auto ret = ParseComponent(json, cfg);
815 cJSON_Delete(json);
816 EXPECT_EQ(ret, ERR_DH_FWK_JSON_PARSE_FAILED);
817 }
818
819 HWTEST_F(ComponentLoaderTest, ParseComponent_004, TestSize.Level0)
820 {
821 CompConfig cfg;
822 cJSON *json = cJSON_CreateObject();
823 if (json == nullptr) {
824 return;
825 }
826 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "comp_name_test");
827 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "comp_type_test");
828 cJSON_AddStringToObject(json, COMP_HANDLER_LOC.c_str(), "comp_handler_loc_test");
829 cJSON_AddNumberToObject(json, COMP_HANDLER_VERSION.c_str(), 100);
830 auto ret = ParseComponent(json, cfg);
831 cJSON_Delete(json);
832 EXPECT_EQ(ret, ERR_DH_FWK_JSON_PARSE_FAILED);
833 }
834
835 HWTEST_F(ComponentLoaderTest, ParseComponent_005, TestSize.Level0)
836 {
837 CompConfig cfg;
838 cJSON *json = cJSON_CreateObject();
839 if (json == nullptr) {
840 return;
841 }
842 cJSON_AddStringToObject(json, COMP_NAME.c_str(), "comp_name_test");
843 cJSON_AddStringToObject(json, COMP_TYPE.c_str(), "comp_type_test");
844 cJSON_AddStringToObject(json, COMP_HANDLER_LOC.c_str(), "comp_handler_loc_test");
845 cJSON_AddStringToObject(json, COMP_HANDLER_VERSION.c_str(), "1.0");
846 auto ret = ParseComponent(json, cfg);
847 cJSON_Delete(json);
848 EXPECT_EQ(ret, DH_FWK_SUCCESS);
849 }
850
851 HWTEST_F(ComponentLoaderTest, ParseSource_001, TestSize.Level0)
852 {
853 CompConfig cfg;
854 cJSON *json = cJSON_CreateObject();
855 if (json == nullptr) {
856 return;
857 }
858 cJSON_AddNumberToObject(json, COMP_SOURCE_LOC.c_str(), 100);
859 auto ret = ParseSource(json, cfg);
860 cJSON_Delete(json);
861 EXPECT_EQ(ret, ERR_DH_FWK_JSON_PARSE_FAILED);
862 }
863
864 HWTEST_F(ComponentLoaderTest, ParseSource_002, TestSize.Level0)
865 {
866 CompConfig cfg;
867 cJSON *json = cJSON_CreateObject();
868 if (json == nullptr) {
869 return;
870 }
871 cJSON_AddStringToObject(json, COMP_SOURCE_LOC.c_str(), "comp_source_loc_test");
872 cJSON_AddNumberToObject(json, COMP_SOURCE_VERSION.c_str(), 100);
873 auto ret = ParseSource(json, cfg);
874 cJSON_Delete(json);
875 EXPECT_EQ(ret, ERR_DH_FWK_JSON_PARSE_FAILED);
876 }
877
878 HWTEST_F(ComponentLoaderTest, ParseSource_003, TestSize.Level0)
879 {
880 CompConfig cfg;
881 cJSON *json = cJSON_CreateObject();
882 if (json == nullptr) {
883 return;
884 }
885 cJSON_AddStringToObject(json, COMP_SOURCE_LOC.c_str(), "comp_source_loc_test");
886 cJSON_AddStringToObject(json, COMP_SOURCE_VERSION.c_str(), "1.0");
887 cJSON_AddStringToObject(json, COMP_SOURCE_SA_ID.c_str(), "4801");
888 auto ret = ParseSource(json, cfg);
889 cJSON_Delete(json);
890 EXPECT_EQ(ret, ERR_DH_FWK_JSON_PARSE_FAILED);
891 }
892
893 HWTEST_F(ComponentLoaderTest, ParseSource_004, TestSize.Level0)
894 {
895 CompConfig cfg;
896 cJSON *json = cJSON_CreateObject();
897 if (json == nullptr) {
898 return;
899 }
900 cJSON_AddStringToObject(json, COMP_SOURCE_LOC.c_str(), "comp_source_loc_test");
901 cJSON_AddStringToObject(json, COMP_SOURCE_VERSION.c_str(), "1.0");
902 cJSON_AddNumberToObject(json, COMP_SOURCE_SA_ID.c_str(), 4801);
903 auto ret = ParseSource(json, cfg);
904 cJSON_Delete(json);
905 EXPECT_EQ(ret, DH_FWK_SUCCESS);
906 }
907
908 HWTEST_F(ComponentLoaderTest, GetHardwareHandler_003, TestSize.Level0)
909 {
910 ComponentLoader::GetInstance().compHandlerMap_.clear();
911 DHType dhType = DHType::AUDIO;
912 IHardwareHandler *hardwareHandlerPtr = nullptr;
913 auto ret = ComponentLoader::GetInstance().GetHardwareHandler(dhType, hardwareHandlerPtr);
914 EXPECT_EQ(ret, ERR_DH_FWK_LOADER_HANDLER_IS_NULL);
915 }
916
917 HWTEST_F(ComponentLoaderTest, GetSource_003, TestSize.Level0)
918 {
919 ComponentLoader::GetInstance().compHandlerMap_.clear();
920 DHType dhType = DHType::AUDIO;
921 IDistributedHardwareSource *dhSourcePtr = nullptr;
922 auto ret = ComponentLoader::GetInstance().GetSource(dhType, dhSourcePtr);
923 EXPECT_EQ(ret, ERR_DH_FWK_LOADER_HANDLER_IS_NULL);
924 }
925
926 HWTEST_F(ComponentLoaderTest, GetSink_003, TestSize.Level0)
927 {
928 ComponentLoader::GetInstance().compHandlerMap_.clear();
929 DHType dhType = DHType::AUDIO;
930 IDistributedHardwareSink *dhSinkPtr = nullptr;
931 auto ret = ComponentLoader::GetInstance().GetSink(dhType, dhSinkPtr);
932 EXPECT_EQ(ret, ERR_DH_FWK_LOADER_HANDLER_IS_NULL);
933 }
934
935 HWTEST_F(ComponentLoaderTest, ReleaseHardwareHandler_002, TestSize.Level0)
936 {
937 ComponentLoader::GetInstance().compHandlerMap_.clear();
938 DHType dhType = DHType::AUDIO;
939 auto ret = ComponentLoader::GetInstance().ReleaseHardwareHandler(dhType);
940 EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST);
941
942 ret = ComponentLoader::GetInstance().ReleaseSource(dhType);
943 EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST);
944
945 ret = ComponentLoader::GetInstance().ReleaseSource(dhType);
946 EXPECT_EQ(ret, ERR_DH_FWK_TYPE_NOT_EXIST);
947
948 ret = ComponentLoader::GetInstance().GetSourceSaId(dhType);
949 EXPECT_EQ(ret, DEFAULT_SA_ID);
950 }
951
952 HWTEST_F(ComponentLoaderTest, GetDHTypeBySrcSaId_001, TestSize.Level0)
953 {
954 ComponentLoader::GetInstance().compHandlerMap_.clear();
955 int32_t saId = 4801;
956 auto ret = ComponentLoader::GetInstance().GetDHTypeBySrcSaId(saId);
957 EXPECT_EQ(ret, DHType::UNKNOWN);
958 }
959 } // namespace DistributedHardware
960 } // namespace OHOS
961