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