1 /*
2 * Copyright (c) 2022 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 #include "gtest/gtest.h"
16 #include "string_ex.h"
17 #include "system_ability_definition.h"
18 #include "test_log.h"
19 #include "tools.h"
20
21 #define private public
22 #include "parse_util.h"
23 using namespace std;
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace SAMGR {
29 namespace {
30 const std::string TEST_RESOURCE_PATH = "/data/test/resource/samgr/profile/";
31 const std::u16string TEST_PROCESS_NAME = u"sa_test";
32 const std::string EVENT_STRING;
33 const std::string EVENT_TYPE ;
34 const std::string EVENT_NAME ;
35 const std::string EVENT_VALUE ;
36 const int32_t TEST_NUM = 123;
37 const int32_t MOCK_SAID = 1492;
38 const int32_t TEST_PROFILE_SAID = 9999;
39 const int32_t TEST_PROFILE_SAID_INVAILD = 9990;
40 constexpr const char* SA_TAG_DEVICE_ON_LINE = "deviceonline";
41 constexpr const char* SA_TAG_SETTING_SWITCH = "settingswitch";
42 constexpr const char* SA_TAG_COMMON_EVENT = "commonevent";
43 constexpr const char* SA_TAG_PARAM = "param";
44 constexpr const char* SA_TAG_TIEMD_EVENT = "timedevent";
45 const string EXTENSIOON_BACKUP = "backup";
46 const string EXTENSIOON_RESTORE = "restore";
47 constexpr int32_t MAX_JSON_STRING_LENGTH = 128;
48 constexpr int32_t MAX_EXTENSIONO_NUM = 100;
49 }
50
51 class ParseUtilTest : public testing::Test {
52 public:
53 static void SetUpTestCase();
54 static void TearDownTestCase();
55 void SetUp();
56 void TearDown();
57 protected:
58 std::shared_ptr<ParseUtil> parser_;
59 };
60
SetUpTestCase()61 void ParseUtilTest::SetUpTestCase()
62 {
63 DTEST_LOG << "SetUpTestCase" << std::endl;
64 }
65
TearDownTestCase()66 void ParseUtilTest::TearDownTestCase()
67 {
68 DTEST_LOG << "TearDownTestCase" << std::endl;
69 }
70
SetUp()71 void ParseUtilTest::SetUp()
72 {
73 DTEST_LOG << "SetUp" << std::endl;
74 if (parser_ == nullptr) {
75 parser_ = std::make_shared<ParseUtil>();
76 }
77 }
78
TearDown()79 void ParseUtilTest::TearDown()
80 {
81 DTEST_LOG << "TearDown" << std::endl;
82 if (parser_ != nullptr) {
83 parser_->ClearResource();
84 }
85 }
86
87 /**
88 * @tc.name: ParseSaProfile001
89 * @tc.desc: Verify if can load not exist file
90 * @tc.type: FUNC
91 */
92 HWTEST_F(ParseUtilTest, ParseSaProfile001, TestSize.Level2)
93 {
94 DTEST_LOG << " ParseSaProfile001 start " << std::endl;
95 /**
96 * @tc.steps: step1. parse not exsit config file
97 * @tc.expected: step1. return false when load not exist file
98 */
99 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "notExist");
100 EXPECT_FALSE(ret);
101 }
102
103 /**
104 * @tc.name: GetSaProfiles001
105 * @tc.desc: Verify if not load sa file return empty list
106 * @tc.type: FUNC
107 */
108 HWTEST_F(ParseUtilTest, GetSaProfiles001, TestSize.Level3)
109 {
110 DTEST_LOG << " GetSaProfiles001 start " << std::endl;
111 /**
112 * @tc.steps: step1. Get empty config when not parse sa file.
113 * @tc.expected: step1. return empty list when not load sa file
114 */
115 list<SaProfile> profiles = parser_->GetAllSaProfiles();
116 EXPECT_TRUE(profiles.empty());
117 }
118
119 /**
120 * @tc.name: GetSaProfiles002
121 * @tc.desc: Verify if can load normal sa profile
122 * @tc.type: FUNC
123 */
124 HWTEST_F(ParseUtilTest, GetSaProfiles002, TestSize.Level3)
125 {
126 DTEST_LOG << " GetSaProfiles002 start " << std::endl;
127 /**
128 * @tc.steps: step1. Get correct profile when parse sa file.
129 * @tc.expected: step1. return correct profile object when load correct config file
130 */
131 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "profile.json");
132 ASSERT_TRUE(ret);
133 SaProfile saRunOnCreateTrue;
134 saRunOnCreateTrue.runOnCreate = true;
135 SaProfile saRunOnCreateFalse;
136 parser_->saProfiles_.emplace_back(saRunOnCreateTrue);
137 parser_->saProfiles_.emplace_back(saRunOnCreateFalse);
138 list<SaProfile> profiles = parser_->GetAllSaProfiles();
139 if (!profiles.empty()) {
140 SaProfile& profile = *(profiles.begin());
141 EXPECT_EQ(profile.process, TEST_PROCESS_NAME);
142 EXPECT_EQ(profile.saId, TEST_PROFILE_SAID);
143 }
144 }
145
146 /**
147 * @tc.name: ParseTrustConfig001
148 * @tc.desc: Verify if can load file with one sa
149 * @tc.type: FUNC
150 */
151 HWTEST_F(ParseUtilTest, ParseTrustConfig001, TestSize.Level1)
152 {
153 DTEST_LOG << " ParseTrustConfig001 start " << std::endl;
154 /**
155 * @tc.steps: step1. Get correct map when parse config file.
156 * @tc.expected: step1. return correct profile object when load correct config file
157 */
158 std::map<std::u16string, std::set<int32_t>> values;
159 bool ret = parser_->ParseTrustConfig(TEST_RESOURCE_PATH + "test_trust_one_sa.json", values);
160 ASSERT_TRUE(ret);
161 /**
162 * @tc.steps: step2. Check map values
163 * @tc.expected: step2. return expect values
164 */
165 for (const auto& [process, saIds] : values) {
166 EXPECT_EQ(Str16ToStr8(process), "test");
167 EXPECT_EQ(saIds.size(), 1);
168 EXPECT_EQ(saIds.count(1401), 1);
169 }
170 }
171
172 /**
173 * @tc.name: ParseTrustConfig002
174 * @tc.desc: Verify if can load file with muti sa
175 * @tc.type: FUNC
176 */
177 HWTEST_F(ParseUtilTest, ParseTrustConfig002, TestSize.Level1)
178 {
179 DTEST_LOG << " ParseTrustConfig002 start " << std::endl;
180 /**
181 * @tc.steps: step1. Get correct map when parse config file.
182 * @tc.expected: step1. return correct profile object when load correct config file
183 */
184 std::map<std::u16string, std::set<int32_t>> values;
185 bool ret = parser_->ParseTrustConfig(TEST_RESOURCE_PATH + "test_trust_muti_sa.json", values);
186 ASSERT_TRUE(ret);
187 /**
188 * @tc.steps: step2. Check map values
189 * @tc.expected: step2. return expect values
190 */
191 for (const auto& [process, saIds] : values) {
192 EXPECT_EQ(Str16ToStr8(process), "test");
193 EXPECT_EQ(saIds.size(), 5);
194 EXPECT_EQ(saIds.count(1401), 1);
195 }
196 }
197
198 /**
199 * @tc.name: ParseTrustConfig003
200 * @tc.desc: Verify if can load not invalid root file
201 * @tc.type: FUNC
202 */
203 HWTEST_F(ParseUtilTest, ParseTrustConfig003, TestSize.Level1)
204 {
205 DTEST_LOG << " ParseTrustConfig003 start " << std::endl;
206 /**
207 * @tc.steps: step1. Get correct map when parse config file.
208 * @tc.expected: step1. return false when load invalid root file
209 */
210 std::map<std::u16string, std::set<int32_t>> values;
211 bool ret = parser_->ParseTrustConfig(TEST_RESOURCE_PATH + "invalid_root_trust.json", values);
212 ASSERT_FALSE(ret);
213 }
214
215 /**
216 * @tc.name: ParseTrustConfig004
217 * @tc.desc: Verify if can load not exist file
218 * @tc.type: FUNC
219 */
220 HWTEST_F(ParseUtilTest, ParseTrustConfig004, TestSize.Level1)
221 {
222 DTEST_LOG << " ParseTrustConfig004 start " << std::endl;
223 /**
224 * @tc.steps: step1. Get correct profile when parse sa file.
225 * @tc.expected: step1. return false when not exist file
226 */
227 std::map<std::u16string, std::set<int32_t>> values;
228 bool ret = parser_->ParseTrustConfig(TEST_RESOURCE_PATH + "notExist", values);
229 ASSERT_FALSE(ret);
230 }
231
232 /**
233 * @tc.name: ParseTrustConfig005
234 * @tc.desc: Verify if can load invalid element config
235 * @tc.type: FUNC
236 */
237 HWTEST_F(ParseUtilTest, ParseTrustConfig005, TestSize.Level1)
238 {
239 DTEST_LOG << " ParseTrustConfig005 start " << std::endl;
240 /**
241 * @tc.steps: step1. Get correct profile when parse invalid element config.
242 * @tc.expected: step1. return correct profile object when load correct config file
243 */
244 std::map<std::u16string, std::set<int32_t>> values;
245 bool ret = parser_->ParseTrustConfig(TEST_RESOURCE_PATH + "invalid_element_trust.json", values);
246 ASSERT_TRUE(ret);
247 for (const auto& [process, saIds] : values) {
248 EXPECT_EQ(Str16ToStr8(process), "test");
249 EXPECT_EQ(saIds.size(), 3);
250 EXPECT_EQ(saIds.count(1401), 1);
251 }
252 }
253
254 /**
255 * @tc.name: ParseTrustConfig006
256 * @tc.desc: Verify if can load invalid muti root file
257 * @tc.type: FUNC
258 */
259 HWTEST_F(ParseUtilTest, ParseTrustConfig006, TestSize.Level1)
260 {
261 DTEST_LOG << " ParseTrustConfig006 start " << std::endl;
262 /**
263 * @tc.steps: step1. Get correct profile when parse sa file.
264 * @tc.expected: step1. return correct profile object when load correct config file
265 */
266 std::map<std::u16string, std::set<int32_t>> values;
267 bool ret = parser_->ParseTrustConfig(TEST_RESOURCE_PATH + "invalid_muti_root_trust.json", values);
268 ASSERT_FALSE(ret);
269 }
270
271 /**
272 * @tc.name: RemoveSaProfile001
273 * @tc.desc: Verify if can remove not-existed id
274 * @tc.type: FUNC
275 */
276 HWTEST_F(ParseUtilTest, RemoveSaProfile001, TestSize.Level3)
277 {
278 DTEST_LOG << " RemoveSaProfile001 start " << std::endl;
279 /**
280 * @tc.steps: step1. parse not exsit config file
281 * @tc.expected: step1. return false when load not exist file
282 */
283 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "notExist");
284 EXPECT_FALSE(ret);
285 /**
286 * @tc.steps: step2. remove not-existed id
287 * @tc.expected: step2. not crash
288 */
289 parser_->RemoveSaProfile(111);
290 auto profiles = parser_->GetAllSaProfiles();
291 EXPECT_EQ(profiles.size(), 0);
292 }
293
294 /**
295 * @tc.name: RemoveSaProfile002
296 * @tc.desc: Verify if can can remove not-existed id
297 * @tc.type: FUNC
298 */
299 HWTEST_F(ParseUtilTest, RemoveSaProfile002, TestSize.Level3)
300 {
301 DTEST_LOG << " RemoveSaProfile002 start " << std::endl;
302 /**
303 * @tc.steps: step1. parse multi-sa profile
304 * @tc.expected: step1. return true when load multi-sa profile
305 */
306 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "multi_sa_profile.json");
307 EXPECT_TRUE(ret);
308 auto profiles = parser_->GetAllSaProfiles();
309 EXPECT_EQ(profiles.size(), 4);
310 /**
311 * @tc.steps: step2. remove not-existed id
312 * @tc.expected: step2. not crash
313 */
314 parser_->RemoveSaProfile(111);
315 profiles = parser_->GetAllSaProfiles();
316 EXPECT_EQ(profiles.size(), 4);
317 }
318
319 /**
320 * @tc.name: RemoveSaProfile003
321 * @tc.desc: Verify if can remove one existed id
322 * @tc.type: FUNC
323 */
324 HWTEST_F(ParseUtilTest, RemoveSaProfile003, TestSize.Level3)
325 {
326 DTEST_LOG << " RemoveSaProfile003 start " << std::endl;
327 /**
328 * @tc.steps: step1. parse multi-sa profile
329 * @tc.expected: step1. return true when load multi-sa profile
330 */
331 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "multi_sa_profile.json");
332 EXPECT_TRUE(ret);
333 auto profiles = parser_->GetAllSaProfiles();
334 EXPECT_EQ(profiles.size(), 4);
335 /**
336 * @tc.steps: step2. remove one existed id
337 * @tc.expected: step2. remove successfully
338 */
339 parser_->RemoveSaProfile(9999);
340 profiles = parser_->GetAllSaProfiles();
341 EXPECT_EQ(profiles.size(), 3);
342 }
343
344 /**
345 * @tc.name: RemoveSaProfile004
346 * @tc.desc: Verify if can remove one existed id
347 * @tc.type: FUNC
348 */
349 HWTEST_F(ParseUtilTest, RemoveSaProfile004, TestSize.Level3)
350 {
351 DTEST_LOG << " RemoveSaProfile004 start " << std::endl;
352 /**
353 * @tc.steps: step1. parse multi-sa profile
354 * @tc.expected: step1. return true when load multi-sa profile
355 */
356 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "multi_sa_profile.json");
357 EXPECT_TRUE(ret);
358 auto profiles = parser_->GetAllSaProfiles();
359 EXPECT_EQ(profiles.size(), 4);
360 /**
361 * @tc.steps: step2. remove one existed id
362 * @tc.expected: step2. remove successfully
363 */
364 parser_->RemoveSaProfile(9997);
365 profiles = parser_->GetAllSaProfiles();
366 EXPECT_EQ(profiles.size(), 2);
367 }
368
369 /**
370 * @tc.name: RemoveSaProfile005
371 * @tc.desc: Verify if can remove more existed id
372 * @tc.type: FUNC
373 */
374 HWTEST_F(ParseUtilTest, RemoveSaProfile005, TestSize.Level3)
375 {
376 DTEST_LOG << " RemoveSaProfile004 start " << std::endl;
377 /**
378 * @tc.steps: step1. parse multi-sa profile
379 * @tc.expected: step1. return true when load multi-sa profile
380 */
381 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "multi_sa_profile.json");
382 EXPECT_TRUE(ret);
383 auto profiles = parser_->GetAllSaProfiles();
384 EXPECT_EQ(profiles.size(), 4);
385 /**
386 * @tc.steps: step2. remove more existed id
387 * @tc.expected: step2. remove successfully
388 */
389 parser_->RemoveSaProfile(9997);
390 parser_->RemoveSaProfile(9998);
391 parser_->RemoveSaProfile(9998);
392 profiles = parser_->GetAllSaProfiles();
393 EXPECT_EQ(profiles.size(), 1);
394 }
395
396 /**
397 * @tc.name: CheckPathExist001
398 * @tc.desc: Verify if can check not exist file
399 * @tc.type: FUNC
400 */
401 HWTEST_F(ParseUtilTest, CheckPathExist001, TestSize.Level2)
402 {
403 DTEST_LOG << " CheckPathExist001 start " << std::endl;
404 /**
405 * @tc.steps: step1. check not exsit config file
406 * @tc.expected: step1. return false when check not exist file
407 */
408 bool ret = parser_->CheckPathExist(TEST_RESOURCE_PATH + "not_exist.json");
409 EXPECT_FALSE(ret);
410 }
411
412 /**
413 * @tc.name: CheckPathExist002
414 * @tc.desc: Verify if can check exist file
415 * @tc.type: FUNC
416 */
417 HWTEST_F(ParseUtilTest, CheckPathExist002, TestSize.Level2)
418 {
419 DTEST_LOG << " CheckPathExist002 start " << std::endl;
420 /**
421 * @tc.steps: step1. check exsit config file
422 * @tc.expected: step1. return true when load not exist file
423 */
424 bool ret = parser_->CheckPathExist(TEST_RESOURCE_PATH + "multi_sa_profile.json");
425 EXPECT_TRUE(ret);
426 }
427
428 /**
429 * @tc.name: GetProfile001
430 * @tc.desc: Verify if can get not-exist profile
431 * @tc.type: FUNC
432 * @tc.require: I5KMF7
433 */
434 HWTEST_F(ParseUtilTest, GetProfile001, TestSize.Level2)
435 {
436 DTEST_LOG << " GetProfile001 start " << std::endl;
437 /**
438 * @tc.steps: step1. check exsit config file
439 * @tc.expected: step1. return true when load not exist file
440 */
441 SaProfile saProfile;
442 bool ret = parser_->GetProfile(TEST_PROFILE_SAID, saProfile);
443 EXPECT_EQ(ret, false);
444 }
445
446 /**
447 * @tc.name: GetProfile002
448 * @tc.desc: Verify if can get exist profile
449 * @tc.type: FUNC
450 * @tc.require: I5KMF7
451 */
452 HWTEST_F(ParseUtilTest, GetProfile002, TestSize.Level2)
453 {
454 DTEST_LOG << " GetProfile002 start " << std::endl;
455 /**
456 * @tc.steps: step1. check exsit config file
457 * @tc.expected: step1. return true when load not exist file
458 */
459 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "multi_sa_profile.json");
460 EXPECT_EQ(ret, true);
461 SaProfile saProfile;
462 ret = parser_->GetProfile(TEST_PROFILE_SAID, saProfile);
463 EXPECT_EQ(ret, true);
464 EXPECT_EQ(saProfile.saId, TEST_PROFILE_SAID);
465 EXPECT_EQ(saProfile.runOnCreate, true);
466 }
467
468 /**
469 * @tc.name: LoadSaLib001
470 * @tc.desc: Verify if can load salib
471 * @tc.type: FUNC
472 * @tc.require: I5KMF7
473 */
474 HWTEST_F(ParseUtilTest, LoadSaLib001, TestSize.Level2)
475 {
476 DTEST_LOG << " LoadSaLib001 start " << std::endl;
477 /**
478 * @tc.steps: step1. check exsit salib
479 * @tc.expected: step1. return true when load exist salib
480 */
481 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "multi_sa_profile.json");
482 EXPECT_EQ(ret, true);
483 ret = parser_->LoadSaLib(TEST_PROFILE_SAID);
484 EXPECT_EQ(ret, true);
485 }
486
487 /**
488 * @tc.name: LoadSaLib002
489 * @tc.desc: Verify if can load salib
490 * @tc.type: FUNC
491 * @tc.require: I5KMF7
492 */
493 HWTEST_F(ParseUtilTest, LoadSaLib002, TestSize.Level2)
494 {
495 DTEST_LOG << " LoadSaLib002 start " << std::endl;
496 /**
497 * @tc.steps: step1. check exsit salib
498 * @tc.expected: step1. return false when load not exist salib
499 */
500 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "multi_sa_profile.json");
501 EXPECT_EQ(ret, true);
502 ret = parser_->LoadSaLib(TEST_PROFILE_SAID_INVAILD);
503 parser_->CloseSo(MOCK_SAID);
504 EXPECT_NE(ret, true);
505 }
506
507 /**
508 * @tc.name: LoadSaLib003
509 * @tc.desc: Verify if can load salib
510 * @tc.type: FUNC
511 * @tc.require: I5KMF7
512 */
513 HWTEST_F(ParseUtilTest, LoadSaLib003, TestSize.Level2)
514 {
515 DTEST_LOG << " LoadSaLib003 start " << std::endl;
516 /**
517 * @tc.steps: step1. check exsit salib
518 * @tc.expected: step1. return false when load not exist salib
519 */
520 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "mock.json");
521 EXPECT_EQ(ret, true);
522 ret = parser_->LoadSaLib(MOCK_SAID);
523 parser_->CloseSo(MOCK_SAID);
524 EXPECT_EQ(ret, true);
525 }
526
527 /**
528 * @tc.name: LoadSaLib004
529 * @tc.desc: Verify if can load salib
530 * @tc.type: FUNC
531 * @tc.require: I5KMF7
532 */
533 HWTEST_F(ParseUtilTest, LoadSaLib004, TestSize.Level2)
534 {
535 DTEST_LOG << " LoadSaLib004 start " << std::endl;
536 /**
537 * @tc.steps: step1. check exsit salib
538 * @tc.expected: step1. return false when load not exist salib
539 */
540 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "mock.json");
541 EXPECT_EQ(ret, true);
542 ret = parser_->LoadSaLib(MOCK_SAID);
543 EXPECT_EQ(ret, true);
544 parser_->LoadSaLib(MOCK_SAID);
545 }
546
547 /**
548 * @tc.name: LoadSaLib005
549 * @tc.desc: Verify if can load salib
550 * @tc.type: FUNC
551 * @tc.require: I5KMF7
552 */
553 HWTEST_F(ParseUtilTest, LoadSaLib005, TestSize.Level2)
554 {
555 DTEST_LOG << " LoadSaLib005 start " << std::endl;
556 /**
557 * @tc.steps: step1. check exsit salib
558 * @tc.expected: step1. return false when load not exist salib
559 */
560 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "empty_libpath.json");
561 EXPECT_EQ(ret, true);
562 ret = parser_->LoadSaLib(MOCK_SAID);
563 EXPECT_EQ(ret, true);
564 }
565 /**
566 * @tc.name: GetProcessName001
567 * @tc.desc: Verify if can get procesname
568 * @tc.type: FUNC
569 * @tc.require: I5KMF7
570 */
571 HWTEST_F(ParseUtilTest, GetProcessName001, TestSize.Level3)
572 {
573 DTEST_LOG << " GetProcessName001 " << std::endl;
574 /**
575 * @tc.steps: step1. get SaProfiles
576 * @tc.expected: step1. return true when SaProfiles
577 */
578 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "multi_sa_profile.json");
579 EXPECT_EQ(ret, true);
580 std::u16string Name = parser_->GetProcessName();
581 EXPECT_EQ(Str16ToStr8(Name), "test");
582 }
583
584 /**
585 * @tc.name: GetProcessName002
586 * @tc.desc: Verify if can get procesname
587 * @tc.type: FUNC
588 * @tc.require: I5KMF7
589 */
590 HWTEST_F(ParseUtilTest, GetProcessName002, TestSize.Level3)
591 {
592 DTEST_LOG << " GetProcessName002 " << std::endl;
593 /**
594 * @tc.steps: step1. get SaProfiles
595 * @tc.expected: step1. return true when SaProfiles
596 */
597 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "multi_sa_profile.json");
598 EXPECT_EQ(ret, true);
599 std::u16string Name = parser_->GetProcessName();
600 EXPECT_NE(Str16ToStr8(Name), "test_1");
601 }
602
603
604 /**
605 * @tc.name: DeleteAllMark001
606 * @tc.desc: Verify if can DeleteAllMark
607 * @tc.type: FUNC
608 * @tc.require: I5KMF7
609 */
610 HWTEST_F(ParseUtilTest, DeleteAllMark001, TestSize.Level3)
611 {
612 DTEST_LOG << " DeleteAllMark001 " << std::endl;
613 u16string temp = u"stests";
614 u16string mask = u"s";
615 u16string res = DeleteAllMark(temp, mask);
616 EXPECT_EQ(res, u"tet");
617 }
618
619 /**
620 * @tc.name: GetOnDemandConditionsFromJson001
621 * @tc.desc: parse OnDemandConditions, conditions is empty.
622 * @tc.type: FUNC
623 * @tc.require: I6JE38
624 */
625 HWTEST_F(ParseUtilTest, GetOnDemandConditionsFromJson001, TestSize.Level3)
626 {
627 DTEST_LOG << " GetOnDemandConditionsFromJson001 BEGIN" << std::endl;
628 nlohmann::json obj;
629 std::string key;
630 std::vector<OnDemandCondition> out;
631 SaProfile saProfile;
632 parser_->GetOnDemandConditionsFromJson(obj, key, out);
633 EXPECT_TRUE(out.empty());
634 DTEST_LOG << " GetOnDemandConditionsFromJson001 END" << std::endl;
635 }
636
637 /**
638 * @tc.name: GetOnDemandConditionsFromJson002
639 * @tc.desc: parse OnDemandConditions, one condition.
640 * @tc.type: FUNC
641 * @tc.require: I6JE38
642 */
643 HWTEST_F(ParseUtilTest, GetOnDemandConditionsFromJson002, TestSize.Level3)
644 {
645 DTEST_LOG << " GetOnDemandConditionsFromJson002 BEGIN" << std::endl;
646 nlohmann::json obj;
647 nlohmann::json conditions;
648 nlohmann::json condition;
649 condition["eventId"] = SA_TAG_DEVICE_ON_LINE;
650 condition["name"] = "mockName";
651 condition["value"] = "mockValue";
652 conditions[0] = condition;
653 obj["conditions"] = conditions;
654
655 std::string key = "conditions";
656 std::vector<OnDemandCondition> out;
657 SaProfile saProfile;
658 parser_->GetOnDemandConditionsFromJson(obj, key, out);
659 EXPECT_EQ(out.size(), 1);
660 DTEST_LOG << " GetOnDemandConditionsFromJson002 END" << std::endl;
661 }
662
663 /**
664 * @tc.name: GetOnDemandConditionsFromJson003
665 * @tc.desc: parse OnDemandConditions, five condition.
666 * @tc.type: FUNC
667 * @tc.require: I6JE38
668 */
669 HWTEST_F(ParseUtilTest, GetOnDemandConditionsFromJson003, TestSize.Level3)
670 {
671 DTEST_LOG << " GetOnDemandConditionsFromJson003 BEGIN" << std::endl;
672 nlohmann::json obj;
673 nlohmann::json conditions;
674 nlohmann::json condition;
675 condition["eventId"] = SA_TAG_DEVICE_ON_LINE;
676 condition["name"] = "mockName";
677 condition["value"] = "mockValue";
678 nlohmann::json condition2;
679 condition2["eventId"] = SA_TAG_SETTING_SWITCH;
680 condition2["name"] = "mockName";
681 condition2["value"] = "mockValue";
682 nlohmann::json condition3;
683 condition3["eventId"] = SA_TAG_COMMON_EVENT;
684 condition3["name"] = "mockName";
685 condition3["value"] = "mockValue";
686 nlohmann::json condition4;
687 condition4["eventId"] = SA_TAG_PARAM;
688 condition4["name"] = "mockName";
689 condition4["value"] = "mockValue";
690 nlohmann::json condition5;
691 condition5["eventId"] = SA_TAG_TIEMD_EVENT;
692 condition5["name"] = "mockName";
693 condition5["value"] = "mockValue";
694 conditions[0] = condition;
695 conditions[1] = condition2;
696 conditions[2] = condition3;
697 conditions[3] = condition4;
698 conditions[4] = condition5;
699 obj["conditions"] = conditions;
700
701 std::string key = "conditions";
702 std::vector<OnDemandCondition> out;
703 SaProfile saProfile;
704 parser_->GetOnDemandConditionsFromJson(obj, key, out);
705 EXPECT_EQ(out.size(), 5);
706 DTEST_LOG << " GetOnDemandConditionsFromJson003 END" << std::endl;
707 }
708
709 /**
710 * @tc.name: GetOnDemandConditionsFromJson004
711 * @tc.desc: parse OnDemandConditions, invalid condition.
712 * @tc.type: FUNC
713 * @tc.require: I6JE38
714 */
715 HWTEST_F(ParseUtilTest, GetOnDemandConditionsFromJson004, TestSize.Level3)
716 {
717 DTEST_LOG << " GetOnDemandConditionsFromJson004 BEGIN" << std::endl;
718 nlohmann::json obj;
719 nlohmann::json conditions;
720 nlohmann::json condition;
721 condition["eventId"] = "mockeventId";
722 condition["name"] = "mockName";
723 condition["value"] = "mockValue";
724 conditions[0] = condition;
725 obj["conditions"] = conditions;
726
727 std::string key = "conditions";
728 std::vector<OnDemandCondition> out;
729 SaProfile saProfile;
730 parser_->GetOnDemandConditionsFromJson(obj, key, out);
731 EXPECT_EQ(out.size(), 0);
732 DTEST_LOG << " GetOnDemandConditionsFromJson004 END" << std::endl;
733 }
734
735 /**
736 * @tc.name: GetOnDemandExtraMessagesFromJson001
737 * @tc.desc: parse OnDemandExtraMessages, ExtraMessages is empty.
738 * @tc.type: FUNC
739 */
740 HWTEST_F(ParseUtilTest, GetOnDemandExtraMessagesFromJson001, TestSize.Level3)
741 {
742 DTEST_LOG << " GetOnDemandExtraMessagesFromJson001 BEGIN" << std::endl;
743 nlohmann::json obj;
744 std::string key;
745 std::map<std::string, std::string> out;
746 parser_->GetOnDemandExtraMessagesFromJson(obj, key, out);
747 EXPECT_TRUE(out.empty());
748 DTEST_LOG << " GetOnDemandExtraMessagesFromJson001 END" << std::endl;
749 }
750
751 /**
752 * @tc.name: GetOnDemandExtraMessagesFromJson002
753 * @tc.desc: parse OnDemandExtraMessages, five ExtraMessages.
754 * @tc.type: FUNC
755 */
756 HWTEST_F(ParseUtilTest, GetOnDemandExtraMessagesFromJson002, TestSize.Level3)
757 {
758 DTEST_LOG << " GetOnDemandExtraMessagesFromJson002 BEGIN" << std::endl;
759 nlohmann::json obj;
760 nlohmann::json extraMessages;
761 extraMessages["eventId"] = SA_TAG_DEVICE_ON_LINE;
762 extraMessages["name"] = "mockName";
763 extraMessages["value"] = "mockValue";
764 extraMessages["123"] = "123";
765 extraMessages["abc"] = "";
766 obj["extra-messages"] = extraMessages;
767 std::string key = "extra-messages";
768 std::map<std::string, std::string> out;
769 parser_->GetOnDemandExtraMessagesFromJson(obj, key, out);
770 EXPECT_EQ(out.size(), 5);
771 DTEST_LOG << " GetOnDemandExtraMessagesFromJson002 END" << std::endl;
772 }
773
774 /**
775 * @tc.name: GetOnDemandExtraMessagesFromJson003
776 * @tc.desc: parse OnDemandExtraMessages, invalid ExtraMessage.
777 * @tc.type: FUNC
778 */
779 HWTEST_F(ParseUtilTest, GetOnDemandExtraMessagesFromJson003, TestSize.Level3)
780 {
781 DTEST_LOG << " GetOnDemandExtraMessagesFromJson003 BEGIN" << std::endl;
782 nlohmann::json obj;
783 nlohmann::json extraMessages;
784 extraMessages["123"] = 123;
785 extraMessages["abc"] = 456.7;
786 obj["extra-messages"] = extraMessages;
787 std::string key = "extra-messages";
788 std::map<std::string, std::string> out;
789 parser_->GetOnDemandExtraMessagesFromJson(obj, key, out);
790 EXPECT_EQ(out.size(), 0);
791 DTEST_LOG << " GetOnDemandExtraMessagesFromJson003 END" << std::endl;
792 }
793
794 /**
795 * @tc.name: ParseJsonFile001
796 * @tc.desc: parse json file using big json file
797 * @tc.type: FUNC
798 */
799 HWTEST_F(ParseUtilTest, ParseJsonFile001, TestSize.Level3)
800 {
801 DTEST_LOG << " ParseJsonFile001 BEGIN" << std::endl;
802 parser_->saProfiles_.clear();
803 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_large.json");
804 EXPECT_EQ(ret, false);
805 DTEST_LOG << " ParseJsonFile001 END" << std::endl;
806 }
807
808 /**
809 * @tc.name: ParseJsonFile002
810 * @tc.desc: parse json file using too long proces.
811 * @tc.type: FUNC
812 */
813 HWTEST_F(ParseUtilTest, ParseJsonFile002, TestSize.Level3)
814 {
815 DTEST_LOG << " ParseJsonFile002 BEGIN" << std::endl;
816 parser_->saProfiles_.clear();
817 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_long_process.json");
818 EXPECT_EQ(ret, false);
819 DTEST_LOG << " ParseJsonFile002 END" << std::endl;
820 }
821
822 /**
823 * @tc.name: ParseJsonFile003
824 * @tc.desc: parse json file using error said.
825 * @tc.type: FUNC
826 */
827 HWTEST_F(ParseUtilTest, ParseJsonFile003, TestSize.Level3)
828 {
829 DTEST_LOG << " ParseJsonFile003 BEGIN" << std::endl;
830 parser_->saProfiles_.clear();
831 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_error_said.json");
832 EXPECT_EQ(ret, false);
833 DTEST_LOG << " ParseJsonFile003 END" << std::endl;
834 }
835
836 /**
837 * @tc.name: ParseJsonFile004
838 * @tc.desc: parse json file using too long libpath.
839 * @tc.type: FUNC
840 */
841 HWTEST_F(ParseUtilTest, ParseJsonFile004, TestSize.Level3)
842 {
843 DTEST_LOG << " ParseJsonFile004 BEGIN" << std::endl;
844 parser_->saProfiles_.clear();
845 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_long_libpath.json");
846 EXPECT_EQ(ret, false);
847 DTEST_LOG << " ParseJsonFile004 END" << std::endl;
848 }
849
850 /**
851 * @tc.name: ParseJsonFile005
852 * @tc.desc: parse json file using error bootPhase
853 * @tc.type: FUNC
854 */
855 HWTEST_F(ParseUtilTest, ParseJsonFile005, TestSize.Level3)
856 {
857 DTEST_LOG << " ParseJsonFile005 BEGIN" << std::endl;
858 parser_->saProfiles_.clear();
859 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_error_bootphase.json");
860 EXPECT_EQ(ret, true);
861 DTEST_LOG << " ParseJsonFile005 END" << std::endl;
862 }
863
864 /**
865 * @tc.name: ParseJsonFile006
866 * @tc.desc: parse json file using error ondemand tag
867 * @tc.type: FUNC
868 */
869 HWTEST_F(ParseUtilTest, ParseJsonFile006, TestSize.Level3)
870 {
871 DTEST_LOG << " ParseJsonFile006 BEGIN" << std::endl;
872 parser_->saProfiles_.clear();
873 // name or vale is more than 128 bytes
874 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_error_ondemanad_tag.json");
875 EXPECT_EQ(ret, true);
876 SaProfile saProfile;
877 parser_->GetProfile(1401, saProfile);
878 EXPECT_EQ(true, saProfile.startOnDemand.onDemandEvents.empty());
879 EXPECT_EQ(true, saProfile.stopOnDemand.onDemandEvents.empty());
880 DTEST_LOG << " ParseJsonFile006 END" << std::endl;
881 }
882
883 /**
884 * @tc.name: ParseJsonFile007
885 * @tc.desc: parse json file using correct profile
886 * @tc.type: FUNC
887 */
888 HWTEST_F(ParseUtilTest, ParseJsonFile007, TestSize.Level3)
889 {
890 DTEST_LOG << " ParseJsonFile007 BEGIN" << std::endl;
891 parser_->saProfiles_.clear();
892 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile.json");
893 EXPECT_EQ(ret, true);
894 EXPECT_EQ(parser_->saProfiles_.empty(), false);
895 SaProfile saProfile1;
896 parser_->GetProfile(1401, saProfile1);
897 EXPECT_EQ(1401, saProfile1.saId);
898 EXPECT_EQ(true, !saProfile1.startOnDemand.onDemandEvents.empty());
899 EXPECT_EQ(1, saProfile1.startOnDemand.onDemandEvents[0].eventId);
900 EXPECT_EQ("deviceonline", saProfile1.startOnDemand.onDemandEvents[0].name);
901 EXPECT_EQ("on", saProfile1.startOnDemand.onDemandEvents[0].value);
902 EXPECT_EQ(true, !saProfile1.stopOnDemand.onDemandEvents.empty());
903 EXPECT_EQ(1, saProfile1.stopOnDemand.onDemandEvents[0].eventId);
904 EXPECT_EQ("deviceonline", saProfile1.stopOnDemand.onDemandEvents[0].name);
905 EXPECT_EQ("off", saProfile1.stopOnDemand.onDemandEvents[0].value);
906 SaProfile saProfile2;
907 parser_->GetProfile(6001, saProfile2);
908 EXPECT_EQ(6001, saProfile2.saId);
909 DTEST_LOG << " ParseJsonFile007 END" << std::endl;
910 }
911
912 /**
913 * @tc.name: ParseJsonFile008
914 * @tc.desc: parse json file using extension profile
915 * @tc.type: FUNC
916 */
917 HWTEST_F(ParseUtilTest, ParseJsonFile008, TestSize.Level3)
918 {
919 DTEST_LOG << " ParseJsonFile008 BEGIN" << std::endl;
920 parser_->saProfiles_.clear();
921 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_extension.json");
922 EXPECT_EQ(ret, true);
923 EXPECT_EQ(parser_->saProfiles_.empty(), false);
924 SaProfile saProfile;
925 parser_->GetProfile(9999, saProfile);
926 EXPECT_EQ(9999, saProfile.saId);
927 EXPECT_EQ(saProfile.extension.size(), 2);
928 auto iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), EXTENSIOON_BACKUP);
929 EXPECT_NE(iter, saProfile.extension.end());
930 iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), EXTENSIOON_RESTORE);
931 EXPECT_NE(iter, saProfile.extension.end());
932
933 DTEST_LOG << " ParseJsonFile008 END" << std::endl;
934 }
935
936 /**
937 * @tc.name: ParseSystemAbility001
938 * @tc.desc: parse sytemability tag with error param.
939 * @tc.type: FUNC
940 */
941 HWTEST_F(ParseUtilTest, ParseSystemAbility001, TestSize.Level3)
942 {
943 DTEST_LOG << " ParseSystemAbility001 BEGIN" << std::endl;
944 nlohmann::json systemAbilityJson;
945 SaProfile saProfile;
946 bool ret = parser_->ParseSystemAbility(saProfile, systemAbilityJson);
947 EXPECT_EQ(ret, false);
948 systemAbilityJson["name"] = -1; // invalid said
949 ret = parser_->ParseSystemAbility(saProfile, systemAbilityJson);
950 EXPECT_EQ(ret, false);
951 systemAbilityJson["name"] = DISTRIBUTED_DEVICE_PROFILE_SA_ID;
952 ret = parser_->ParseSystemAbility(saProfile, systemAbilityJson);
953 EXPECT_EQ(ret, false);
954 systemAbilityJson["libpath"] = "/system/lib/test.so";
955 ret = parser_->ParseSystemAbility(saProfile, systemAbilityJson);
956 EXPECT_EQ(ret, true);
957 systemAbilityJson["bootphase"] = "aaa";
958 ret = parser_->ParseSystemAbility(saProfile, systemAbilityJson);
959 EXPECT_EQ(ret, true);
960 DTEST_LOG << " ParseSystemAbility001 END" << std::endl;
961 }
962
963 /**
964 * @tc.name: JsonObjToMap
965 * @tc.desc: eventJson.contains is nullptr
966 * @tc.type: FUNC
967 * @tc.require: I6MNUA
968 */
969 HWTEST_F(ParseUtilTest, JsonObjToMap001, TestSize.Level3)
970 {
971 DTEST_LOG << " JsonObjToMap001 BEGIN" << std::endl;
972 nlohmann::json systemAbilityJson;
973 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
974 EXPECT_EQ(res.size(), 3);
975 DTEST_LOG << " JsonObjToMap001 END" << std::endl;
976 }
977 /**
978 * @tc.name: JsonObjToMap
979 * @tc.desc: eventJson.contains is event_type,evnt_name and event_value
980 * @tc.type: FUNC
981 * @tc.require: I6MNUA
982 */
983 HWTEST_F(ParseUtilTest, JsonObjToMap002, TestSize.Level3)
984 {
985 DTEST_LOG << " JsonObjToMap002 BEGIN" << std::endl;
986 std::unordered_map<std::string, std::string> strMap;
987 strMap[EVENT_TYPE] = "test";
988 strMap[EVENT_NAME] = "test";
989 strMap[EVENT_VALUE] = "test";
990 nlohmann::json systemAbilityJson;
991 for (auto it = strMap.begin(); it != strMap.end(); it++) {
992 systemAbilityJson[it->first] = it->second;
993 }
994 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
995 EXPECT_EQ(res.size(), 3);
996 DTEST_LOG << " JsonObjToMap002 END" << std::endl;
997 }
998 /**
999 * @tc.name: JsonObjToMap
1000 * @tc.desc: eventJson.contains is evnt_name and event_value
1001 * @tc.type: FUNC
1002 * @tc.require: I6MNUA
1003 */
1004 HWTEST_F(ParseUtilTest, JsonObjToMap003, TestSize.Level3)
1005 {
1006 DTEST_LOG << " JsonObjToMap003 BEGIN" << std::endl;
1007 std::unordered_map<std::string, std::string> strMap;
1008 strMap[EVENT_NAME] = "test";
1009 strMap[EVENT_VALUE] = "test";
1010 nlohmann::json systemAbilityJson;
1011 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1012 systemAbilityJson[it->first] = it->second;
1013 }
1014 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1015 EXPECT_EQ(res.size(), 3);
1016 DTEST_LOG << " JsonObjToMap003 END" << std::endl;
1017 }
1018 /**
1019 * @tc.name: JsonObjToMap
1020 * @tc.desc: eventJson.contains is event_type and event_value
1021 * @tc.type: FUNC
1022 * @tc.require: I6MNUA
1023 */
1024 HWTEST_F(ParseUtilTest, JsonObjToMap004, TestSize.Level3)
1025 {
1026 DTEST_LOG << " JsonObjToMap004 BEGIN" << std::endl;
1027 std::unordered_map<std::string, std::string> strMap;
1028 strMap[EVENT_TYPE] = "test";
1029 strMap[EVENT_VALUE] = "test";
1030 nlohmann::json systemAbilityJson;
1031 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1032 systemAbilityJson[it->first] = it->second;
1033 }
1034 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1035 EXPECT_EQ(res.size(), 3);
1036 DTEST_LOG << " JsonObjToMap004 END" << std::endl;
1037 }
1038 /**
1039 * @tc.name: JsonObjToMap
1040 * @tc.desc: eventJson.contains is event_type and evnt_name
1041 * @tc.type: FUNC
1042 * @tc.require: I6MNUA
1043 */
1044 HWTEST_F(ParseUtilTest, JsonObjToMap005, TestSize.Level3)
1045 {
1046 DTEST_LOG << " JsonObjToMap005 BEGIN" << std::endl;
1047 std::unordered_map<std::string, std::string> strMap;
1048 strMap[EVENT_TYPE] = "test";
1049 strMap[EVENT_NAME] = "test";
1050 nlohmann::json systemAbilityJson;
1051 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1052 systemAbilityJson[it->first] = it->second;
1053 }
1054 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1055 EXPECT_EQ(res.size(), 3);
1056 DTEST_LOG << " JsonObjToMap005 END" << std::endl;
1057 }
1058 /**
1059 * @tc.name: JsonObjToMap
1060 * @tc.desc: eventJson.contains is event_type
1061 * @tc.type: FUNC
1062 * @tc.require: I6MNUA
1063 */
1064 HWTEST_F(ParseUtilTest, JsonObjToMap006, TestSize.Level3)
1065 {
1066 DTEST_LOG << " JsonObjToMap006 BEGIN" << std::endl;
1067 std::unordered_map<std::string, std::string> strMap;
1068 strMap[EVENT_TYPE] = "test";
1069 nlohmann::json systemAbilityJson;
1070 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1071 systemAbilityJson[it->first] = it->second;
1072 }
1073 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1074 EXPECT_EQ(res.size(), 3);
1075 DTEST_LOG << " JsonObjToMap006 END" << std::endl;
1076 }
1077 /**
1078 * @tc.name: JsonObjToMap
1079 * @tc.desc: eventJson.contains is event_name
1080 * @tc.type: FUNC
1081 * @tc.require: I6MNUA
1082 */
1083 HWTEST_F(ParseUtilTest, JsonObjToMap007, TestSize.Level3)
1084 {
1085 DTEST_LOG << " JsonObjToMap007 BEGIN" << std::endl;
1086 std::unordered_map<std::string, std::string> strMap;
1087 strMap[EVENT_NAME] = "test";
1088 nlohmann::json systemAbilityJson;
1089 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1090 systemAbilityJson[it->first] = it->second;
1091 }
1092 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1093 EXPECT_EQ(res.size(), 3);
1094 DTEST_LOG << " JsonObjToMap007 END" << std::endl;
1095 }
1096 /**
1097 * @tc.name: JsonObjToMap
1098 * @tc.desc: eventJson.contains is event_value
1099 * @tc.type: FUNC
1100 * @tc.require: I6MNUA
1101 */
1102 HWTEST_F(ParseUtilTest, JsonObjToMap008, TestSize.Level3)
1103 {
1104 DTEST_LOG << " JsonObjToMap008 BEGIN" << std::endl;
1105 std::unordered_map<std::string, std::string> strMap;
1106 strMap[EVENT_VALUE] = "test";
1107 nlohmann::json systemAbilityJson;
1108 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1109 systemAbilityJson[it->first] = it->second;
1110 }
1111 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1112 EXPECT_EQ(res.size(), 3);
1113 DTEST_LOG << " JsonObjToMap008 END" << std::endl;
1114 }
1115 /**
1116 * @tc.name: JsonObjToMap
1117 * @tc.desc: eventJson.contains is event_type,evnt_name and event_value;eventJson.is_string is false
1118 * @tc.type: FUNC
1119 * @tc.require: I6MNUA
1120 */
1121 HWTEST_F(ParseUtilTest, JsonObjToMap009, TestSize.Level3)
1122 {
1123 DTEST_LOG << " JsonObjToMap009 BEGIN" << std::endl;
1124 std::unordered_map<std::string, int> strMap;
1125 strMap[EVENT_TYPE] = TEST_NUM;
1126 strMap[EVENT_NAME] = TEST_NUM;
1127 strMap[EVENT_VALUE] = TEST_NUM;
1128 nlohmann::json systemAbilityJson;
1129 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1130 systemAbilityJson[it->first] = it->second;
1131 }
1132 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1133 EXPECT_EQ(res.size(), 3);
1134 DTEST_LOG << " JsonObjToMap009 END" << std::endl;
1135 }
1136 /**
1137 * @tc.name: JsonObjToMap
1138 * @tc.desc: eventJson.contains is evnt_name and event_value;eventJson.is_string is false
1139 * @tc.type: FUNC
1140 * @tc.require: I6MNUA
1141 */
1142 HWTEST_F(ParseUtilTest, JsonObjToMap010, TestSize.Level3)
1143 {
1144 DTEST_LOG << " JsonObjToMap010 BEGIN" << std::endl;
1145 std::unordered_map<std::string, int> strMap;
1146 strMap[EVENT_NAME] = TEST_NUM;
1147 strMap[EVENT_VALUE] = TEST_NUM;
1148 nlohmann::json systemAbilityJson;
1149 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1150 systemAbilityJson[it->first] = it->second;
1151 }
1152 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1153 EXPECT_EQ(res.size(), 3);
1154 DTEST_LOG << " JsonObjToMap010 END" << std::endl;
1155 }
1156 /**
1157 * @tc.name: JsonObjToMap
1158 * @tc.desc: eventJson.contains is event_value;eventJson.is_string is false
1159 * @tc.type: FUNC
1160 * @tc.require: I6MNUA
1161 */
1162 HWTEST_F(ParseUtilTest, JsonObjToMap011, TestSize.Level3)
1163 {
1164 DTEST_LOG << " JsonObjToMap011 BEGIN" << std::endl;
1165 std::unordered_map<std::string, int> strMap;
1166 strMap[EVENT_VALUE] = TEST_NUM;
1167 nlohmann::json systemAbilityJson;
1168 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1169 systemAbilityJson[it->first] = it->second;
1170 }
1171 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1172 EXPECT_EQ(res.size(), 3);
1173 DTEST_LOG << " JsonObjToMap011 END" << std::endl;
1174 }
1175
1176 /**
1177 * @tc.name: StringToMap001
1178 * @tc.desc: test StringToMap with empty string
1179 * @tc.type: FUNC
1180 * @tc.require: I6YJH6
1181 */
1182 HWTEST_F(ParseUtilTest, StringtoMap001, TestSize.Level3)
1183 {
1184 unordered_map<std::string, std::string> ret = parser_->StringToMap(EVENT_STRING);
1185 EXPECT_FALSE(ret.empty());
1186 }
1187
1188 /**
1189 * @tc.name: CloseHandle001
1190 * @tc.desc: test CloseHandle with nullptr
1191 * @tc.type: FUNC
1192 * @tc.require: I7G775
1193 */
1194 HWTEST_F(ParseUtilTest, CloseHandle001, TestSize.Level3)
1195 {
1196 SaProfile saProfile;
1197 parser_->CloseHandle(saProfile);
1198 EXPECT_EQ(saProfile.handle, nullptr);
1199 }
1200
1201 /**
1202 * @tc.name: CheckLogicRelationship001
1203 * @tc.desc: test CheckLogicRelationship
1204 * @tc.type: FUNC
1205 */
1206 HWTEST_F(ParseUtilTest, CheckLogicRelationship001, TestSize.Level3)
1207 {
1208 bool ret;
1209 ret = parser_->CheckLogicRelationship("1", "");
1210 EXPECT_EQ(ret, true);
1211 ret = parser_->CheckLogicRelationship("1", "1");
1212 EXPECT_EQ(ret, true);
1213 ret = parser_->CheckLogicRelationship("", "1");
1214 EXPECT_EQ(ret, false);
1215 ret = parser_->CheckLogicRelationship("1", ">1");
1216 EXPECT_EQ(ret, false);
1217 ret = parser_->CheckLogicRelationship("2", ">1");
1218 EXPECT_EQ(ret, true);
1219 ret = parser_->CheckLogicRelationship("1", ">=1");
1220 EXPECT_EQ(ret, true);
1221 ret = parser_->CheckLogicRelationship("1", "<1");
1222 EXPECT_EQ(ret, false);
1223 ret = parser_->CheckLogicRelationship("0", "<1");
1224 EXPECT_EQ(ret, true);
1225 ret = parser_->CheckLogicRelationship("1", "<=1");
1226 EXPECT_EQ(ret, true);
1227 ret = parser_->CheckLogicRelationship("1", ">=abc");
1228 EXPECT_EQ(ret, false);
1229 ret = parser_->CheckLogicRelationship("abc", ">=1");
1230 EXPECT_EQ(ret, false);
1231 }
1232
1233 /**
1234 * @tc.name: ParseSystemAbilityGetExtension001
1235 * @tc.desc: parse sytemability extension tag
1236 * @tc.type: FUNC
1237 */
1238 HWTEST_F(ParseUtilTest, ParseSystemAbilityGetExtension001, TestSize.Level3)
1239 {
1240 DTEST_LOG << " ParseSystemAbilityGetExtension001 BEGIN" << std::endl;
1241 nlohmann::json systemAbilityJson;
1242 SaProfile saProfile;
1243 bool ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1244 EXPECT_EQ(ret, true);
1245 systemAbilityJson["extension"] = nlohmann::json::array();
1246 systemAbilityJson["extension"].push_back(EXTENSIOON_BACKUP);
1247 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1248 EXPECT_EQ(ret, true);
1249 EXPECT_EQ(saProfile.extension.size(), 1);
1250 auto iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), EXTENSIOON_BACKUP);
1251 EXPECT_NE(iter, saProfile.extension.end());
1252
1253 systemAbilityJson["extension"].push_back(EXTENSIOON_RESTORE);
1254 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1255 EXPECT_EQ(ret, true);
1256 EXPECT_EQ(saProfile.extension.size(), 2);
1257 iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), EXTENSIOON_RESTORE);
1258 EXPECT_NE(iter, saProfile.extension.end());
1259
1260 systemAbilityJson["extension"].push_back(EXTENSIOON_BACKUP);
1261 systemAbilityJson["extension"].push_back(EXTENSIOON_RESTORE);
1262
1263 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1264 EXPECT_EQ(ret, true);
1265 EXPECT_EQ(saProfile.extension.size(), 2);
1266
1267 DTEST_LOG << " ParseSystemAbilityGetExtension001 END" << std::endl;
1268 }
1269
1270 /**
1271 * @tc.name: ParseSystemAbilityGetExtension002
1272 * @tc.desc: parse sytemability extension string length test
1273 * @tc.type: FUNC
1274 */
1275 HWTEST_F(ParseUtilTest, ParseSystemAbilityGetExtension002, TestSize.Level3)
1276 {
1277 DTEST_LOG << " ParseSystemAbilityGetExtension002 BEGIN" << std::endl;
1278 nlohmann::json systemAbilityJson;
1279 SaProfile saProfile;
1280 systemAbilityJson["extension"] = nlohmann::json::array();
1281
1282 char ch = 'a';
1283 std::string aExceedstr(MAX_JSON_STRING_LENGTH + 1, ch);
1284 systemAbilityJson["extension"].push_back(aExceedstr);
1285 bool ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1286 EXPECT_EQ(ret, false);
1287 EXPECT_EQ(saProfile.extension.size(), 0);
1288
1289 systemAbilityJson["extension"].clear();
1290
1291 ch = 'a';
1292 std::string astr(MAX_JSON_STRING_LENGTH, ch);
1293 systemAbilityJson["extension"].push_back(astr);
1294 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1295 EXPECT_EQ(ret, true);
1296 EXPECT_EQ(saProfile.extension.size(), 1);
1297 auto iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), astr);
1298 EXPECT_NE(iter, saProfile.extension.end());
1299
1300 ch = 'b';
1301 std::string bstr(MAX_JSON_STRING_LENGTH - 1, ch);
1302 systemAbilityJson["extension"].push_back(bstr);
1303 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1304 EXPECT_EQ(ret, true);
1305 EXPECT_EQ(saProfile.extension.size(), 2);
1306 iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), bstr);
1307 EXPECT_NE(iter, saProfile.extension.end());
1308
1309 DTEST_LOG << " ParseSystemAbilityGetExtension002 END" << std::endl;
1310 }
1311
1312 /**
1313 * @tc.name: ParseSystemAbilityGetExtension002
1314 * @tc.desc: parse sytemability extension string num test
1315 * @tc.type: FUNC
1316 */
1317 HWTEST_F(ParseUtilTest, ParseSystemAbilityGetExtension003, TestSize.Level3)
1318 {
1319 DTEST_LOG << " ParseSystemAbilityGetExtension003 BEGIN" << std::endl;
1320 nlohmann::json systemAbilityJson;
1321 SaProfile saProfile;
1322 systemAbilityJson["extension"] = nlohmann::json::array();
1323
1324 bool ret;
1325 char ch = 0;
1326 for (int32_t loop = 0; loop < MAX_EXTENSIONO_NUM; ++loop) {
1327 std::string str(MAX_JSON_STRING_LENGTH, ch + loop);
1328 systemAbilityJson["extension"].push_back(str);
1329 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1330 EXPECT_EQ(ret, true);
1331 EXPECT_EQ(saProfile.extension.size(), loop + 1);
1332 auto iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), str);
1333 EXPECT_NE(iter, saProfile.extension.end());
1334 }
1335
1336 std::string str(MAX_JSON_STRING_LENGTH, ch + MAX_EXTENSIONO_NUM);
1337 systemAbilityJson["extension"].push_back(str);
1338 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1339 EXPECT_EQ(ret, false);
1340 EXPECT_EQ(saProfile.extension.size(), MAX_EXTENSIONO_NUM);
1341
1342 DTEST_LOG << " ParseSystemAbilityGetExtension003 END" << std::endl;
1343 }
1344
1345 /**
1346 * @tc.name: GetOndemandPriorityPara001
1347 * @tc.desc: GetOndemandPriorityPara
1348 * @tc.type: FUNC
1349 */
1350 HWTEST_F(ParseUtilTest, GetOndemandPriorityPara001, TestSize.Level3)
1351 {
1352 DTEST_LOG << " GetOndemandPriorityPara001 BEGIN" << std::endl;
1353
1354 std::string loadPriority = "HighPriority";
1355 uint32_t ret = parser_->GetOndemandPriorityPara(loadPriority);
1356 EXPECT_EQ(ret, static_cast<uint32_t>(HIGH_PRIORITY));
1357
1358 loadPriority = "MediumPriority";
1359 ret = parser_->GetOndemandPriorityPara(loadPriority);
1360 EXPECT_EQ(ret, static_cast<uint32_t>(MEDIUM_PRIORITY));
1361
1362 loadPriority = "NEW_TEST";
1363 ret = parser_->GetOndemandPriorityPara(loadPriority);
1364 EXPECT_EQ(ret, static_cast<uint32_t>(LOW_PRIORITY));
1365 }
1366 } // namespace SAMGR
1367 } // namespace OHOS
1368