• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include <memory>
16 #include "gmock/gmock.h"
17 #define private public
18 #include "data_collection.h"
19 #include "security_event_ruler.h"
20 #include "collector_cfg_marshalling.h"
21 #undef private
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::Security::SecurityCollector;
25 namespace OHOS::Security::SecurityCollectorTest {
26 class DataCollectionTest : public testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase() {};
29 
TearDownTestCase()30     static void TearDownTestCase() {};
31 
SetUp()32     void SetUp() override {};
33 
TearDown()34     void TearDown() override {};
35 };
36 
37 class TestFwk : public SecurityCollector::ICollectorFwk {
38 public:
OnNotify(const Event & event)39     void OnNotify(const Event &event) override {};
GetExtraInfo()40     std::string GetExtraInfo() override { return "";};
41 };
42 
43 class TestCollector : public SecurityCollector::ICollector {
44 public:
Start(std::shared_ptr<ICollectorFwk> api)45     int Start(std::shared_ptr<ICollectorFwk> api) override {return 0;};
Stop()46     int Stop()  override {return 0;};
47 };
48 
49 class MockMyClass : public DataCollection {
50 public:
51     MOCK_METHOD3(LoadCollector, ErrorCode(int64_t eventId, std::string path, std::shared_ptr<ICollectorFwk> api));
52     MOCK_METHOD2(GetCollectorPath, ErrorCode(int64_t eventId, std::string& path));
53     MOCK_METHOD1(IsCollectorStarted, bool(int64_t eventId));
54 };
55 
56 HWTEST_F(DataCollectionTest, Instance01, testing::ext::TestSize.Level1)
57 {
58     int32_t type = 0;
59     EXPECT_TRUE(DataCollection::GetInstance().GetCollectorType(0, type) != SUCCESS);
60 }
61 
62 HWTEST_F(DataCollectionTest, Instance02, testing::ext::TestSize.Level1)
63 {
64     std::shared_ptr<ICollectorFwk> api = std::make_shared<TestFwk> ();
65     EXPECT_EQ(DataCollection::GetInstance().LoadCollector(1, "", api), FAILED);
66     std::vector<SecurityEvent> eventIds {};
67     SecurityEventRuler ruler;
68     EXPECT_EQ(DataCollection::GetInstance().LoadCollector("", ruler, eventIds), FAILED);
69 }
70 
71 HWTEST_F(DataCollectionTest, StartCollectors01, testing::ext::TestSize.Level1)
72 {
73     DataCollection collec {};
74     std::vector<int64_t> eventIds {};
75     std::shared_ptr<SecurityCollector::ICollectorFwk> api;
76     EXPECT_FALSE(collec.StartCollectors(eventIds, api));
77     eventIds.emplace_back(1);
78     EXPECT_FALSE(collec.StartCollectors(eventIds, api));
79 }
80 
81 HWTEST_F(DataCollectionTest, StartCollectors02, testing::ext::TestSize.Level1)
82 {
83     DataCollection collec {};
84     std::vector<int64_t> eventIds {1};
85     std::shared_ptr<SecurityCollector::ICollectorFwk> api = std::make_shared<TestFwk> ();
86     EXPECT_FALSE(collec.StartCollectors(eventIds, api));
87 }
88 
89 HWTEST_F(DataCollectionTest, StartCollectors03, testing::ext::TestSize.Level1)
90 {
91     MockMyClass myOb;
92     std::vector<int64_t> eventIds {1};
93     std::shared_ptr<SecurityCollector::ICollectorFwk> api = std::make_shared<TestFwk> ();
94     EXPECT_CALL(myOb, IsCollectorStarted).WillOnce(Return(false));
95     EXPECT_CALL(myOb, GetCollectorPath).WillOnce(Return(SUCCESS));
96     EXPECT_CALL(myOb, LoadCollector(1, "", api)).WillOnce(Return(SUCCESS));
97     EXPECT_TRUE(myOb.StartCollectors(eventIds, api));
98 }
99 
100 HWTEST_F(DataCollectionTest, StartCollectors04, testing::ext::TestSize.Level1)
101 {
102     MockMyClass myOb;
103     std::vector<int64_t> eventIds {1};
104     std::shared_ptr<SecurityCollector::ICollectorFwk> api = std::make_shared<TestFwk> ();
105     EXPECT_CALL(myOb, IsCollectorStarted).WillOnce(Return(false));
106     EXPECT_CALL(myOb, GetCollectorPath).WillOnce(Return(SUCCESS));
107     EXPECT_CALL(myOb, LoadCollector(1, "", api)).WillOnce(Return(FAILED));
108     EXPECT_FALSE(myOb.StartCollectors(eventIds, api));
109 }
110 
111 HWTEST_F(DataCollectionTest, StartCollectors05, testing::ext::TestSize.Level1)
112 {
113     MockMyClass myOb;
114     std::vector<int64_t> eventIds {1};
115     std::shared_ptr<SecurityCollector::ICollectorFwk> api = std::make_shared<TestFwk> ();
116     EXPECT_CALL(myOb, GetCollectorPath).WillOnce(Return(FAILED));
117     EXPECT_CALL(myOb, IsCollectorStarted).WillOnce(Return(false));
118     EXPECT_FALSE(myOb.StartCollectors(eventIds, api));
119 }
120 
121 HWTEST_F(DataCollectionTest, StartCollectors06, testing::ext::TestSize.Level1)
122 {
123     MockMyClass myOb;
124     std::vector<int64_t> eventIds {1};
125     std::shared_ptr<SecurityCollector::ICollectorFwk> api = std::make_shared<TestFwk> ();
126     EXPECT_CALL(myOb, IsCollectorStarted).WillOnce(Return(true));
127     EXPECT_TRUE(myOb.StartCollectors(eventIds, api));
128 }
129 
130 HWTEST_F(DataCollectionTest, StopCollectors01, testing::ext::TestSize.Level1)
131 {
132     DataCollection myOb;
133     std::vector<int64_t> eventIds;
134     EXPECT_TRUE(myOb.StopCollectors(eventIds));
135 }
136 
137 HWTEST_F(DataCollectionTest, StopCollectors02, testing::ext::TestSize.Level1)
138 {
139     DataCollection myOb;
140     std::vector<int64_t> eventIds {1};
141     EXPECT_TRUE(myOb.StopCollectors(eventIds));
142 }
143 
144 HWTEST_F(DataCollectionTest, StopCollectors03, testing::ext::TestSize.Level1)
145 {
146     DataCollection myOb;
147     std::vector<int64_t> eventIds {1};
148     EXPECT_TRUE(myOb.StopCollectors(eventIds));
149 }
150 
151 HWTEST_F(DataCollectionTest, StopCollectors04, testing::ext::TestSize.Level1)
152 {
153     DataCollection myOb;
154     myOb.eventIdToLoaderMap_.emplace(1, LibLoader("testPath"));
155     std::vector<int64_t> eventIds {1};
156     EXPECT_FALSE(myOb.StopCollectors(eventIds));
157 }
158 
159 class MockMyCheckFileStreamClass : public DataCollection {
160 public:
161     MOCK_METHOD1(CheckFileStream, ErrorCode(std::ifstream &stream));
162 };
163 
164 HWTEST_F(DataCollectionTest, GetCollectorPath01, testing::ext::TestSize.Level1)
165 {
166     MockMyCheckFileStreamClass myOb;
167     std::string path;
168     EXPECT_CALL(myOb, CheckFileStream).WillOnce(Return(FAILED));
169     EXPECT_EQ(myOb.GetCollectorPath(1, path), FAILED);
170 }
171 
172 HWTEST_F(DataCollectionTest, GetCollectorPath02, testing::ext::TestSize.Level1)
173 {
174     MockMyCheckFileStreamClass myOb;
175     std::string path;
176     EXPECT_CALL(myOb, CheckFileStream).WillOnce(Return(SUCCESS));
177     EXPECT_EQ(myOb.GetCollectorPath(0, path), FAILED);
178 }
179 
180 HWTEST_F(DataCollectionTest, GetCollectorType01, testing::ext::TestSize.Level1)
181 {
182     MockMyCheckFileStreamClass myOb;
183     int32_t collectorType;
184     EXPECT_CALL(myOb, CheckFileStream).WillOnce(Return(FAILED));
185     EXPECT_EQ(myOb.GetCollectorType(1, collectorType), FAILED);
186 }
187 
188 HWTEST_F(DataCollectionTest, GetCollectorType02, testing::ext::TestSize.Level1)
189 {
190     MockMyCheckFileStreamClass myOb;
191     int32_t collectorType;
192     EXPECT_CALL(myOb, CheckFileStream).WillOnce(Return(SUCCESS));
193     EXPECT_EQ(myOb.GetCollectorType(0, collectorType), FAILED);
194 }
195 
196 class MockQuerySecurityEventClass : public DataCollection {
197 public:
198     MOCK_METHOD3(LoadCollector, ErrorCode(std::string path, const SecurityEventRuler &ruler,
199         std::vector<SecurityEvent> &events));
200     MOCK_METHOD2(GetCollectorPath, ErrorCode(int64_t eventId, std::string& path));
201 };
202 
203 HWTEST_F(DataCollectionTest, SecurityGuardSubscribeCollector01, testing::ext::TestSize.Level1)
204 {
205     std::vector<int64_t> eventIds;
206     MockMyClass myOb;
207     EXPECT_TRUE(myOb.SecurityGuardSubscribeCollector(eventIds));
208 }
209 
210 HWTEST_F(DataCollectionTest, SecurityGuardSubscribeCollector04, testing::ext::TestSize.Level1)
211 {
212     std::vector<int64_t> eventIds {1};
213     MockMyClass myOb;
214     std::shared_ptr<SecurityCollector::ICollectorFwk> api;
215     EXPECT_CALL(myOb, GetCollectorPath).WillOnce(Return(SUCCESS));
216     EXPECT_CALL(myOb, LoadCollector(1, "", api)).WillOnce(Return(SUCCESS));
217     EXPECT_CALL(myOb, IsCollectorStarted).WillOnce(Return(false));
218     EXPECT_TRUE(myOb.SecurityGuardSubscribeCollector(eventIds));
219 }
220 
221 HWTEST_F(DataCollectionTest, SecurityGuardSubscribeCollector02, testing::ext::TestSize.Level1)
222 {
223     std::vector<int64_t> eventIds {1};
224     MockMyClass myOb;
225     std::shared_ptr<SecurityCollector::ICollectorFwk> api;
226     EXPECT_CALL(myOb, GetCollectorPath).WillOnce(Return(SUCCESS));
227     EXPECT_CALL(myOb, LoadCollector(1, "", api)).WillOnce(Return(FAILED));
228     EXPECT_CALL(myOb, IsCollectorStarted).WillOnce(Return(false));
229     EXPECT_TRUE(myOb.SecurityGuardSubscribeCollector(eventIds));
230 }
231 
232 HWTEST_F(DataCollectionTest, SecurityGuardSubscribeCollector03, testing::ext::TestSize.Level1)
233 {
234     std::vector<int64_t> eventIds {1};
235     MockMyClass myOb;
236     EXPECT_CALL(myOb, GetCollectorPath).WillOnce(Return(FAILED));
237     EXPECT_CALL(myOb, IsCollectorStarted).WillOnce(Return(false));
238     EXPECT_TRUE(myOb.SecurityGuardSubscribeCollector(eventIds));
239 }
240 
241 HWTEST_F(DataCollectionTest, SecurityGuardSubscribeCollector05, testing::ext::TestSize.Level1)
242 {
243     std::vector<int64_t> eventIds {1};
244     MockMyClass myOb;
245     std::shared_ptr<SecurityCollector::ICollectorFwk> api = std::make_shared<TestFwk> ();
246     EXPECT_CALL(myOb, IsCollectorStarted).WillOnce(Return(true));
247     EXPECT_TRUE(myOb.SecurityGuardSubscribeCollector(eventIds));
248 }
249 
250 HWTEST_F(DataCollectionTest, SecurityGuardSubscribeCollector06, testing::ext::TestSize.Level1)
251 {
252     MockQuerySecurityEventClass myOb;
253     std::vector<SecurityEvent> events;
254     std::vector<SecurityEventRuler> rulers;
255     EXPECT_FALSE(myOb.QuerySecurityEvent(rulers, events));
256 
257     SecurityEventRuler rule(11111);
258     std::string path("/system/lib64/chipset-pub-sdk/libeventhandler.z.so");
259     rulers.emplace_back(rule);
260     EXPECT_CALL(myOb, GetCollectorPath).WillOnce(Return(SUCCESS));
261     EXPECT_CALL(myOb, LoadCollector).WillOnce(Return(SUCCESS));
262     EXPECT_TRUE(myOb.QuerySecurityEvent(rulers, events));
263 }
264 
265 HWTEST_F(DataCollectionTest, SecurityGuardSubscribeCollector07, testing::ext::TestSize.Level1)
266 {
267     MockQuerySecurityEventClass myOb;
268     std::vector<SecurityEvent> events;
269     std::vector<SecurityEventRuler> rulers;
270     EXPECT_FALSE(myOb.QuerySecurityEvent(rulers, events));
271 
272     SecurityEventRuler rule(11111);
273     std::string path("/system/lib64/chipset-pub-sdk/libeventhandler.z.so");
274     rulers.emplace_back(rule);
275     EXPECT_CALL(myOb, GetCollectorPath).WillOnce(Return(FAILED));
276 
277     EXPECT_FALSE(myOb.QuerySecurityEvent(rulers, events));
278 }
279 
280 HWTEST_F(DataCollectionTest, SecurityGuardSubscribeCollector08, testing::ext::TestSize.Level1)
281 {
282     MockQuerySecurityEventClass myOb;
283     std::vector<SecurityEvent> events;
284     std::vector<SecurityEventRuler> rulers;
285     EXPECT_FALSE(myOb.QuerySecurityEvent(rulers, events));
286 
287     SecurityEventRuler rule(11111);
288     std::string path("/system/lib64/chipset-pub-sdk/libeventhandler.z.so");
289     rulers.emplace_back(rule);
290 
291     EXPECT_CALL(myOb, GetCollectorPath).WillOnce(Return(SUCCESS));
292     EXPECT_CALL(myOb, LoadCollector).WillOnce(Return(FAILED));
293     EXPECT_FALSE(myOb.QuerySecurityEvent(rulers, events));
294 }
295 
296 HWTEST_F(DataCollectionTest, LoadCollectorWithApi01, testing::ext::TestSize.Level1)
297 {
298     std::string path = "/system/lib64/module/security/libsecurityguard_napi.z.so";
299     std::shared_ptr<ICollectorFwk> api = nullptr;
300     int64_t eventId = 1;
301     EXPECT_EQ(DataCollection::GetInstance().LoadCollector(eventId, path, api), FAILED);
302 }
303 
304 HWTEST_F(DataCollectionTest, LoadCollectorWithNonApi01, testing::ext::TestSize.Level1)
305 {
306     std::string path = "/system/lib64/module/security/libsecurityguard_napi.z.so";
307     std::vector<SecurityEvent> events;
308     SecurityCollector::SecurityEventRuler ruler(11111);
309     EXPECT_EQ(DataCollection::GetInstance().LoadCollector(path, ruler, events), FAILED);
310 }
311 
312 HWTEST_F(DataCollectionTest, Mute, testing::ext::TestSize.Level1)
313 {
314     MockMyClass myOb;
315     SecurityCollector::SecurityCollectorEventMuteFilter collectorFilter {};
316     collectorFilter.eventId = 1;
317     collectorFilter.mutes.emplace_back("1111");
318     collectorFilter.type = SecurityCollector::EVENT_SUB_TYPE_EQUAL;
319     collectorFilter.isSetMute = false;
320     myOb.eventIdToLoaderMap_.emplace(1, LibLoader("testPath"));
321     EXPECT_CALL(myOb, IsCollectorStarted).WillOnce(Return(false)).WillOnce(Return(true));
322     EXPECT_FALSE(myOb.Mute(collectorFilter, "1111"));
323     EXPECT_FALSE(myOb.Mute(collectorFilter, "1111"));
324 }
325 
326 HWTEST_F(DataCollectionTest, Unmute, testing::ext::TestSize.Level1)
327 {
328     MockMyClass myOb;
329     SecurityCollector::SecurityCollectorEventMuteFilter collectorFilter {};
330     collectorFilter.eventId = 1;
331     collectorFilter.mutes.emplace_back("1111");
332     collectorFilter.type = SecurityCollector::EVENT_SUB_TYPE_EQUAL;
333     collectorFilter.isSetMute = false;
334     ModuleCfgSt st {};
335     nlohmann::json json = st;
336     myOb.eventIdToLoaderMap_.emplace(1, LibLoader("testPath"));
337     EXPECT_CALL(myOb, IsCollectorStarted).WillOnce(Return(false)).WillOnce(Return(true));
338     EXPECT_FALSE(myOb.Unmute(collectorFilter, "1111"));
339     EXPECT_FALSE(myOb.Unmute(collectorFilter, "1111"));
340 }
341 
342 HWTEST_F(DataCollectionTest, ICollector01, testing::ext::TestSize.Level1)
343 {
344     TestCollector collector;
345     SecurityCollector::SecurityCollectorEventMuteFilter collectorFilter {};
346     std::vector<SecurityEvent> eventIds {};
347     SecurityEventRuler ruler;
348     EXPECT_EQ(collector.IsStartWithSub(), 0);
349     EXPECT_EQ(collector.Mute(collectorFilter, "111"), -1);
350     EXPECT_EQ(collector.Unmute(collectorFilter, "111"), -1);
351     EXPECT_EQ(collector.Query(ruler, eventIds), 0);
352     EXPECT_EQ(collector.Subscribe(0), 0);
353     EXPECT_EQ(collector.Unsubscribe(0), 0);
354 }
355 
356 }