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 "rdb_helper.h"
41 #include "security_guard_log.h"
42 #include "event_group_config.h"
43 #undef private
44 #undef protected
45
46 using namespace testing;
47 using namespace testing::ext;
48 using namespace OHOS::Security::SecurityGuard;
49 using namespace OHOS::Security::SecurityGuardTest;
50 namespace OHOS {
51 std::shared_ptr<NativeRdb::MockRdbHelperInterface> NativeRdb::RdbHelper::instance_ = nullptr;
52 std::mutex NativeRdb::RdbHelper::mutex_ {};
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 EXPECT_TRUE(ConfigSubscriber::UpdateConfig("/data/service/el1/public/security_guard/tmp/signature_rule.json"));
343 }
344
345 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling001, TestSize.Level1)
346 {
347 nlohmann::json jsonObj;
348 SecurityGuard::AppDetectionCfg cfg = jsonObj.get<SecurityGuard::AppDetectionCfg>();
349 EXPECT_TRUE(cfg.detectionCategory == "");
350 }
351
352 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling002, TestSize.Level1)
353 {
354 nlohmann::json jsonObj;
355 jsonObj["detectionCategory"] = 0;
356 SecurityGuard::AppDetectionCfg cfg = jsonObj.get<SecurityGuard::AppDetectionCfg>();
357 EXPECT_TRUE(cfg.detectionCategory == "");
358 }
359
360 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling003, TestSize.Level1)
361 {
362 nlohmann::json jsonObj;
363 jsonObj["detectionCategory"] = "detectionCategory";
364 SecurityGuard::AppDetectionCfg cfg = jsonObj.get<SecurityGuard::AppDetectionCfg>();
365 EXPECT_TRUE(cfg.detectionCategory == "detectionCategory");
366 }
367
368 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling004, TestSize.Level1)
369 {
370 nlohmann::json jsonObj;
371 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
372 EXPECT_TRUE(field.fieldName == "");
373 EXPECT_TRUE(field.fieldType == "");
374 EXPECT_TRUE(field.value == "");
375 }
376
377 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling005, TestSize.Level1)
378 {
379 nlohmann::json jsonObj;
380 jsonObj["fieldName"] = 0;
381 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
382 EXPECT_TRUE(field.fieldName == "");
383 EXPECT_TRUE(field.fieldType == "");
384 EXPECT_TRUE(field.value == "");
385 }
386
387 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling006, TestSize.Level1)
388 {
389 nlohmann::json jsonObj;
390 jsonObj["fieldName"] = 0;
391 jsonObj["fieldType"] = 0;
392 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
393 EXPECT_TRUE(field.fieldName == "");
394 EXPECT_TRUE(field.fieldType == "");
395 EXPECT_TRUE(field.value == "");
396 }
397
398 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling007, TestSize.Level1)
399 {
400 nlohmann::json jsonObj;
401 jsonObj["fieldName"] = 0;
402 jsonObj["fieldType"] = 0;
403 jsonObj["value"] = 0;
404 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
405 EXPECT_TRUE(field.fieldName == "");
406 EXPECT_TRUE(field.fieldType == "");
407 EXPECT_TRUE(field.value == "");
408 }
409
410 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling008, TestSize.Level1)
411 {
412 nlohmann::json jsonObj;
413 jsonObj["fieldName"] = "fieldName";
414 jsonObj["fieldType"] = 0;
415 jsonObj["value"] = 0;
416 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
417 EXPECT_TRUE(field.fieldName == "");
418 EXPECT_TRUE(field.fieldType == "");
419 EXPECT_TRUE(field.value == "");
420 }
421
422 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling009, TestSize.Level1)
423 {
424 nlohmann::json jsonObj;
425 jsonObj["fieldName"] = "fieldName";
426 jsonObj["fieldType"] = "fieldType";
427 jsonObj["value"] = 0;
428 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
429 EXPECT_TRUE(field.fieldName == "");
430 EXPECT_TRUE(field.fieldType == "");
431 EXPECT_TRUE(field.value == "");
432 }
433
434 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling010, TestSize.Level1)
435 {
436 nlohmann::json jsonObj;
437 jsonObj["fieldName"] = "fieldName";
438 jsonObj["fieldType"] = "fieldType";
439 jsonObj["value"] = "value";
440 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
441 EXPECT_TRUE(field.fieldName == "fieldName");
442 EXPECT_TRUE(field.fieldType == "fieldType");
443 EXPECT_TRUE(field.value == "value");
444 }
445
446 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling011, TestSize.Level1)
447 {
448 nlohmann::json jsonObj;
449 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
450 EXPECT_TRUE(rule.eventId == 0);
451 EXPECT_TRUE(rule.fields.empty());
452 EXPECT_TRUE(rule.fieldsRelation == "");
453 }
454
455 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling012, TestSize.Level1)
456 {
457 nlohmann::json jsonObj;
458 jsonObj["eventId"] = "";
459 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
460 EXPECT_TRUE(rule.eventId == 0);
461 EXPECT_TRUE(rule.fields.empty());
462 EXPECT_TRUE(rule.fieldsRelation == "");
463 }
464
465 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling013, TestSize.Level1)
466 {
467 nlohmann::json jsonObj;
468 jsonObj["eventId"] = "";
469 jsonObj["fields"] = 0;
470 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
471 EXPECT_TRUE(rule.eventId == 0);
472 EXPECT_TRUE(rule.fields.empty());
473 EXPECT_TRUE(rule.fieldsRelation == "");
474 }
475
476 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling014, TestSize.Level1)
477 {
478 nlohmann::json jsonObj;
479 jsonObj["eventId"] = "";
480 jsonObj["fields"] = 0;
481 jsonObj["fieldsRelation"] = 0;
482 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
483 EXPECT_TRUE(rule.eventId == 0);
484 EXPECT_TRUE(rule.fields.empty());
485 EXPECT_TRUE(rule.fieldsRelation == "");
486 }
487
488 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling015, TestSize.Level1)
489 {
490 nlohmann::json jsonObj;
491 jsonObj["eventId"] = 0;
492 jsonObj["fields"] = 0;
493 jsonObj["fieldsRelation"] = 0;
494 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
495 EXPECT_TRUE(rule.eventId == 0);
496 EXPECT_TRUE(rule.fields.empty());
497 EXPECT_TRUE(rule.fieldsRelation == "");
498 }
499
500 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling016, TestSize.Level1)
501 {
502 nlohmann::json jsonObj;
503 jsonObj["eventId"] = 0;
504 jsonObj["fields"] = 0;
505 jsonObj["fieldsRelation"] = 0;
506 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
507 EXPECT_TRUE(rule.eventId == 0);
508 EXPECT_TRUE(rule.fields.empty());
509 EXPECT_TRUE(rule.fieldsRelation == "");
510 }
511
512 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling017, TestSize.Level1)
513 {
514 nlohmann::json jsonObj;
515 nlohmann::json jsonField;
516 jsonField["fieldName"] = "fieldName";
517 jsonField["fieldType"] = "fieldType";
518 jsonField["value"] = "value";
519 jsonObj["eventId"] = 0;
520 jsonObj["fields"] = {jsonField, jsonField};
521 jsonObj["fieldsRelation"] = 0;
522 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
523 EXPECT_TRUE(rule.eventId == 0);
524 EXPECT_TRUE(rule.fields.empty());
525 EXPECT_TRUE(rule.fieldsRelation == "");
526 }
527
528 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling018, TestSize.Level1)
529 {
530 nlohmann::json jsonObj;
531 nlohmann::json jsonField;
532 jsonField["fieldName"] = "fieldName";
533 jsonField["fieldType"] = "fieldType";
534 jsonField["value"] = "value";
535 jsonObj["eventId"] = 0;
536 jsonObj["fields"] = {jsonField, jsonField};
537 jsonObj["fieldsRelation"] = "fieldsRelation";
538 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
539 EXPECT_TRUE(rule.eventId == 0);
540 EXPECT_TRUE(!rule.fields.empty());
541 EXPECT_TRUE(rule.fieldsRelation == "fieldsRelation");
542 }
543
544 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling019, TestSize.Level1)
545 {
546 nlohmann::json jsonObj;
547 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
548 EXPECT_TRUE(cfg.rules.empty());
549 EXPECT_TRUE(cfg.rulesRelation == "");
550 EXPECT_TRUE(cfg.trueResult == "");
551 EXPECT_TRUE(cfg.falseResult == "");
552 }
553
554 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling020, TestSize.Level1)
555 {
556 nlohmann::json jsonObj;
557 jsonObj["rules"] = 0;
558 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
559 EXPECT_TRUE(cfg.rules.empty());
560 EXPECT_TRUE(cfg.rulesRelation == "");
561 EXPECT_TRUE(cfg.trueResult == "");
562 EXPECT_TRUE(cfg.falseResult == "");
563 }
564
565 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling021, TestSize.Level1)
566 {
567 nlohmann::json jsonObj;
568 jsonObj["rules"] = 0;
569 jsonObj["rulesRelation"] = 0;
570 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
571 EXPECT_TRUE(cfg.rules.empty());
572 EXPECT_TRUE(cfg.rulesRelation == "");
573 EXPECT_TRUE(cfg.trueResult == "");
574 EXPECT_TRUE(cfg.falseResult == "");
575 }
576
577 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling022, TestSize.Level1)
578 {
579 nlohmann::json jsonObj;
580 jsonObj["rules"] = 0;
581 jsonObj["rulesRelation"] = 0;
582 jsonObj["trueResult"] = 0;
583 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
584 EXPECT_TRUE(cfg.rules.empty());
585 EXPECT_TRUE(cfg.rulesRelation == "");
586 EXPECT_TRUE(cfg.trueResult == "");
587 EXPECT_TRUE(cfg.falseResult == "");
588 }
589
590 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling023, TestSize.Level1)
591 {
592 nlohmann::json jsonObj;
593 jsonObj["rules"] = 0;
594 jsonObj["rulesRelation"] = 0;
595 jsonObj["trueResult"] = 0;
596 jsonObj["falseResult"] = 0;
597 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
598 EXPECT_TRUE(cfg.rules.empty());
599 EXPECT_TRUE(cfg.rulesRelation == "");
600 EXPECT_TRUE(cfg.trueResult == "");
601 EXPECT_TRUE(cfg.falseResult == "");
602 }
603
604 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling024, TestSize.Level1)
605 {
606 nlohmann::json jsonObj;
607 nlohmann::json jsonRule;
608 nlohmann::json jsonField;
609 jsonField["fieldName"] = "fieldName";
610 jsonField["fieldType"] = "fieldType";
611 jsonField["value"] = "value";
612 jsonRule["eventId"] = 0;
613 jsonRule["fields"] = {jsonField, jsonField};
614 jsonRule["fieldsRelation"] = "fieldsRelation";
615 jsonObj["rules"] = {jsonRule, jsonRule};
616 jsonObj["rulesRelation"] = 0;
617 jsonObj["trueResult"] = 0;
618 jsonObj["falseResult"] = 0;
619 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
620 EXPECT_TRUE(cfg.rules.empty());
621 EXPECT_TRUE(cfg.rulesRelation == "");
622 EXPECT_TRUE(cfg.trueResult == "");
623 EXPECT_TRUE(cfg.falseResult == "");
624 }
625
626 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling025, TestSize.Level1)
627 {
628 nlohmann::json jsonObj;
629 nlohmann::json jsonRule;
630 nlohmann::json jsonField;
631 jsonField["fieldName"] = "fieldName";
632 jsonField["fieldType"] = "fieldType";
633 jsonField["value"] = "value";
634 jsonRule["eventId"] = 0;
635 jsonRule["fields"] = {jsonField, jsonField};
636 jsonRule["fieldsRelation"] = "fieldsRelation";
637 jsonObj["rules"] = {jsonRule, jsonRule};
638 jsonObj["rulesRelation"] = "rulesRelation";
639 jsonObj["trueResult"] = 0;
640 jsonObj["falseResult"] = 0;
641 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
642 EXPECT_TRUE(cfg.rules.empty());
643 EXPECT_TRUE(cfg.rulesRelation == "");
644 EXPECT_TRUE(cfg.trueResult == "");
645 EXPECT_TRUE(cfg.falseResult == "");
646 }
647
648 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling026, TestSize.Level1)
649 {
650 nlohmann::json jsonObj;
651 nlohmann::json jsonRule;
652 nlohmann::json jsonField;
653 jsonField["fieldName"] = "fieldName";
654 jsonField["fieldType"] = "fieldType";
655 jsonField["value"] = "value";
656 jsonRule["eventId"] = 0;
657 jsonRule["fields"] = {jsonField, jsonField};
658 jsonRule["fieldsRelation"] = "fieldsRelation";
659 jsonObj["rules"] = {jsonRule, jsonRule};
660 jsonObj["rulesRelation"] = "rulesRelation";
661 jsonObj["trueResult"] = "trueResult";
662 jsonObj["falseResult"] = 0;
663 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
664 EXPECT_TRUE(cfg.rules.empty());
665 EXPECT_TRUE(cfg.rulesRelation == "");
666 EXPECT_TRUE(cfg.trueResult == "");
667 EXPECT_TRUE(cfg.falseResult == "");
668 }
669
670 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling027, TestSize.Level1)
671 {
672 nlohmann::json jsonObj;
673 nlohmann::json jsonRule;
674 nlohmann::json jsonField;
675 jsonField["fieldName"] = "fieldName";
676 jsonField["fieldType"] = "fieldType";
677 jsonField["value"] = "value";
678 jsonRule["eventId"] = 0;
679 jsonRule["fields"] = {jsonField, jsonField};
680 jsonRule["fieldsRelation"] = "fieldsRelation";
681 jsonObj["rules"] = {jsonRule, jsonRule};
682 jsonObj["rulesRelation"] = "rulesRelation";
683 jsonObj["trueResult"] = "trueResult";
684 jsonObj["falseResult"] = "falseResult";
685 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
686 EXPECT_TRUE(!cfg.rules.empty());
687 EXPECT_TRUE(cfg.rulesRelation == "rulesRelation");
688 EXPECT_TRUE(cfg.trueResult == "trueResult");
689 EXPECT_TRUE(cfg.falseResult == "falseResult");
690 }
691
692 HWTEST_F(SecurityGuardConfigManagerTest, TestUnmarshal001, testing::ext::TestSize.Level1)
693 {
694 using namespace OHOS::Security::SecurityGuard;
695 nlohmann::json jsonOb {
696 {"version", "xxx"},
697 {"releaseTime", "xxx"},
698 {"detectMaxRecord", 999},
699 {"uidMaxDnsRecord", 10},
700 {"detectMaxTime", 86399},
701 {"ipBlackList", {"1", "2"}},
702 {"ipBlackListMock", {"xxx", "xxx"}},
703 {"dnsBlackList", {"1", "2"}},
704 {"number", {1, 2}}
705 };
706 std::vector<std::string> testVec {};
707 std::vector<int64_t> testVecInt {};
708 std::vector<int32_t> testVecIntS{};
709 uint64_t data;
710 int64_t dataInt;
711 int32_t i32Data;
712 uint32_t u32Data;
713
714 EXPECT_TRUE(JsonCfg::Unmarshal(i32Data, jsonOb, "detectMaxRecord"));
715 EXPECT_FALSE(JsonCfg::Unmarshal(i32Data, jsonOb, "releaseTime"));
716 EXPECT_FALSE(JsonCfg::Unmarshal(i32Data, jsonOb, "isexist"));
717
718 EXPECT_TRUE(JsonCfg::Unmarshal(u32Data, jsonOb, "detectMaxRecord"));
719 EXPECT_FALSE(JsonCfg::Unmarshal(u32Data, jsonOb, "releaseTime"));
720 EXPECT_FALSE(JsonCfg::Unmarshal(u32Data, jsonOb, "isexist"));
721
722 EXPECT_TRUE(JsonCfg::Unmarshal(data, jsonOb, "detectMaxRecord"));
723 EXPECT_FALSE(JsonCfg::Unmarshal(data, jsonOb, "releaseTime"));
724 EXPECT_FALSE(JsonCfg::Unmarshal(data, jsonOb, "isexist"));
725
726 EXPECT_TRUE(JsonCfg::Unmarshal(dataInt, jsonOb, "uidMaxDnsRecord"));
727 EXPECT_FALSE(JsonCfg::Unmarshal(dataInt, jsonOb, "releaseTime"));
728 EXPECT_FALSE(JsonCfg::Unmarshal(dataInt, jsonOb, "isexist"));
729
730 EXPECT_TRUE(JsonCfg::Unmarshal(testVec, jsonOb, "ipBlackList"));
731 EXPECT_FALSE(JsonCfg::Unmarshal(testVec, jsonOb, "isexist"));
732 EXPECT_FALSE(JsonCfg::Unmarshal(testVec, jsonOb, "number"));
733
734 EXPECT_TRUE(JsonCfg::Unmarshal(testVecInt, jsonOb, "number"));
735 EXPECT_FALSE(JsonCfg::Unmarshal(testVecInt, jsonOb, "ipBlackList"));
736 EXPECT_FALSE(JsonCfg::Unmarshal(testVecInt, jsonOb, "isexist"));
737
738 EXPECT_TRUE(JsonCfg::Unmarshal(testVecIntS, jsonOb, "number"));
739 EXPECT_FALSE(JsonCfg::Unmarshal(testVecIntS, jsonOb, "ipBlackList"));
740 EXPECT_FALSE(JsonCfg::Unmarshal(testVecIntS, jsonOb, "isexist"));
741 }
742
743 HWTEST_F(SecurityGuardConfigManagerTest, TestEventGroupConfig001, TestSize.Level1)
744 {
745 EventGroupConfig config;
746 bool success = config.Load(INIT_MODE);
747 EXPECT_TRUE(success);
748 config.Parse();
749 EXPECT_FALSE(config.Load(UPDATE_MODE));
750 EXPECT_TRUE(config.Update());
751 }
752
753 HWTEST_F(SecurityGuardConfigManagerTest, TestEventGroupConfigFail001, TestSize.Level1)
754 {
755 EventGroupConfig config;
756 nlohmann::json jsonObj;
757 nlohmann::json jsonGroupInfo;
758 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
759 jsonObj["eventGroupList"] = "test";
760 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
761 jsonObj.clear();
762 jsonGroupInfo["test"] = "";
763 jsonObj["eventGroupList"] = {jsonGroupInfo, jsonGroupInfo};
764 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
765 jsonGroupInfo.clear();
766 jsonObj.clear();
767 jsonGroupInfo["eventGroupName"] = "";
768 jsonObj["eventGroupList"] = {jsonGroupInfo, jsonGroupInfo};
769 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
770 jsonGroupInfo.clear();
771 jsonObj.clear();
772 jsonGroupInfo["eventGroupName"] = "Sec";
773 jsonObj["eventGroupList"] = {jsonGroupInfo, jsonGroupInfo};
774 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
775 jsonGroupInfo.clear();
776 jsonObj.clear();
777 jsonGroupInfo["eventGroupName"] = "Sec";
778 jsonGroupInfo["eventList"] = {"", "1111111111111111111111111111111111111111111111111111111"};
779 jsonObj["eventGroupList"] = {jsonGroupInfo, jsonGroupInfo};
780 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
781 jsonGroupInfo.clear();
782 jsonObj.clear();
783 jsonGroupInfo["eventGroupName"] = "Sec";
784 jsonGroupInfo["eventList"] = {"123", "11111"};
785 jsonGroupInfo["permission"] = {"test", "test1"};
786 jsonObj["eventGroupList"] = {jsonGroupInfo, jsonGroupInfo};
787 EXPECT_FALSE(config.ParseEventGroupConfig(jsonObj));
788 }
789
790 HWTEST_F(SecurityGuardConfigManagerTest, GetEventGroupConfig001, TestSize.Level1)
791 {
792 EventGroupCfg config {};
793 EXPECT_FALSE(ConfigDataManager::GetInstance().GetEventGroupConfig("testttt", config));
794 ConfigDataManager::GetInstance().eventGroupMap_.insert({"testttt", config});
795 EXPECT_TRUE(ConfigDataManager::GetInstance().GetEventGroupConfig("testttt", config));
796 }
797
798 HWTEST_F(SecurityGuardConfigManagerTest, GetIsBatchUpload001, TestSize.Level1)
799 {
800 EXPECT_FALSE(ConfigDataManager::GetInstance().GetIsBatchUpload("test11"));
801 EventGroupCfg config {};
802 config.isBatchUpload = true;
803 ConfigDataManager::GetInstance().eventGroupMap_.insert({"test11", config});
804 EXPECT_TRUE(ConfigDataManager::GetInstance().GetIsBatchUpload("test11"));
805 }
806 }
807