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 */
HWTEST_F(ParseUtilTest, ParseSaProfile001, TestSize.Level2)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 */
HWTEST_F(ParseUtilTest, GetSaProfiles001, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, GetSaProfiles002, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, ParseTrustConfig001, TestSize.Level1)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 */
HWTEST_F(ParseUtilTest, ParseTrustConfig002, TestSize.Level1)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 */
HWTEST_F(ParseUtilTest, ParseTrustConfig003, TestSize.Level1)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 */
HWTEST_F(ParseUtilTest, ParseTrustConfig004, TestSize.Level1)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 */
HWTEST_F(ParseUtilTest, ParseTrustConfig005, TestSize.Level1)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 */
HWTEST_F(ParseUtilTest, ParseTrustConfig006, TestSize.Level1)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 */
HWTEST_F(ParseUtilTest, RemoveSaProfile001, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, RemoveSaProfile002, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, RemoveSaProfile003, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, RemoveSaProfile004, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, RemoveSaProfile005, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, CheckPathExist001, TestSize.Level2)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 */
HWTEST_F(ParseUtilTest, CheckPathExist002, TestSize.Level2)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 */
HWTEST_F(ParseUtilTest, GetProfile001, TestSize.Level2)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 */
HWTEST_F(ParseUtilTest, GetProfile002, TestSize.Level2)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 */
HWTEST_F(ParseUtilTest, LoadSaLib001, TestSize.Level2)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 */
HWTEST_F(ParseUtilTest, LoadSaLib002, TestSize.Level2)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 */
HWTEST_F(ParseUtilTest, LoadSaLib003, TestSize.Level2)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 */
HWTEST_F(ParseUtilTest, LoadSaLib004, TestSize.Level2)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 */
HWTEST_F(ParseUtilTest, LoadSaLib005, TestSize.Level2)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 */
HWTEST_F(ParseUtilTest, GetProcessName001, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, GetProcessName002, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, DeleteAllMark001, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, GetOnDemandConditionsFromJson001, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, GetOnDemandConditionsFromJson002, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, GetOnDemandConditionsFromJson003, TestSize.Level3)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 */
HWTEST_F(ParseUtilTest, GetOnDemandConditionsFromJson004, TestSize.Level3)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 /**
737 * @tc.name: GetOnDemandExtraMessagesFromJson001
738 * @tc.desc: parse OnDemandExtraMessages, ExtraMessages is empty.
739 * @tc.type: FUNC
740 */
HWTEST_F(ParseUtilTest, GetOnDemandExtraMessagesFromJson001, TestSize.Level3)741 HWTEST_F(ParseUtilTest, GetOnDemandExtraMessagesFromJson001, TestSize.Level3)
742 {
743 DTEST_LOG << " GetOnDemandExtraMessagesFromJson001 BEGIN" << std::endl;
744 nlohmann::json obj;
745 std::string key;
746 std::map<std::string, std::string> out;
747 parser_->GetOnDemandExtraMessagesFromJson(obj, key, out);
748 EXPECT_TRUE(out.empty());
749 DTEST_LOG << " GetOnDemandExtraMessagesFromJson001 END" << std::endl;
750 }
751
752 /**
753 * @tc.name: GetOnDemandExtraMessagesFromJson002
754 * @tc.desc: parse OnDemandExtraMessages, five ExtraMessages.
755 * @tc.type: FUNC
756 */
HWTEST_F(ParseUtilTest, GetOnDemandExtraMessagesFromJson002, TestSize.Level3)757 HWTEST_F(ParseUtilTest, GetOnDemandExtraMessagesFromJson002, TestSize.Level3)
758 {
759 DTEST_LOG << " GetOnDemandExtraMessagesFromJson002 BEGIN" << std::endl;
760 nlohmann::json obj;
761 nlohmann::json extraMessages;
762 extraMessages["eventId"] = SA_TAG_DEVICE_ON_LINE;
763 extraMessages["name"] = "mockName";
764 extraMessages["value"] = "mockValue";
765 extraMessages["123"] = "123";
766 extraMessages["abc"] = "";
767 obj["extra-messages"] = extraMessages;
768 std::string key = "extra-messages";
769 std::map<std::string, std::string> out;
770 parser_->GetOnDemandExtraMessagesFromJson(obj, key, out);
771 EXPECT_EQ(out.size(), 5);
772 DTEST_LOG << " GetOnDemandExtraMessagesFromJson002 END" << std::endl;
773 }
774
775 /**
776 * @tc.name: GetOnDemandExtraMessagesFromJson003
777 * @tc.desc: parse OnDemandExtraMessages, invalid ExtraMessage.
778 * @tc.type: FUNC
779 */
HWTEST_F(ParseUtilTest, GetOnDemandExtraMessagesFromJson003, TestSize.Level3)780 HWTEST_F(ParseUtilTest, GetOnDemandExtraMessagesFromJson003, TestSize.Level3)
781 {
782 DTEST_LOG << " GetOnDemandExtraMessagesFromJson003 BEGIN" << std::endl;
783 nlohmann::json obj;
784 nlohmann::json extraMessages;
785 extraMessages["123"] = 123;
786 extraMessages["abc"] = 456.7;
787 obj["extra-messages"] = extraMessages;
788 std::string key = "extra-messages";
789 std::map<std::string, std::string> out;
790 parser_->GetOnDemandExtraMessagesFromJson(obj, key, out);
791 EXPECT_EQ(out.size(), 0);
792 DTEST_LOG << " GetOnDemandExtraMessagesFromJson003 END" << std::endl;
793 }
794
795
796 /**
797 * @tc.name: ParseJsonFile001
798 * @tc.desc: parse json file using big json file
799 * @tc.type: FUNC
800 */
HWTEST_F(ParseUtilTest, ParseJsonFile001, TestSize.Level3)801 HWTEST_F(ParseUtilTest, ParseJsonFile001, TestSize.Level3)
802 {
803 DTEST_LOG << " ParseJsonFile001 BEGIN" << std::endl;
804 parser_->saProfiles_.clear();
805 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_large.json");
806 EXPECT_EQ(ret, false);
807 DTEST_LOG << " ParseJsonFile001 END" << std::endl;
808 }
809
810 /**
811 * @tc.name: ParseJsonFile002
812 * @tc.desc: parse json file using too long proces.
813 * @tc.type: FUNC
814 */
HWTEST_F(ParseUtilTest, ParseJsonFile002, TestSize.Level3)815 HWTEST_F(ParseUtilTest, ParseJsonFile002, TestSize.Level3)
816 {
817 DTEST_LOG << " ParseJsonFile002 BEGIN" << std::endl;
818 parser_->saProfiles_.clear();
819 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_long_process.json");
820 EXPECT_EQ(ret, false);
821 DTEST_LOG << " ParseJsonFile002 END" << std::endl;
822 }
823
824 /**
825 * @tc.name: ParseJsonFile003
826 * @tc.desc: parse json file using error said.
827 * @tc.type: FUNC
828 */
HWTEST_F(ParseUtilTest, ParseJsonFile003, TestSize.Level3)829 HWTEST_F(ParseUtilTest, ParseJsonFile003, TestSize.Level3)
830 {
831 DTEST_LOG << " ParseJsonFile003 BEGIN" << std::endl;
832 parser_->saProfiles_.clear();
833 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_error_said.json");
834 EXPECT_EQ(ret, false);
835 DTEST_LOG << " ParseJsonFile003 END" << std::endl;
836 }
837
838 /**
839 * @tc.name: ParseJsonFile004
840 * @tc.desc: parse json file using too long libpath.
841 * @tc.type: FUNC
842 */
HWTEST_F(ParseUtilTest, ParseJsonFile004, TestSize.Level3)843 HWTEST_F(ParseUtilTest, ParseJsonFile004, TestSize.Level3)
844 {
845 DTEST_LOG << " ParseJsonFile004 BEGIN" << std::endl;
846 parser_->saProfiles_.clear();
847 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_long_libpath.json");
848 EXPECT_EQ(ret, false);
849 DTEST_LOG << " ParseJsonFile004 END" << std::endl;
850 }
851
852 /**
853 * @tc.name: ParseJsonFile005
854 * @tc.desc: parse json file using error bootPhase
855 * @tc.type: FUNC
856 */
HWTEST_F(ParseUtilTest, ParseJsonFile005, TestSize.Level3)857 HWTEST_F(ParseUtilTest, ParseJsonFile005, TestSize.Level3)
858 {
859 DTEST_LOG << " ParseJsonFile005 BEGIN" << std::endl;
860 parser_->saProfiles_.clear();
861 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_error_bootphase.json");
862 EXPECT_EQ(ret, true);
863 DTEST_LOG << " ParseJsonFile005 END" << std::endl;
864 }
865
866 /**
867 * @tc.name: ParseJsonFile006
868 * @tc.desc: parse json file using error ondemand tag
869 * @tc.type: FUNC
870 */
HWTEST_F(ParseUtilTest, ParseJsonFile006, TestSize.Level3)871 HWTEST_F(ParseUtilTest, ParseJsonFile006, TestSize.Level3)
872 {
873 DTEST_LOG << " ParseJsonFile006 BEGIN" << std::endl;
874 parser_->saProfiles_.clear();
875 // name or vale is more than 128 bytes
876 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_error_ondemanad_tag.json");
877 EXPECT_EQ(ret, true);
878 SaProfile saProfile;
879 parser_->GetProfile(1401, saProfile);
880 EXPECT_EQ(true, saProfile.startOnDemand.onDemandEvents.empty());
881 EXPECT_EQ(true, saProfile.stopOnDemand.onDemandEvents.empty());
882 DTEST_LOG << " ParseJsonFile006 END" << std::endl;
883 }
884
885 /**
886 * @tc.name: ParseJsonFile007
887 * @tc.desc: parse json file using correct profile
888 * @tc.type: FUNC
889 */
HWTEST_F(ParseUtilTest, ParseJsonFile007, TestSize.Level3)890 HWTEST_F(ParseUtilTest, ParseJsonFile007, TestSize.Level3)
891 {
892 DTEST_LOG << " ParseJsonFile007 BEGIN" << std::endl;
893 parser_->saProfiles_.clear();
894 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile.json");
895 EXPECT_EQ(ret, true);
896 EXPECT_EQ(parser_->saProfiles_.empty(), false);
897 SaProfile saProfile1;
898 parser_->GetProfile(1401, saProfile1);
899 EXPECT_EQ(1401, saProfile1.saId);
900 EXPECT_EQ(true, !saProfile1.startOnDemand.onDemandEvents.empty());
901 EXPECT_EQ(1, saProfile1.startOnDemand.onDemandEvents[0].eventId);
902 EXPECT_EQ("deviceonline", saProfile1.startOnDemand.onDemandEvents[0].name);
903 EXPECT_EQ("on", saProfile1.startOnDemand.onDemandEvents[0].value);
904 EXPECT_EQ(true, !saProfile1.stopOnDemand.onDemandEvents.empty());
905 EXPECT_EQ(1, saProfile1.stopOnDemand.onDemandEvents[0].eventId);
906 EXPECT_EQ("deviceonline", saProfile1.stopOnDemand.onDemandEvents[0].name);
907 EXPECT_EQ("off", saProfile1.stopOnDemand.onDemandEvents[0].value);
908 SaProfile saProfile2;
909 parser_->GetProfile(6001, saProfile2);
910 EXPECT_EQ(6001, saProfile2.saId);
911 DTEST_LOG << " ParseJsonFile007 END" << std::endl;
912 }
913
914 /**
915 * @tc.name: ParseJsonFile008
916 * @tc.desc: parse json file using extension profile
917 * @tc.type: FUNC
918 */
HWTEST_F(ParseUtilTest, ParseJsonFile008, TestSize.Level3)919 HWTEST_F(ParseUtilTest, ParseJsonFile008, TestSize.Level3)
920 {
921 DTEST_LOG << " ParseJsonFile008 BEGIN" << std::endl;
922 parser_->saProfiles_.clear();
923 bool ret = parser_->ParseSaProfiles(TEST_RESOURCE_PATH + "sa_profile_extension.json");
924 EXPECT_EQ(ret, true);
925 EXPECT_EQ(parser_->saProfiles_.empty(), false);
926 SaProfile saProfile;
927 parser_->GetProfile(9999, saProfile);
928 EXPECT_EQ(9999, saProfile.saId);
929 EXPECT_EQ(saProfile.extension.size(), 2);
930 auto iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), EXTENSIOON_BACKUP);
931 EXPECT_NE(iter, saProfile.extension.end());
932 iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), EXTENSIOON_RESTORE);
933 EXPECT_NE(iter, saProfile.extension.end());
934
935 DTEST_LOG << " ParseJsonFile008 END" << std::endl;
936 }
937
938 /**
939 * @tc.name: ParseSystemAbility001
940 * @tc.desc: parse sytemability tag with error param.
941 * @tc.type: FUNC
942 */
HWTEST_F(ParseUtilTest, ParseSystemAbility001, TestSize.Level3)943 HWTEST_F(ParseUtilTest, ParseSystemAbility001, TestSize.Level3)
944 {
945 DTEST_LOG << " ParseSystemAbility001 BEGIN" << std::endl;
946 nlohmann::json systemAbilityJson;
947 SaProfile saProfile;
948 bool ret = parser_->ParseSystemAbility(saProfile, systemAbilityJson);
949 EXPECT_EQ(ret, false);
950 systemAbilityJson["name"] = -1; // invalid said
951 ret = parser_->ParseSystemAbility(saProfile, systemAbilityJson);
952 EXPECT_EQ(ret, false);
953 systemAbilityJson["name"] = DISTRIBUTED_DEVICE_PROFILE_SA_ID;
954 ret = parser_->ParseSystemAbility(saProfile, systemAbilityJson);
955 EXPECT_EQ(ret, false);
956 systemAbilityJson["libpath"] = "/system/lib/test.so";
957 ret = parser_->ParseSystemAbility(saProfile, systemAbilityJson);
958 EXPECT_EQ(ret, true);
959 systemAbilityJson["bootphase"] = "aaa";
960 ret = parser_->ParseSystemAbility(saProfile, systemAbilityJson);
961 EXPECT_EQ(ret, true);
962
963 int32_t testTime = 9999;
964 systemAbilityJson["stop-on-demand"] = {{"longtimeunused-unload", testTime}, {"unreferenced-unload", true}};
965 ret = parser_->ParseSystemAbility(saProfile, systemAbilityJson);
966 EXPECT_EQ(ret, true);
967 EXPECT_EQ(saProfile.stopOnDemand.unusedTimeout, testTime);
968
969 EXPECT_EQ(saProfile.stopOnDemand.unrefUnload, true);
970
971 DTEST_LOG << " ParseSystemAbility001 END" << std::endl;
972 }
973
974 /**
975 * @tc.name: JsonObjToMap
976 * @tc.desc: eventJson.contains is nullptr
977 * @tc.type: FUNC
978 * @tc.require: I6MNUA
979 */
HWTEST_F(ParseUtilTest, JsonObjToMap001, TestSize.Level3)980 HWTEST_F(ParseUtilTest, JsonObjToMap001, TestSize.Level3)
981 {
982 DTEST_LOG << " JsonObjToMap001 BEGIN" << std::endl;
983 nlohmann::json systemAbilityJson;
984 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
985 EXPECT_EQ(res.size(), 3);
986 DTEST_LOG << " JsonObjToMap001 END" << std::endl;
987 }
988 /**
989 * @tc.name: JsonObjToMap
990 * @tc.desc: eventJson.contains is event_type,evnt_name and event_value
991 * @tc.type: FUNC
992 * @tc.require: I6MNUA
993 */
HWTEST_F(ParseUtilTest, JsonObjToMap002, TestSize.Level3)994 HWTEST_F(ParseUtilTest, JsonObjToMap002, TestSize.Level3)
995 {
996 DTEST_LOG << " JsonObjToMap002 BEGIN" << std::endl;
997 std::unordered_map<std::string, std::string> strMap;
998 strMap[EVENT_TYPE] = "test";
999 strMap[EVENT_NAME] = "test";
1000 strMap[EVENT_VALUE] = "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 << " JsonObjToMap002 END" << std::endl;
1008 }
1009 /**
1010 * @tc.name: JsonObjToMap
1011 * @tc.desc: eventJson.contains is evnt_name and event_value
1012 * @tc.type: FUNC
1013 * @tc.require: I6MNUA
1014 */
HWTEST_F(ParseUtilTest, JsonObjToMap003, TestSize.Level3)1015 HWTEST_F(ParseUtilTest, JsonObjToMap003, TestSize.Level3)
1016 {
1017 DTEST_LOG << " JsonObjToMap003 BEGIN" << std::endl;
1018 std::unordered_map<std::string, std::string> strMap;
1019 strMap[EVENT_NAME] = "test";
1020 strMap[EVENT_VALUE] = "test";
1021 nlohmann::json systemAbilityJson;
1022 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1023 systemAbilityJson[it->first] = it->second;
1024 }
1025 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1026 EXPECT_EQ(res.size(), 3);
1027 DTEST_LOG << " JsonObjToMap003 END" << std::endl;
1028 }
1029 /**
1030 * @tc.name: JsonObjToMap
1031 * @tc.desc: eventJson.contains is event_type and event_value
1032 * @tc.type: FUNC
1033 * @tc.require: I6MNUA
1034 */
HWTEST_F(ParseUtilTest, JsonObjToMap004, TestSize.Level3)1035 HWTEST_F(ParseUtilTest, JsonObjToMap004, TestSize.Level3)
1036 {
1037 DTEST_LOG << " JsonObjToMap004 BEGIN" << std::endl;
1038 std::unordered_map<std::string, std::string> strMap;
1039 strMap[EVENT_TYPE] = "test";
1040 strMap[EVENT_VALUE] = "test";
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 << " JsonObjToMap004 END" << std::endl;
1048 }
1049 /**
1050 * @tc.name: JsonObjToMap
1051 * @tc.desc: eventJson.contains is event_type and evnt_name
1052 * @tc.type: FUNC
1053 * @tc.require: I6MNUA
1054 */
HWTEST_F(ParseUtilTest, JsonObjToMap005, TestSize.Level3)1055 HWTEST_F(ParseUtilTest, JsonObjToMap005, TestSize.Level3)
1056 {
1057 DTEST_LOG << " JsonObjToMap005 BEGIN" << std::endl;
1058 std::unordered_map<std::string, std::string> strMap;
1059 strMap[EVENT_TYPE] = "test";
1060 strMap[EVENT_NAME] = "test";
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 << " JsonObjToMap005 END" << std::endl;
1068 }
1069 /**
1070 * @tc.name: JsonObjToMap
1071 * @tc.desc: eventJson.contains is event_type
1072 * @tc.type: FUNC
1073 * @tc.require: I6MNUA
1074 */
HWTEST_F(ParseUtilTest, JsonObjToMap006, TestSize.Level3)1075 HWTEST_F(ParseUtilTest, JsonObjToMap006, TestSize.Level3)
1076 {
1077 DTEST_LOG << " JsonObjToMap006 BEGIN" << std::endl;
1078 std::unordered_map<std::string, std::string> strMap;
1079 strMap[EVENT_TYPE] = "test";
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 << " JsonObjToMap006 END" << std::endl;
1087 }
1088 /**
1089 * @tc.name: JsonObjToMap
1090 * @tc.desc: eventJson.contains is event_name
1091 * @tc.type: FUNC
1092 * @tc.require: I6MNUA
1093 */
HWTEST_F(ParseUtilTest, JsonObjToMap007, TestSize.Level3)1094 HWTEST_F(ParseUtilTest, JsonObjToMap007, TestSize.Level3)
1095 {
1096 DTEST_LOG << " JsonObjToMap007 BEGIN" << std::endl;
1097 std::unordered_map<std::string, std::string> strMap;
1098 strMap[EVENT_NAME] = "test";
1099 nlohmann::json systemAbilityJson;
1100 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1101 systemAbilityJson[it->first] = it->second;
1102 }
1103 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1104 EXPECT_EQ(res.size(), 3);
1105 DTEST_LOG << " JsonObjToMap007 END" << std::endl;
1106 }
1107 /**
1108 * @tc.name: JsonObjToMap
1109 * @tc.desc: eventJson.contains is event_value
1110 * @tc.type: FUNC
1111 * @tc.require: I6MNUA
1112 */
HWTEST_F(ParseUtilTest, JsonObjToMap008, TestSize.Level3)1113 HWTEST_F(ParseUtilTest, JsonObjToMap008, TestSize.Level3)
1114 {
1115 DTEST_LOG << " JsonObjToMap008 BEGIN" << std::endl;
1116 std::unordered_map<std::string, std::string> strMap;
1117 strMap[EVENT_VALUE] = "test";
1118 nlohmann::json systemAbilityJson;
1119 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1120 systemAbilityJson[it->first] = it->second;
1121 }
1122 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1123 EXPECT_EQ(res.size(), 3);
1124 DTEST_LOG << " JsonObjToMap008 END" << std::endl;
1125 }
1126 /**
1127 * @tc.name: JsonObjToMap
1128 * @tc.desc: eventJson.contains is event_type,evnt_name and event_value;eventJson.is_string is false
1129 * @tc.type: FUNC
1130 * @tc.require: I6MNUA
1131 */
HWTEST_F(ParseUtilTest, JsonObjToMap009, TestSize.Level3)1132 HWTEST_F(ParseUtilTest, JsonObjToMap009, TestSize.Level3)
1133 {
1134 DTEST_LOG << " JsonObjToMap009 BEGIN" << std::endl;
1135 std::unordered_map<std::string, int> strMap;
1136 strMap[EVENT_TYPE] = TEST_NUM;
1137 strMap[EVENT_NAME] = TEST_NUM;
1138 strMap[EVENT_VALUE] = TEST_NUM;
1139 nlohmann::json systemAbilityJson;
1140 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1141 systemAbilityJson[it->first] = it->second;
1142 }
1143 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1144 EXPECT_EQ(res.size(), 3);
1145 DTEST_LOG << " JsonObjToMap009 END" << std::endl;
1146 }
1147 /**
1148 * @tc.name: JsonObjToMap
1149 * @tc.desc: eventJson.contains is evnt_name and event_value;eventJson.is_string is false
1150 * @tc.type: FUNC
1151 * @tc.require: I6MNUA
1152 */
HWTEST_F(ParseUtilTest, JsonObjToMap010, TestSize.Level3)1153 HWTEST_F(ParseUtilTest, JsonObjToMap010, TestSize.Level3)
1154 {
1155 DTEST_LOG << " JsonObjToMap010 BEGIN" << std::endl;
1156 std::unordered_map<std::string, int> strMap;
1157 strMap[EVENT_NAME] = TEST_NUM;
1158 strMap[EVENT_VALUE] = TEST_NUM;
1159 nlohmann::json systemAbilityJson;
1160 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1161 systemAbilityJson[it->first] = it->second;
1162 }
1163 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1164 EXPECT_EQ(res.size(), 3);
1165 DTEST_LOG << " JsonObjToMap010 END" << std::endl;
1166 }
1167 /**
1168 * @tc.name: JsonObjToMap
1169 * @tc.desc: eventJson.contains is event_value;eventJson.is_string is false
1170 * @tc.type: FUNC
1171 * @tc.require: I6MNUA
1172 */
HWTEST_F(ParseUtilTest, JsonObjToMap011, TestSize.Level3)1173 HWTEST_F(ParseUtilTest, JsonObjToMap011, TestSize.Level3)
1174 {
1175 DTEST_LOG << " JsonObjToMap011 BEGIN" << std::endl;
1176 std::unordered_map<std::string, int> strMap;
1177 strMap[EVENT_VALUE] = TEST_NUM;
1178 nlohmann::json systemAbilityJson;
1179 for (auto it = strMap.begin(); it != strMap.end(); it++) {
1180 systemAbilityJson[it->first] = it->second;
1181 }
1182 std::unordered_map<std::string, std::string> res = parser_->JsonObjToMap(systemAbilityJson);
1183 EXPECT_EQ(res.size(), 3);
1184 DTEST_LOG << " JsonObjToMap011 END" << std::endl;
1185 }
1186
1187 /**
1188 * @tc.name: StringToMap001
1189 * @tc.desc: test StringToMap with empty string
1190 * @tc.type: FUNC
1191 * @tc.require: I6YJH6
1192 */
HWTEST_F(ParseUtilTest, StringtoMap001, TestSize.Level3)1193 HWTEST_F(ParseUtilTest, StringtoMap001, TestSize.Level3)
1194 {
1195 unordered_map<std::string, std::string> ret = parser_->StringToMap(EVENT_STRING);
1196 EXPECT_FALSE(ret.empty());
1197 }
1198
1199 /**
1200 * @tc.name: CloseHandle001
1201 * @tc.desc: test CloseHandle with nullptr
1202 * @tc.type: FUNC
1203 * @tc.require: I7G775
1204 */
HWTEST_F(ParseUtilTest, CloseHandle001, TestSize.Level3)1205 HWTEST_F(ParseUtilTest, CloseHandle001, TestSize.Level3)
1206 {
1207 SaProfile saProfile;
1208 parser_->CloseHandle(saProfile);
1209 EXPECT_EQ(saProfile.handle, nullptr);
1210 }
1211
1212 /**
1213 * @tc.name: CheckLogicRelationship001
1214 * @tc.desc: test CheckLogicRelationship
1215 * @tc.type: FUNC
1216 */
HWTEST_F(ParseUtilTest, CheckLogicRelationship001, TestSize.Level3)1217 HWTEST_F(ParseUtilTest, CheckLogicRelationship001, TestSize.Level3)
1218 {
1219 bool ret;
1220 ret = parser_->CheckLogicRelationship("1", "");
1221 EXPECT_EQ(ret, true);
1222 ret = parser_->CheckLogicRelationship("1", "1");
1223 EXPECT_EQ(ret, true);
1224 ret = parser_->CheckLogicRelationship("", "1");
1225 EXPECT_EQ(ret, false);
1226 ret = parser_->CheckLogicRelationship("1", ">1");
1227 EXPECT_EQ(ret, false);
1228 ret = parser_->CheckLogicRelationship("2", ">1");
1229 EXPECT_EQ(ret, true);
1230 ret = parser_->CheckLogicRelationship("1", ">=1");
1231 EXPECT_EQ(ret, true);
1232 ret = parser_->CheckLogicRelationship("1", "<1");
1233 EXPECT_EQ(ret, false);
1234 ret = parser_->CheckLogicRelationship("0", "<1");
1235 EXPECT_EQ(ret, true);
1236 ret = parser_->CheckLogicRelationship("1", "<=1");
1237 EXPECT_EQ(ret, true);
1238 ret = parser_->CheckLogicRelationship("1", ">=abc");
1239 EXPECT_EQ(ret, false);
1240 ret = parser_->CheckLogicRelationship("abc", ">=1");
1241 EXPECT_EQ(ret, false);
1242 }
1243
1244 /**
1245 * @tc.name: ParseSystemAbilityGetExtension001
1246 * @tc.desc: parse sytemability extension tag
1247 * @tc.type: FUNC
1248 */
HWTEST_F(ParseUtilTest, ParseSystemAbilityGetExtension001, TestSize.Level3)1249 HWTEST_F(ParseUtilTest, ParseSystemAbilityGetExtension001, TestSize.Level3)
1250 {
1251 DTEST_LOG << " ParseSystemAbilityGetExtension001 BEGIN" << std::endl;
1252 nlohmann::json systemAbilityJson;
1253 SaProfile saProfile;
1254 bool ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1255 EXPECT_EQ(ret, true);
1256 systemAbilityJson["extension"] = nlohmann::json::array();
1257 systemAbilityJson["extension"].push_back(EXTENSIOON_BACKUP);
1258 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1259 EXPECT_EQ(ret, true);
1260 EXPECT_EQ(saProfile.extension.size(), 1);
1261 auto iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), EXTENSIOON_BACKUP);
1262 EXPECT_NE(iter, saProfile.extension.end());
1263
1264 systemAbilityJson["extension"].push_back(EXTENSIOON_RESTORE);
1265 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1266 EXPECT_EQ(ret, true);
1267 EXPECT_EQ(saProfile.extension.size(), 2);
1268 iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), EXTENSIOON_RESTORE);
1269 EXPECT_NE(iter, saProfile.extension.end());
1270
1271 systemAbilityJson["extension"].push_back(EXTENSIOON_BACKUP);
1272 systemAbilityJson["extension"].push_back(EXTENSIOON_RESTORE);
1273
1274 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1275 EXPECT_EQ(ret, true);
1276 EXPECT_EQ(saProfile.extension.size(), 2);
1277
1278 DTEST_LOG << " ParseSystemAbilityGetExtension001 END" << std::endl;
1279 }
1280
1281 /**
1282 * @tc.name: ParseSystemAbilityGetExtension002
1283 * @tc.desc: parse sytemability extension string length test
1284 * @tc.type: FUNC
1285 */
HWTEST_F(ParseUtilTest, ParseSystemAbilityGetExtension002, TestSize.Level3)1286 HWTEST_F(ParseUtilTest, ParseSystemAbilityGetExtension002, TestSize.Level3)
1287 {
1288 DTEST_LOG << " ParseSystemAbilityGetExtension002 BEGIN" << std::endl;
1289 nlohmann::json systemAbilityJson;
1290 SaProfile saProfile;
1291 systemAbilityJson["extension"] = nlohmann::json::array();
1292
1293 char ch = 'a';
1294 std::string aExceedstr(MAX_JSON_STRING_LENGTH + 1, ch);
1295 systemAbilityJson["extension"].push_back(aExceedstr);
1296 bool ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1297 EXPECT_EQ(ret, false);
1298 EXPECT_EQ(saProfile.extension.size(), 0);
1299
1300 systemAbilityJson["extension"].clear();
1301
1302 ch = 'a';
1303 std::string astr(MAX_JSON_STRING_LENGTH, ch);
1304 systemAbilityJson["extension"].push_back(astr);
1305 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1306 EXPECT_EQ(ret, true);
1307 EXPECT_EQ(saProfile.extension.size(), 1);
1308 auto iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), astr);
1309 EXPECT_NE(iter, saProfile.extension.end());
1310
1311 ch = 'b';
1312 std::string bstr(MAX_JSON_STRING_LENGTH - 1, ch);
1313 systemAbilityJson["extension"].push_back(bstr);
1314 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1315 EXPECT_EQ(ret, true);
1316 EXPECT_EQ(saProfile.extension.size(), 2);
1317 iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), bstr);
1318 EXPECT_NE(iter, saProfile.extension.end());
1319
1320 DTEST_LOG << " ParseSystemAbilityGetExtension002 END" << std::endl;
1321 }
1322
1323 /**
1324 * @tc.name: ParseSystemAbilityGetExtension002
1325 * @tc.desc: parse sytemability extension string num test
1326 * @tc.type: FUNC
1327 */
HWTEST_F(ParseUtilTest, ParseSystemAbilityGetExtension003, TestSize.Level3)1328 HWTEST_F(ParseUtilTest, ParseSystemAbilityGetExtension003, TestSize.Level3)
1329 {
1330 DTEST_LOG << " ParseSystemAbilityGetExtension003 BEGIN" << std::endl;
1331 nlohmann::json systemAbilityJson;
1332 SaProfile saProfile;
1333 systemAbilityJson["extension"] = nlohmann::json::array();
1334
1335 bool ret;
1336 char ch = 0;
1337 for (int32_t loop = 0; loop < MAX_EXTENSIONO_NUM; ++loop) {
1338 std::string str(MAX_JSON_STRING_LENGTH, ch + loop);
1339 systemAbilityJson["extension"].push_back(str);
1340 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1341 EXPECT_EQ(ret, true);
1342 EXPECT_EQ(saProfile.extension.size(), loop + 1);
1343 auto iter = std::find(saProfile.extension.begin(), saProfile.extension.end(), str);
1344 EXPECT_NE(iter, saProfile.extension.end());
1345 }
1346
1347 std::string str(MAX_JSON_STRING_LENGTH, ch + MAX_EXTENSIONO_NUM);
1348 systemAbilityJson["extension"].push_back(str);
1349 ret = parser_->ParseSystemAbilityGetExtension(saProfile, systemAbilityJson);
1350 EXPECT_EQ(ret, false);
1351 EXPECT_EQ(saProfile.extension.size(), MAX_EXTENSIONO_NUM);
1352
1353 DTEST_LOG << " ParseSystemAbilityGetExtension003 END" << std::endl;
1354 }
1355
1356 /**
1357 * @tc.name: GetOndemandPriorityPara001
1358 * @tc.desc: GetOndemandPriorityPara
1359 * @tc.type: FUNC
1360 */
HWTEST_F(ParseUtilTest, GetOndemandPriorityPara001, TestSize.Level3)1361 HWTEST_F(ParseUtilTest, GetOndemandPriorityPara001, TestSize.Level3)
1362 {
1363 DTEST_LOG << " GetOndemandPriorityPara001 BEGIN" << std::endl;
1364 std::string loadPriority = "HighPriority";
1365 uint32_t ret = parser_->GetOndemandPriorityPara(loadPriority);
1366 EXPECT_EQ(ret, static_cast<uint32_t>(HIGH_PRIORITY));
1367
1368 loadPriority = "MediumPriority";
1369 ret = parser_->GetOndemandPriorityPara(loadPriority);
1370 EXPECT_EQ(ret, static_cast<uint32_t>(MEDIUM_PRIORITY));
1371
1372 loadPriority = "NEW_TEST";
1373 ret = parser_->GetOndemandPriorityPara(loadPriority);
1374 EXPECT_EQ(ret, static_cast<uint32_t>(LOW_PRIORITY));
1375 }
1376
1377 } // namespace SAMGR
1378 } // namespace OHOS
1379