• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MockMyModelManager : public ModelManager {
77 public:
78     MOCK_METHOD1(InitModel, int32_t(uint32_t));
79 };
80 class MockDbOperate : public IDbOperate {
81 public:
82     MOCK_METHOD1(InsertEvent, int(SecEvent&));
83     MOCK_METHOD1(QueryAllEvent, int(std::vector<SecEvent> &));
84     MOCK_METHOD2(QueryRecentEventByEventId, int(int64_t, SecEvent &));
85     MOCK_METHOD2(QueryRecentEventByEventId, int(const std::vector<int64_t> &, std::vector<SecEvent> &));
86     MOCK_METHOD2(QueryEventByEventId, int(int64_t, std::vector<SecEvent> &));
87     MOCK_METHOD2(QueryEventByEventId, int(std::vector<int64_t> &, std::vector<SecEvent> &));
88     MOCK_METHOD4(QueryEventByEventIdAndDate, int(std::vector<int64_t> &,
89         std::vector<SecEvent> &, std::string, std::string));
90     MOCK_METHOD2(QueryEventByEventType, int(int32_t, std::vector<SecEvent> &));
91     MOCK_METHOD2(QueryEventByLevel, int(int32_t, std::vector<SecEvent> &));
92     MOCK_METHOD2(QueryEventByOwner, int(std::string, std::vector<SecEvent> &));
93     MOCK_METHOD0(CountAllEvent, int64_t());
94     MOCK_METHOD1(CountEventByEventId, int64_t(int64_t));
95     MOCK_METHOD2(DeleteOldEventByEventId, int(int64_t, int64_t));
96     MOCK_METHOD1(DeleteAllEventByEventId, int(int64_t));
97 };
98 
99 class MockConfigOperate : public IConfigOperate {
100 public:
101     MOCK_METHOD2(GetModelConfig, bool(uint32_t, ModelCfg &));
102     MOCK_METHOD2(GetEventConfig, bool(int64_t, EventCfg &));
103 };
104 
105 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerImpl001, TestSize.Level0)
106 {
107     auto impl = std::make_shared<ModelManagerImpl>();
108     std::shared_ptr<IDbOperate> oper = impl->GetDbOperate("risk_event");
109     EXPECT_FALSE(oper == nullptr);
110     oper = impl->GetDbOperate("audit_event");
111     EXPECT_FALSE(oper == nullptr);
112     oper = impl->GetDbOperate("test_table");
113     EXPECT_TRUE(oper == nullptr);
114 }
115 
116 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerInit001, TestSize.Level0)
117 {
118     std::vector<uint32_t> emptyVector{};
119     std::vector<uint32_t> vector{0};
120     EXPECT_CALL(ConfigDataManager::GetInstance(), GetAllModelIds).Times(AtLeast(5)).WillOnce(Return(emptyVector))
121         .WillRepeatedly(Return(vector));
122     EXPECT_CALL(ConfigDataManager::GetInstance(), GetModelConfig).Times(AtLeast(5)).WillOnce(Return(false))
__anon678802480202(uint32_t modelId, ModelCfg &config) 123         .WillOnce([](uint32_t modelId, ModelCfg &config) {
124             config.startMode = 0;
125             return true;
126         })
__anon678802480302(uint32_t modelId, ModelCfg &config) 127         .WillOnce([](uint32_t modelId, ModelCfg &config) {
128             config.startMode = 1;
129             config.modelId = 0;
130             return true;
131         })
__anon678802480402(uint32_t modelId, ModelCfg &config) 132         .WillRepeatedly([](uint32_t modelId, ModelCfg &config) {
133             config.startMode = 1;
134             config.modelId = 3001000003;
135             return true;
136         });
137     ModelManager::GetInstance().Init();
138     ModelManager::GetInstance().Init();
139     ModelManager::GetInstance().Init();
140     ModelManager::GetInstance().Init();
141     ModelManager::GetInstance().Init();
142     EXPECT_TRUE(ModelManager::GetInstance().InitModel(0) != SUCCESS);
143 }
144 
145 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerInitModel001, TestSize.Level0)
146 {
147     MockModel *model = new MockModel();
148     EXPECT_CALL(*model, Release()).Times(1);
149     EXPECT_CALL(ConfigDataManager::GetInstance(), GetModelConfig).Times(1).WillOnce(Return(false));
150     std::unique_ptr<ModelAttrs> attr = std::make_unique<ModelAttrs>();
151     attr->SetModelApi(model);
152     ModelManager::GetInstance().modelIdApiMap_[8888] = std::move(attr);
153     EXPECT_TRUE(ModelManager::GetInstance().InitModel(8888) != SUCCESS);
154 }
155 
156 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerInitModel002, TestSize.Level0)
157 {
158     EXPECT_CALL(ConfigDataManager::GetInstance(), GetModelConfig).Times(AtLeast(6))
__anon678802480502(uint32_t modelId, ModelCfg &config) 159         .WillOnce([](uint32_t modelId, ModelCfg &config) {
160             config.path = "/system/lib64/sg_test";
161             return true;
162         })
__anon678802480602(uint32_t modelId, ModelCfg &config) 163         .WillOnce([](uint32_t modelId, ModelCfg &config) {
164             config.path = "/system/lib64/libsg_collector_sdk.z.so";
165             return true;
166         })
__anon678802480702(uint32_t modelId, ModelCfg &config) 167         .WillOnce([](uint32_t modelId, ModelCfg &config) {
168             config.path = "/system/lib64/libsg_system_risk_detection.z.so";
169             return true;
170         })
__anon678802480802(uint32_t modelId, ModelCfg &config) 171         .WillOnce([](uint32_t modelId, ModelCfg &config) {
172             config.startMode = NOT_SUPPORT;
173             return false;
174         })
__anon678802480902(uint32_t modelId, ModelCfg &config) 175         .WillOnce([](uint32_t modelId, ModelCfg &config) {
176             config.startMode = NOT_SUPPORT;
177             return true;
178         })
__anon678802480a02(uint32_t modelId, ModelCfg &config) 179         .WillRepeatedly([](uint32_t modelId, ModelCfg &config) {
180             config.startMode = START_ON_DEMAND;
181             return true;
182         });
183     ModelManager::GetInstance().InitModel(9999);
184     ModelManager::GetInstance().GetResult(9999, "");
185     ModelManager::GetInstance().SubscribeResult(9999, nullptr);
186     ModelManager::GetInstance().Release(9999);
187     ModelManager::GetInstance().InitModel(9999);
188     ModelManager::GetInstance().GetResult(9999, "");
189     ModelManager::GetInstance().SubscribeResult(9999, nullptr);
190     ModelManager::GetInstance().Release(9999);
191     ModelManager::GetInstance().InitModel(9999);
192     ModelManager::GetInstance().GetResult(9999, "");
193     ModelManager::GetInstance().GetResult(9999, "");
194     ModelManager::GetInstance().GetResult(9999, "");
195     ModelManager::GetInstance().SubscribeResult(9999, nullptr);
196     ModelManager::GetInstance().Release(9999);
197     EXPECT_TRUE(ModelManager::GetInstance().InitModel(9999) != SUCCESS);
198 }
199 
200 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerStartSecurityModel001, TestSize.Level0)
201 {
202     EXPECT_CALL(ConfigDataManager::GetInstance(), GetModelConfig)
__anon678802480b02(uint32_t modelId, ModelCfg &config) 203     .WillOnce([](uint32_t modelId, ModelCfg &config) {
204         config.path = "/system/lib64/sg_test";
205         return false;
206     });
207     EXPECT_EQ(ModelManager::GetInstance().StartSecurityModel(111, "test"), NOT_FOUND);
208 }
209 
210 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerStartSecurityModel002, TestSize.Level0)
211 {
212     EXPECT_CALL(ConfigDataManager::GetInstance(), GetModelConfig)
__anon678802480c02(uint32_t modelId, ModelCfg &config) 213     .WillOnce([](uint32_t modelId, ModelCfg &config) {
214         config.path = "/system/lib64/sg_test";
215         return true;
216     });
217     EXPECT_EQ(ModelManager::GetInstance().StartSecurityModel(111, "test"), FILE_ERR);
218 }
219 
220 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerGetResult002, TestSize.Level0)
221 {
222     MockMyModelManager manager {};
223     EXPECT_CALL(ConfigDataManager::GetInstance(), GetModelConfig)
__anon678802480d02(uint32_t modelId, ModelCfg &config) 224     .WillOnce([](uint32_t modelId, ModelCfg &config) {
225         config.path = "/system/lib64/sg_test";
226         config.startMode = START_ON_DEMAND;
227         return true;
228     });
229     EXPECT_CALL(manager, InitModel)
__anon678802480e02(uint32_t modelId) 230     .WillOnce([](uint32_t modelId) {
231         return SUCCESS;
232     });
233     EXPECT_EQ(manager.GetResult(111, "test"), "unknown");
234 }
235 
236 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerGetResult003, TestSize.Level0)
237 {
238     MockMyModelManager manager {};
239     EXPECT_CALL(ConfigDataManager::GetInstance(), GetModelConfig)
__anon678802480f02(uint32_t modelId, ModelCfg &config) 240     .WillOnce([](uint32_t modelId, ModelCfg &config) {
241         config.path = "/system/lib64/sg_test";
242         config.startMode = START_ON_DEMAND;
243         return true;
244     });
245     EXPECT_CALL(manager, InitModel)
__anon678802481002(uint32_t modelId) 246     .WillOnce([](uint32_t modelId) {
247         return FAILED;
248     });
249     EXPECT_EQ(manager.GetResult(111, "test"), "unknown");
250 }
251 
252 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerSubscribeResult002, TestSize.Level0)
253 {
254     MockMyModelManager manager {};
255     EXPECT_CALL(manager, InitModel)
__anon678802481102(uint32_t modelId) 256     .WillOnce([](uint32_t modelId) {
257         return SUCCESS;
258     });
259     EXPECT_EQ(manager.SubscribeResult(111, nullptr), FAILED);
260 }
261 
262 HWTEST_F(SecurityGuardModelManagerTest, TestModelManagerStartSecurityModel003, TestSize.Level0)
263 {
264     MockMyModelManager manager {};
265     EXPECT_CALL(manager, InitModel)
__anon678802481202(uint32_t modelId) 266     .WillOnce([](uint32_t modelId) {
267         return SUCCESS;
268     });
269     EXPECT_EQ(manager.StartSecurityModel(111, "test"), FAILED);
270 }
271 }