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
16 #include "security_guard_config_manager_test.h"
17
18 #include "file_ex.h"
19 #include "gmock/gmock.h"
20 #include "nlohmann/json.hpp"
21 #include <thread>
22 #include <fstream>
23 #include "accesstoken_kit.h"
24 #include "nativetoken_kit.h"
25 #include "token_setproc.h"
26 #include "security_event_info.h"
27 #include "json_cfg.h"
28 #define private public
29 #define protected public
30 #include "base_config.h"
31 #include "config_data_manager.h"
32 #include "i_model_info.h"
33 #include "config_define.h"
34 #include "config_manager.h"
35 #include "config_operator.h"
36 #include "config_subscriber.h"
37 #include "event_config.h"
38 #include "model_cfg_marshalling.h"
39 #include "model_config.h"
40 #include "security_guard_log.h"
41 #include "event_group_config.h"
42 #include "json_util.h"
43 #include "file_util.h"
44 #undef private
45 #undef protected
46
47 using namespace testing;
48 using namespace testing::ext;
49 using namespace OHOS::Security::SecurityGuard;
50 using namespace OHOS::Security::SecurityGuardTest;
51 using namespace OHOS::Security::SecurityGuard::FileUtil;
52 using namespace OHOS::Security::SecurityGuard::JsonUtil;
53
54 namespace OHOS::Security::SecurityGuardTest {
55
SetUpTestCase()56 void SecurityGuardConfigManagerTest::SetUpTestCase()
57 {
58 static const char *permission[] = { "ohos.permission.securityguard.REPORT_SECURITY_INFO" };
59 uint64_t tokenId;
60 NativeTokenInfoParams infoParams = {
61 .dcapsNum = 0,
62 .permsNum = 1,
63 .aclsNum = 0,
64 .dcaps = nullptr,
65 .perms = permission,
66 .acls = nullptr,
67 .processName = "security_guard",
68 .aplStr = "system_basic",
69 };
70 tokenId = GetAccessTokenId(&infoParams);
71 SetSelfTokenID(tokenId);
72 }
73
TearDownTestCase()74 void SecurityGuardConfigManagerTest::TearDownTestCase()
75 {
76 }
77
SetUp()78 void SecurityGuardConfigManagerTest::SetUp()
79 {
80 }
81
TearDown()82 void SecurityGuardConfigManagerTest::TearDown()
83 {
84 }
85
86 class MockBaseConfig : public BaseConfig {
87 public:
88 MockBaseConfig() = default;
89 ~MockBaseConfig() override = default;
90 MOCK_METHOD0(Check, bool());
91 MOCK_METHOD1(Load, bool(int));
92 MOCK_METHOD0(Parse, bool());
93 MOCK_METHOD0(Update, bool());
94 };
95
96 class TestBaseConfig : public BaseConfig {
97 public:
98 TestBaseConfig() = default;
99 ~TestBaseConfig() override = default;
100 MOCK_METHOD1(Load, bool(int));
101 MOCK_METHOD0(Parse, bool());
102 MOCK_METHOD0(Update, bool());
103 };
104
105 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigOperator001, TestSize.Level1)
106 {
107 MockBaseConfig config;
108 auto configOptor = std::make_unique<ConfigOperator>(config);
109 EXPECT_CALL(config, Load).WillOnce(Return(false)).WillRepeatedly(Return(true));
110 EXPECT_CALL(config, Check).WillOnce(Return(false)).WillRepeatedly(Return(true));
111 EXPECT_CALL(config, Parse).WillOnce(Return(false)).WillRepeatedly(Return(true));
112 bool success = configOptor->Init();
113 EXPECT_FALSE(success);
114 success = configOptor->Init();
115 EXPECT_FALSE(success);
116 success = configOptor->Init();
117 EXPECT_FALSE(success);
118 success = configOptor->Init();
119 EXPECT_TRUE(success);
120 }
121
122 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigOperator002, TestSize.Level1)
123 {
124 MockBaseConfig config;
125 auto configOptor = std::make_unique<ConfigOperator>(config);
126 EXPECT_CALL(config, Load).WillOnce(Return(false)).WillRepeatedly(Return(true));
127 EXPECT_CALL(config, Check).WillOnce(Return(false)).WillRepeatedly(Return(true));
128 EXPECT_CALL(config, Update).WillOnce(Return(false)).WillRepeatedly(Return(true));
129 bool success = configOptor->Update();
130 EXPECT_FALSE(success);
131 success = configOptor->Update();
132 EXPECT_FALSE(success);
133 success = configOptor->Update();
134 EXPECT_FALSE(success);
135 success = configOptor->Update();
136 EXPECT_TRUE(success);
137 }
138
139 HWTEST_F(SecurityGuardConfigManagerTest, TestBaseConfig001, TestSize.Level1)
140 {
141 TestBaseConfig config;
142 config.stream_.close();
143 bool success = config.Check();
144 EXPECT_FALSE(success);
145 config.stream_.open("test.txt");
146 success = config.Check();
147 EXPECT_FALSE(success);
148 config.stream_.open("/data/test/unittest/resource/stream_empty.txt");
149 success = config.Check();
150 EXPECT_FALSE(success);
151 config.stream_.open("/data/test/unittest/resource/stream_not_empty.txt");
152 success = config.Check();
153 EXPECT_TRUE(success);
154 }
155
156 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigDataManager001, TestSize.Level1)
157 {
158 EventCfg config = {};
159 std::string eventName = "test eventName";
160 config.eventName = eventName;
161 ConfigDataManager::GetInstance().InsertEventMap(config.eventId, config);
162 EventCfg outConfig = {};
163 bool success = ConfigDataManager::GetInstance().GetEventConfig(config.eventId, outConfig);
164 EXPECT_TRUE(success);
165 EXPECT_TRUE(outConfig.eventName == config.eventName);
166 std::vector<int64_t> eventIds = ConfigDataManager::GetInstance().GetAllEventIds();
167 EXPECT_TRUE(eventIds.size() == 1);
168 EXPECT_TRUE(eventIds[0] == 0);
169 ConfigDataManager::GetInstance().ResetEventMap();
170 success = ConfigDataManager::GetInstance().GetEventConfig(config.eventId, outConfig);
171 EXPECT_FALSE(success);
172 eventIds = ConfigDataManager::GetInstance().GetAllEventIds();
173 EXPECT_TRUE(eventIds.size() == 0);
174 }
175
176 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigDataManager002, TestSize.Level1)
177 {
178 ModelCfg config = {};
179 std::string path = "test path";
180 config.path = path;
181 ConfigDataManager::GetInstance().InsertModelMap(config.modelId, config);
182 ModelCfg outConfig = {};
183 bool success = ConfigDataManager::GetInstance().GetModelConfig(config.modelId, outConfig);
184 EXPECT_TRUE(success);
185 EXPECT_TRUE(outConfig.path == config.path);
186 ConfigDataManager::GetInstance().ResetModelMap();
187 success = ConfigDataManager::GetInstance().GetModelConfig(config.modelId, outConfig);
188 EXPECT_FALSE(success);
189 }
190
191 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigDataManager003, TestSize.Level1)
192 {
193 std::set<int64_t> eventIds {1};
194 int32_t modelId = 0;
195 ConfigDataManager::GetInstance().InsertModelToEventMap(modelId, eventIds);
196 std::vector<int64_t> outEventIds = ConfigDataManager::GetInstance().GetEventIds(modelId);
197 EXPECT_TRUE(outEventIds.size() == 1);
198 EXPECT_TRUE(outEventIds[0] == 1);
199 ConfigDataManager::GetInstance().ResetModelToEventMap();
200 outEventIds = ConfigDataManager::GetInstance().GetEventIds(modelId);
201 EXPECT_TRUE(outEventIds.size() == 0);
202 }
203
204 HWTEST_F(SecurityGuardConfigManagerTest, TestEventConfig002, TestSize.Level1)
205 {
206 ConfigDataManager::GetInstance().ResetEventMap();
207 ConfigDataManager::GetInstance().ResetModelMap();
208 ConfigDataManager::GetInstance().ResetModelToEventMap();
209 EventConfig config;
210 bool success = config.Parse();
211 EXPECT_FALSE(success);
212 config.stream_.open("/data/test/unittest/resource/security_guard_preset_event.cfg");
213 success = config.Parse();
214 EXPECT_TRUE(success);
215 EventCfg eventCfg;
216 eventCfg.eventId = 2;
217 success = ConfigDataManager::GetInstance().GetEventConfig(eventCfg.eventId, eventCfg);
218 EXPECT_TRUE(success);
219 EXPECT_TRUE(eventCfg.eventName == "preset_event");
220 }
221
222 HWTEST_F(SecurityGuardConfigManagerTest, TestEventConfig003, TestSize.Level1)
223 {
224 ConfigDataManager::GetInstance().ResetEventMap();
225 ConfigDataManager::GetInstance().ResetModelMap();
226 ConfigDataManager::GetInstance().ResetModelToEventMap();
227 EventConfig config;
228 bool success = config.Parse();
229 EXPECT_FALSE(success);
230 config.stream_.open("/data/test/unittest/resource/security_guard_update_event.cfg");
231 EXPECT_TRUE(config.stream_.is_open());
232 success = config.Parse();
233 EXPECT_TRUE(success);
234 EventCfg eventCfg;
235 eventCfg.eventId = 3;
236 success = ConfigDataManager::GetInstance().GetEventConfig(eventCfg.eventId, eventCfg);
237 EXPECT_TRUE(success);
238 EXPECT_TRUE(eventCfg.eventName == "update_event");
239 }
240
241 HWTEST_F(SecurityGuardConfigManagerTest, TestEventConfig001, TestSize.Level1)
242 {
243 EventConfig config;
244 bool success = config.Load(INIT_MODE);
245 EXPECT_TRUE(success);
246 EXPECT_TRUE(config.Load(UPDATE_MODE));
247 config.Update();
248 }
249
250 HWTEST_F(SecurityGuardConfigManagerTest, TestModelConfig001, TestSize.Level1)
251 {
252 ModelConfig config;
253 bool success = config.Load(INIT_MODE);
254 EXPECT_TRUE(success);
255 EXPECT_TRUE(config.Load(UPDATE_MODE));
256 }
257
258 HWTEST_F(SecurityGuardConfigManagerTest, TestModelConfig002, TestSize.Level1)
259 {
260 ConfigDataManager::GetInstance().ResetEventMap();
261 ConfigDataManager::GetInstance().ResetModelMap();
262 ConfigDataManager::GetInstance().ResetModelToEventMap();
263 ModelConfig config;
264 bool success = config.Parse();
265 EXPECT_FALSE(success);
266 config.stream_.open("/data/test/unittest/resource/security_guard_preset_model.cfg");
267 EXPECT_TRUE(config.stream_.is_open());
268 success = config.Parse();
269 EXPECT_TRUE(success);
270 ModelCfg modelCfg;
271 modelCfg.modelId = 2;
272 success = ConfigDataManager::GetInstance().GetModelConfig(modelCfg.modelId, modelCfg);
273 EXPECT_TRUE(success);
274 EXPECT_TRUE(modelCfg.path == "preset_model");
275 }
276
277 HWTEST_F(SecurityGuardConfigManagerTest, TestModelConfig003, TestSize.Level1)
278 {
279 ConfigDataManager::GetInstance().ResetEventMap();
280 ConfigDataManager::GetInstance().ResetModelMap();
281 ConfigDataManager::GetInstance().ResetModelToEventMap();
282 ModelConfig config;
283 bool success = config.Parse();
284 EXPECT_FALSE(success);
285 config.stream_.open("/data/test/unittest/resource/security_guard_update_model.cfg");
286 EXPECT_TRUE(config.stream_.is_open());
287 success = config.Parse();
288 EXPECT_TRUE(success);
289 ModelCfg modelCfg;
290 modelCfg.modelId = 3;
291 success = ConfigDataManager::GetInstance().GetModelConfig(modelCfg.modelId, modelCfg);
292 EXPECT_TRUE(success);
293 EXPECT_TRUE(modelCfg.path == "update_model");
294 }
295
296 HWTEST_F(SecurityGuardConfigManagerTest, TestModelConfig004, TestSize.Level1)
297 {
298 ConfigDataManager::GetInstance().ResetEventMap();
299 ConfigDataManager::GetInstance().ResetModelMap();
300 ConfigDataManager::GetInstance().ResetModelToEventMap();
301 ModelConfig config;
302 nlohmann::json jsonObj;
303 ModelCfg modelCfg;
304 to_json(jsonObj, modelCfg);
305 DataMgrCfgSt dataMgrCfg{};
306 to_json(jsonObj, dataMgrCfg);
307 from_json(jsonObj, dataMgrCfg);
308 SecEvent eventDataSt{};
309 to_json(jsonObj, eventDataSt);
310 EventContentSt eventContentSt{};
311 to_json(jsonObj, eventContentSt);
312 from_json(jsonObj, eventContentSt);
313 EventCfg eventCfg{};
314 jsonObj["modelId"] = "xxx";
315 from_json(jsonObj, modelCfg);
316 jsonObj["eventId"] = "xxx";
317 from_json(jsonObj, eventCfg);
318 bool success = config.Parse();
319 EXPECT_FALSE(success);
320 config.stream_.open("/data/test/unittest/resource/security_guard_preset_model.cfg");
321 EXPECT_TRUE(config.stream_.is_open());
322 EXPECT_TRUE(config.Update());
323 }
324
325 HWTEST_F(SecurityGuardConfigManagerTest, TestModelConfig005, TestSize.Level1)
326 {
327 ConfigDataManager::GetInstance().ResetEventMap();
328 ConfigDataManager::GetInstance().ResetModelMap();
329 ConfigDataManager::GetInstance().ResetModelToEventMap();
330 ConfigDataManager::GetInstance().GetAllModelIds();
331 ConfigDataManager::GetInstance().ResetEventToTableMap();
332 EXPECT_TRUE(ConfigDataManager::GetInstance().GetTableFromEventId(0).empty());
333 EXPECT_TRUE(ConfigDataManager::GetInstance().GetAllEventConfigs().empty());
334 }
335
336 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigSubsciber003, TestSize.Level1)
337 {
338 EXPECT_TRUE(
339 ConfigSubscriber::UpdateConfig(CONFIG_CACHE_FILES[EVENT_CFG_INDEX]));
340 EXPECT_TRUE(
341 ConfigSubscriber::UpdateConfig(CONFIG_CACHE_FILES[MODEL_CFG_INDEX]));
342 }
343
344 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling001, TestSize.Level1)
345 {
346 nlohmann::json jsonObj;
347 SecurityGuard::AppDetectionCfg cfg = jsonObj.get<SecurityGuard::AppDetectionCfg>();
348 EXPECT_TRUE(cfg.detectionCategory == "");
349 }
350
351 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling002, TestSize.Level1)
352 {
353 nlohmann::json jsonObj;
354 jsonObj["detectionCategory"] = 0;
355 SecurityGuard::AppDetectionCfg cfg = jsonObj.get<SecurityGuard::AppDetectionCfg>();
356 EXPECT_TRUE(cfg.detectionCategory == "");
357 }
358
359 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling003, TestSize.Level1)
360 {
361 nlohmann::json jsonObj;
362 jsonObj["detectionCategory"] = "detectionCategory";
363 SecurityGuard::AppDetectionCfg cfg = jsonObj.get<SecurityGuard::AppDetectionCfg>();
364 EXPECT_TRUE(cfg.detectionCategory == "detectionCategory");
365 }
366
367 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling004, TestSize.Level1)
368 {
369 nlohmann::json jsonObj;
370 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
371 EXPECT_TRUE(field.fieldName == "");
372 EXPECT_TRUE(field.fieldType == "");
373 EXPECT_TRUE(field.value == "");
374 }
375
376 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling005, TestSize.Level1)
377 {
378 nlohmann::json jsonObj;
379 jsonObj["fieldName"] = 0;
380 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
381 EXPECT_TRUE(field.fieldName == "");
382 EXPECT_TRUE(field.fieldType == "");
383 EXPECT_TRUE(field.value == "");
384 }
385
386 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling006, TestSize.Level1)
387 {
388 nlohmann::json jsonObj;
389 jsonObj["fieldName"] = 0;
390 jsonObj["fieldType"] = 0;
391 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
392 EXPECT_TRUE(field.fieldName == "");
393 EXPECT_TRUE(field.fieldType == "");
394 EXPECT_TRUE(field.value == "");
395 }
396
397 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling007, TestSize.Level1)
398 {
399 nlohmann::json jsonObj;
400 jsonObj["fieldName"] = 0;
401 jsonObj["fieldType"] = 0;
402 jsonObj["value"] = 0;
403 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
404 EXPECT_TRUE(field.fieldName == "");
405 EXPECT_TRUE(field.fieldType == "");
406 EXPECT_TRUE(field.value == "");
407 }
408
409 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling008, TestSize.Level1)
410 {
411 nlohmann::json jsonObj;
412 jsonObj["fieldName"] = "fieldName";
413 jsonObj["fieldType"] = 0;
414 jsonObj["value"] = 0;
415 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
416 EXPECT_TRUE(field.fieldName == "");
417 EXPECT_TRUE(field.fieldType == "");
418 EXPECT_TRUE(field.value == "");
419 }
420
421 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling009, TestSize.Level1)
422 {
423 nlohmann::json jsonObj;
424 jsonObj["fieldName"] = "fieldName";
425 jsonObj["fieldType"] = "fieldType";
426 jsonObj["value"] = 0;
427 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
428 EXPECT_TRUE(field.fieldName == "");
429 EXPECT_TRUE(field.fieldType == "");
430 EXPECT_TRUE(field.value == "");
431 }
432
433 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling010, TestSize.Level1)
434 {
435 nlohmann::json jsonObj;
436 jsonObj["fieldName"] = "fieldName";
437 jsonObj["fieldType"] = "fieldType";
438 jsonObj["value"] = "value";
439 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
440 EXPECT_TRUE(field.fieldName == "fieldName");
441 EXPECT_TRUE(field.fieldType == "fieldType");
442 EXPECT_TRUE(field.value == "value");
443 }
444
445 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling011, TestSize.Level1)
446 {
447 nlohmann::json jsonObj;
448 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
449 EXPECT_TRUE(rule.eventId == 0);
450 EXPECT_TRUE(rule.fields.empty());
451 EXPECT_TRUE(rule.fieldsRelation == "");
452 }
453
454 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling012, TestSize.Level1)
455 {
456 nlohmann::json jsonObj;
457 jsonObj["eventId"] = "";
458 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
459 EXPECT_TRUE(rule.eventId == 0);
460 EXPECT_TRUE(rule.fields.empty());
461 EXPECT_TRUE(rule.fieldsRelation == "");
462 }
463
464 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling013, TestSize.Level1)
465 {
466 nlohmann::json jsonObj;
467 jsonObj["eventId"] = "";
468 jsonObj["fields"] = 0;
469 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
470 EXPECT_TRUE(rule.eventId == 0);
471 EXPECT_TRUE(rule.fields.empty());
472 EXPECT_TRUE(rule.fieldsRelation == "");
473 }
474
475 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling014, TestSize.Level1)
476 {
477 nlohmann::json jsonObj;
478 jsonObj["eventId"] = "";
479 jsonObj["fields"] = 0;
480 jsonObj["fieldsRelation"] = 0;
481 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
482 EXPECT_TRUE(rule.eventId == 0);
483 EXPECT_TRUE(rule.fields.empty());
484 EXPECT_TRUE(rule.fieldsRelation == "");
485 }
486
487 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling015, TestSize.Level1)
488 {
489 nlohmann::json jsonObj;
490 jsonObj["eventId"] = 0;
491 jsonObj["fields"] = 0;
492 jsonObj["fieldsRelation"] = 0;
493 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
494 EXPECT_TRUE(rule.eventId == 0);
495 EXPECT_TRUE(rule.fields.empty());
496 EXPECT_TRUE(rule.fieldsRelation == "");
497 }
498
499 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling016, TestSize.Level1)
500 {
501 nlohmann::json jsonObj;
502 jsonObj["eventId"] = 0;
503 jsonObj["fields"] = 0;
504 jsonObj["fieldsRelation"] = 0;
505 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
506 EXPECT_TRUE(rule.eventId == 0);
507 EXPECT_TRUE(rule.fields.empty());
508 EXPECT_TRUE(rule.fieldsRelation == "");
509 }
510
511 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling017, TestSize.Level1)
512 {
513 nlohmann::json jsonObj;
514 nlohmann::json jsonField;
515 jsonField["fieldName"] = "fieldName";
516 jsonField["fieldType"] = "fieldType";
517 jsonField["value"] = "value";
518 jsonObj["eventId"] = 0;
519 jsonObj["fields"] = {jsonField, jsonField};
520 jsonObj["fieldsRelation"] = 0;
521 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
522 EXPECT_TRUE(rule.eventId == 0);
523 EXPECT_TRUE(rule.fields.empty());
524 EXPECT_TRUE(rule.fieldsRelation == "");
525 }
526
527 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling018, TestSize.Level1)
528 {
529 nlohmann::json jsonObj;
530 nlohmann::json jsonField;
531 jsonField["fieldName"] = "fieldName";
532 jsonField["fieldType"] = "fieldType";
533 jsonField["value"] = "value";
534 jsonObj["eventId"] = 0;
535 jsonObj["fields"] = {jsonField, jsonField};
536 jsonObj["fieldsRelation"] = "fieldsRelation";
537 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
538 EXPECT_TRUE(rule.eventId == 0);
539 EXPECT_TRUE(!rule.fields.empty());
540 EXPECT_TRUE(rule.fieldsRelation == "fieldsRelation");
541 }
542
543 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling019, TestSize.Level1)
544 {
545 nlohmann::json jsonObj;
546 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
547 EXPECT_TRUE(cfg.rules.empty());
548 EXPECT_TRUE(cfg.rulesRelation == "");
549 EXPECT_TRUE(cfg.trueResult == "");
550 EXPECT_TRUE(cfg.falseResult == "");
551 }
552
553 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling020, TestSize.Level1)
554 {
555 nlohmann::json jsonObj;
556 jsonObj["rules"] = 0;
557 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
558 EXPECT_TRUE(cfg.rules.empty());
559 EXPECT_TRUE(cfg.rulesRelation == "");
560 EXPECT_TRUE(cfg.trueResult == "");
561 EXPECT_TRUE(cfg.falseResult == "");
562 }
563
564 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling021, TestSize.Level1)
565 {
566 nlohmann::json jsonObj;
567 jsonObj["rules"] = 0;
568 jsonObj["rulesRelation"] = 0;
569 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
570 EXPECT_TRUE(cfg.rules.empty());
571 EXPECT_TRUE(cfg.rulesRelation == "");
572 EXPECT_TRUE(cfg.trueResult == "");
573 EXPECT_TRUE(cfg.falseResult == "");
574 }
575
576 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling022, TestSize.Level1)
577 {
578 nlohmann::json jsonObj;
579 jsonObj["rules"] = 0;
580 jsonObj["rulesRelation"] = 0;
581 jsonObj["trueResult"] = 0;
582 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
583 EXPECT_TRUE(cfg.rules.empty());
584 EXPECT_TRUE(cfg.rulesRelation == "");
585 EXPECT_TRUE(cfg.trueResult == "");
586 EXPECT_TRUE(cfg.falseResult == "");
587 }
588
589 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling023, TestSize.Level1)
590 {
591 nlohmann::json jsonObj;
592 jsonObj["rules"] = 0;
593 jsonObj["rulesRelation"] = 0;
594 jsonObj["trueResult"] = 0;
595 jsonObj["falseResult"] = 0;
596 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
597 EXPECT_TRUE(cfg.rules.empty());
598 EXPECT_TRUE(cfg.rulesRelation == "");
599 EXPECT_TRUE(cfg.trueResult == "");
600 EXPECT_TRUE(cfg.falseResult == "");
601 }
602
603 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling024, TestSize.Level1)
604 {
605 nlohmann::json jsonObj;
606 nlohmann::json jsonRule;
607 nlohmann::json jsonField;
608 jsonField["fieldName"] = "fieldName";
609 jsonField["fieldType"] = "fieldType";
610 jsonField["value"] = "value";
611 jsonRule["eventId"] = 0;
612 jsonRule["fields"] = {jsonField, jsonField};
613 jsonRule["fieldsRelation"] = "fieldsRelation";
614 jsonObj["rules"] = {jsonRule, jsonRule};
615 jsonObj["rulesRelation"] = 0;
616 jsonObj["trueResult"] = 0;
617 jsonObj["falseResult"] = 0;
618 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
619 EXPECT_TRUE(cfg.rules.empty());
620 EXPECT_TRUE(cfg.rulesRelation == "");
621 EXPECT_TRUE(cfg.trueResult == "");
622 EXPECT_TRUE(cfg.falseResult == "");
623 }
624
625 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling025, TestSize.Level1)
626 {
627 nlohmann::json jsonObj;
628 nlohmann::json jsonRule;
629 nlohmann::json jsonField;
630 jsonField["fieldName"] = "fieldName";
631 jsonField["fieldType"] = "fieldType";
632 jsonField["value"] = "value";
633 jsonRule["eventId"] = 0;
634 jsonRule["fields"] = {jsonField, jsonField};
635 jsonRule["fieldsRelation"] = "fieldsRelation";
636 jsonObj["rules"] = {jsonRule, jsonRule};
637 jsonObj["rulesRelation"] = "rulesRelation";
638 jsonObj["trueResult"] = 0;
639 jsonObj["falseResult"] = 0;
640 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
641 EXPECT_TRUE(cfg.rules.empty());
642 EXPECT_TRUE(cfg.rulesRelation == "");
643 EXPECT_TRUE(cfg.trueResult == "");
644 EXPECT_TRUE(cfg.falseResult == "");
645 }
646
647 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling026, TestSize.Level1)
648 {
649 nlohmann::json jsonObj;
650 nlohmann::json jsonRule;
651 nlohmann::json jsonField;
652 jsonField["fieldName"] = "fieldName";
653 jsonField["fieldType"] = "fieldType";
654 jsonField["value"] = "value";
655 jsonRule["eventId"] = 0;
656 jsonRule["fields"] = {jsonField, jsonField};
657 jsonRule["fieldsRelation"] = "fieldsRelation";
658 jsonObj["rules"] = {jsonRule, jsonRule};
659 jsonObj["rulesRelation"] = "rulesRelation";
660 jsonObj["trueResult"] = "trueResult";
661 jsonObj["falseResult"] = 0;
662 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
663 EXPECT_TRUE(cfg.rules.empty());
664 EXPECT_TRUE(cfg.rulesRelation == "");
665 EXPECT_TRUE(cfg.trueResult == "");
666 EXPECT_TRUE(cfg.falseResult == "");
667 }
668
669 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling027, TestSize.Level1)
670 {
671 nlohmann::json jsonObj;
672 nlohmann::json jsonRule;
673 nlohmann::json jsonField;
674 jsonField["fieldName"] = "fieldName";
675 jsonField["fieldType"] = "fieldType";
676 jsonField["value"] = "value";
677 jsonRule["eventId"] = 0;
678 jsonRule["fields"] = {jsonField, jsonField};
679 jsonRule["fieldsRelation"] = "fieldsRelation";
680 jsonObj["rules"] = {jsonRule, jsonRule};
681 jsonObj["rulesRelation"] = "rulesRelation";
682 jsonObj["trueResult"] = "trueResult";
683 jsonObj["falseResult"] = "falseResult";
684 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
685 EXPECT_TRUE(!cfg.rules.empty());
686 EXPECT_TRUE(cfg.rulesRelation == "rulesRelation");
687 EXPECT_TRUE(cfg.trueResult == "trueResult");
688 EXPECT_TRUE(cfg.falseResult == "falseResult");
689 }
690
691 HWTEST_F(SecurityGuardConfigManagerTest, TestUnmarshal001, testing::ext::TestSize.Level1)
692 {
693 using namespace OHOS::Security::SecurityGuard;
694 nlohmann::json jsonOb {
695 {"version", "xxx"},
696 {"releaseTime", "xxx"},
697 {"detectMaxRecord", 999},
698 {"uidMaxDnsRecord", 10},
699 {"detectMaxTime", 86399},
700 {"ipBlackList", {"1", "2"}},
701 {"ipBlackListMock", {"xxx", "xxx"}},
702 {"dnsBlackList", {"1", "2"}},
703 {"number", {1, 2}}
704 };
705 std::vector<std::string> testVec {};
706 std::vector<int64_t> testVecInt {};
707 std::vector<int32_t> testVecIntS{};
708 uint64_t data;
709 int64_t dataInt;
710 int32_t i32Data;
711 uint32_t u32Data;
712
713 EXPECT_TRUE(JsonCfg::Unmarshal(i32Data, jsonOb, "detectMaxRecord"));
714 EXPECT_FALSE(JsonCfg::Unmarshal(i32Data, jsonOb, "releaseTime"));
715 EXPECT_FALSE(JsonCfg::Unmarshal(i32Data, jsonOb, "isexist"));
716
717 EXPECT_TRUE(JsonCfg::Unmarshal(u32Data, jsonOb, "detectMaxRecord"));
718 EXPECT_FALSE(JsonCfg::Unmarshal(u32Data, jsonOb, "releaseTime"));
719 EXPECT_FALSE(JsonCfg::Unmarshal(u32Data, jsonOb, "isexist"));
720
721 EXPECT_TRUE(JsonCfg::Unmarshal(data, jsonOb, "detectMaxRecord"));
722 EXPECT_FALSE(JsonCfg::Unmarshal(data, jsonOb, "releaseTime"));
723 EXPECT_FALSE(JsonCfg::Unmarshal(data, jsonOb, "isexist"));
724
725 EXPECT_TRUE(JsonCfg::Unmarshal(dataInt, jsonOb, "uidMaxDnsRecord"));
726 EXPECT_FALSE(JsonCfg::Unmarshal(dataInt, jsonOb, "releaseTime"));
727 EXPECT_FALSE(JsonCfg::Unmarshal(dataInt, jsonOb, "isexist"));
728
729 EXPECT_TRUE(JsonCfg::Unmarshal(testVec, jsonOb, "ipBlackList"));
730 EXPECT_FALSE(JsonCfg::Unmarshal(testVec, jsonOb, "isexist"));
731 EXPECT_FALSE(JsonCfg::Unmarshal(testVec, jsonOb, "number"));
732
733 EXPECT_TRUE(JsonCfg::Unmarshal(testVecInt, jsonOb, "number"));
734 EXPECT_FALSE(JsonCfg::Unmarshal(testVecInt, jsonOb, "ipBlackList"));
735 EXPECT_FALSE(JsonCfg::Unmarshal(testVecInt, jsonOb, "isexist"));
736
737 EXPECT_TRUE(JsonCfg::Unmarshal(testVecIntS, jsonOb, "number"));
738 EXPECT_FALSE(JsonCfg::Unmarshal(testVecIntS, jsonOb, "ipBlackList"));
739 EXPECT_FALSE(JsonCfg::Unmarshal(testVecIntS, jsonOb, "isexist"));
740 }
741
742 HWTEST_F(SecurityGuardConfigManagerTest, TestEventGroupConfig001, TestSize.Level1)
743 {
744 EventGroupConfig config;
745 bool success = config.Load(INIT_MODE);
746 EXPECT_TRUE(success);
747 config.Parse();
748 EXPECT_FALSE(config.Load(UPDATE_MODE));
749 EXPECT_TRUE(config.Update());
750 }
751
752 HWTEST_F(SecurityGuardConfigManagerTest, TestEventGroupConfigFail001, TestSize.Level1)
753 {
754 EventGroupConfig config;
755 nlohmann::json jsonObj;
756 nlohmann::json jsonGroupInfo;
757 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
758 jsonObj["eventGroupList"] = "test";
759 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
760 jsonObj.clear();
761 jsonGroupInfo["test"] = "";
762 jsonObj["eventGroupList"] = {jsonGroupInfo, jsonGroupInfo};
763 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
764 jsonGroupInfo.clear();
765 jsonObj.clear();
766 jsonGroupInfo["eventGroupName"] = "";
767 jsonObj["eventGroupList"] = {jsonGroupInfo, jsonGroupInfo};
768 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
769 jsonGroupInfo.clear();
770 jsonObj.clear();
771 jsonGroupInfo["eventGroupName"] = "Sec";
772 jsonObj["eventGroupList"] = {jsonGroupInfo, jsonGroupInfo};
773 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
774 jsonGroupInfo.clear();
775 jsonObj.clear();
776 jsonGroupInfo["eventGroupName"] = "Sec";
777 jsonGroupInfo["eventList"] = {"", "1111111111111111111111111111111111111111111111111111111"};
778 jsonObj["eventGroupList"] = {jsonGroupInfo, jsonGroupInfo};
779 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
780 jsonGroupInfo.clear();
781 jsonObj.clear();
782 jsonGroupInfo["eventGroupName"] = "Sec";
783 jsonGroupInfo["eventList"] = {"123", "11111"};
784 jsonGroupInfo["permission"] = {"test", "test1"};
785 jsonObj["eventGroupList"] = {jsonGroupInfo, jsonGroupInfo};
786 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
787 }
788
789 HWTEST_F(SecurityGuardConfigManagerTest, GetEventGroupConfig001, TestSize.Level1)
790 {
791 EventGroupCfg config {};
792 EXPECT_FALSE(ConfigDataManager::GetInstance().GetEventGroupConfig("testttt", config));
793 ConfigDataManager::GetInstance().eventGroupMap_.insert({"testttt", config});
794 EXPECT_TRUE(ConfigDataManager::GetInstance().GetEventGroupConfig("testttt", config));
795 }
796
797 HWTEST_F(SecurityGuardConfigManagerTest, GetIsBatchUpload001, TestSize.Level1)
798 {
799 EXPECT_FALSE(ConfigDataManager::GetInstance().GetIsBatchUpload("test11"));
800 EventGroupCfg config {};
801 config.isBatchUpload = true;
802 ConfigDataManager::GetInstance().eventGroupMap_.insert({"test11", config});
803 EXPECT_TRUE(ConfigDataManager::GetInstance().GetIsBatchUpload("test11"));
804 }
805
SetUpTestCase()806 void SecurityGuardUtilsTest::SetUpTestCase()
807 {
808 }
809
TearDownTestCase()810 void SecurityGuardUtilsTest::TearDownTestCase()
811 {
812 }
813
SetUp()814 void SecurityGuardUtilsTest::SetUp()
815 {
816 constexpr int64_t overInt32 = 2147483648;
817 rootJson = cJSON_CreateObject();
818 cJSON_AddBoolToObject(rootJson, "key1", true);
819 cJSON_AddBoolToObject(rootJson, "key2", false);
820 cJSON_AddNumberToObject(rootJson, "key3", 0);
821 cJSON_AddNumberToObject(rootJson, "key4", 1);
822 cJSON_AddNumberToObject(rootJson, "key5", -1);
823 cJSON_AddNumberToObject(rootJson, "key6", overInt32);
824 cJSON_AddNullToObject(rootJson, "key7");
825 cJSON_AddStringToObject(rootJson, "key8", "valid_string");
826 }
827
TearDown()828 void SecurityGuardUtilsTest::TearDown()
829 {
830 cJSON_Delete(rootJson);
831 }
832
833 HWTEST_F(SecurityGuardUtilsTest, ReadFileToStrReturnFalse001, TestSize.Level1)
834 {
835 std::string fileName = "non_existent_file.txt";
836 std::ios::pos_type fileMaxSize = 1024;
837 std::string str;
838
839 EXPECT_FALSE(ReadFileToStr(fileName, fileMaxSize, str));
840 }
841
842 HWTEST_F(SecurityGuardUtilsTest, ReadFileToStrReturnFalse002, TestSize.Level1)
843 {
844 std::string fileName = "empty_file.txt";
845 std::ios::pos_type fileMaxSize = 1024;
846 std::string str;
847 std::ofstream emptyFile(fileName);
848 emptyFile.close();
849 EXPECT_FALSE(ReadFileToStr(fileName, fileMaxSize, str));
850 std::remove(fileName.c_str());
851 }
852
853 HWTEST_F(SecurityGuardUtilsTest, ReadFileToStrReturnFalse003, TestSize.Level1)
854 {
855 std::string fileName = "large_file.txt";
856 std::ios::pos_type fileMaxSize = 10;
857 std::string str;
858 std::ofstream largeFile(fileName);
859
860 largeFile << "aaaaaaaaaaaaaaaaaaaa";
861 largeFile.close();
862
863 EXPECT_FALSE(ReadFileToStr(fileName, fileMaxSize, str));
864 std::remove(fileName.c_str());
865 }
866
867 HWTEST_F(SecurityGuardUtilsTest, ReadFileToStrReturnTrue001, TestSize.Level1)
868 {
869 std::string fileName = "valid_file.txt";
870 std::ios::pos_type fileMaxSize = 1024;
871 std::string str;
872
873 std::ofstream validFile(fileName);
874 validFile << "hello, world!";
875 validFile.close();
876
877 EXPECT_TRUE(ReadFileToStr(fileName, fileMaxSize, str));
878 EXPECT_EQ(str, "hello, world!");
879
880 std::remove(fileName.c_str());
881 }
882
883 HWTEST_F(SecurityGuardUtilsTest, GetBoolTestTrue, TestSize.Level1)
884 {
885 bool result;
886 bool success = GetBool(rootJson, "key1", result);
887 EXPECT_TRUE(success);
888 EXPECT_TRUE(result);
889 }
890
891 HWTEST_F(SecurityGuardUtilsTest, GetBoolTestFalse001, TestSize.Level1)
892 {
893 bool result;
894 bool success = GetBool(rootJson, "key2", result);
895 EXPECT_TRUE(success);
896 EXPECT_FALSE(result);
897 }
898
899 HWTEST_F(SecurityGuardUtilsTest, GetBoolTestFalse002, TestSize.Level1)
900 {
901 bool result;
902 bool success = GetBool(rootJson, "key_error", result);
903 EXPECT_FALSE(success);
904 }
905
906 HWTEST_F(SecurityGuardUtilsTest, GetBoolTestFalse003, TestSize.Level1)
907 {
908 bool result;
909 bool success = GetBool(rootJson, "key3", result);
910 EXPECT_FALSE(success);
911 }
912
913 HWTEST_F(SecurityGuardUtilsTest, GetBoolTestFalse004, TestSize.Level1)
914 {
915 bool result;
916 bool success = GetBool(nullptr, "key1", result);
917 EXPECT_FALSE(success);
918 }
919
920 HWTEST_F(SecurityGuardUtilsTest, TestValidFailed001, TestSize.Level1)
921 {
922 int64_t result;
923 bool success = GetNumberInt64(nullptr, "key3", result);
924 EXPECT_FALSE(success);
925 }
926
927 HWTEST_F(SecurityGuardUtilsTest, TestValidFailed002, TestSize.Level1)
928 {
929 int64_t result;
930 bool success = GetNumberInt64(rootJson, "key_error", result);
931 EXPECT_FALSE(success);
932 }
933
934 HWTEST_F(SecurityGuardUtilsTest, TestValidFailed003, TestSize.Level1)
935 {
936 int64_t result;
937 bool success = GetNumberInt64(rootJson, "key1", result);
938 EXPECT_FALSE(success);
939 }
940
941 HWTEST_F(SecurityGuardUtilsTest, TestValidZero002, TestSize.Level1)
942 {
943 int64_t result;
944 bool success = GetNumberInt64(rootJson, "key3", result);
945 EXPECT_TRUE(success);
946 EXPECT_EQ(result, 0);
947 }
948
949 HWTEST_F(SecurityGuardUtilsTest, TestValidPositive002, TestSize.Level1)
950 {
951 int64_t result;
952 bool success = GetNumberInt64(rootJson, "key4", result);
953 EXPECT_TRUE(success);
954 EXPECT_EQ(result, 1);
955 }
956
957 HWTEST_F(SecurityGuardUtilsTest, TestValidNegative002, TestSize.Level1)
958 {
959 int64_t result;
960 bool success = GetNumberInt64(rootJson, "key5", result);
961 EXPECT_TRUE(success);
962 EXPECT_EQ(result, -1);
963 }
964
965 HWTEST_F(SecurityGuardUtilsTest, TestValidFailed011, TestSize.Level1)
966 {
967 int32_t result;
968 bool success = GetNumberInt32(nullptr, "key3", result);
969 EXPECT_FALSE(success);
970 }
971
972 HWTEST_F(SecurityGuardUtilsTest, TestValidFailed012, TestSize.Level1)
973 {
974 int32_t result;
975 bool success = GetNumberInt32(rootJson, "key_error", result);
976 EXPECT_FALSE(success);
977 }
978
979 HWTEST_F(SecurityGuardUtilsTest, TestValidFailed013, TestSize.Level1)
980 {
981 int32_t result;
982 bool success = GetNumberInt32(rootJson, "key1", result);
983 EXPECT_FALSE(success);
984 }
985
986 HWTEST_F(SecurityGuardUtilsTest, TestValidZero001, TestSize.Level1)
987 {
988 int32_t result;
989 bool success = GetNumberInt32(rootJson, "key3", result);
990 EXPECT_TRUE(success);
991 EXPECT_EQ(result, 0);
992 }
993
994 HWTEST_F(SecurityGuardUtilsTest, TestValidPositive001, TestSize.Level1)
995 {
996 int32_t result;
997 bool success = GetNumberInt32(rootJson, "key4", result);
998 EXPECT_TRUE(success);
999 EXPECT_EQ(result, 1);
1000 }
1001
1002 HWTEST_F(SecurityGuardUtilsTest, TestValidNegative001, TestSize.Level1)
1003 {
1004 int32_t result;
1005 bool success = GetNumberInt32(rootJson, "key5", result);
1006 EXPECT_TRUE(success);
1007 EXPECT_EQ(result, -1);
1008 }
1009
1010 HWTEST_F(SecurityGuardUtilsTest, TestValidOverMaxInt32, TestSize.Level1)
1011 {
1012 int32_t result;
1013 bool success = GetNumberInt32(rootJson, "key6", result);
1014 EXPECT_FALSE(success);
1015 }
1016
1017 HWTEST_F(SecurityGuardUtilsTest, TestValidFailed021, TestSize.Level1)
1018 {
1019 std::string result;
1020 bool success = GetString(nullptr, "key3", result);
1021 EXPECT_FALSE(success);
1022 }
1023
1024 HWTEST_F(SecurityGuardUtilsTest, TestValidFailed022, TestSize.Level1)
1025 {
1026 std::string result;
1027 bool success = GetString(rootJson, "key_error", result);
1028 EXPECT_FALSE(success);
1029 }
1030
1031 HWTEST_F(SecurityGuardUtilsTest, TestValidFailed023, TestSize.Level1)
1032 {
1033 std::string result;
1034 bool success = GetString(rootJson, "key7", result);
1035 EXPECT_FALSE(success);
1036 }
1037
1038 HWTEST_F(SecurityGuardUtilsTest, TestValidSuccess020, TestSize.Level1)
1039 {
1040 std::string result;
1041 bool success = GetString(rootJson, "key8", result);
1042 EXPECT_TRUE(success);
1043 }
1044
1045 HWTEST_F(SecurityGuardUtilsTest, TestGetStringNoKeyFail001, TestSize.Level1)
1046 {
1047 std::string result;
1048 bool success = GetStringNoKey(nullptr, result);
1049 EXPECT_FALSE(success);
1050 }
1051
1052 HWTEST_F(SecurityGuardUtilsTest, TestAddString, TestSize.Level1)
1053 {
1054 std::string key = "testKey";
1055 std::string value = "testValue";
1056 bool success = AddString(rootJson, key, value);
1057 EXPECT_TRUE(success);
1058
1059 cJSON *item = cJSON_GetObjectItem(rootJson, key.c_str());
1060 EXPECT_TRUE(item != nullptr);
1061 EXPECT_TRUE(cJSON_IsString(item));
1062 EXPECT_STREQ(cJSON_GetStringValue(item), value.c_str());
1063 }
1064
1065 HWTEST_F(SecurityGuardUtilsTest, TestAddNumberInt32, TestSize.Level1)
1066 {
1067 std::string key = "testKey";
1068 int32_t value = 12345;
1069 bool success = AddNumberInt32(rootJson, key, value);
1070 EXPECT_TRUE(success);
1071
1072 cJSON *item = cJSON_GetObjectItem(rootJson, key.c_str());
1073 EXPECT_TRUE(item != nullptr);
1074 EXPECT_TRUE(cJSON_IsNumber(item));
1075 EXPECT_EQ(cJSON_GetNumberValue(item), value);
1076 }
1077
1078 HWTEST_F(SecurityGuardUtilsTest, TestAddNumberInt64, TestSize.Level1)
1079 {
1080 std::string key = "testKey";
1081 int64_t value = 123456789012345LL;
1082 bool success = AddNumberInt64(rootJson, key, value);
1083 EXPECT_TRUE(success);
1084
1085 cJSON *item = cJSON_GetObjectItem(rootJson, key.c_str());
1086 EXPECT_TRUE(item != nullptr);
1087 EXPECT_TRUE(cJSON_IsNumber(item));
1088 EXPECT_EQ(cJSON_GetNumberValue(item), value);
1089 }
1090
1091 HWTEST_F(SecurityGuardUtilsTest, TestAddStrArrayInfo, TestSize.Level1)
1092 {
1093 std::vector<std::string> values = {"item1", "item2", "item3"};
1094 const char *key = "testArrayKey";
1095 bool success = AddStrArrayInfo(rootJson, values, key);
1096 EXPECT_TRUE(success);
1097
1098 cJSON *array = cJSON_GetObjectItem(rootJson, key);
1099 EXPECT_TRUE(array != nullptr);
1100 EXPECT_TRUE(cJSON_IsArray(array));
1101 EXPECT_EQ(cJSON_GetArraySize(array), values.size());
1102
1103 for (size_t i = 0; i < values.size(); i++) {
1104 cJSON *item = cJSON_GetArrayItem(array, i);
1105 EXPECT_TRUE(item != nullptr);
1106 EXPECT_TRUE(cJSON_IsString(item));
1107 EXPECT_STREQ(cJSON_GetStringValue(item), values[i].c_str());
1108 }
1109 }
1110
1111 HWTEST_F(SecurityGuardUtilsTest, TestAddStringNullJson, TestSize.Level1)
1112 {
1113 std::string key = "testKey";
1114 std::string value = "testValue";
1115 bool success = AddString(nullptr, key, value);
1116 EXPECT_FALSE(success);
1117 }
1118
1119 HWTEST_F(SecurityGuardUtilsTest, TestAddNumberInt32NullJson, TestSize.Level1)
1120 {
1121 std::string key = "testKey";
1122 int32_t value = 12345;
1123 bool success = AddNumberInt32(nullptr, key, value);
1124 EXPECT_FALSE(success);
1125 }
1126
1127 HWTEST_F(SecurityGuardUtilsTest, TestAddNumberInt64NullJson, TestSize.Level1)
1128 {
1129 std::string key = "testKey";
1130 int64_t value = 123456789012345LL;
1131 bool success = AddNumberInt64(nullptr, key, value);
1132 EXPECT_FALSE(success);
1133 }
1134
1135 HWTEST_F(SecurityGuardUtilsTest, TestAddStrArrayInfoNullJson, TestSize.Level1)
1136 {
1137 std::vector<std::string> values = {"item1", "item2", "item3"};
1138 const char *key = "testArrayKey";
1139 bool success = AddStrArrayInfo(nullptr, values, key);
1140 EXPECT_FALSE(success);
1141 }
1142 }
1143