• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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_TRUE(ret != nullptr);
148     std::vector<std::u16string> dest;
149     auto ret1 = AshMemUtils::ReadBulkData(data, dest);
150     ASSERT_TRUE(ret1);
151     ASSERT_TRUE(src.size() == dest.size());
152     ASSERT_TRUE(Str16ToStr8(dest[0]) == "0" && Str16ToStr8(dest[1]) == "1");
153     AshMemUtils::CloseAshmem(nullptr);
154     ASSERT_TRUE(true);
155     AshMemUtils::CloseAshmem(GetAshmem());
156     ASSERT_TRUE(true);
157 }
158 
159 /**
160  * @tc.name: TestHiSysEventDelegateApisWithInvalidInstance
161  * @tc.desc: Call Add/Removelistener/SetDebugMode with a HiSysEventDelegate instance directly
162  * @tc.type: FUNC
163  * @tc.require: issueI62BDW
164  */
165 HWTEST_F(HiSysEventAdapterNativeTest, TestHiSysEventDelegateApisWithInvalidInstance, TestSize.Level1)
166 {
167     std::shared_ptr<OHOS::HiviewDFX::HiSysEventDelegate> delegate =
168         std::make_shared<OHOS::HiviewDFX::HiSysEventDelegate>();
__anon3f66a3760202() 169     std::thread t([delegate] () {
170         delegate->BinderFunc();
171     });
172     t.detach();
173     auto ret = delegate->RemoveListener(nullptr);
174     ASSERT_TRUE(ret == ERR_LISTENER_NOT_EXIST);
175     ret = delegate->SetDebugMode(nullptr, true);
176     ASSERT_TRUE(ret == ERR_LISTENER_NOT_EXIST);
177     auto listener = std::make_shared<HiSysEventBaseListener>();
178     std::vector<ListenerRule> rules;
179     ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "TAG", RuleType::WHOLE_WORD);
180     rules.emplace_back(listenerRule);
181     ret = delegate->AddListener(listener, rules);
182     ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
183     ret = delegate->SetDebugMode(listener, true);
184     if (ret == IPC_CALL_SUCCEED) {
185         ret = delegate->SetDebugMode(listener, false);
186         ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
187     }
188     ret = delegate->RemoveListener(listener);
189     ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
190     long long defaultTimeStap = -1;
191     int queryCount = 10;
192     struct QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
193     std::vector<QueryRule> queryRules;
194     std::vector<std::string> eventNames {"START_ABILITY"};
195     QueryRule rule("AAFWK", eventNames);
196     queryRules.emplace_back(rule);
197     auto baseQuerier = std::make_shared<HiSysEventBaseQueryCallback>();
198     ret = delegate->Query(args, queryRules, baseQuerier);
199     ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
200     int64_t currentTime = static_cast<int64_t>(GetMilliseconds());
201     auto result = delegate->Subscribe(queryRules);
202     ASSERT_TRUE(std::to_string(result).length() >= std::to_string(currentTime).length());
203     ret = delegate->Unsubscribe();
204     ASSERT_TRUE(ret == 0);
205 }
206 
207 /**
208  * @tc.name: TestQuerySysEventCallback
209  * @tc.desc: QuerySysEventCallbackStub test
210  * @tc.type: FUNC
211  * @tc.require: issueI62WJT
212  */
213 HWTEST_F(HiSysEventAdapterNativeTest, TestQuerySysEventCallback, TestSize.Level1)
214 {
215     QuerySysEventCallbackStub* querySysEventCallbackStub = new(std::nothrow) QuerySysEventCallbackStubTest();
216     MessageParcel data, reply;
217     MessageOption option;
218     querySysEventCallbackStub->OnRemoteRequest(QuerySysEventCallbackStubTest::Code::DEFAULT, data, reply, option);
219     ASSERT_TRUE(true);
220     querySysEventCallbackStub->OnRemoteRequest(QuerySysEventCallbackStubTest::Code::ON_QUERY, data, reply, option);
221     ASSERT_TRUE(true);
222     querySysEventCallbackStub->OnRemoteRequest(QuerySysEventCallbackStubTest::Code::ON_COMPLETE, data, reply, option);
223     ASSERT_TRUE(true);
224 }
225 
226 /**
227  * @tc.name: TestSysEventCallback
228  * @tc.desc: SysEventCallbackStub test
229  * @tc.type: FUNC
230  * @tc.require: issueI62WJT
231  */
232 HWTEST_F(HiSysEventAdapterNativeTest, TestSysEventCallback, TestSize.Level1)
233 {
234     SysEventCallbackStub* sysEventCallbackStub = new(std::nothrow) SysEventCallbackStubTest();
235     MessageParcel data, reply;
236     MessageOption option;
237     sysEventCallbackStub->OnRemoteRequest(SysEventCallbackStubTest::Code::DEFAULT, data, reply, option);
238     ASSERT_TRUE(true);
239     sysEventCallbackStub->OnRemoteRequest(SysEventCallbackStubTest::Code::HANDLE, data, reply, option);
240     ASSERT_TRUE(true);
241 }
242 
243 /**
244  * @tc.name: FileUtilOhosTest001
245  * @tc.desc: FileUtil test
246  * @tc.type: FUNC
247  * @tc.require: SR000I1G43
248  */
249 HWTEST_F(HiSysEventAdapterNativeTest, FileUtilOhosTest001, TestSize.Level3)
250 {
251     auto ret = FileUtil::ForceCreateDirectory(LOG_DIR_PATH);
252     ASSERT_TRUE(ret);
253     ret = FileUtil::IsDirectory(LOG_DIR_PATH);
254     ASSERT_TRUE(ret);
255     ret = FileUtil::RemoveDirectory(LOG_DIR_PATH);
256     ASSERT_TRUE(ret);
257     ret = FileUtil::IsDirectory(LOG_DIR_PATH);
258     ASSERT_TRUE(!ret);
259     auto result = FileUtil::GetFilePathByDir(LOG_DIR_PATH, "test.log");
260     ASSERT_TRUE(result == FILE_PATH);
261 }
262 
263 /**
264  * @tc.name: FileUtilOhosTest002
265  * @tc.desc: FileUtil test
266  * @tc.type: FUNC
267  * @tc.require: SR000I1G43
268  */
269 HWTEST_F(HiSysEventAdapterNativeTest, FileUtilOhosTest002, TestSize.Level3)
270 {
271     auto ret = FileUtil::IsLegalPath("aa/../bb");
272     ASSERT_TRUE(!ret);
273     ret = FileUtil::IsLegalPath("aa/./bb");
274     ASSERT_TRUE(!ret);
275     ret = FileUtil::IsLegalPath("aa/bb/");
276     ASSERT_TRUE(ret);
277     ret = FileUtil::IsLegalPath("aa/bb/cc");
278     ASSERT_TRUE(ret);
279 }
280 
281 /**
282  * @tc.name: MarshallingTAndUnmarshallingTest
283  * @tc.desc: Unmarshalling test
284  * @tc.type: FUNC
285  * @tc.require: issueI62WJT
286  */
287 HWTEST_F(HiSysEventAdapterNativeTest, MarshallingTAndUnmarshallingTest, TestSize.Level1)
288 {
289     long long defaultTimeStap = -1;
290     int queryCount = 10;
291     OHOS::HiviewDFX::QueryArgument args(defaultTimeStap, defaultTimeStap, queryCount);
292     MessageParcel parcel1;
293     auto ret = args.Marshalling(parcel1);
294     ASSERT_TRUE(ret);
295     QueryArgument* argsPtr = args.Unmarshalling(parcel1);
296     ASSERT_TRUE(argsPtr != nullptr && argsPtr->maxEvents == 10 && argsPtr->beginTime == -1);
297     OHOS::HiviewDFX::SysEventRule sysEventRule("DOMAIN", "EVENT_NAME", "TAG", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
298     MessageParcel parcel2;
299     ret = sysEventRule.Marshalling(parcel2);
300     ASSERT_TRUE(ret);
301     OHOS::HiviewDFX::SysEventRule* sysEventRulePtr = sysEventRule.Unmarshalling(parcel2);
302     ASSERT_TRUE(sysEventRulePtr != nullptr && sysEventRulePtr->domain == "DOMAIN" &&
303         sysEventRulePtr->eventName == "EVENT_NAME" && sysEventRulePtr->tag == "TAG");
304 
305     std::vector<std::string> eventNames { "EVENT_NAME1", "EVENT_NAME2" };
306     OHOS::HiviewDFX::SysEventQueryRule queryRule("DOMAIN", eventNames);
307     MessageParcel parcel3;
308     ret = queryRule.Marshalling(parcel3);
309     ASSERT_TRUE(ret);
310     OHOS::HiviewDFX::SysEventQueryRule* queryRulePtr = queryRule.Unmarshalling(parcel3);
311     ASSERT_TRUE(queryRulePtr != nullptr && queryRulePtr->domain == "DOMAIN" &&
312         queryRulePtr->eventList.size() == 2 && queryRulePtr->eventList[0] == "EVENT_NAME1");
313 }
314 
315 /**
316  * @tc.name: CStringUtilTest
317  * @tc.desc: Test methods which defined in namespace StringUtil
318  * @tc.type: FUNC
319  * @tc.require: issueI62WJT
320  */
321 HWTEST_F(HiSysEventAdapterNativeTest, CStringUtilTest, TestSize.Level1)
322 {
323     char dest[100] {};
324     std::string src = "01234567";
325     auto ret = StringUtil::CopyCString(dest, src, 3);
326     ASSERT_TRUE(ret == -1);
327     ret = StringUtil::CopyCString(dest, src, 10);
328     ASSERT_TRUE(ret != -1);
329     char* dest2p = dest;
330     char** dest2pp = &dest2p;
331     ret = StringUtil::CreateCString(dest2pp, src, 3);
332     ASSERT_TRUE(ret == -1);
333     ret = StringUtil::CreateCString(dest2pp, src, 10);
334     ASSERT_TRUE(ret != -1);
335     ret = StringUtil::ConvertCString(src, dest2pp, 3);
336     ASSERT_TRUE(ret == -1);
337     ret = StringUtil::ConvertCString(src, dest2pp, 10);
338     ASSERT_TRUE(ret != -1);
339     char v3[10][100] {};
340     char* dest3p = v3[0];
341     char** dest3pp = &dest3p;
342     char*** dest3ppp = &dest3pp;
343     std::vector<std::string> srcs1 = {};
344     std::vector<std::string> srcs2 = {
345         "01234567",
346         "01234567",
347     };
348     size_t len;
349     ret = StringUtil::ConvertCStringVec(srcs1, dest3ppp, len);
350     ASSERT_TRUE(ret == 0 && len == 0);
351     ret = StringUtil::ConvertCStringVec(srcs2, dest3ppp, len);
352     ASSERT_TRUE(ret == 0 && len == 2);
353     char dest4[3] = {'0', '1', '2'};
354     StringUtil::MemsetSafe(reinterpret_cast<void*>(dest4), 3);
355     ASSERT_TRUE(dest4[0] == 0 && dest4[1] == 0 && dest4[2] == 0);
356 }
357 
358 /**
359  * @tc.name: HiSysEventListenerProxyTest
360  * @tc.desc: Test apis of HiSysEventListenerProxy
361  * @tc.type: FUNC
362  * @tc.require: issueI62WJT
363  */
364 HWTEST_F(HiSysEventAdapterNativeTest, HiSysEventListenerProxyTest, TestSize.Level1)
365 {
366     auto baseListener = std::make_shared<HiSysEventBaseListener>();
367     HiSysEventListenerProxy proxy(baseListener);
368     proxy.Handle(Str8ToStr16(std::string("DOMAIN")), Str8ToStr16(std::string("EVENT_NAME")), 0,
369         Str8ToStr16(std::string("{}")));
370     auto listener = proxy.GetEventListener();
371     ASSERT_TRUE(listener != nullptr);
372     auto deathRecipient = proxy.GetCallbackDeathRecipient();
373     ASSERT_TRUE(deathRecipient != nullptr);
374     if (deathRecipient != nullptr) {
375         deathRecipient->OnRemoteDied(nullptr);
376         ASSERT_TRUE(deathRecipient->GetEventListener() != nullptr);
377     }
378 }
379 
380 /**
381  * @tc.name: HiSysEventQueryProxyTest
382  * @tc.desc: Test apis of HiSysEventQueryProxy
383  * @tc.type: FUNC
384  * @tc.require: issueI62WJT
385  */
386 HWTEST_F(HiSysEventAdapterNativeTest, HiSysEventQueryProxyTest, TestSize.Level1)
387 {
388     auto baseQuerier = std::make_shared<HiSysEventBaseQueryCallback>();
389     HiSysEventQueryProxy proxy(baseQuerier);
390     std::vector<std::u16string> sysEvent {};
391     std::vector<int64_t> seq {};
392     proxy.OnQuery(sysEvent, seq);
393     ASSERT_TRUE(true);
394     proxy.OnComplete(0, 0, 0);
395     ASSERT_TRUE(true);
396 }