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
27 #define private public
28 #define protected public
29 #include "base_config.h"
30 #include "config_data_manager.h"
31 #include "config_define.h"
32 #include "config_manager.h"
33 #include "config_operator.h"
34 #include "config_subscriber.h"
35 #include "event_config.h"
36 #include "model_cfg_marshalling.h"
37 #include "model_config.h"
38 #include "local_app_config.h"
39 #include "global_app_config.h"
40 #include "rdb_helper.h"
41 #include "app_info_rdb_helper.h"
42 #include "security_guard_log.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
56 namespace {
57 constexpr int SUCCESS = 0;
58 constexpr int FAILED = 1;
59 constexpr size_t MAXAPPSIZE = 500;
60 }
61
SetUpTestCase()62 void SecurityGuardConfigManagerTest::SetUpTestCase()
63 {
64 static const char *permission[] = { "ohos.permission.securityguard.REPORT_SECURITY_INFO" };
65 uint64_t tokenId;
66 NativeTokenInfoParams infoParams = {
67 .dcapsNum = 0,
68 .permsNum = 1,
69 .aclsNum = 0,
70 .dcaps = nullptr,
71 .perms = permission,
72 .acls = nullptr,
73 .processName = "security_guard",
74 .aplStr = "system_basic",
75 };
76 tokenId = GetAccessTokenId(&infoParams);
77 SetSelfTokenID(tokenId);
78 }
79
TearDownTestCase()80 void SecurityGuardConfigManagerTest::TearDownTestCase()
81 {
82 }
83
SetUp()84 void SecurityGuardConfigManagerTest::SetUp()
85 {
86 }
87
TearDown()88 void SecurityGuardConfigManagerTest::TearDown()
89 {
90 }
91
92 class MockBaseConfig : public BaseConfig {
93 public:
94 MockBaseConfig() = default;
95 ~MockBaseConfig() override = default;
96 MOCK_METHOD0(Check, bool());
97 MOCK_METHOD1(Load, bool(int));
98 MOCK_METHOD0(Parse, bool());
99 MOCK_METHOD0(Update, bool());
100 };
101
102 class TestBaseConfig : public BaseConfig {
103 public:
104 TestBaseConfig() = default;
105 ~TestBaseConfig() override = default;
106 MOCK_METHOD1(Load, bool(int));
107 MOCK_METHOD0(Parse, bool());
108 MOCK_METHOD0(Update, bool());
109 };
110
111 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigOperator001, TestSize.Level1)
112 {
113 MockBaseConfig config;
114 auto configOptor = std::make_unique<ConfigOperator>(config);
115 EXPECT_CALL(config, Load).WillOnce(Return(false)).WillRepeatedly(Return(true));
116 EXPECT_CALL(config, Check).WillOnce(Return(false)).WillRepeatedly(Return(true));
117 EXPECT_CALL(config, Parse).WillOnce(Return(false)).WillRepeatedly(Return(true));
118 bool success = configOptor->Init();
119 EXPECT_FALSE(success);
120 success = configOptor->Init();
121 EXPECT_FALSE(success);
122 success = configOptor->Init();
123 EXPECT_FALSE(success);
124 success = configOptor->Init();
125 EXPECT_TRUE(success);
126 }
127
128 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigOperator002, TestSize.Level1)
129 {
130 MockBaseConfig config;
131 auto configOptor = std::make_unique<ConfigOperator>(config);
132 EXPECT_CALL(config, Load).WillOnce(Return(false)).WillRepeatedly(Return(true));
133 EXPECT_CALL(config, Check).WillOnce(Return(false)).WillRepeatedly(Return(true));
134 EXPECT_CALL(config, Update).WillOnce(Return(false)).WillRepeatedly(Return(true));
135 bool success = configOptor->Update();
136 EXPECT_FALSE(success);
137 success = configOptor->Update();
138 EXPECT_FALSE(success);
139 success = configOptor->Update();
140 EXPECT_FALSE(success);
141 success = configOptor->Update();
142 EXPECT_TRUE(success);
143 }
144
145 HWTEST_F(SecurityGuardConfigManagerTest, TestBaseConfig001, TestSize.Level1)
146 {
147 TestBaseConfig config;
148 config.stream_.close();
149 bool success = config.Check();
150 EXPECT_FALSE(success);
151 config.stream_.open("test.txt");
152 success = config.Check();
153 EXPECT_FALSE(success);
154 config.stream_.open("/data/test/unittest/resource/stream_empty.txt");
155 success = config.Check();
156 EXPECT_FALSE(success);
157 config.stream_.open("/data/test/unittest/resource/stream_not_empty.txt");
158 success = config.Check();
159 EXPECT_TRUE(success);
160 }
161
162 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigDataManager001, TestSize.Level1)
163 {
164 EventCfg config = {};
165 std::string eventName = "test eventName";
166 config.eventName = eventName;
167 ConfigDataManager::GetInstance().InsertEventMap(config.eventId, config);
168 EventCfg outConfig = {};
169 bool success = ConfigDataManager::GetInstance().GetEventConfig(config.eventId, outConfig);
170 EXPECT_TRUE(success);
171 EXPECT_TRUE(outConfig.eventName == config.eventName);
172 std::vector<int64_t> eventIds = ConfigDataManager::GetInstance().GetAllEventIds();
173 EXPECT_TRUE(eventIds.size() == 1);
174 EXPECT_TRUE(eventIds[0] == 0);
175 ConfigDataManager::GetInstance().ResetEventMap();
176 success = ConfigDataManager::GetInstance().GetEventConfig(config.eventId, outConfig);
177 EXPECT_FALSE(success);
178 eventIds = ConfigDataManager::GetInstance().GetAllEventIds();
179 EXPECT_TRUE(eventIds.size() == 0);
180 }
181
182 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigDataManager002, TestSize.Level1)
183 {
184 ModelCfg config = {};
185 std::string path = "test path";
186 config.path = path;
187 ConfigDataManager::GetInstance().InsertModelMap(config.modelId, config);
188 ModelCfg outConfig = {};
189 bool success = ConfigDataManager::GetInstance().GetModelConfig(config.modelId, outConfig);
190 EXPECT_TRUE(success);
191 EXPECT_TRUE(outConfig.path == config.path);
192 ConfigDataManager::GetInstance().ResetModelMap();
193 success = ConfigDataManager::GetInstance().GetModelConfig(config.modelId, outConfig);
194 EXPECT_FALSE(success);
195 }
196
197 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigDataManager003, TestSize.Level1)
198 {
199 std::set<int64_t> eventIds {1};
200 int32_t modelId = 0;
201 ConfigDataManager::GetInstance().InsertModelToEventMap(modelId, eventIds);
202 std::vector<int64_t> outEventIds = ConfigDataManager::GetInstance().GetEventIds(modelId);
203 EXPECT_TRUE(outEventIds.size() == 1);
204 EXPECT_TRUE(outEventIds[0] == 1);
205 ConfigDataManager::GetInstance().ResetModelToEventMap();
206 outEventIds = ConfigDataManager::GetInstance().GetEventIds(modelId);
207 EXPECT_TRUE(outEventIds.size() == 0);
208 }
209
210 HWTEST_F(SecurityGuardConfigManagerTest, TestConfigSubsciber001, TestSize.Level1)
211 {
212 TimeEventRelatedCallBack callBack = nullptr;
213 bool success = ConfigSubscriber::RegisterTimeEventRelatedCallBack(callBack);
214 EXPECT_FALSE(success);
215 }
216
217 HWTEST_F(SecurityGuardConfigManagerTest, TestEventConfig001, TestSize.Level1)
218 {
219 EventConfig config;
220 bool success = config.Load(INIT_MODE);
221 EXPECT_TRUE(success);
222 }
223
224 HWTEST_F(SecurityGuardConfigManagerTest, TestEventConfig002, TestSize.Level1)
225 {
226 ConfigDataManager::GetInstance().ResetEventMap();
227 ConfigDataManager::GetInstance().ResetModelMap();
228 ConfigDataManager::GetInstance().ResetModelToEventMap();
229 EventConfig config;
230 bool success = config.Parse();
231 EXPECT_FALSE(success);
232 config.stream_.open("/data/test/unittest/resource/security_guard_preset_event.cfg");
233 success = config.Parse();
234 EXPECT_TRUE(success);
235 EventCfg eventCfg;
236 eventCfg.eventId = 2;
237 success = ConfigDataManager::GetInstance().GetEventConfig(eventCfg.eventId, eventCfg);
238 EXPECT_TRUE(success);
239 EXPECT_TRUE(eventCfg.eventName == "preset_event");
240 }
241
242 HWTEST_F(SecurityGuardConfigManagerTest, TestEventConfig003, TestSize.Level1)
243 {
244 ConfigDataManager::GetInstance().ResetEventMap();
245 ConfigDataManager::GetInstance().ResetModelMap();
246 ConfigDataManager::GetInstance().ResetModelToEventMap();
247 EventConfig config;
248 bool success = config.Parse();
249 EXPECT_FALSE(success);
250 config.stream_.open("/data/test/unittest/resource/security_guard_update_event.cfg");
251 EXPECT_TRUE(config.stream_.is_open());
252 success = config.Parse();
253 EXPECT_TRUE(success);
254 EventCfg eventCfg;
255 eventCfg.eventId = 3;
256 success = ConfigDataManager::GetInstance().GetEventConfig(eventCfg.eventId, eventCfg);
257 EXPECT_TRUE(success);
258 EXPECT_TRUE(eventCfg.eventName == "update_event");
259 }
260
261 HWTEST_F(SecurityGuardConfigManagerTest, TestModelConfig001, TestSize.Level1)
262 {
263 ModelConfig config;
264 bool success = config.Load(INIT_MODE);
265 EXPECT_TRUE(success);
266 }
267
268 HWTEST_F(SecurityGuardConfigManagerTest, TestModelConfig002, TestSize.Level1)
269 {
270 ConfigDataManager::GetInstance().ResetEventMap();
271 ConfigDataManager::GetInstance().ResetModelMap();
272 ConfigDataManager::GetInstance().ResetModelToEventMap();
273 ModelConfig config;
274 bool success = config.Parse();
275 EXPECT_FALSE(success);
276 config.stream_.open("/data/test/unittest/resource/security_guard_preset_model.cfg");
277 EXPECT_TRUE(config.stream_.is_open());
278 success = config.Parse();
279 EXPECT_TRUE(success);
280 ModelCfg modelCfg;
281 modelCfg.modelId = 2;
282 success = ConfigDataManager::GetInstance().GetModelConfig(modelCfg.modelId, modelCfg);
283 EXPECT_TRUE(success);
284 EXPECT_TRUE(modelCfg.path == "preset_model");
285 }
286
287 HWTEST_F(SecurityGuardConfigManagerTest, TestModelConfig003, TestSize.Level1)
288 {
289 ConfigDataManager::GetInstance().ResetEventMap();
290 ConfigDataManager::GetInstance().ResetModelMap();
291 ConfigDataManager::GetInstance().ResetModelToEventMap();
292 ModelConfig config;
293 bool success = config.Parse();
294 EXPECT_FALSE(success);
295 config.stream_.open("/data/test/unittest/resource/security_guard_update_model.cfg");
296 EXPECT_TRUE(config.stream_.is_open());
297 success = config.Parse();
298 EXPECT_TRUE(success);
299 ModelCfg modelCfg;
300 modelCfg.modelId = 3;
301 success = ConfigDataManager::GetInstance().GetModelConfig(modelCfg.modelId, modelCfg);
302 EXPECT_TRUE(success);
303 EXPECT_TRUE(modelCfg.path == "update_model");
304 }
305
306 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig000, TestSize.Level1)
307 {
308 LocalAppConfig config;
309 bool success = config.Load(INIT_MODE);
310 EXPECT_TRUE(success);
311 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), QueryAllAppInfo(An<std::vector<AppInfo> &>())).
312 WillRepeatedly(Return(SUCCESS));
313 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), InsertAppInfo(
314 An<const AppInfo &>())).WillRepeatedly(Return(SUCCESS));
315 config.stream_ = std::ifstream("/data/test/unittest/resource/local_app_attribute_update.json", std::ios::in);
316 EXPECT_TRUE(config.stream_.is_open());
317 EXPECT_FALSE(!config.stream_);
318 success = config.Parse();
319 EXPECT_TRUE(success);
320 success = config.Update();
321 EXPECT_TRUE(success);
322 }
323
324
325 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig001, TestSize.Level1)
326 {
327 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), QueryAllAppInfo(An<std::vector<AppInfo> &>())).
328 WillRepeatedly(Return(FAILED));
329 LocalAppConfig config;
330 config.stream_.open("/data/test/unittest/resource/local_app_attribute_update.json");
331 EXPECT_TRUE(config.stream_.is_open());
332 bool success = config.Parse();
333 EXPECT_TRUE(success);
334 success = config.Update();
335 EXPECT_FALSE(success);
336 }
337
338 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig002, TestSize.Level1)
339 {
340 LocalAppConfig config;
341 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), InsertAllAppInfo(
342 An<const std::vector<AppInfo> &>())).WillRepeatedly(Return(SUCCESS));
343 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), QueryAllAppInfo(An<std::vector<AppInfo> &>())).
344 WillRepeatedly(Return(SUCCESS));
345 std::ofstream out("/data/test/unittest/resource/local_app_attribute_update.json");
346 std::string errtmp = R"({
347 "version":"001",
348 "apps":""
349 })";
350 out << errtmp << std::endl;
351 config.stream_.open("/data/test/unittest/resource/local_app_attribute_update.json");
352 EXPECT_TRUE(config.stream_.is_open());
353 bool success = config.Parse();
354 EXPECT_TRUE(success);
355 success = config.Update();
356 EXPECT_FALSE(success);
357 std::string tmp = R"({
358 "version":"001",
359 "apps":[
360 {
361 "name":"com.sohu.harmonynews",
362 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
363 "attribute":["monitoring"],
364 "isUpdate":1
365 },
366 {
367 "name":"com.sohu.harmonynews",
368 "fingerprint":"ED2D188FACD5EB93248B287366324F6A12DF3A7B8D464C89FDD88FF1588C6596",
369 "attribute":[],
370 "isUpdate":1
371 }
372 ]
373 })";
374 std::ofstream out1("/data/test/unittest/resource/local_app_attribute_update.json");
375 out1 << tmp << std::endl;
376 config.stream_.open("/data/test/unittest/resource/local_app_attribute_update.json");
377 EXPECT_TRUE(config.stream_.is_open());
378 success = config.Parse();
379 EXPECT_TRUE(success);
380 success = config.Update();
381 EXPECT_FALSE(success);
382 }
383
384 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig003, TestSize.Level1)
385 {
386 LocalAppConfig config;
387 std::vector<AppInfo> configs;
388 std::string jsonStr = R"({
389 "version":"001"
390 })";
391 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
392 EXPECT_FALSE(extraJson.is_discarded());
393 bool success = config.ParseAppListConfig(configs, extraJson);
394 EXPECT_FALSE(success);
395 }
396
397 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig004, TestSize.Level1)
398 {
399 LocalAppConfig config;
400 std::vector<AppInfo> configs;
401 std::string jsonStr = R"({
402 "version":"001",
403 "apps":"111"
404 })";
405 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
406 EXPECT_FALSE(extraJson.is_discarded());
407 bool success = config.ParseAppListConfig(configs, extraJson);
408 EXPECT_FALSE(success);
409 }
410
411 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig005, TestSize.Level1)
412 {
413 LocalAppConfig config;
414 std::vector<AppInfo> configs;
415 std::string jsonStr = R"({
416 "version":"001",
417 "apps": []
418 })";
419 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
420 EXPECT_FALSE(extraJson.is_discarded());
421 bool success = config.ParseAppListConfig(configs, extraJson);
422 EXPECT_TRUE(success);
423 }
424
425 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig006, TestSize.Level1)
426 {
427 LocalAppConfig config;
428 std::vector<AppInfo> configs;
429 std::string jsonStr = R"({
430 "version":"001",
431 "apps": [
432 {
433 "name":"",
434 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
435 "attribute":["monitoring"],
436 "isUpdate":1
437 }
438 ]
439 })";
440 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
441 EXPECT_FALSE(extraJson.is_discarded());
442 bool success = config.ParseAppListConfig(configs, extraJson);
443 EXPECT_FALSE(success);
444 }
445
446 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig007, TestSize.Level1)
447 {
448 LocalAppConfig config;
449 std::vector<AppInfo> configs;
450 std::string jsonStr = R"({
451 "version":"001",
452 "apps": [
453 {
454 "name":"",
455 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
456 "attribute":["monitoring"],
457 "isUpdate":2
458 }
459 ]
460 })";
461 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
462 EXPECT_FALSE(extraJson.is_discarded());
463 bool success = config.ParseAppListConfig(configs, extraJson);
464 EXPECT_FALSE(success);
465 }
466
467 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig008, TestSize.Level1)
468 {
469 LocalAppConfig config;
470 std::vector<AppInfo> configs;
471 std::string jsonStr = R"({
472 "version":"001",
473 "apps": [
474 {
475 "name":"com.sohu.harmonynews",
476 "attribute":["monitoring"],
477 "isUpdate":1
478 }
479 ]
480 })";
481 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
482 EXPECT_FALSE(extraJson.is_discarded());
483 bool success = config.ParseAppListConfig(configs, extraJson);
484 EXPECT_FALSE(success);
485 }
486
487 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig009, TestSize.Level1)
488 {
489 LocalAppConfig config;
490 std::vector<AppInfo> configs;
491 std::string jsonStr = R"({
492 "version":"001",
493 "apps": [
494 {
495 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
496 "attribute":["monitoring"],
497 "isUpdate":1
498 }
499 ]
500 })";
501 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
502 EXPECT_FALSE(extraJson.is_discarded());
503 bool success = config.ParseAppListConfig(configs, extraJson);
504 EXPECT_FALSE(success);
505 }
506
507 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig010, TestSize.Level1)
508 {
509 LocalAppConfig config;
510 std::vector<AppInfo> configs;
511 std::string jsonStr = R"({
512 "version":"001",
513 "apps": [
514 {
515 "name":"com.sohu.harmonynews",
516 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
517 "isUpdate":1
518 }
519 ]
520 })";
521 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
522 EXPECT_FALSE(extraJson.is_discarded());
523 bool success = config.ParseAppListConfig(configs, extraJson);
524 EXPECT_FALSE(success);
525 }
526
527 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig012, TestSize.Level1)
528 {
529 LocalAppConfig config;
530 std::vector<AppInfo> configs;
531 std::string jsonStr = R"({
532 "version":"001",
533 "apps": [
534 {
535 "name":"com.sohu.harmonynews",
536 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
537 "attribute":["monitoring"]
538 }
539 ]
540 })";
541 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
542 EXPECT_FALSE(extraJson.is_discarded());
543 bool success = config.ParseAppListConfig(configs, extraJson);
544 EXPECT_FALSE(success);
545 }
546
547 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig013, TestSize.Level1)
548 {
549 LocalAppConfig config;
550 std::vector<AppInfo> configs;
551 std::string jsonStr = R"({
552 "version":"001",
553 "apps": [
554 {
555 "name":"com.sohu.harmonynews",
556 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
557 "attribute":["monitoringL"],
558 "isUpdate":1
559 }
560 ]
561 })";
562 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
563 EXPECT_FALSE(extraJson.is_discarded());
564 bool success = config.ParseAppListConfig(configs, extraJson);
565 EXPECT_FALSE(success);
566 }
567
568 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig014, TestSize.Level1)
569 {
570 LocalAppConfig config;
571 std::vector<AppInfo> configs;
572 std::string jsonStr = R"({
573 "version":"001",
574 "apps": [
575 {
576 "name":"com.sohu.harmonynews",
577 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
578 "attribute":"monitoringL",
579 "isUpdate":1
580 }
581 ]
582 })";
583 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
584 EXPECT_FALSE(extraJson.is_discarded());
585 bool success = config.ParseAppListConfig(configs, extraJson);
586 EXPECT_FALSE(success);
587 }
588
589 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig015, TestSize.Level1)
590 {
591 LocalAppConfig config;
592 std::vector<AppInfo> configs;
593 std::string jsonStr = R"({
594 "version":"001",
595 "apps": [
596 {
597 "name":"com.sohu.harmonynews",
598 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
599 "attribute":["monitoring", "payment", "malicious"],
600 "isUpdate":1
601 }
602 ]
603 })";
604 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
605 EXPECT_FALSE(extraJson.is_discarded());
606 bool success = config.ParseAppListConfig(configs, extraJson);
607 EXPECT_TRUE(success);
608 }
609
610 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig017, TestSize.Level1)
611 {
612 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), QueryAllAppInfo).WillOnce(Return(FAILED)).WillRepeatedly(
__anonde7b66a80202(std::vector<AppInfo> &infos) 613 [] (std::vector<AppInfo> &infos) {
614 AppInfo info{};
615 info.appName = "com.sohu.harmonynews";
616 infos.emplace_back(info);
617 return SUCCESS;
618 });
619 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), DeleteAppInfoByNameAndGlobbalFlag(
620 An<const std::string &>(), An<int>())).WillRepeatedly(Return(SUCCESS));
621 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), InsertAppInfo(
622 An<const AppInfo &>())).WillRepeatedly(Return(SUCCESS));
623 LocalAppConfig config;
624 std::vector<AppInfo> configs;
625 std::string jsonStr = R"({
626 "version":"001",
627 "apps": [
628 {
629 "name":"com.sohu.harmonynews",
630 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
631 "attribute":["monitoring", "payment", "malicious"],
632 "isUpdate":1
633 }
634 ]
635 })";
636 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
637 EXPECT_FALSE(extraJson.is_discarded());
638 bool success = config.ParseAppListConfig(configs, extraJson);
639 EXPECT_TRUE(success);
640 config.UpdateInfoToDb(configs);
641 EXPECT_TRUE(success);
642 config.UpdateInfoToDb(configs);
643 EXPECT_TRUE(success);
644 }
645
646 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig018, TestSize.Level1)
647 {
648 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), QueryAllAppInfo).WillRepeatedly(
__anonde7b66a80302(std::vector<AppInfo> &infos) 649 [] (std::vector<AppInfo> &infos) {
650 AppInfo info{};
651 info.appName = "com.sohu.harmonynews";
652 info.isGlobalApp = 0;
653 infos.emplace_back(info);
654 return SUCCESS;
655 });
656 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), InsertAppInfo).WillOnce(Return(SUCCESS)).
657 WillRepeatedly(Return(FAILED));
658 LocalAppConfig config;
659 std::vector<AppInfo> configs;
660 std::string jsonStr = R"({
661 "version":"001",
662 "apps": [
663 {
664 "name":"com.sohu.harmonynews",
665 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
666 "attribute":["monitoring", "payment", "malicious"],
667 "isUpdate":1
668 },
669 {
670 "name":"ttttt",
671 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
672 "attribute":["monitoring", "payment", "malicious"],
673 "isUpdate":1
674 }
675 ]
676 })";
677 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
678 EXPECT_FALSE(extraJson.is_discarded());
679 bool success = config.ParseAppListConfig(configs, extraJson);
680 EXPECT_TRUE(success);
681 success = config.UpdateInfoToDb(configs);
682 EXPECT_FALSE(success);
683 }
684
685 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig019, TestSize.Level1)
686 {
687 LocalAppConfig conf;
688 AppInfo config {};
689 config.appName = "a";
690 config.isUpdate = 1;
691 AppInfo dbConfig {};
692 dbConfig.appName = "a";
693 bool isFind;
694 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), InsertAppInfo).WillRepeatedly([](
__anonde7b66a80402( const AppInfo & info) 695 const AppInfo & info) {
696 return FAILED;
697 });
698 EXPECT_FALSE(conf.IsNeedUpdate(config, dbConfig, isFind));
699 }
700
701 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig020, TestSize.Level1)
702 {
703 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), QueryAllAppInfo).WillRepeatedly(
__anonde7b66a80502(std::vector<AppInfo> &infos) 704 [] (std::vector<AppInfo> &infos) {
705 AppInfo info{};
706 info.appName = "com.sohu.harmonynews";
707 info.isGlobalApp = 0;
708 infos.emplace_back(info);
709 return SUCCESS;
710 });
711 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), InsertAppInfo).WillOnce(Return(SUCCESS)).
712 WillRepeatedly(Return(FAILED));
713 LocalAppConfig config;
714 std::vector<AppInfo> configs;
715 std::string jsonStr = R"({
716 "version":"001",
717 "apps": [
718 {
719 "name":"com.sohu.harmonynews",
720 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
721 "attribute":["monitoring", "payment", "malicious"],
722 "isUpdate":1
723 },
724 {
725 "name":"ttttt",
726 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
727 "attribute":["monitoring", "payment", "malicious"],
728 "isUpdate":1
729 }
730 ]
731 })";
732 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
733 EXPECT_FALSE(extraJson.is_discarded());
734 bool success = config.ParseAppListConfig(configs, extraJson);
735 EXPECT_TRUE(success);
736 success = config.UpdateInfoToDb(configs);
737 EXPECT_FALSE(success);
738 }
739
740 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig021, TestSize.Level1)
741 {
742 LocalAppConfig config;
743 bool success = config.Load(INIT_MODE);
744 EXPECT_TRUE(success);
745 }
746
747 HWTEST_F(SecurityGuardConfigManagerTest, TestLocalAppConfig022, TestSize.Level1)
748 {
749 LocalAppConfig config;
750 std::vector<AppInfo> configs;
751 nlohmann::json::array_t arr;
752 std::vector<std::string> attrs = {"monitoring"};
753 for (size_t i = 0; i < MAXAPPSIZE + 1; i++) {
754 nlohmann::json jsonObj {
755 {"name", std::to_string(i)},
756 {"fingerprint", "C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A"},
757 {"attribute", attrs},
758 {"isUpdate", 1}
759 };
760 arr.push_back(jsonObj);
761 }
762 nlohmann::json jsonOb {
763 {"apps", arr}
764 };
765 std::string jsonStr = jsonOb.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
766 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
767 EXPECT_FALSE(extraJson.is_discarded());
768 bool success = config.ParseAppListConfig(configs, extraJson);
769 EXPECT_TRUE(success);
770 EXPECT_TRUE(configs.size() == MAXAPPSIZE + 1);
771 }
772
773 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig000, TestSize.Level1)
774 {
775 GlobalAppConfig config;
776 bool success = config.Load(INIT_MODE);
777 EXPECT_TRUE(success);
778 }
779
780 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig001, TestSize.Level1)
781 {
782 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), DeleteAppInfoByIsGlobalApp(
783 An<int>())).WillRepeatedly(Return(FAILED));
784 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), InsertAllAppInfo(
785 An<const std::vector<AppInfo> &>())).WillRepeatedly(Return(FAILED));
786 GlobalAppConfig config;
787 config.stream_.open("/data/test/unittest/resource/global_app_attribute_update.json");
788 EXPECT_TRUE(config.stream_.is_open());
789 bool success = config.Parse();
790 EXPECT_TRUE(success);
791 success = config.Update();
792 EXPECT_FALSE(success);
793 }
794
795
796 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig002, TestSize.Level1)
797 {
798 GlobalAppConfig config;
799 std::vector<AppInfo> configs;
800 std::string jsonStr = R"({
801 "version":"001"
802 })";
803 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
804 EXPECT_FALSE(extraJson.is_discarded());
805 bool success = config.ParseAppListConfig(configs, extraJson);
806 EXPECT_FALSE(success);
807 }
808
809 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig003, TestSize.Level1)
810 {
811 GlobalAppConfig config;
812 std::vector<AppInfo> configs;
813 std::string jsonStr = R"({
814 "version":"001"
815 })";
816 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
817 EXPECT_FALSE(extraJson.is_discarded());
818 bool success = config.ParseAppListConfig(configs, extraJson);
819 EXPECT_FALSE(success);
820 }
821
822 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig004, TestSize.Level1)
823 {
824 GlobalAppConfig config;
825 std::vector<AppInfo> configs;
826 std::string jsonStr = R"({
827 "version":"001",
828 "apps":"111"
829 })";
830 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
831 EXPECT_FALSE(extraJson.is_discarded());
832 bool success = config.ParseAppListConfig(configs, extraJson);
833 EXPECT_FALSE(success);
834 }
835
836 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig005, TestSize.Level1)
837 {
838 GlobalAppConfig config;
839 std::vector<AppInfo> configs;
840 std::string jsonStr = R"({
841 "version":"001",
842 "apps": []
843 })";
844 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
845 EXPECT_FALSE(extraJson.is_discarded());
846 bool success = config.ParseAppListConfig(configs, extraJson);
847 EXPECT_TRUE(success);
848 }
849
850 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig006, TestSize.Level1)
851 {
852 GlobalAppConfig config;
853 std::vector<AppInfo> configs;
854 std::string jsonStr = R"({
855 "version":"001",
856 "apps": [
857 {
858 "name":"",
859 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
860 "attribute":["monitoring"]
861 }
862 ]
863 })";
864 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
865 EXPECT_FALSE(extraJson.is_discarded());
866 bool success = config.ParseAppListConfig(configs, extraJson);
867 EXPECT_FALSE(success);
868 }
869
870 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig007, TestSize.Level1)
871 {
872 GlobalAppConfig config;
873 std::vector<AppInfo> configs;
874 std::string jsonStr = R"({
875 "version":"001",
876 "apps": [
877 {
878 "name":"",
879 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC517572",
880 "attribute":["monitoring"]
881 }
882 ]
883 })";
884 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
885 EXPECT_FALSE(extraJson.is_discarded());
886 bool success = config.ParseAppListConfig(configs, extraJson);
887 EXPECT_FALSE(success);
888 }
889
890 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig008, TestSize.Level1)
891 {
892 GlobalAppConfig config;
893 std::vector<AppInfo> configs;
894 std::string jsonStr = R"({
895 "version":"001",
896 "apps": [
897 {
898 "name":"com.sohu.harmonynews",
899 "attribute":["monitoring"]
900 }
901 ]
902 })";
903 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
904 EXPECT_FALSE(extraJson.is_discarded());
905 bool success = config.ParseAppListConfig(configs, extraJson);
906 EXPECT_FALSE(success);
907 }
908
909 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig009, TestSize.Level1)
910 {
911 GlobalAppConfig config;
912 std::vector<AppInfo> configs;
913 std::string jsonStr = R"({
914 "version":"001",
915 "apps": [
916 {
917 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
918 "attribute":["monitoring"]
919 }
920 ]
921 })";
922 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
923 EXPECT_FALSE(extraJson.is_discarded());
924 bool success = config.ParseAppListConfig(configs, extraJson);
925 EXPECT_FALSE(success);
926 }
927
928 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig010, TestSize.Level1)
929 {
930 GlobalAppConfig config;
931 std::vector<AppInfo> configs;
932 std::string jsonStr = R"({
933 "version":"001",
934 "apps": [
935 {
936 "name":"com.sohu.harmonynews",
937 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A"
938 }
939 ]
940 })";
941 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
942 EXPECT_FALSE(extraJson.is_discarded());
943 bool success = config.ParseAppListConfig(configs, extraJson);
944 EXPECT_FALSE(success);
945 }
946
947 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig011, TestSize.Level1)
948 {
949 GlobalAppConfig config;
950 std::vector<AppInfo> configs;
951 std::string jsonStr = R"({
952 "version":"001",
953 "apps": [
954 {
955 "name":"com.sohu.harmonynews",
956 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
957 "attribute":["monitoringL"]
958 }
959 ]
960 })";
961 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
962 EXPECT_FALSE(extraJson.is_discarded());
963 bool success = config.ParseAppListConfig(configs, extraJson);
964 EXPECT_FALSE(success);
965 }
966
967 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig012, TestSize.Level1)
968 {
969 GlobalAppConfig config;
970 std::vector<AppInfo> configs;
971 std::string jsonStr = R"({
972 "version":"001",
973 "apps": [
974 {
975 "name":"com.sohu.harmonynews",
976 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
977 "attribute":"monitoringL"
978 }
979 ]
980 })";
981 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
982 EXPECT_FALSE(extraJson.is_discarded());
983 bool success = config.ParseAppListConfig(configs, extraJson);
984 EXPECT_FALSE(success);
985 }
986
987 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig013, TestSize.Level1)
988 {
989 GlobalAppConfig config;
990 std::vector<AppInfo> configs;
991 std::string jsonStr = R"({
992 "version":"001",
993 "apps": [
994 {
995 "name":"com.sohu.harmonynews",
996 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
997 "attribute":["monitoring", "payment", "malicious"]
998 }
999 ]
1000 })";
1001 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
1002 EXPECT_FALSE(extraJson.is_discarded());
1003 bool success = config.ParseAppListConfig(configs, extraJson);
1004 EXPECT_TRUE(success);
1005 }
1006
1007 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig014, TestSize.Level1)
1008 {
1009 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), DeleteAppInfoByIsGlobalApp(
1010 An<int>())).WillRepeatedly(Return(SUCCESS));
1011 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), InsertAllAppInfo(
1012 An<const std::vector<AppInfo> &>())).WillRepeatedly(Return(SUCCESS));
1013 GlobalAppConfig config;
1014 config.stream_.open("/data/test/unittest/resource/global_app_attribute_update.json");
1015 EXPECT_TRUE(config.stream_.is_open());
1016 bool success = config.Parse();
1017 EXPECT_TRUE(success);
1018 success = config.Update();
1019 EXPECT_TRUE(success);
1020 }
1021
1022 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig015, TestSize.Level1)
1023 {
1024 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), DeleteAppInfoByIsGlobalApp(
1025 An<int>())).WillRepeatedly(Return(SUCCESS));
1026 EXPECT_CALL(AppInfoRdbHelper::GetInstance(), InsertAllAppInfo(
1027 An<const std::vector<AppInfo> &>())).WillRepeatedly(Return(SUCCESS));
1028 std::ofstream out("/data/test/unittest/resource/global_app_attribute_update.json");
1029 std::string errTmp = R"({
1030 "version":"001",
1031 "apps":""
1032 })";
1033 out << errTmp << std::endl;
1034 GlobalAppConfig config;
1035 config.stream_.open("/data/test/unittest/resource/global_app_attribute_update.json");
1036 EXPECT_TRUE(config.stream_.is_open());
1037 bool success = config.Parse();
1038 EXPECT_TRUE(success);
1039 success = config.Update();
1040 EXPECT_FALSE(success);
1041 std::string tmp = R"({
1042 "version":"001",
1043 "apps":[
1044 {
1045 "name":"com.sohu.harmonynews",
1046 "fingerprint":"C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A",
1047 "attribute":["monitoring"]
1048 },
1049 {
1050 "name":"com.sohu.harmonynews",
1051 "fingerprint":"ED2D188FACD5EB93248B287366324F6A12DF3A7B8D464C89FDD88FF1588C6596",
1052 "attribute":[]
1053 }
1054 ]
1055 })";
1056 out << tmp << std::endl;
1057 success = config.Update();
1058 EXPECT_FALSE(success);
1059 }
1060
1061 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig016, TestSize.Level1)
1062 {
1063 GlobalAppConfig config;
1064 std::vector<AppInfo> configs;
1065 nlohmann::json::array_t arr;
1066 std::vector<std::string> attrs = {"monitoring"};
1067 for (size_t i = 0; i < MAXAPPSIZE + 1; i++) {
1068 nlohmann::json jsonObj {
1069 {"name", std::to_string(i)},
1070 {"fingerprint", "C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A"},
1071 {"attribute", attrs},
1072 };
1073 arr.push_back(jsonObj);
1074 }
1075 nlohmann::json jsonOb {
1076 {"apps", arr}
1077 };
1078 std::string jsonStr = jsonOb.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
1079 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
1080 EXPECT_FALSE(extraJson.is_discarded());
1081 bool success = config.ParseAppListConfig(configs, extraJson);
1082 EXPECT_TRUE(success);
1083 EXPECT_TRUE(configs.size() == MAXAPPSIZE + 1) ;
1084 }
1085
1086 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig017, TestSize.Level1)
1087 {
1088 GlobalAppConfig config;
1089 bool success = config.Update();
1090 EXPECT_FALSE(success);
1091 }
1092
1093 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig018, TestSize.Level1)
1094 {
1095 GlobalAppConfig config;
1096 std::vector<AppInfo> configs;
1097 nlohmann::json::array_t arr;
1098 std::vector<std::string> attrs = {"monitoring"};
1099 for (size_t i = 0; i < MAXAPPSIZE + 1; i++) {
1100 nlohmann::json jsonObj {
1101 {"name", std::to_string(i)},
1102 {"fingerprint", "C8C9687FD68B738417ED2BFA6B91609A3F63720D30369130DEB802DC5175724A"},
1103 {"attribute", attrs},
1104 {"isUpdate", 1},
1105 };
1106 arr.push_back(jsonObj);
1107 }
1108 nlohmann::json jsonOb {
1109 {"apps", arr}
1110 };
1111 std::string jsonStr = jsonOb.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
1112 nlohmann::json extraJson = nlohmann::json::parse(jsonStr, nullptr, false);
1113 EXPECT_FALSE(extraJson.is_discarded());
1114 bool success = config.ParseAppListConfig(configs, extraJson);
1115 EXPECT_TRUE(success);
1116 EXPECT_TRUE(configs.size() == MAXAPPSIZE + 1);
1117 }
1118
1119 HWTEST_F(SecurityGuardConfigManagerTest, TestGlobalAppConfig019, TestSize.Level1)
1120 {
1121 GlobalAppConfig config;
1122 bool success = config.Load(INIT_MODE);
1123 EXPECT_TRUE(success);
1124 }
1125
1126 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling001, TestSize.Level1)
1127 {
1128 nlohmann::json jsonObj;
1129 SecurityGuard::AppDetectionCfg cfg = jsonObj.get<SecurityGuard::AppDetectionCfg>();
1130 EXPECT_TRUE(cfg.detectionCategory == "");
1131 }
1132
1133 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling002, TestSize.Level1)
1134 {
1135 nlohmann::json jsonObj;
1136 jsonObj["detectionCategory"] = 0;
1137 SecurityGuard::AppDetectionCfg cfg = jsonObj.get<SecurityGuard::AppDetectionCfg>();
1138 EXPECT_TRUE(cfg.detectionCategory == "");
1139 }
1140
1141 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling003, TestSize.Level1)
1142 {
1143 nlohmann::json jsonObj;
1144 jsonObj["detectionCategory"] = "detectionCategory";
1145 SecurityGuard::AppDetectionCfg cfg = jsonObj.get<SecurityGuard::AppDetectionCfg>();
1146 EXPECT_TRUE(cfg.detectionCategory == "detectionCategory");
1147 }
1148
1149 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling004, TestSize.Level1)
1150 {
1151 nlohmann::json jsonObj;
1152 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
1153 EXPECT_TRUE(field.fieldName == "");
1154 EXPECT_TRUE(field.fieldType == "");
1155 EXPECT_TRUE(field.value == "");
1156 }
1157
1158 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling005, TestSize.Level1)
1159 {
1160 nlohmann::json jsonObj;
1161 jsonObj["fieldName"] = 0;
1162 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
1163 EXPECT_TRUE(field.fieldName == "");
1164 EXPECT_TRUE(field.fieldType == "");
1165 EXPECT_TRUE(field.value == "");
1166 }
1167
1168 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling006, TestSize.Level1)
1169 {
1170 nlohmann::json jsonObj;
1171 jsonObj["fieldName"] = 0;
1172 jsonObj["fieldType"] = 0;
1173 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
1174 EXPECT_TRUE(field.fieldName == "");
1175 EXPECT_TRUE(field.fieldType == "");
1176 EXPECT_TRUE(field.value == "");
1177 }
1178
1179 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling007, TestSize.Level1)
1180 {
1181 nlohmann::json jsonObj;
1182 jsonObj["fieldName"] = 0;
1183 jsonObj["fieldType"] = 0;
1184 jsonObj["value"] = 0;
1185 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
1186 EXPECT_TRUE(field.fieldName == "");
1187 EXPECT_TRUE(field.fieldType == "");
1188 EXPECT_TRUE(field.value == "");
1189 }
1190
1191 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling008, TestSize.Level1)
1192 {
1193 nlohmann::json jsonObj;
1194 jsonObj["fieldName"] = "fieldName";
1195 jsonObj["fieldType"] = 0;
1196 jsonObj["value"] = 0;
1197 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
1198 EXPECT_TRUE(field.fieldName == "");
1199 EXPECT_TRUE(field.fieldType == "");
1200 EXPECT_TRUE(field.value == "");
1201 }
1202
1203 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling009, TestSize.Level1)
1204 {
1205 nlohmann::json jsonObj;
1206 jsonObj["fieldName"] = "fieldName";
1207 jsonObj["fieldType"] = "fieldType";
1208 jsonObj["value"] = 0;
1209 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
1210 EXPECT_TRUE(field.fieldName == "");
1211 EXPECT_TRUE(field.fieldType == "");
1212 EXPECT_TRUE(field.value == "");
1213 }
1214
1215 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling010, TestSize.Level1)
1216 {
1217 nlohmann::json jsonObj;
1218 jsonObj["fieldName"] = "fieldName";
1219 jsonObj["fieldType"] = "fieldType";
1220 jsonObj["value"] = "value";
1221 SecurityGuard::Field field = jsonObj.get<SecurityGuard::Field>();
1222 EXPECT_TRUE(field.fieldName == "fieldName");
1223 EXPECT_TRUE(field.fieldType == "fieldType");
1224 EXPECT_TRUE(field.value == "value");
1225 }
1226
1227 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling011, TestSize.Level1)
1228 {
1229 nlohmann::json jsonObj;
1230 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
1231 EXPECT_TRUE(rule.eventId == 0);
1232 EXPECT_TRUE(rule.fields.empty());
1233 EXPECT_TRUE(rule.fieldsRelation == "");
1234 }
1235
1236 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling012, TestSize.Level1)
1237 {
1238 nlohmann::json jsonObj;
1239 jsonObj["eventId"] = "";
1240 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
1241 EXPECT_TRUE(rule.eventId == 0);
1242 EXPECT_TRUE(rule.fields.empty());
1243 EXPECT_TRUE(rule.fieldsRelation == "");
1244 }
1245
1246 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling013, TestSize.Level1)
1247 {
1248 nlohmann::json jsonObj;
1249 jsonObj["eventId"] = "";
1250 jsonObj["fields"] = 0;
1251 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
1252 EXPECT_TRUE(rule.eventId == 0);
1253 EXPECT_TRUE(rule.fields.empty());
1254 EXPECT_TRUE(rule.fieldsRelation == "");
1255 }
1256
1257 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling014, TestSize.Level1)
1258 {
1259 nlohmann::json jsonObj;
1260 jsonObj["eventId"] = "";
1261 jsonObj["fields"] = 0;
1262 jsonObj["fieldsRelation"] = 0;
1263 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
1264 EXPECT_TRUE(rule.eventId == 0);
1265 EXPECT_TRUE(rule.fields.empty());
1266 EXPECT_TRUE(rule.fieldsRelation == "");
1267 }
1268
1269 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling015, TestSize.Level1)
1270 {
1271 nlohmann::json jsonObj;
1272 jsonObj["eventId"] = 0;
1273 jsonObj["fields"] = 0;
1274 jsonObj["fieldsRelation"] = 0;
1275 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
1276 EXPECT_TRUE(rule.eventId == 0);
1277 EXPECT_TRUE(rule.fields.empty());
1278 EXPECT_TRUE(rule.fieldsRelation == "");
1279 }
1280
1281 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling016, TestSize.Level1)
1282 {
1283 nlohmann::json jsonObj;
1284 jsonObj["eventId"] = 0;
1285 jsonObj["fields"] = 0;
1286 jsonObj["fieldsRelation"] = 0;
1287 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
1288 EXPECT_TRUE(rule.eventId == 0);
1289 EXPECT_TRUE(rule.fields.empty());
1290 EXPECT_TRUE(rule.fieldsRelation == "");
1291 }
1292
1293 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling017, TestSize.Level1)
1294 {
1295 nlohmann::json jsonObj;
1296 nlohmann::json jsonField;
1297 jsonField["fieldName"] = "fieldName";
1298 jsonField["fieldType"] = "fieldType";
1299 jsonField["value"] = "value";
1300 jsonObj["eventId"] = 0;
1301 jsonObj["fields"] = {jsonField, jsonField};
1302 jsonObj["fieldsRelation"] = 0;
1303 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
1304 EXPECT_TRUE(rule.eventId == 0);
1305 EXPECT_TRUE(rule.fields.empty());
1306 EXPECT_TRUE(rule.fieldsRelation == "");
1307 }
1308
1309 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling018, TestSize.Level1)
1310 {
1311 nlohmann::json jsonObj;
1312 nlohmann::json jsonField;
1313 jsonField["fieldName"] = "fieldName";
1314 jsonField["fieldType"] = "fieldType";
1315 jsonField["value"] = "value";
1316 jsonObj["eventId"] = 0;
1317 jsonObj["fields"] = {jsonField, jsonField};
1318 jsonObj["fieldsRelation"] = "fieldsRelation";
1319 SecurityGuard::Rule rule = jsonObj.get<SecurityGuard::Rule>();
1320 EXPECT_TRUE(rule.eventId == 0);
1321 EXPECT_TRUE(!rule.fields.empty());
1322 EXPECT_TRUE(rule.fieldsRelation == "fieldsRelation");
1323 }
1324
1325 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling019, TestSize.Level1)
1326 {
1327 nlohmann::json jsonObj;
1328 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
1329 EXPECT_TRUE(cfg.rules.empty());
1330 EXPECT_TRUE(cfg.rulesRelation == "");
1331 EXPECT_TRUE(cfg.trueResult == "");
1332 EXPECT_TRUE(cfg.falseResult == "");
1333 }
1334
1335 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling020, TestSize.Level1)
1336 {
1337 nlohmann::json jsonObj;
1338 jsonObj["rules"] = 0;
1339 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
1340 EXPECT_TRUE(cfg.rules.empty());
1341 EXPECT_TRUE(cfg.rulesRelation == "");
1342 EXPECT_TRUE(cfg.trueResult == "");
1343 EXPECT_TRUE(cfg.falseResult == "");
1344 }
1345
1346 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling021, TestSize.Level1)
1347 {
1348 nlohmann::json jsonObj;
1349 jsonObj["rules"] = 0;
1350 jsonObj["rulesRelation"] = 0;
1351 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
1352 EXPECT_TRUE(cfg.rules.empty());
1353 EXPECT_TRUE(cfg.rulesRelation == "");
1354 EXPECT_TRUE(cfg.trueResult == "");
1355 EXPECT_TRUE(cfg.falseResult == "");
1356 }
1357
1358 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling022, TestSize.Level1)
1359 {
1360 nlohmann::json jsonObj;
1361 jsonObj["rules"] = 0;
1362 jsonObj["rulesRelation"] = 0;
1363 jsonObj["trueResult"] = 0;
1364 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
1365 EXPECT_TRUE(cfg.rules.empty());
1366 EXPECT_TRUE(cfg.rulesRelation == "");
1367 EXPECT_TRUE(cfg.trueResult == "");
1368 EXPECT_TRUE(cfg.falseResult == "");
1369 }
1370
1371 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling023, TestSize.Level1)
1372 {
1373 nlohmann::json jsonObj;
1374 jsonObj["rules"] = 0;
1375 jsonObj["rulesRelation"] = 0;
1376 jsonObj["trueResult"] = 0;
1377 jsonObj["falseResult"] = 0;
1378 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
1379 EXPECT_TRUE(cfg.rules.empty());
1380 EXPECT_TRUE(cfg.rulesRelation == "");
1381 EXPECT_TRUE(cfg.trueResult == "");
1382 EXPECT_TRUE(cfg.falseResult == "");
1383 }
1384
1385 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling024, TestSize.Level1)
1386 {
1387 nlohmann::json jsonObj;
1388 nlohmann::json jsonRule;
1389 nlohmann::json jsonField;
1390 jsonField["fieldName"] = "fieldName";
1391 jsonField["fieldType"] = "fieldType";
1392 jsonField["value"] = "value";
1393 jsonRule["eventId"] = 0;
1394 jsonRule["fields"] = {jsonField, jsonField};
1395 jsonRule["fieldsRelation"] = "fieldsRelation";
1396 jsonObj["rules"] = {jsonRule, jsonRule};
1397 jsonObj["rulesRelation"] = 0;
1398 jsonObj["trueResult"] = 0;
1399 jsonObj["falseResult"] = 0;
1400 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
1401 EXPECT_TRUE(cfg.rules.empty());
1402 EXPECT_TRUE(cfg.rulesRelation == "");
1403 EXPECT_TRUE(cfg.trueResult == "");
1404 EXPECT_TRUE(cfg.falseResult == "");
1405 }
1406
1407 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling025, TestSize.Level1)
1408 {
1409 nlohmann::json jsonObj;
1410 nlohmann::json jsonRule;
1411 nlohmann::json jsonField;
1412 jsonField["fieldName"] = "fieldName";
1413 jsonField["fieldType"] = "fieldType";
1414 jsonField["value"] = "value";
1415 jsonRule["eventId"] = 0;
1416 jsonRule["fields"] = {jsonField, jsonField};
1417 jsonRule["fieldsRelation"] = "fieldsRelation";
1418 jsonObj["rules"] = {jsonRule, jsonRule};
1419 jsonObj["rulesRelation"] = "rulesRelation";
1420 jsonObj["trueResult"] = 0;
1421 jsonObj["falseResult"] = 0;
1422 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
1423 EXPECT_TRUE(cfg.rules.empty());
1424 EXPECT_TRUE(cfg.rulesRelation == "");
1425 EXPECT_TRUE(cfg.trueResult == "");
1426 EXPECT_TRUE(cfg.falseResult == "");
1427 }
1428
1429 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling026, TestSize.Level1)
1430 {
1431 nlohmann::json jsonObj;
1432 nlohmann::json jsonRule;
1433 nlohmann::json jsonField;
1434 jsonField["fieldName"] = "fieldName";
1435 jsonField["fieldType"] = "fieldType";
1436 jsonField["value"] = "value";
1437 jsonRule["eventId"] = 0;
1438 jsonRule["fields"] = {jsonField, jsonField};
1439 jsonRule["fieldsRelation"] = "fieldsRelation";
1440 jsonObj["rules"] = {jsonRule, jsonRule};
1441 jsonObj["rulesRelation"] = "rulesRelation";
1442 jsonObj["trueResult"] = "trueResult";
1443 jsonObj["falseResult"] = 0;
1444 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
1445 EXPECT_TRUE(cfg.rules.empty());
1446 EXPECT_TRUE(cfg.rulesRelation == "");
1447 EXPECT_TRUE(cfg.trueResult == "");
1448 EXPECT_TRUE(cfg.falseResult == "");
1449 }
1450
1451 HWTEST_F(SecurityGuardConfigManagerTest, TestModelCfgMarshalling027, TestSize.Level1)
1452 {
1453 nlohmann::json jsonObj;
1454 nlohmann::json jsonRule;
1455 nlohmann::json jsonField;
1456 jsonField["fieldName"] = "fieldName";
1457 jsonField["fieldType"] = "fieldType";
1458 jsonField["value"] = "value";
1459 jsonRule["eventId"] = 0;
1460 jsonRule["fields"] = {jsonField, jsonField};
1461 jsonRule["fieldsRelation"] = "fieldsRelation";
1462 jsonObj["rules"] = {jsonRule, jsonRule};
1463 jsonObj["rulesRelation"] = "rulesRelation";
1464 jsonObj["trueResult"] = "trueResult";
1465 jsonObj["falseResult"] = "falseResult";
1466 SecurityGuard::BuildInDetectionCfg cfg = jsonObj.get<SecurityGuard::BuildInDetectionCfg>();
1467 EXPECT_TRUE(!cfg.rules.empty());
1468 EXPECT_TRUE(cfg.rulesRelation == "rulesRelation");
1469 EXPECT_TRUE(cfg.trueResult == "trueResult");
1470 EXPECT_TRUE(cfg.falseResult == "falseResult");
1471 }
1472 }
1473