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, const SecEvent& event, 34 const std::set<std::string> &eventSubscribes = {}) = 0; 35 virtual int QueryAllEvent(std::string table, std::vector<SecEvent> &events) = 0; 36 virtual int QueryRecentEventByEventId(int64_t eventId, SecEvent &event) = 0; 37 virtual int QueryRecentEventByEventId(std::string table, const std::vector<int64_t> &eventId, 38 std::vector<SecEvent> &event) = 0; 39 virtual int QueryEventByEventIdAndDate(std::string table, std::vector<int64_t> &eventIds, 40 std::vector<SecEvent> &events, std::string beginTime, std::string endTime) = 0; 41 virtual int QueryEventByEventId(int64_t eventId, std::vector<SecEvent> &events) = 0; 42 virtual int QueryEventByEventId(std::string table, std::vector<int64_t> &eventIds, 43 std::vector<SecEvent> &events) = 0; 44 virtual int QueryEventByEventType(std::string table, int32_t eventType, std::vector<SecEvent> &events) = 0; 45 virtual int QueryEventByLevel(std::string table, int32_t level, std::vector<SecEvent> &events) = 0; 46 virtual int QueryEventByOwner(std::string table, std::string owner, std::vector<SecEvent> &events) = 0; 47 virtual int64_t CountAllEvent(std::string table) = 0; 48 virtual int64_t CountEventByEventId(int64_t eventId) = 0; 49 virtual int DeleteOldEventByEventId(int64_t eventId, int64_t count) = 0; 50 virtual int DeleteAllEventByEventId(int64_t eventId) = 0; 51 virtual int32_t SubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener) = 0; 52 virtual int32_t UnSubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener) = 0; 53 }; 54 55 class DatabaseManager : public BaseDatabaseManager { 56 public: GetInstance()57 static DatabaseManager &GetInstance() 58 { 59 static DatabaseManager instance; 60 return instance; 61 }; 62 63 MOCK_METHOD0(Init, void()); 64 MOCK_METHOD1(SetAuditState, int32_t(bool enable)); 65 MOCK_METHOD3(InsertEvent, int(uint32_t source, const SecEvent& event, 66 const std::set<std::string> &eventSubscribes)); 67 MOCK_METHOD2(QueryAllEvent, int(std::string table, std::vector<SecEvent> &events)); 68 MOCK_METHOD2(QueryRecentEventByEventId, int(int64_t eventId, SecEvent &event)); 69 MOCK_METHOD3(QueryRecentEventByEventId, int(std::string table, const std::vector<int64_t> &eventId, 70 std::vector<SecEvent> &event)); 71 MOCK_METHOD5(QueryEventByEventIdAndDate, int(std::string table, std::vector<int64_t> &eventIds, 72 std::vector<SecEvent> &events, std::string beginTime, std::string endTime)); 73 MOCK_METHOD2(QueryEventByEventId, int(int64_t eventId, std::vector<SecEvent> &events)); 74 MOCK_METHOD3(QueryEventByEventId, int(std::string table, std::vector<int64_t> &eventIds, 75 std::vector<SecEvent> &events)); 76 MOCK_METHOD3(QueryEventByEventType, int(std::string table, int32_t eventType, std::vector<SecEvent> &events)); 77 MOCK_METHOD3(QueryEventByLevel, int(std::string table, int32_t level, std::vector<SecEvent> &events)); 78 MOCK_METHOD3(QueryEventByOwner, int(std::string table, std::string owner, std::vector<SecEvent> &events)); 79 MOCK_METHOD1(CountAllEvent, int64_t(std::string table)); 80 MOCK_METHOD1(CountEventByEventId, int64_t(int64_t eventId)); 81 MOCK_METHOD2(DeleteOldEventByEventId, int(int64_t eventId, int64_t count)); 82 MOCK_METHOD1(DeleteAllEventByEventId, int(int64_t eventId)); 83 MOCK_METHOD2(SubscribeDb, int32_t(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener)); 84 MOCK_METHOD2(UnSubscribeDb, int32_t(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener)); 85 }; 86 } // namespace OHOS::Security::SecurityGuard 87 #endif // SECURITY_GUARD_DATABASE_MANAGER_H 88