1 /*
2 * Copyright (c) 2022 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 <iosfwd>
19 #include <string>
20 #include <thread>
21 #include <unistd.h>
22 #include <vector>
23
24 #include "gtest/gtest-message.h"
25 #include "gtest/gtest-test-part.h"
26 #include "gtest/hwext/gtest-ext.h"
27 #include "gtest/hwext/gtest-tag.h"
28
29 #include "hilog/log.h"
30 #include "ash_mem_utils.h"
31 #include "hisysevent_base_listener.h"
32 #include "hisysevent_base_query_callback.h"
33 #include "hisysevent_delegate.h"
34 #include "hisysevent_listener_proxy.h"
35 #include "hisysevent_query_proxy.h"
36 #include "hisysevent_rules.h"
37 #include "iquery_sys_event_callback.h"
38 #include "query_argument.h"
39 #include "query_sys_event_callback_proxy.h"
40 #include "query_sys_event_callback_stub.h"
41 #include "ret_code.h"
42 #include "string_ex.h"
43 #include "string_util.h"
44 #include "sys_event_callback_proxy.h"
45 #include "sys_event_callback_stub.h"
46 #include "sys_event_service_proxy.h"
47 #include "sys_event_service_stub.h"
48
49
50 using namespace testing::ext;
51 using namespace OHOS;
52 using namespace OHOS::HiviewDFX;
53
54 namespace {
55 constexpr char ASH_MEM_NAME[] = "TestSharedMemory";
56 constexpr int32_t ASH_MEM_SIZE = 1024 * 2; // 2K
57
GetAshmem()58 sptr<Ashmem> GetAshmem()
59 {
60 auto ashmem = Ashmem::CreateAshmem(ASH_MEM_NAME, ASH_MEM_SIZE);
61 if (ashmem == nullptr) {
62 return nullptr;
63 }
64 if (!ashmem->MapReadAndWriteAshmem()) {
65 return ashmem;
66 }
67 return ashmem;
68 }
69
70 class QuerySysEventCallbackStubTest : public QuerySysEventCallbackStub {
71 public:
QuerySysEventCallbackStubTest()72 QuerySysEventCallbackStubTest() {}
~QuerySysEventCallbackStubTest()73 virtual ~QuerySysEventCallbackStubTest() {}
74
OnQuery(const std::vector<std::u16string> & sysEvent,const std::vector<int64_t> & seq)75 void OnQuery(const std::vector<std::u16string>& sysEvent, const std::vector<int64_t>& seq) {}
OnComplete(int32_t reason,int32_t total,int64_t seq)76 void OnComplete(int32_t reason, int32_t total, int64_t seq) {}
77
78 public:
79 enum Code {
80 DEFAULT = -1,
81 ON_QUERY = 0,
82 ON_COMPLETE,
83 };
84 };
85
86 class SysEventCallbackStubTest : public SysEventCallbackStub {
87 public:
SysEventCallbackStubTest()88 SysEventCallbackStubTest() {}
~SysEventCallbackStubTest()89 virtual ~SysEventCallbackStubTest() {}
90
Handle(const std::u16string & domain,const std::u16string & eventName,uint32_t eventType,const std::u16string & eventDetail)91 void Handle(const std::u16string& domain, const std::u16string& eventName, uint32_t eventType,
92 const std::u16string& eventDetail) {}
93
94 public:
95 enum Code {
96 DEFAULT = -1,
97 HANDLE = 0,
98 };
99 };
100
101 class SysEventServiceStubTest : public SysEventServiceStub {
102 public:
SysEventServiceStubTest()103 SysEventServiceStubTest() {}
~SysEventServiceStubTest()104 virtual ~SysEventServiceStubTest() {}
105
AddListener(const std::vector<SysEventRule> & rules,const sptr<ISysEventCallback> & callback)106 int32_t AddListener(const std::vector<SysEventRule>& rules, const sptr<ISysEventCallback>& callback)
107 {
108 return 0;
109 }
110
RemoveListener(const sptr<ISysEventCallback> & callback)111 int32_t RemoveListener(const sptr<ISysEventCallback>& callback)
112 {
113 return 0;
114 }
115
Query(const QueryArgument & queryArgument,const std::vector<SysEventQueryRule> & rules,const sptr<IQuerySysEventCallback> & callback)116 int32_t Query(const QueryArgument& queryArgument, const std::vector<SysEventQueryRule>& rules,
117 const sptr<IQuerySysEventCallback>& callback)
118 {
119 return 0;
120 }
121
SetDebugMode(const sptr<ISysEventCallback> & callback,bool mode)122 int32_t SetDebugMode(const sptr<ISysEventCallback>& callback, bool mode)
123 {
124 return 0;
125 }
126
127 public:
128 enum Code {
129 DEFAULT = -1,
130 ADD_SYS_EVENT_LISTENER = 0,
131 REMOVE_SYS_EVENT_LISTENER,
132 QUERY_SYS_EVENT,
133 SET_DEBUG_MODE
134 };
135 };
136 }
137
138 class HiSysEventAdapterNativeTest : public testing::Test {
139 public:
140 static void SetUpTestCase(void);
141 static void TearDownTestCase(void);
142 void SetUp();
143 void TearDown();
144 };
145
SetUpTestCase(void)146 void HiSysEventAdapterNativeTest::SetUpTestCase(void)
147 {
148 }
149
TearDownTestCase(void)150 void HiSysEventAdapterNativeTest::TearDownTestCase(void)
151 {
152 }
153
SetUp(void)154 void HiSysEventAdapterNativeTest::SetUp(void)
155 {
156 }
157
TearDown(void)158 void HiSysEventAdapterNativeTest::TearDown(void)
159 {
160 }
161
162 /**
163 * @tc.name: TestAshMemory
164 * @tc.desc: Ashmemory test
165 * @tc.type: FUNC
166 * @tc.require: issueI62BDW
167 */
168 HWTEST_F(HiSysEventAdapterNativeTest, TestAshMemory, TestSize.Level1)
169 {
170 MessageParcel data;
171 std::vector<std::u16string> src = {
172 Str8ToStr16(std::string("0")),
173 Str8ToStr16(std::string("1")),
174 };
175 auto ret = AshMemUtils::WriteBulkData(data, src);
176 ASSERT_TRUE(ret != nullptr);
177 std::vector<std::u16string> dest;
178 auto ret1 = AshMemUtils::ReadBulkData(data, dest);
179 ASSERT_TRUE(ret1);
180 ASSERT_TRUE(src.size() == dest.size());
181 ASSERT_TRUE(Str16ToStr8(dest[0]) == "0" && Str16ToStr8(dest[1]) == "1");
182 AshMemUtils::CloseAshmem(nullptr);
183 ASSERT_TRUE(true);
184 AshMemUtils::CloseAshmem(GetAshmem());
185 ASSERT_TRUE(true);
186 }
187
188 /**
189 * @tc.name: TestHiSysEventDelegateApisWithInvalidInstance
190 * @tc.desc: Call Add/Removelistener/SetDebugMode with a HiSysEventDelegate instance directly
191 * @tc.type: FUNC
192 * @tc.require: issueI62BDW
193 */
194 HWTEST_F(HiSysEventAdapterNativeTest, TestHiSysEventDelegateApisWithInvalidInstance, TestSize.Level1)
195 {
196 std::shared_ptr<OHOS::HiviewDFX::HiSysEventDelegate> delegate =
197 std::make_shared<OHOS::HiviewDFX::HiSysEventDelegate>();
__anon236c18170202() 198 std::thread t([delegate] () {
199 delegate->BinderFunc();
200 });
201 t.detach();
202 auto ret = delegate->RemoveListener(nullptr);
203 ASSERT_TRUE(ret == ERR_LISTENER_NOT_EXIST);
204 ret = delegate->SetDebugMode(nullptr, true);
205 ASSERT_TRUE(ret == ERR_LISTENER_NOT_EXIST);
206 auto listener = std::make_shared<HiSysEventBaseListener>();
207 std::vector<ListenerRule> rules;
208 ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "TAG", RuleType::WHOLE_WORD);
209 rules.emplace_back(listenerRule);
210 ret = delegate->AddListener(listener, rules);
211 ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
212 ret = delegate->SetDebugMode(listener, true);
213 if (ret == IPC_CALL_SUCCEED) {
214 ret = delegate->SetDebugMode(listener, false);
215 ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
216 }
217 ret = delegate->RemoveListener(listener);
218 ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
219 long long defaultTimeStap = -1;
220 int queryCount = 10;
221 struct QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
222 std::vector<QueryRule> queryRules;
223 std::vector<std::string> eventNames {"START_ABILITY"};
224 QueryRule rule("AAFWK", eventNames);
225 queryRules.emplace_back(rule);
226 auto baseQuerier = std::make_shared<HiSysEventBaseQueryCallback>();
227 ret = delegate->Query(args, queryRules, baseQuerier);
228 ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
229 }
230
231 /**
232 * @tc.name: TestQuerySysEventCallback
233 * @tc.desc: QuerySysEventCallbackProxy/Stub test
234 * @tc.type: FUNC
235 * @tc.require: issueI62WJT
236 */
237 HWTEST_F(HiSysEventAdapterNativeTest, TestQuerySysEventCallback, TestSize.Level1)
238 {
239 QuerySysEventCallbackStub* querySysEventCallbackStub = new(std::nothrow) QuerySysEventCallbackStubTest();
240 MessageParcel data, reply;
241 MessageOption option;
242 querySysEventCallbackStub->OnRemoteRequest(QuerySysEventCallbackStubTest::Code::DEFAULT, data, reply, option);
243 ASSERT_TRUE(true);
244 querySysEventCallbackStub->OnRemoteRequest(QuerySysEventCallbackStubTest::Code::ON_QUERY, data, reply, option);
245 ASSERT_TRUE(true);
246 querySysEventCallbackStub->OnRemoteRequest(QuerySysEventCallbackStubTest::Code::ON_COMPLETE, data, reply, option);
247 ASSERT_TRUE(true);
248 const sptr<IRemoteObject>& impl(querySysEventCallbackStub);
249 QuerySysEventCallbackProxy proxy(impl);
250 std::vector<std::u16string> sysEvent {};
251 std::vector<int64_t> seq {};
252 proxy.OnQuery(sysEvent, seq);
253 ASSERT_TRUE(true);
254 sysEvent.emplace_back(Str8ToStr16(std::string("0")));
255 seq.emplace_back(1);
256 proxy.OnQuery(sysEvent, seq);
257 ASSERT_TRUE(true);
258 proxy.OnComplete(0, 0, 0);
259 ASSERT_TRUE(true);
260 }
261
262 /**
263 * @tc.name: TestSysEventCallback
264 * @tc.desc: SysEventCallbackProxy/Stub test
265 * @tc.type: FUNC
266 * @tc.require: issueI62WJT
267 */
268 HWTEST_F(HiSysEventAdapterNativeTest, TestSysEventCallback, TestSize.Level1)
269 {
270 SysEventCallbackStub* sysEventCallbackStub = new(std::nothrow) SysEventCallbackStubTest();
271 MessageParcel data, reply;
272 MessageOption option;
273 sysEventCallbackStub->OnRemoteRequest(SysEventCallbackStubTest::Code::DEFAULT, data, reply, option);
274 ASSERT_TRUE(true);
275 sysEventCallbackStub->OnRemoteRequest(SysEventCallbackStubTest::Code::HANDLE, data, reply, option);
276 ASSERT_TRUE(true);
277 const sptr<IRemoteObject>& impl(sysEventCallbackStub);
278 SysEventCallbackProxy proxy(impl);
279 proxy.Handle(Str8ToStr16(std::string("DOMAIN")), Str8ToStr16(std::string("EVENT_NAME")), 0,
280 Str8ToStr16(std::string("{}")));
281 ASSERT_TRUE(true);
282 }
283
284 /**
285 * @tc.name: TestSysEventService
286 * @tc.desc: SysEventServiceProxy/Stub test
287 * @tc.type: FUNC
288 * @tc.require: issueI62WJT
289 */
290 HWTEST_F(HiSysEventAdapterNativeTest, TestSysEventService, TestSize.Level1)
291 {
292 SysEventServiceStub* sysEventServiceStub = new(std::nothrow) SysEventServiceStubTest();
293 MessageParcel data, reply;
294 MessageOption option;
295 sysEventServiceStub->OnRemoteRequest(SysEventServiceStubTest::Code::DEFAULT, data, reply, option);
296 ASSERT_TRUE(true);
297 sysEventServiceStub->OnRemoteRequest(SysEventServiceStubTest::Code::ADD_SYS_EVENT_LISTENER, data, reply, option);
298 ASSERT_TRUE(true);
299 sysEventServiceStub->OnRemoteRequest(SysEventServiceStubTest::Code::REMOVE_SYS_EVENT_LISTENER, data, reply,
300 option);
301 ASSERT_TRUE(true);
302 sysEventServiceStub->OnRemoteRequest(SysEventServiceStubTest::Code::QUERY_SYS_EVENT, data, reply, option);
303 ASSERT_TRUE(true);
304 sysEventServiceStub->OnRemoteRequest(SysEventServiceStubTest::Code::SET_DEBUG_MODE, data, reply, option);
305 ASSERT_TRUE(true);
306 const sptr<IRemoteObject>& impl(sysEventServiceStub);
307 SysEventServiceProxy proxy(impl);
308 OHOS::HiviewDFX::SysEventRule sysEventRule("DOMAIN", "EVENT_NAME", "TAG", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
309 std::vector<OHOS::HiviewDFX::SysEventRule> sysRules;
310 sysRules.emplace_back(sysEventRule);
311 const sptr<SysEventCallbackStub>& watcher(new(std::nothrow) SysEventCallbackStubTest);
312 auto ret = proxy.AddListener(sysRules, watcher);
313 ASSERT_TRUE(ret == 0);
314 ret = proxy.SetDebugMode(watcher, true);
315 ASSERT_TRUE(ret == 0);
316 ret = proxy.RemoveListener(watcher);
317 ASSERT_TRUE(ret == 0);
318 const sptr<QuerySysEventCallbackStub>& querier(new(std::nothrow) QuerySysEventCallbackStubTest);
319 long long defaultTimeStap = -1;
320 int queryCount = 10;
321 OHOS::HiviewDFX::QueryArgument args(defaultTimeStap, defaultTimeStap, queryCount);
322 std::vector<OHOS::HiviewDFX::SysEventQueryRule> queryRules;
323 std::vector<std::string> eventNames { "EVENT_NAME1", "EVENT_NAME2" };
324 OHOS::HiviewDFX::SysEventQueryRule queryRule("DOMAIN", eventNames);
325 queryRules.emplace_back(queryRule);
326 ret = proxy.Query(args, queryRules, querier);
327 ASSERT_TRUE(ret == 0);
328 }
329
330 /**
331 * @tc.name: MarshallingTAndUnmarshallingTest
332 * @tc.desc: Unmarshalling test
333 * @tc.type: FUNC
334 * @tc.require: issueI62WJT
335 */
336 HWTEST_F(HiSysEventAdapterNativeTest, MarshallingTAndUnmarshallingTest, TestSize.Level1)
337 {
338 long long defaultTimeStap = -1;
339 int queryCount = 10;
340 OHOS::HiviewDFX::QueryArgument args(defaultTimeStap, defaultTimeStap, queryCount);
341 MessageParcel parcel1;
342 auto ret = args.Marshalling(parcel1);
343 ASSERT_TRUE(ret);
344 QueryArgument* argsPtr = args.Unmarshalling(parcel1);
345 ASSERT_TRUE(argsPtr != nullptr && argsPtr->maxEvents == 10 && argsPtr->beginTime == -1);
346 OHOS::HiviewDFX::SysEventRule sysEventRule("DOMAIN", "EVENT_NAME", "TAG", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
347 MessageParcel parcel2;
348 ret = sysEventRule.Marshalling(parcel2);
349 ASSERT_TRUE(ret);
350 OHOS::HiviewDFX::SysEventRule* sysEventRulePtr = sysEventRule.Unmarshalling(parcel2);
351 ASSERT_TRUE(sysEventRulePtr != nullptr && sysEventRulePtr->domain == "DOMAIN" &&
352 sysEventRulePtr->eventName == "EVENT_NAME" && sysEventRulePtr->tag == "TAG");
353
354 std::vector<std::string> eventNames { "EVENT_NAME1", "EVENT_NAME2" };
355 OHOS::HiviewDFX::SysEventQueryRule queryRule("DOMAIN", eventNames);
356 MessageParcel parcel3;
357 ret = queryRule.Marshalling(parcel3);
358 ASSERT_TRUE(ret);
359 OHOS::HiviewDFX::SysEventQueryRule* queryRulePtr = queryRule.Unmarshalling(parcel3);
360 ASSERT_TRUE(queryRulePtr != nullptr && queryRulePtr->domain == "DOMAIN" &&
361 queryRulePtr->eventList.size() == 2 && queryRulePtr->eventList[0] == "EVENT_NAME1");
362 }
363
364 /**
365 * @tc.name: CStringUtilTest
366 * @tc.desc: Test methods which defined in namespace StringUtil
367 * @tc.type: FUNC
368 * @tc.require: issueI62WJT
369 */
370 HWTEST_F(HiSysEventAdapterNativeTest, CStringUtilTest, TestSize.Level1)
371 {
372 char dest[100] {};
373 std::string src = "01234567";
374 auto ret = StringUtil::CopyCString(dest, src, 3);
375 ASSERT_TRUE(ret == -1);
376 ret = StringUtil::CopyCString(dest, src, 10);
377 ASSERT_TRUE(ret != -1);
378 char* dest2p = dest;
379 char** dest2pp = &dest2p;
380 ret = StringUtil::CreateCString(dest2pp, src, 3);
381 ASSERT_TRUE(ret == -1);
382 ret = StringUtil::CreateCString(dest2pp, src, 10);
383 ASSERT_TRUE(ret != -1);
384 ret = StringUtil::ConvertCString(src, dest2pp, 3);
385 ASSERT_TRUE(ret == -1);
386 ret = StringUtil::ConvertCString(src, dest2pp, 10);
387 ASSERT_TRUE(ret != -1);
388 char v3[10][100] {};
389 char* dest3p = v3[0];
390 char** dest3pp = &dest3p;
391 char*** dest3ppp = &dest3pp;
392 std::vector<std::string> srcs1 = {};
393 std::vector<std::string> srcs2 = {
394 "01234567",
395 "01234567",
396 };
397 size_t len;
398 ret = StringUtil::ConvertCStringVec(srcs1, dest3ppp, len);
399 ASSERT_TRUE(ret == 0 && len == 0);
400 ret = StringUtil::ConvertCStringVec(srcs2, dest3ppp, len);
401 ASSERT_TRUE(ret == 0 && len == 2);
402 char dest4[3] = {'0', '1', '2'};
403 StringUtil::MemsetSafe(reinterpret_cast<void*>(dest4), 3);
404 ASSERT_TRUE(dest4[0] == 0 && dest4[1] == 0 && dest4[2] == 0);
405 }
406
407 /**
408 * @tc.name: HiSysEventListenerProxyTest
409 * @tc.desc: Test apis of HiSysEventListenerProxy
410 * @tc.type: FUNC
411 * @tc.require: issueI62WJT
412 */
413 HWTEST_F(HiSysEventAdapterNativeTest, HiSysEventListenerProxyTest, TestSize.Level1)
414 {
415 auto baseListener = std::make_shared<HiSysEventBaseListener>();
416 HiSysEventListenerProxy proxy(baseListener);
417 proxy.Handle(Str8ToStr16(std::string("DOMAIN")), Str8ToStr16(std::string("EVENT_NAME")), 0,
418 Str8ToStr16(std::string("{}")));
419 auto listener = proxy.GetEventListener();
420 ASSERT_TRUE(listener != nullptr);
421 auto deathRecipient = proxy.GetCallbackDeathRecipient();
422 ASSERT_TRUE(deathRecipient != nullptr);
423 if (deathRecipient != nullptr) {
424 deathRecipient->OnRemoteDied(nullptr);
425 ASSERT_TRUE(deathRecipient->GetEventListener() != nullptr);
426 }
427 }
428
429 /**
430 * @tc.name: HiSysEventQueryProxyTest
431 * @tc.desc: Test apis of HiSysEventQueryProxy
432 * @tc.type: FUNC
433 * @tc.require: issueI62WJT
434 */
435 HWTEST_F(HiSysEventAdapterNativeTest, HiSysEventQueryProxyTest, TestSize.Level1)
436 {
437 auto baseQuerier = std::make_shared<HiSysEventBaseQueryCallback>();
438 HiSysEventQueryProxy proxy(baseQuerier);
439 std::vector<std::u16string> sysEvent {};
440 std::vector<int64_t> seq {};
441 proxy.OnQuery(sysEvent, seq);
442 ASSERT_TRUE(true);
443 proxy.OnComplete(0, 0, 0);
444 ASSERT_TRUE(true);
445 }