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_model_manager_test.h"
17
18
19 #include "file_ex.h"
20 #include "gmock/gmock.h"
21
22 #include "security_guard_define.h"
23 #include "security_guard_log.h"
24 #include "security_guard_utils.h"
25 #include "i_config_operate.h"
26 #define private public
27 #define protected public
28 #include "config_data_manager.h"
29 #include "i_model.h"
30 #include "model_manager.h"
31 #include "model_manager_impl.h"
32 #undef private
33 #undef protected
34
35 using namespace testing;
36 using namespace testing::ext;
37 using namespace OHOS::Security::SecurityGuard;
38 using namespace OHOS::Security::SecurityGuardTest;
39
40 namespace OHOS::Security::SecurityGuardTest {
41 namespace {
42 }
43
SetUpTestCase()44 void SecurityGuardModelManagerTest::SetUpTestCase()
45 {
46 }
47
TearDownTestCase()48 void SecurityGuardModelManagerTest::TearDownTestCase()
49 {
50 }
51
SetUp()52 void SecurityGuardModelManagerTest::SetUp()
53 {
54 }
55
TearDown()56 void SecurityGuardModelManagerTest::TearDown()
57 {
58 }
59
60 class MockModel : public IModel {
61 public:
62 MOCK_METHOD1(Init, int32_t(std::shared_ptr<IModelManager>));
63 MOCK_METHOD2(GetResult, std::string(uint32_t, const std::string &));
64 MOCK_METHOD1(SubscribeResult, int32_t(std::shared_ptr<IModelResultListener>));
65 MOCK_METHOD0(Release, void());
66 };
67
68 class MockModelManager : public IModelManager {
69 public:
70 MOCK_METHOD0(GetConfigOperate, std::shared_ptr<IConfigOperate>());
71 MOCK_METHOD1(GetDbOperate, std::shared_ptr<IDbOperate>(std::string));
72 MOCK_METHOD2(SubscribeDb, int32_t(std::vector<int64_t>, std::shared_ptr<IDbListener>));
73 MOCK_METHOD2(UnSubscribeDb, int32_t(std::vector<int64_t>, std::shared_ptr<IDbListener>));
74 };
75
76 class MockDbOperate : public IDbOperate {
77 public:
78 MOCK_METHOD1(InsertEvent, int(SecEvent&));
79 MOCK_METHOD1(QueryAllEvent, int(std::vector<SecEvent> &));
80 MOCK_METHOD2(QueryRecentEventByEventId, int(int64_t, SecEvent &));
81 MOCK_METHOD2(QueryRecentEventByEventId, int(const std::vector<int64_t> &, std::vector<SecEvent> &));
82 MOCK_METHOD2(QueryEventByEventId, int(int64_t, std::vector<SecEvent> &));
83 MOCK_METHOD2(QueryEventByEventId, int(std::vector<int64_t> &, std::vector<SecEvent> &));
84 MOCK_METHOD2(QueryEventByEventType, int(int32_t, std::vector<SecEvent> &));
85 MOCK_METHOD2(QueryEventByLevel, int(int32_t, std::vector<SecEvent> &));
86 MOCK_METHOD2(QueryEventByOwner, int(std::string, std::vector<SecEvent> &));
87 MOCK_METHOD0(CountAllEvent, int64_t());
88 MOCK_METHOD1(CountEventByEventId, int64_t(int64_t));
89 MOCK_METHOD2(DeleteOldEventByEventId, int(int64_t, int64_t));
90 MOCK_METHOD1(DeleteAllEventByEventId, int(int64_t));
91 };
92
93 class MockConfigOperate : public IConfigOperate {
94 public:
95 MOCK_METHOD2(GetModelConfig, bool(uint32_t, ModelCfg &));
96 MOCK_METHOD2(GetEventConfig, bool(int64_t, EventCfg &));
97 };
98
99 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerImpl001, TestSize.Level1)
100 {
101 auto impl = std::make_shared<ModelManagerImpl>();
102 std::shared_ptr<IDbOperate> oper = impl->GetDbOperate("risk_event");
103 EXPECT_FALSE(oper == nullptr);
104 oper = impl->GetDbOperate("audit_event");
105 EXPECT_FALSE(oper == nullptr);
106 oper = impl->GetDbOperate("test_table");
107 EXPECT_TRUE(oper == nullptr);
108 }
109
110 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerInit001, TestSize.Level1)
111 {
112 std::vector<uint32_t> emptyVector{};
113 std::vector<uint32_t> vector{0};
114 EXPECT_CALL(ConfigDataManager::GetInstance(), GetAllModelIds).Times(AtLeast(5)).WillOnce(Return(emptyVector))
115 .WillRepeatedly(Return(vector));
116 EXPECT_CALL(ConfigDataManager::GetInstance(), GetModelConfig).Times(AtLeast(5)).WillOnce(Return(false))
__anonc95a48e60202(uint32_t modelId, ModelCfg &config) 117 .WillOnce([](uint32_t modelId, ModelCfg &config) {
118 config.startMode = 0;
119 return true;
120 })
__anonc95a48e60302(uint32_t modelId, ModelCfg &config) 121 .WillOnce([](uint32_t modelId, ModelCfg &config) {
122 config.startMode = 1;
123 config.modelId = 0;
124 return true;
125 })
__anonc95a48e60402(uint32_t modelId, ModelCfg &config) 126 .WillRepeatedly([](uint32_t modelId, ModelCfg &config) {
127 config.startMode = 1;
128 config.modelId = 3001000003;
129 return true;
130 });
131 ModelManager::GetInstance().Init();
132 ModelManager::GetInstance().Init();
133 ModelManager::GetInstance().Init();
134 ModelManager::GetInstance().Init();
135 ModelManager::GetInstance().Init();
136 EXPECT_TRUE(ModelManager::GetInstance().InitModel(0) != SUCCESS);
137 }
138
139 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerInitModel001, TestSize.Level1)
140 {
141 MockModel *model = new MockModel();
142 EXPECT_CALL(*model, Release()).Times(1);
143 EXPECT_CALL(ConfigDataManager::GetInstance(), GetModelConfig).Times(1).WillOnce(Return(false));
144 std::unique_ptr<ModelAttrs> attr = std::make_unique<ModelAttrs>();
145 attr->SetModelApi(model);
146 ModelManager::GetInstance().modelIdApiMap_[8888] = std::move(attr);
147 EXPECT_TRUE(ModelManager::GetInstance().InitModel(8888) != SUCCESS);
148 }
149
150 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerInitModel002, TestSize.Level1)
151 {
152 EXPECT_CALL(ConfigDataManager::GetInstance(), GetModelConfig).Times(AtLeast(6))
__anonc95a48e60502(uint32_t modelId, ModelCfg &config) 153 .WillOnce([](uint32_t modelId, ModelCfg &config) {
154 config.path = "/system/lib64/sg_test";
155 return true;
156 })
__anonc95a48e60602(uint32_t modelId, ModelCfg &config) 157 .WillOnce([](uint32_t modelId, ModelCfg &config) {
158 config.path = "/system/lib64/libsg_collector_sdk.z.so";
159 return true;
160 })
__anonc95a48e60702(uint32_t modelId, ModelCfg &config) 161 .WillOnce([](uint32_t modelId, ModelCfg &config) {
162 config.path = "/system/lib64/libsg_system_risk_detection.z.so";
163 return true;
164 })
__anonc95a48e60802(uint32_t modelId, ModelCfg &config) 165 .WillOnce([](uint32_t modelId, ModelCfg &config) {
166 config.startMode = NOT_SUPPORT;
167 return false;
168 })
__anonc95a48e60902(uint32_t modelId, ModelCfg &config) 169 .WillOnce([](uint32_t modelId, ModelCfg &config) {
170 config.startMode = NOT_SUPPORT;
171 return true;
172 })
__anonc95a48e60a02(uint32_t modelId, ModelCfg &config) 173 .WillRepeatedly([](uint32_t modelId, ModelCfg &config) {
174 config.startMode = START_ON_DEMAND;
175 return true;
176 });
177 ModelManager::GetInstance().InitModel(9999);
178 ModelManager::GetInstance().GetResult(9999, "");
179 ModelManager::GetInstance().SubscribeResult(9999, nullptr);
180 ModelManager::GetInstance().Release(9999);
181 ModelManager::GetInstance().InitModel(9999);
182 ModelManager::GetInstance().GetResult(9999, "");
183 ModelManager::GetInstance().SubscribeResult(9999, nullptr);
184 ModelManager::GetInstance().Release(9999);
185 ModelManager::GetInstance().InitModel(9999);
186 ModelManager::GetInstance().GetResult(9999, "");
187 ModelManager::GetInstance().GetResult(9999, "");
188 ModelManager::GetInstance().GetResult(9999, "");
189 ModelManager::GetInstance().SubscribeResult(9999, nullptr);
190 ModelManager::GetInstance().Release(9999);
191 EXPECT_TRUE(ModelManager::GetInstance().InitModel(9999) != SUCCESS);
192 }
193 }