1 /*
2 * Copyright (c) 2022-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
16 #include <gtest/gtest.h>
17
18 #include <chrono>
19 #include <iosfwd>
20 #include <string>
21 #include <thread>
22 #include <unistd.h>
23 #include <vector>
24
25 #include "gtest/gtest-message.h"
26 #include "gtest/gtest-test-part.h"
27 #include "gtest/hwext/gtest-ext.h"
28 #include "gtest/hwext/gtest-tag.h"
29
30 #include "ash_mem_utils.h"
31 #include "file_util.h"
32 #include "hilog/log.h"
33 #include "hisysevent_base_listener.h"
34 #include "hisysevent_base_query_callback.h"
35 #include "hisysevent_delegate.h"
36 #include "hisysevent_listener_proxy.h"
37 #include "hisysevent_query_proxy.h"
38 #include "hisysevent_rules.h"
39 #include "iquery_sys_event_callback.h"
40 #include "query_argument.h"
41 #include "query_sys_event_callback_stub.h"
42 #include "ret_code.h"
43 #include "string_ex.h"
44 #include "string_util.h"
45 #include "sys_event_service_proxy.h"
46
47 using namespace testing::ext;
48 using namespace OHOS;
49 using namespace OHOS::HiviewDFX;
50
51 namespace {
52 constexpr char ASH_MEM_NAME[] = "TestSharedMemory";
53 constexpr int32_t ASH_MEM_SIZE = 1024 * 2; // 2K
54 constexpr char LOG_DIR_PATH[] = "/data/test/adapter_native_test";
55 constexpr char FILE_PATH[] = "/data/test/adapter_native_test/test.log";
56
GetAshmem()57 sptr<Ashmem> GetAshmem()
58 {
59 auto ashmem = Ashmem::CreateAshmem(ASH_MEM_NAME, ASH_MEM_SIZE);
60 if (ashmem == nullptr) {
61 return nullptr;
62 }
63 if (!ashmem->MapReadAndWriteAshmem()) {
64 return ashmem;
65 }
66 return ashmem;
67 }
68 }
69
70 class HiSysEventAdapterNativeTest : public testing::Test {
71 public:
72 static void SetUpTestCase(void);
73 static void TearDownTestCase(void);
74 void SetUp();
75 void TearDown();
76 };
77
SetUpTestCase(void)78 void HiSysEventAdapterNativeTest::SetUpTestCase(void)
79 {
80 }
81
TearDownTestCase(void)82 void HiSysEventAdapterNativeTest::TearDownTestCase(void)
83 {
84 }
85
SetUp(void)86 void HiSysEventAdapterNativeTest::SetUp(void)
87 {
88 }
89
TearDown(void)90 void HiSysEventAdapterNativeTest::TearDown(void)
91 {
92 }
93
94 /**
95 * @tc.name: TestAshMemory
96 * @tc.desc: Ashmemory test
97 * @tc.type: FUNC
98 * @tc.require: issueI62BDW
99 */
100 HWTEST_F(HiSysEventAdapterNativeTest, TestAshMemory, TestSize.Level1)
101 {
102 MessageParcel data;
103 std::vector<std::u16string> src = {
104 Str8ToStr16(std::string("0")),
105 Str8ToStr16(std::string("1")),
106 };
107 auto ret = AshMemUtils::WriteBulkData(data, src);
108 ASSERT_NE(ret, nullptr);
109 std::vector<std::u16string> dest;
110 auto ret1 = AshMemUtils::ReadBulkData(data, dest);
111 ASSERT_TRUE(ret1);
112 ASSERT_EQ(src.size(), dest.size());
113 ASSERT_EQ(Str16ToStr8(dest[0]), "0");
114 ASSERT_EQ(Str16ToStr8(dest[1]), "1");
115 AshMemUtils::CloseAshmem(nullptr);
116 ASSERT_TRUE(true);
117 AshMemUtils::CloseAshmem(GetAshmem());
118 ASSERT_TRUE(true);
119 }
120
121 /**
122 * @tc.name: TestHiSysEventDelegateApisWithInvalidInstance
123 * @tc.desc: Call Add/Removelistener with a HiSysEventDelegate instance directly
124 * @tc.type: FUNC
125 * @tc.require: issueI62BDW
126 */
127 HWTEST_F(HiSysEventAdapterNativeTest, TestHiSysEventDelegateApisWithInvalidInstance, TestSize.Level1)
128 {
129 std::shared_ptr<OHOS::HiviewDFX::HiSysEventDelegate> delegate =
130 std::make_shared<OHOS::HiviewDFX::HiSysEventDelegate>();
__anon86f3df770202() 131 std::thread t([delegate] () {
132 delegate->BinderFunc();
133 });
134 t.detach();
135 auto ret = delegate->RemoveListener(nullptr);
136 ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
137 auto listener = std::make_shared<HiSysEventBaseListener>();
138 std::vector<ListenerRule> rules;
139 ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "TAG", RuleType::WHOLE_WORD);
140 rules.emplace_back(listenerRule);
141 ret = delegate->AddListener(listener, rules);
142 ASSERT_EQ(ret, IPC_CALL_SUCCEED);
143 ret = delegate->RemoveListener(listener);
144 ASSERT_EQ(ret, IPC_CALL_SUCCEED);
145 long long defaultTimeStap = -1;
146 int queryCount = 10;
147 struct QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
148 std::vector<QueryRule> queryRules;
149 std::vector<std::string> eventNames {"START_ABILITY"};
150 QueryRule rule("AAFWK", eventNames);
151 queryRules.emplace_back(rule);
152 auto baseQuerier = std::make_shared<HiSysEventBaseQueryCallback>();
153 ret = delegate->Query(args, queryRules, baseQuerier);
154 ASSERT_EQ(ret, IPC_CALL_SUCCEED);
155
156 // invalid call
157 auto result = delegate->Subscribe(queryRules);
158 ASSERT_EQ(result, ERR_NO_PERMISSION);
159 ret = delegate->Unsubscribe();
160 ASSERT_EQ(ret, ERR_NO_PERMISSION);
161 ret = delegate->Export(args, queryRules);
162 ASSERT_EQ(ret, ERR_NO_PERMISSION);
163 }
164
165 /**
166 * @tc.name: FileUtilOhosTest001
167 * @tc.desc: FileUtil test
168 * @tc.type: FUNC
169 * @tc.require: SR000I1G43
170 */
171 HWTEST_F(HiSysEventAdapterNativeTest, FileUtilOhosTest001, TestSize.Level3)
172 {
173 auto ret = FileUtil::ForceCreateDirectory(LOG_DIR_PATH);
174 ASSERT_TRUE(ret);
175 ret = FileUtil::IsDirectory(LOG_DIR_PATH);
176 ASSERT_TRUE(ret);
177 ret = FileUtil::RemoveDirectory(LOG_DIR_PATH);
178 ASSERT_TRUE(ret);
179 ret = FileUtil::IsDirectory(LOG_DIR_PATH);
180 ASSERT_TRUE(!ret);
181 auto result = FileUtil::GetFilePathByDir(LOG_DIR_PATH, "test.log");
182 ASSERT_EQ(result, FILE_PATH);
183 }
184
185 /**
186 * @tc.name: FileUtilOhosTest002
187 * @tc.desc: FileUtil test
188 * @tc.type: FUNC
189 * @tc.require: SR000I1G43
190 */
191 HWTEST_F(HiSysEventAdapterNativeTest, FileUtilOhosTest002, TestSize.Level3)
192 {
193 auto ret = FileUtil::IsLegalPath("aa/../bb");
194 ASSERT_TRUE(!ret);
195 ret = FileUtil::IsLegalPath("aa/./bb");
196 ASSERT_TRUE(!ret);
197 ret = FileUtil::IsLegalPath("aa/bb/");
198 ASSERT_TRUE(ret);
199 ret = FileUtil::IsLegalPath("aa/bb/cc");
200 ASSERT_TRUE(ret);
201 }
202
203 /**
204 * @tc.name: MarshallingTAndUnmarshallingTest
205 * @tc.desc: Unmarshalling test
206 * @tc.type: FUNC
207 * @tc.require: issueI62WJT
208 */
209 HWTEST_F(HiSysEventAdapterNativeTest, MarshallingTAndUnmarshallingTest, TestSize.Level1)
210 {
211 long long defaultTimeStap = -1;
212 int queryCount = 10;
213 OHOS::HiviewDFX::QueryArgument args(defaultTimeStap, defaultTimeStap, queryCount);
214 MessageParcel parcel1;
215 auto ret = args.Marshalling(parcel1);
216 ASSERT_TRUE(ret);
217 QueryArgument* argsPtr = args.Unmarshalling(parcel1);
218 ASSERT_NE(argsPtr, nullptr);
219 ASSERT_EQ(argsPtr->maxEvents, 10); // 10 is a expcted test value
220 ASSERT_EQ(argsPtr->beginTime, -1); // -1 is a expcted test value
221 OHOS::HiviewDFX::SysEventRule sysEventRule("DOMAIN", "EVENT_NAME", "TAG", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
222 MessageParcel parcel2;
223 ret = sysEventRule.Marshalling(parcel2);
224 ASSERT_TRUE(ret);
225 OHOS::HiviewDFX::SysEventRule* sysEventRulePtr = sysEventRule.Unmarshalling(parcel2);
226 ASSERT_NE(sysEventRulePtr, nullptr);
227 ASSERT_EQ(sysEventRulePtr->domain, "DOMAIN");
228 ASSERT_EQ(sysEventRulePtr->eventName, "EVENT_NAME");
229 ASSERT_EQ(sysEventRulePtr->tag, "TAG");
230 std::vector<std::string> eventNames { "EVENT_NAME1", "EVENT_NAME2" };
231 OHOS::HiviewDFX::SysEventQueryRule queryRule("DOMAIN", eventNames);
232 MessageParcel parcel3;
233 ret = queryRule.Marshalling(parcel3);
234 ASSERT_TRUE(ret);
235 OHOS::HiviewDFX::SysEventQueryRule* queryRulePtr = queryRule.Unmarshalling(parcel3);
236 ASSERT_NE(queryRulePtr, nullptr);
237 ASSERT_EQ(queryRulePtr->domain, "DOMAIN");;
238 ASSERT_EQ(queryRulePtr->eventList.size(), 2); // 2 is a expcted test value
239 ASSERT_EQ(queryRulePtr->eventList[0], "EVENT_NAME1");
240 }
241
242 /**
243 * @tc.name: CStringUtilTest
244 * @tc.desc: Test methods which defined in namespace StringUtil
245 * @tc.type: FUNC
246 * @tc.require: issueI62WJT
247 */
248 HWTEST_F(HiSysEventAdapterNativeTest, CStringUtilTest, TestSize.Level1)
249 {
250 char dest[100] {}; // 100 is a test length
251 std::string src = "01234567";
252
253 auto ret = StringUtil::CopyCString(dest, src, 3); // 3 is a test length
254 ASSERT_EQ(ret, -1); // -1 is a expected result
255 ret = StringUtil::CopyCString(dest, src, 10); // 3 is a test length
256 ASSERT_NE(ret, -1); // -1 is a expected result
257 char* dest2p = dest;
258 char** dest2pp = &dest2p;
259 ret = StringUtil::CreateCString(dest2pp, src, 3); // 3 is a test length
260 ASSERT_EQ(ret, -1); // -1 is a expected result
261 ret = StringUtil::CreateCString(dest2pp, src, 10); // 3 is a test length
262 ASSERT_NE(ret, -1); // -1 is a expected result
263 ret = StringUtil::ConvertCString(src, dest2pp, 3); // 3 is a test length
264 ASSERT_EQ(ret, -1); // -1 is a expected result
265 ret = StringUtil::ConvertCString(src, dest2pp, 10); // 3 is a test length
266 ASSERT_NE(ret, -1); // -1 is a expected result
267 char v3[10][100] {};
268 char* dest3p = v3[0];
269 char** dest3pp = &dest3p;
270 char*** dest3ppp = &dest3pp;
271 std::vector<std::string> srcs1 = {};
272 std::vector<std::string> srcs2 = {
273 "01234567",
274 "01234567",
275 };
276 size_t len;
277 ret = StringUtil::ConvertCStringVec(srcs1, dest3ppp, len);
278 ASSERT_EQ(ret, 0);
279 ASSERT_EQ(len, 0);
280 ret = StringUtil::ConvertCStringVec(srcs2, dest3ppp, len);
281 ASSERT_EQ(ret, 0);
282 ASSERT_EQ(len, 2); // 2 is a expected length
283 char dest4[3] = {'0', '1', '2'};
284 StringUtil::MemsetSafe(reinterpret_cast<void*>(dest4), 3); // 3 is a test length
285 ASSERT_EQ(dest4[0], 0);
286 ASSERT_EQ(dest4[1], 0); // 1 is a test index
287 ASSERT_EQ(dest4[2], 0); // 2 is a test index
288 }
289
290 /**
291 * @tc.name: HiSysEventListenerProxyTest
292 * @tc.desc: Test apis of HiSysEventListenerProxy
293 * @tc.type: FUNC
294 * @tc.require: issueI62WJT
295 */
296 HWTEST_F(HiSysEventAdapterNativeTest, HiSysEventListenerProxyTest, TestSize.Level1)
297 {
298 auto baseListener = std::make_shared<HiSysEventBaseListener>();
299 HiSysEventListenerProxy proxy(baseListener);
300 proxy.Handle("DOMAIN", "EVENT_NAME", 0, "{}");
301 auto listener = proxy.GetEventListener();
302 ASSERT_NE(listener, nullptr);
303 auto deathRecipient = proxy.GetCallbackDeathRecipient();
304 ASSERT_NE(deathRecipient, nullptr);
305 if (deathRecipient != nullptr) {
306 deathRecipient->OnRemoteDied(nullptr);
307 ASSERT_NE(deathRecipient->GetEventListener(), nullptr);
308 }
309 }
310
311 /**
312 * @tc.name: HiSysEventQueryProxyTest
313 * @tc.desc: Test apis of HiSysEventQueryProxy
314 * @tc.type: FUNC
315 * @tc.require: issueI62WJT
316 */
317 HWTEST_F(HiSysEventAdapterNativeTest, HiSysEventQueryProxyTest, TestSize.Level1)
318 {
319 auto baseQuerier = std::make_shared<HiSysEventBaseQueryCallback>();
320 HiSysEventQueryProxy proxy(baseQuerier);
321 std::vector<std::u16string> sysEvent {};
322 std::vector<int64_t> seq {};
323 proxy.OnQuery(sysEvent, seq);
324 ASSERT_TRUE(true);
325 proxy.OnComplete(0, 0, 0);
326 ASSERT_TRUE(true);
327 }