1 /*
2 * Copyright (c) 2025 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_file_system_store_helper_test.h"
17
18 #include <iostream>
19 #include <fstream>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 #include <chrono>
24 #include <iomanip>
25 #include <dirent.h>
26 #include <zlib.h>
27 #include <sys/stat.h>
28 #include <algorithm>
29 #include "gmock/gmock.h"
30 #include "security_event_query_callback_proxy.h"
31 #include "security_event_info.h"
32 #define private public
33 #define protected public
34 #include "file_system_store_helper.h"
35 #undef private
36 #undef protected
37
38 using namespace testing;
39 using namespace testing::ext;
40 using namespace OHOS::Security::SecurityGuard;
41 using namespace OHOS::Security::SecurityGuardTest;
42
43 namespace OHOS {
44 class MockRemoteObject final : public IRemoteObject {
45 public:
MockRemoteObject()46 MockRemoteObject() : IRemoteObject(u"")
47 {
48 }
GetObjectRefCount()49 int32_t GetObjectRefCount() { return 0; };
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)50 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { return 0; };
AddDeathRecipient(const sptr<DeathRecipient> & recipient)51 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; };
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)52 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; };
Dump(int fd,const std::vector<std::u16string> & args)53 int Dump(int fd, const std::vector<std::u16string> &args) { return 0; };
54 };
55 }
56
57 namespace OHOS::Security::SecurityGuardTest {
58 namespace {
59 constexpr int SUCCESS = 0;
60 }
61
SetUpTestCase()62 void SecurityGuardFileSystemStoreHelperTest::SetUpTestCase()
63 {
64 }
65
TearDownTestCase()66 void SecurityGuardFileSystemStoreHelperTest::TearDownTestCase()
67 {
68 }
69
SetUp()70 void SecurityGuardFileSystemStoreHelperTest::SetUp()
71 {
72 }
73
TearDown()74 void SecurityGuardFileSystemStoreHelperTest::TearDown()
75 {
76 }
77
78 HWTEST_F(SecurityGuardFileSystemStoreHelperTest, QuerySecurityEventTest001, TestSize.Level1)
79 {
80 int64_t eventId = 111;
81 SecurityCollector::SecurityEventRuler ruler{eventId};
82 sptr<IRemoteObject> obj(new (std::nothrow) MockRemoteObject());
83 auto proxy = iface_cast<ISecurityEventQueryCallback>(obj);
84 EXPECT_EQ(FileSystemStoreHelper::GetInstance().QuerySecurityEvent(ruler, proxy), SUCCESS);
85 }
86
87 HWTEST_F(SecurityGuardFileSystemStoreHelperTest, QuerySecurityEventTest002, TestSize.Level1)
88 {
89 int64_t eventId = 111;
90 std::string startTime = "20250228150000";
91 std::string endTime = "20250228150100";
92 SecurityCollector::SecurityEventRuler ruler{eventId};
93 sptr<IRemoteObject> obj(new (std::nothrow) MockRemoteObject());
94 auto proxy = iface_cast<ISecurityEventQueryCallback>(obj);
95 EXPECT_EQ(FileSystemStoreHelper::GetInstance().QuerySecurityEvent(ruler, proxy), SUCCESS);
96 }
97
98 HWTEST_F(SecurityGuardFileSystemStoreHelperTest, InsertEventTest001, TestSize.Level1)
99 {
100 std::vector<SecurityCollector::SecurityEvent> events;
101 for (size_t i = 0; i < 2; i++) {
102 int64_t eventId = 111;
103 SecurityCollector::SecurityEvent event(eventId);
104 events.push_back(event);
105 }
106 sptr<IRemoteObject> obj(new (std::nothrow) MockRemoteObject());
107 auto proxy = iface_cast<ISecurityEventQueryCallback>(obj);
108 FileSystemStoreHelper::GetInstance().QuerySecurityEventCallBack(proxy, events);
109 for (size_t i = 0; i < 200; i++) {
110 int64_t eventId = 111;
111 SecurityCollector::SecurityEvent event(eventId);
112 events.push_back(event);
113 }
114 FileSystemStoreHelper::GetInstance().QuerySecurityEventCallBack(proxy, events);
115 SecEvent event {
116 .eventId = 111,
117 .version = "1.0",
118 .date = "20250228150000",
119 .content = "{\"aaa\": \"111\"}"
120 };
121 EXPECT_EQ(FileSystemStoreHelper::GetInstance().InsertEvent(event), SUCCESS);
122 }
123
124 }