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 
48 using namespace testing::ext;
49 using namespace OHOS;
50 using namespace OHOS::HiviewDFX;
51 
52 namespace {
53 constexpr char ASH_MEM_NAME[] = "TestSharedMemory";
54 constexpr int32_t ASH_MEM_SIZE = 1024 * 2; // 2K
55 constexpr char LOG_DIR_PATH[] = "/data/test/adapter_native_test";
56 constexpr char FILE_PATH[] = "/data/test/adapter_native_test/test.log";
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 
GetMilliseconds()70 uint64_t GetMilliseconds()
71 {
72     auto now = std::chrono::system_clock::now();
73     auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
74     return millisecs.count();
75 }
76 
77 class QuerySysEventCallbackStubTest : public QuerySysEventCallbackStub {
78 public:
QuerySysEventCallbackStubTest()79     QuerySysEventCallbackStubTest() {}
~QuerySysEventCallbackStubTest()80     virtual ~QuerySysEventCallbackStubTest() {}
81 
OnQuery(const std::vector<std::u16string> & sysEvent,const std::vector<int64_t> & seq)82     void OnQuery(const std::vector<std::u16string>& sysEvent, const std::vector<int64_t>& seq) {}
OnComplete(int32_t reason,int32_t total,int64_t seq)83     void OnComplete(int32_t reason, int32_t total, int64_t seq) {}
84 
85 public:
86     enum Code {
87         DEFAULT = -1,
88         ON_QUERY = 0,
89         ON_COMPLETE,
90     };
91 };
92 
93 class SysEventCallbackStubTest : public SysEventCallbackStub {
94 public:
SysEventCallbackStubTest()95     SysEventCallbackStubTest() {}
~SysEventCallbackStubTest()96     virtual ~SysEventCallbackStubTest() {}
97 
Handle(const std::u16string & domain,const std::u16string & eventName,uint32_t eventType,const std::u16string & eventDetail)98     void Handle(const std::u16string& domain, const std::u16string& eventName, uint32_t eventType,
99         const std::u16string& eventDetail) {}
100 
101 public:
102     enum Code {
103         DEFAULT = -1,
104         HANDLE = 0,
105     };
106 };
107 }
108 
109 class HiSysEventAdapterNativeTest : public testing::Test {
110 public:
111     static void SetUpTestCase(void);
112     static void TearDownTestCase(void);
113     void SetUp();
114     void TearDown();
115 };
116 
SetUpTestCase(void)117 void HiSysEventAdapterNativeTest::SetUpTestCase(void)
118 {
119 }
120 
TearDownTestCase(void)121 void HiSysEventAdapterNativeTest::TearDownTestCase(void)
122 {
123 }
124 
SetUp(void)125 void HiSysEventAdapterNativeTest::SetUp(void)
126 {
127 }
128 
TearDown(void)129 void HiSysEventAdapterNativeTest::TearDown(void)
130 {
131 }
132 
133 /**
134  * @tc.name: TestAshMemory
135  * @tc.desc: Ashmemory test
136  * @tc.type: FUNC
137  * @tc.require: issueI62BDW
138  */
139 HWTEST_F(HiSysEventAdapterNativeTest, TestAshMemory, TestSize.Level1)
140 {
141     MessageParcel data;
142     std::vector<std::u16string> src = {
143         Str8ToStr16(std::string("0")),
144         Str8ToStr16(std::string("1")),
145     };
146     auto ret = AshMemUtils::WriteBulkData(data, src);
147     ASSERT_NE(ret, nullptr);
148     std::vector<std::u16string> dest;
149     auto ret1 = AshMemUtils::ReadBulkData(data, dest);
150     ASSERT_TRUE(ret1);
151     ASSERT_EQ(src.size(), dest.size());
152     ASSERT_EQ(Str16ToStr8(dest[0]), "0");
153     ASSERT_EQ(Str16ToStr8(dest[1]), "1");
154     AshMemUtils::CloseAshmem(nullptr);
155     ASSERT_TRUE(true);
156     AshMemUtils::CloseAshmem(GetAshmem());
157     ASSERT_TRUE(true);
158 }
159 
160 /**
161  * @tc.name: TestHiSysEventDelegateApisWithInvalidInstance
162  * @tc.desc: Call Add/Removelistener/SetDebugMode with a HiSysEventDelegate instance directly
163  * @tc.type: FUNC
164  * @tc.require: issueI62BDW
165  */
166 HWTEST_F(HiSysEventAdapterNativeTest, TestHiSysEventDelegateApisWithInvalidInstance, TestSize.Level1)
167 {
168     std::shared_ptr<OHOS::HiviewDFX::HiSysEventDelegate> delegate =
169         std::make_shared<OHOS::HiviewDFX::HiSysEventDelegate>();
__anon25687ecd0202() 170     std::thread t([delegate] () {
171         delegate->BinderFunc();
172     });
173     t.detach();
174     auto ret = delegate->RemoveListener(nullptr);
175     ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
176     ret = delegate->SetDebugMode(nullptr, true);
177     ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
178     auto listener = std::make_shared<HiSysEventBaseListener>();
179     std::vector<ListenerRule> rules;
180     ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "TAG", RuleType::WHOLE_WORD);
181     rules.emplace_back(listenerRule);
182     ret = delegate->AddListener(listener, rules);
183     ASSERT_EQ(ret, IPC_CALL_SUCCEED);
184     ret = delegate->SetDebugMode(listener, true);
185     if (ret == IPC_CALL_SUCCEED) {
186         ret = delegate->SetDebugMode(listener, false);
187         ASSERT_EQ(ret, IPC_CALL_SUCCEED);
188     }
189     ret = delegate->RemoveListener(listener);
190     ASSERT_EQ(ret, IPC_CALL_SUCCEED);
191     long long defaultTimeStap = -1;
192     int queryCount = 10;
193     struct QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
194     std::vector<QueryRule> queryRules;
195     std::vector<std::string> eventNames {"START_ABILITY"};
196     QueryRule rule("AAFWK", eventNames);
197     queryRules.emplace_back(rule);
198     auto baseQuerier = std::make_shared<HiSysEventBaseQueryCallback>();
199     ret = delegate->Query(args, queryRules, baseQuerier);
200     ASSERT_EQ(ret, IPC_CALL_SUCCEED);
201     int64_t currentTime = static_cast<int64_t>(GetMilliseconds());
202     auto result = delegate->Subscribe(queryRules);
203     ASSERT_GE(std::to_string(result).length(), std::to_string(currentTime).length());
204     ret = delegate->Unsubscribe();
205     ASSERT_EQ(ret, 0);
206 }
207 
208 /**
209  * @tc.name: TestQuerySysEventCallback
210  * @tc.desc: QuerySysEventCallbackStub test
211  * @tc.type: FUNC
212  * @tc.require: issueI62WJT
213  */
214 HWTEST_F(HiSysEventAdapterNativeTest, TestQuerySysEventCallback, TestSize.Level1)
215 {
216     QuerySysEventCallbackStub* querySysEventCallbackStub = new(std::nothrow) QuerySysEventCallbackStubTest();
217     MessageParcel data, reply;
218     MessageOption option;
219     querySysEventCallbackStub->OnRemoteRequest(QuerySysEventCallbackStubTest::Code::DEFAULT, data, reply, option);
220     ASSERT_TRUE(true);
221     querySysEventCallbackStub->OnRemoteRequest(QuerySysEventCallbackStubTest::Code::ON_QUERY, data, reply, option);
222     ASSERT_TRUE(true);
223     querySysEventCallbackStub->OnRemoteRequest(QuerySysEventCallbackStubTest::Code::ON_COMPLETE, data, reply, option);
224     ASSERT_TRUE(true);
225 }
226 
227 /**
228  * @tc.name: TestSysEventCallback
229  * @tc.desc: SysEventCallbackStub test
230  * @tc.type: FUNC
231  * @tc.require: issueI62WJT
232  */
233 HWTEST_F(HiSysEventAdapterNativeTest, TestSysEventCallback, TestSize.Level1)
234 {
235     SysEventCallbackStub* sysEventCallbackStub = new(std::nothrow) SysEventCallbackStubTest();
236     MessageParcel data, reply;
237     MessageOption option;
238     sysEventCallbackStub->OnRemoteRequest(SysEventCallbackStubTest::Code::DEFAULT, data, reply, option);
239     ASSERT_TRUE(true);
240     sysEventCallbackStub->OnRemoteRequest(SysEventCallbackStubTest::Code::HANDLE, data, reply, option);
241     ASSERT_TRUE(true);
242 }
243 
244 /**
245  * @tc.name: FileUtilOhosTest001
246  * @tc.desc: FileUtil test
247  * @tc.type: FUNC
248  * @tc.require: SR000I1G43
249  */
250 HWTEST_F(HiSysEventAdapterNativeTest, FileUtilOhosTest001, TestSize.Level3)
251 {
252     auto ret = FileUtil::ForceCreateDirectory(LOG_DIR_PATH);
253     ASSERT_TRUE(ret);
254     ret = FileUtil::IsDirectory(LOG_DIR_PATH);
255     ASSERT_TRUE(ret);
256     ret = FileUtil::RemoveDirectory(LOG_DIR_PATH);
257     ASSERT_TRUE(ret);
258     ret = FileUtil::IsDirectory(LOG_DIR_PATH);
259     ASSERT_TRUE(!ret);
260     auto result = FileUtil::GetFilePathByDir(LOG_DIR_PATH, "test.log");
261     ASSERT_EQ(result, FILE_PATH);
262 }
263 
264 /**
265  * @tc.name: FileUtilOhosTest002
266  * @tc.desc: FileUtil test
267  * @tc.type: FUNC
268  * @tc.require: SR000I1G43
269  */
270 HWTEST_F(HiSysEventAdapterNativeTest, FileUtilOhosTest002, TestSize.Level3)
271 {
272     auto ret = FileUtil::IsLegalPath("aa/../bb");
273     ASSERT_TRUE(!ret);
274     ret = FileUtil::IsLegalPath("aa/./bb");
275     ASSERT_TRUE(!ret);
276     ret = FileUtil::IsLegalPath("aa/bb/");
277     ASSERT_TRUE(ret);
278     ret = FileUtil::IsLegalPath("aa/bb/cc");
279     ASSERT_TRUE(ret);
280 }
281 
282 /**
283  * @tc.name: MarshallingTAndUnmarshallingTest
284  * @tc.desc: Unmarshalling test
285  * @tc.type: FUNC
286  * @tc.require: issueI62WJT
287  */
288 HWTEST_F(HiSysEventAdapterNativeTest, MarshallingTAndUnmarshallingTest, TestSize.Level1)
289 {
290     long long defaultTimeStap = -1;
291     int queryCount = 10;
292     OHOS::HiviewDFX::QueryArgument args(defaultTimeStap, defaultTimeStap, queryCount);
293     MessageParcel parcel1;
294     auto ret = args.Marshalling(parcel1);
295     ASSERT_TRUE(ret);
296     QueryArgument* argsPtr = args.Unmarshalling(parcel1);
297     ASSERT_NE(argsPtr, nullptr);
298     ASSERT_EQ(argsPtr->maxEvents, 10); // 10 is a expcted test value
299     ASSERT_EQ(argsPtr->beginTime, -1); // -1 is a expcted test value
300     OHOS::HiviewDFX::SysEventRule sysEventRule("DOMAIN", "EVENT_NAME", "TAG", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
301     MessageParcel parcel2;
302     ret = sysEventRule.Marshalling(parcel2);
303     ASSERT_TRUE(ret);
304     OHOS::HiviewDFX::SysEventRule* sysEventRulePtr = sysEventRule.Unmarshalling(parcel2);
305     ASSERT_NE(sysEventRulePtr, nullptr);
306     ASSERT_EQ(sysEventRulePtr->domain, "DOMAIN");
307     ASSERT_EQ(sysEventRulePtr->eventName, "EVENT_NAME");
308     ASSERT_EQ(sysEventRulePtr->tag, "TAG");
309     std::vector<std::string> eventNames { "EVENT_NAME1", "EVENT_NAME2" };
310     OHOS::HiviewDFX::SysEventQueryRule queryRule("DOMAIN", eventNames);
311     MessageParcel parcel3;
312     ret = queryRule.Marshalling(parcel3);
313     ASSERT_TRUE(ret);
314     OHOS::HiviewDFX::SysEventQueryRule* queryRulePtr = queryRule.Unmarshalling(parcel3);
315     ASSERT_NE(queryRulePtr, nullptr);
316     ASSERT_EQ(queryRulePtr->domain, "DOMAIN");;
317     ASSERT_EQ(queryRulePtr->eventList.size(), 2); // 2 is a expcted test value
318     ASSERT_EQ(queryRulePtr->eventList[0], "EVENT_NAME1");
319 }
320 
321 /**
322  * @tc.name: CStringUtilTest
323  * @tc.desc: Test methods which defined in namespace StringUtil
324  * @tc.type: FUNC
325  * @tc.require: issueI62WJT
326  */
327 HWTEST_F(HiSysEventAdapterNativeTest, CStringUtilTest, TestSize.Level1)
328 {
329     char dest[100] {}; // 100 is a test length
330     std::string src = "01234567";
331 
332     auto ret = StringUtil::CopyCString(dest, src, 3); // 3 is a test length
333     ASSERT_EQ(ret, -1); // -1 is a expected result
334     ret = StringUtil::CopyCString(dest, src, 10); // 3 is a test length
335     ASSERT_NE(ret, -1); // -1 is a expected result
336     char* dest2p = dest;
337     char** dest2pp = &dest2p;
338     ret = StringUtil::CreateCString(dest2pp, src, 3); // 3 is a test length
339     ASSERT_EQ(ret, -1); // -1 is a expected result
340     ret = StringUtil::CreateCString(dest2pp, src, 10); // 3 is a test length
341     ASSERT_NE(ret, -1); // -1 is a expected result
342     ret = StringUtil::ConvertCString(src, dest2pp, 3); // 3 is a test length
343     ASSERT_EQ(ret, -1); // -1 is a expected result
344     ret = StringUtil::ConvertCString(src, dest2pp, 10); // 3 is a test length
345     ASSERT_NE(ret, -1); // -1 is a expected result
346     char v3[10][100] {};
347     char* dest3p = v3[0];
348     char** dest3pp = &dest3p;
349     char*** dest3ppp = &dest3pp;
350     std::vector<std::string> srcs1 = {};
351     std::vector<std::string> srcs2 = {
352         "01234567",
353         "01234567",
354     };
355     size_t len;
356     ret = StringUtil::ConvertCStringVec(srcs1, dest3ppp, len);
357     ASSERT_EQ(ret, 0);
358     ASSERT_EQ(len, 0);
359     ret = StringUtil::ConvertCStringVec(srcs2, dest3ppp, len);
360     ASSERT_EQ(ret, 0);
361     ASSERT_EQ(len, 2); // 2 is a expected length
362     char dest4[3] = {'0', '1', '2'};
363     StringUtil::MemsetSafe(reinterpret_cast<void*>(dest4), 3); // 3 is a test length
364     ASSERT_EQ(dest4[0], 0);
365     ASSERT_EQ(dest4[1], 0); // 1 is a test index
366     ASSERT_EQ(dest4[2], 0); // 2 is a test index
367 }
368 
369 /**
370  * @tc.name: HiSysEventListenerProxyTest
371  * @tc.desc: Test apis of HiSysEventListenerProxy
372  * @tc.type: FUNC
373  * @tc.require: issueI62WJT
374  */
375 HWTEST_F(HiSysEventAdapterNativeTest, HiSysEventListenerProxyTest, TestSize.Level1)
376 {
377     auto baseListener = std::make_shared<HiSysEventBaseListener>();
378     HiSysEventListenerProxy proxy(baseListener);
379     proxy.Handle(Str8ToStr16(std::string("DOMAIN")), Str8ToStr16(std::string("EVENT_NAME")), 0,
380         Str8ToStr16(std::string("{}")));
381     auto listener = proxy.GetEventListener();
382     ASSERT_NE(listener, nullptr);
383     auto deathRecipient = proxy.GetCallbackDeathRecipient();
384     ASSERT_NE(deathRecipient, nullptr);
385     if (deathRecipient != nullptr) {
386         deathRecipient->OnRemoteDied(nullptr);
387         ASSERT_NE(deathRecipient->GetEventListener(), nullptr);
388     }
389 }
390 
391 /**
392  * @tc.name: HiSysEventQueryProxyTest
393  * @tc.desc: Test apis of HiSysEventQueryProxy
394  * @tc.type: FUNC
395  * @tc.require: issueI62WJT
396  */
397 HWTEST_F(HiSysEventAdapterNativeTest, HiSysEventQueryProxyTest, TestSize.Level1)
398 {
399     auto baseQuerier = std::make_shared<HiSysEventBaseQueryCallback>();
400     HiSysEventQueryProxy proxy(baseQuerier);
401     std::vector<std::u16string> sysEvent {};
402     std::vector<int64_t> seq {};
403     proxy.OnQuery(sysEvent, seq);
404     ASSERT_TRUE(true);
405     proxy.OnComplete(0, 0, 0);
406     ASSERT_TRUE(true);
407 }