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 <gtest/gtest.h>
17
18 #include <codecvt>
19 #include <locale>
20 #include <string>
21
22 #include "ash_mem_utils.h"
23 #include "compliant_event_checker.h"
24 #include "file_util.h"
25 #include "hiview_global.h"
26 #include "parameter_ex.h"
27 #include "running_status_log_util.h"
28 #include "string_ex.h"
29 #include "sys_event_rule.h"
30 #include "sys_event_service_adapter.h"
31 #include "sys_event_service_ohos.h"
32
33 namespace OHOS::HiviewDFX {
34 namespace EventStore {
35 using namespace testing::ext;
36 namespace {
37 constexpr char TEST_LOG_DIR[] = "/data/test/EventServiceAdapterUtilsTestDir/";
38 constexpr char TETS_ASH_MEM_NAME[] = "TestSharedMemory";
39 constexpr int32_t TETS_ASH_MEM_SIZE = 1024 * 2; // 2K
40 constexpr int64_t TEST_SECURE_ENABALED_VAL = 1;
41
GetAshmem()42 sptr<Ashmem> GetAshmem()
43 {
44 auto ashmem = Ashmem::CreateAshmem(TETS_ASH_MEM_NAME, TETS_ASH_MEM_SIZE);
45 if (ashmem == nullptr) {
46 return nullptr;
47 }
48 if (!ashmem->MapReadAndWriteAshmem()) {
49 return ashmem;
50 }
51 return ashmem;
52 }
53 }
54
55 class HiviewTestContext : public HiviewContext {
56 public:
GetHiViewDirectory(DirectoryType type __UNUSED)57 std::string GetHiViewDirectory(DirectoryType type __UNUSED)
58 {
59 return TEST_LOG_DIR;
60 }
61 };
62
GetDestDirWithSuffix(const std::string & dirSuffix)63 std::string GetDestDirWithSuffix(const std::string& dirSuffix)
64 {
65 std::string workPath = HiviewGlobal::GetInstance()->GetHiViewDirectory(
66 HiviewContext::DirectoryType::CONFIG_DIRECTORY);
67 if (workPath.back() != '/') {
68 workPath = workPath + "/";
69 }
70 workPath.append("/").append(dirSuffix);
71 if (!FileUtil::FileExists(workPath)) {
72 FileUtil::ForceCreateDirectory(workPath, FileUtil::FILE_PERM_770);
73 }
74 return workPath;
75 }
76
77 class EventServiceAdapterUtilsTest : public testing::Test {
78 public:
79 static void SetUpTestCase();
80 static void TearDownTestCase();
81 void SetUp();
82 void TearDown();
83 };
84
SetUpTestCase()85 void EventServiceAdapterUtilsTest::SetUpTestCase()
86 {
87 }
88
TearDownTestCase()89 void EventServiceAdapterUtilsTest::TearDownTestCase()
90 {
91 }
92
SetUp()93 void EventServiceAdapterUtilsTest::SetUp()
94 {
95 }
96
TearDown()97 void EventServiceAdapterUtilsTest::TearDown()
98 {
99 }
100
101 /**
102 * @tc.name: EventServiceAdapterUtilsTest001
103 * @tc.desc: test WriteBulkData of AshMemUtils
104 * @tc.type: FUNC
105 * @tc.require: issueIBT9BB
106 */
107 HWTEST_F(EventServiceAdapterUtilsTest, EventServiceAdapterUtilsTest001, TestSize.Level1)
108 {
109 MessageParcel msgParcel;
110 std::vector<std::u16string> emptyRes;
111 ASSERT_NE(AshMemUtils::WriteBulkData(msgParcel, emptyRes), nullptr);
112 std::vector<std::u16string> normalRes = {
113 Str8ToStr16(std::string("AshMemUtilsTest001")),
114 Str8ToStr16(std::string("AshMemUtilsTest001")),
115 };
116 ASSERT_NE(AshMemUtils::WriteBulkData(msgParcel, normalRes), nullptr);
117 }
118
119 /**
120 * @tc.name: EventServiceAdapterUtilsTest002
121 * @tc.desc: test ReadBulkData & CloseAshmem of AshMemUtils
122 * @tc.type: FUNC
123 * @tc.require: issueIBT9BB
124 */
125 HWTEST_F(EventServiceAdapterUtilsTest, EventServiceAdapterUtilsTest002, TestSize.Level1)
126 {
127 MessageParcel msgParcel;
128 std::vector<std::u16string> src = {
129 Str8ToStr16(std::string("AshMemUtilsTest002")),
130 Str8ToStr16(std::string("AshMemUtilsTest002")),
131 };
132 ASSERT_NE(AshMemUtils::WriteBulkData(msgParcel, src), nullptr);
133
134 std::vector<std::u16string> dest;
135 ASSERT_TRUE(AshMemUtils::ReadBulkData(msgParcel, dest));
136 ASSERT_TRUE(src.size() == dest.size());
137 ASSERT_TRUE(Str16ToStr8(dest[0]) == "AshMemUtilsTest002" && Str16ToStr8(dest[1]) == "AshMemUtilsTest002");
138 AshMemUtils::CloseAshmem(nullptr);
139 AshMemUtils::CloseAshmem(GetAshmem());
140 }
141
142 /**
143 * @tc.name: EventServiceAdapterUtilsTest003
144 * @tc.desc: test IsCompliantEvent of CompliantEventChecker
145 * @tc.type: FUNC
146 * @tc.require: issueIBT9BB
147 */
148 HWTEST_F(EventServiceAdapterUtilsTest, EventServiceAdapterUtilsTest003, TestSize.Level1)
149 {
150 bool enabled = (Parameter::GetInteger("const.secure", TEST_SECURE_ENABALED_VAL) == TEST_SECURE_ENABALED_VAL);
151 CompliantEventChecker compliantEventChecker;
152 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("AAFWK", "ABILITY_ONACTIVE"));
153 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("ACE", "INTERACTION_COMPLETED_LATENCY"));
154 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("AV_CODEC", "CODEC_START_INFO"));
155 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("GRAPHIC", "INTERACTION_COMPLETED_LATENCY"));
156 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("LOCATION", "GNSS_STATE"));
157 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("PERFORMANCE", "ANY_EVENT"));
158 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("RELIABILITY", "ANY_EVENT"));
159 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("SANITIZER", "ADDR_SANITIZER"));
160 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("WORKSCHEDULER", "WORK_ADD"));
161 if (enabled) {
162 ASSERT_FALSE(compliantEventChecker.IsCompliantEvent("AAFWK", "ANY_EVENT"));
163 ASSERT_FALSE(compliantEventChecker.IsCompliantEvent("ACE", "ANY_EVENT"));
164 ASSERT_FALSE(compliantEventChecker.IsCompliantEvent("AV_CODEC", "ANY_EVENT"));
165 ASSERT_FALSE(compliantEventChecker.IsCompliantEvent("GRAPHIC", "ANY_EVENT"));
166 ASSERT_FALSE(compliantEventChecker.IsCompliantEvent("LOCATION", "ANY_EVENT"));
167 ASSERT_FALSE(compliantEventChecker.IsCompliantEvent("SANITIZER", "ANY_EVENT"));
168 ASSERT_FALSE(compliantEventChecker.IsCompliantEvent("WORKSCHEDULER", "WORK_ADD"));
169 } else {
170 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("AAFWK", "ANY_EVENT"));
171 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("ACE", "ANY_EVENT"));
172 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("AV_CODEC", "ANY_EVENT"));
173 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("GRAPHIC", "ANY_EVENT"));
174 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("LOCATION", "ANY_EVENT"));
175 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("SANITIZER", "ANY_EVENT"));
176 ASSERT_TRUE(compliantEventChecker.IsCompliantEvent("WORKSCHEDULER", "WORK_ADD"));
177 }
178 }
179
180 /**
181 * @tc.name: RunningStatusLogUtilTest
182 * @tc.desc: Test apis of RunningStatusLogUtil
183 * @tc.type: FUNC
184 * @tc.require: issueI62WJT
185 */
186 HWTEST_F(EventServiceAdapterUtilsTest, RunningStatusLogUtilTest, testing::ext::TestSize.Level1)
187 {
188 HiviewTestContext hiviewTestContext;
189 HiviewGlobal::CreateInstance(hiviewTestContext);
190
191 std::vector<OHOS::HiviewDFX::SysEventQueryRule> queryRules;
192 std::vector<std::string> eventNames { "EVENT_NAME1", "EVENT_NAME2" };
193 OHOS::HiviewDFX::SysEventQueryRule queryRule("DOMAIN", eventNames);
194 RunningStatusLogUtil::LogTooManyQueryRules(queryRules);
195 queryRules.emplace_back(queryRule);
196 RunningStatusLogUtil::LogTooManyQueryRules(queryRules);
197
198 vector<SysEventRule> sysEventRules;
199 RunningStatusLogUtil::LogTooManyWatchRules(sysEventRules);
200 sysEventRules.emplace_back("", "");
201 RunningStatusLogUtil::LogTooManyWatchRules(sysEventRules);
202 sysEventRules.emplace_back("TETS_TAG");
203 RunningStatusLogUtil::LogTooManyWatchRules(sysEventRules);
204
205 RunningStatusLogUtil::LogTooManyWatchers(30); // 30 is a test value
206
207 RunningStatusLogUtil::LogTooManyEvents(1000); // 1000 is a test value
208 std::vector<std::string> files;
209 FileUtil::GetDirFiles(GetDestDirWithSuffix("sys_event_log"), files);
210 ASSERT_GE(files.size(), 0);
211 }
212
213 /**
214 * @tc.name: SysEventServiceAdapterTest001
215 * @tc.desc: Test apis of SysEventServiceAdapterTest001
216 * @tc.type: FUNC
217 * @tc.require: issueIBT9BB
218 */
219 HWTEST_F(EventServiceAdapterUtilsTest, SysEventServiceAdapterTest001, testing::ext::TestSize.Level1)
220 {
221 SysEventServiceAdapter::StartService(nullptr);
222 std::shared_ptr<SysEvent> event = nullptr;
223 SysEventServiceAdapter::OnSysEvent(event);
224 ASSERT_NE(OHOS::HiviewDFX::SysEventServiceOhos::GetInstance(), nullptr);
225 }
226 }
227 }