1 /*
2  * Copyright (C) 2023 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 #ifndef SECURITY_GUARD_DATABASE_MANAGER_H
17 #define SECURITY_GUARD_DATABASE_MANAGER_H
18 
19 #include <set>
20 #include <unordered_map>
21 
22 #include "gmock/gmock.h"
23 
24 #include "singleton.h"
25 
26 #include "i_db_listener.h"
27 
28 namespace OHOS::Security::SecurityGuard {
29 class BaseDatabaseManager {
30 public:
31     virtual void Init() = 0;
32     virtual int32_t SetAuditState(bool enable) = 0;
33     virtual int InsertEvent(uint32_t source, SecEvent& event) = 0;
34     virtual int QueryAllEvent(std::string table, std::vector<SecEvent> &events) = 0;
35     virtual int QueryRecentEventByEventId(int64_t eventId, SecEvent &event) = 0;
36     virtual int QueryRecentEventByEventId(std::string table, const std::vector<int64_t> &eventId,
37         std::vector<SecEvent> &event) = 0;
38     virtual int QueryEventByEventIdAndDate(std::string table, std::vector<int64_t> &eventIds,
39         std::vector<SecEvent> &events, std::string beginTime, std::string endTime) = 0;
40     virtual int QueryEventByEventId(int64_t eventId, std::vector<SecEvent> &events) = 0;
41     virtual int QueryEventByEventId(std::string table, std::vector<int64_t> &eventIds,
42         std::vector<SecEvent> &events) = 0;
43     virtual int QueryEventByEventType(std::string table, int32_t eventType, std::vector<SecEvent> &events) = 0;
44     virtual int QueryEventByLevel(std::string table, int32_t level, std::vector<SecEvent> &events) = 0;
45     virtual int QueryEventByOwner(std::string table, std::string owner, std::vector<SecEvent> &events) = 0;
46     virtual int64_t CountAllEvent(std::string table) = 0;
47     virtual int64_t CountEventByEventId(int64_t eventId) = 0;
48     virtual int DeleteOldEventByEventId(int64_t eventId, int64_t count) = 0;
49     virtual int DeleteAllEventByEventId(int64_t eventId) = 0;
50     virtual int32_t SubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener) = 0;
51     virtual int32_t UnSubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener) = 0;
52 };
53 
54 class DatabaseManager : public BaseDatabaseManager {
55 public:
GetInstance()56     static DatabaseManager &GetInstance()
57     {
58         static DatabaseManager instance;
59         return instance;
60     };
61 
62     MOCK_METHOD0(Init, void());
63     MOCK_METHOD1(SetAuditState, int32_t(bool enable));
64     MOCK_METHOD2(InsertEvent, int(uint32_t source, SecEvent& event));
65     MOCK_METHOD2(QueryAllEvent, int(std::string table, std::vector<SecEvent> &events));
66     MOCK_METHOD2(QueryRecentEventByEventId, int(int64_t eventId, SecEvent &event));
67     MOCK_METHOD3(QueryRecentEventByEventId, int(std::string table, const std::vector<int64_t> &eventId,
68         std::vector<SecEvent> &event));
69     MOCK_METHOD5(QueryEventByEventIdAndDate, int(std::string table, std::vector<int64_t> &eventIds,
70         std::vector<SecEvent> &events, std::string beginTime, std::string endTime));
71     MOCK_METHOD2(QueryEventByEventId, int(int64_t eventId, std::vector<SecEvent> &events));
72     MOCK_METHOD3(QueryEventByEventId, int(std::string table, std::vector<int64_t> &eventIds,
73         std::vector<SecEvent> &events));
74     MOCK_METHOD3(QueryEventByEventType, int(std::string table, int32_t eventType, std::vector<SecEvent> &events));
75     MOCK_METHOD3(QueryEventByLevel, int(std::string table, int32_t level, std::vector<SecEvent> &events));
76     MOCK_METHOD3(QueryEventByOwner, int(std::string table, std::string owner, std::vector<SecEvent> &events));
77     MOCK_METHOD1(CountAllEvent, int64_t(std::string table));
78     MOCK_METHOD1(CountEventByEventId, int64_t(int64_t eventId));
79     MOCK_METHOD2(DeleteOldEventByEventId, int(int64_t eventId, int64_t count));
80     MOCK_METHOD1(DeleteAllEventByEventId, int(int64_t eventId));
81     MOCK_METHOD2(SubscribeDb, int32_t(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener));
82     MOCK_METHOD2(UnSubscribeDb, int32_t(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener));
83 };
84 } // namespace OHOS::Security::SecurityGuard
85 #endif // SECURITY_GUARD_DATABASE_MANAGER_H
86