1 /*
2  * Copyright (c) 2021-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 "hiappevent_native_test.h"
17 
18 #include <cstring>
19 #include <string>
20 #include <vector>
21 
22 #include "hiappevent/hiappevent.h"
23 #include "hiappevent_base.h"
24 #include "hiappevent_config.h"
25 #include "hiappevent_userinfo.h"
26 #include "ndk_app_event_processor_service.h"
27 #include "time_util.h"
28 
29 using namespace testing::ext;
30 using namespace OHOS::HiviewDFX;
31 
32 namespace {
33 const std::string TEST_STORAGE_PATH = "/data/test/hiappevent/";
34 const char* TEST_DOMAIN_NAME = "test_domain";
35 const char* TEST_EVENT_NAME = "test_event";
36 const char* TEST_EVENT_PARAM_KEY = "test_param_key";
37 const char* TEST_EVENT_PARAM = "{\"test_param_key\":1}";
38 constexpr int TEST_EVENT_PARAM_LENGTH = 20;
39 constexpr int TEST_EVENT_NUM = 2;
40 
41 const char* TEST_PROCESSOR_NAME = "test_processor";
42 constexpr int32_t TEST_UID = 200000 * 100;
43 
WriteEvent()44 static void WriteEvent()
45 {
46     ParamList list = OH_HiAppEvent_CreateParamList();
47     OH_HiAppEvent_AddInt16Param(list, TEST_EVENT_PARAM_KEY, 1);
48     OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
49 }
50 
OnReceive(const char * domain,const struct HiAppEvent_AppEventGroup * appEventGroups,uint32_t groupSize)51 static void OnReceive(const char* domain, const struct HiAppEvent_AppEventGroup* appEventGroups, uint32_t groupSize)
52 {
53     ASSERT_EQ(strcmp(domain, TEST_DOMAIN_NAME), 0);
54     ASSERT_EQ(groupSize, 1);
55     ASSERT_TRUE(appEventGroups);
56     ASSERT_EQ(appEventGroups[0].infoLen, 1);
57     ASSERT_TRUE(appEventGroups[0].appEventInfos);
58     ASSERT_EQ(strcmp(appEventGroups[0].appEventInfos[0].name, TEST_EVENT_NAME), 0);
59     ASSERT_EQ(strcmp(appEventGroups[0].appEventInfos[0].domain, TEST_DOMAIN_NAME), 0);
60     ASSERT_EQ(appEventGroups[0].appEventInfos[0].type, SECURITY);
61     ASSERT_EQ(strncmp(appEventGroups[0].appEventInfos[0].params, TEST_EVENT_PARAM, TEST_EVENT_PARAM_LENGTH), 0);
62 }
63 
OnTrigger(int32_t row,int32_t size)64 static void OnTrigger(int32_t row, int32_t size)
65 {
66     ASSERT_EQ(row, TEST_EVENT_NUM);
67     ASSERT_GT(size, 0);
68 }
69 
OnTake(const char * const * events,uint32_t eventLen)70 static void OnTake(const char* const *events, uint32_t eventLen)
71 {
72     ASSERT_TRUE(events != nullptr);
73     ASSERT_EQ(eventLen, TEST_EVENT_NUM);
74 }
75 
GetStorageFilePath()76 std::string GetStorageFilePath()
77 {
78     return "app_event_" + TimeUtil::GetDate() + ".log";
79 }
80 }
81 
SetUpTestCase()82 void HiAppEventNativeTest::SetUpTestCase()
83 {
84     // set app uid
85     setuid(TEST_UID);
86     HiAppEventConfig::GetInstance().SetStorageDir(TEST_STORAGE_PATH);
87 }
88 
89 /**
90  * @tc.name: HiAppEventNDKTest001
91  * @tc.desc: check the logging function
92  * @tc.type: FUNC
93  * @tc.require: AR000GIKMA
94  */
95 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest001, TestSize.Level0)
96 {
97     /**
98      * @tc.steps: step1. create a ParamList pointer.
99      * @tc.steps: step2. add params to the ParamList.
100      * @tc.steps: step3. write event to the file.
101      * @tc.steps: step4. check the result of logging.
102      * @tc.steps: step5. destroy the ParamList pointer.
103      */
104     ParamList list = OH_HiAppEvent_CreateParamList();
105     bool boolean = true;
106     OH_HiAppEvent_AddBoolParam(list, "bool_key", boolean);
107     bool booleans[] = {true, false, true};
108     OH_HiAppEvent_AddBoolArrayParam(list, "bool_arr_key", booleans, sizeof(booleans) / sizeof(booleans[0]));
109     int8_t num1 = 1;
110     OH_HiAppEvent_AddInt8Param(list, "int8_key", num1);
111     int8_t nums1[] = {1, INT8_MIN, INT8_MAX};
112     OH_HiAppEvent_AddInt8ArrayParam(list, "int8_arr_key", nums1, sizeof(nums1) / sizeof(nums1[0]));
113     int16_t num2 = 1;
114     OH_HiAppEvent_AddInt16Param(list, "int16_key", num2);
115     int16_t nums2[] = {1, INT16_MAX, INT16_MIN};
116     OH_HiAppEvent_AddInt16ArrayParam(list, "int16_arr_key", nums2, sizeof(nums2) / sizeof(nums2[0]));
117     int32_t num3 = 1;
118     OH_HiAppEvent_AddInt32Param(list, "int32_key", num3);
119     int32_t nums3[] = {1, INT32_MAX, INT32_MIN};
120     OH_HiAppEvent_AddInt32ArrayParam(list, "int32_arr_key", nums3, sizeof(nums3) / sizeof(nums3[0]));
121     int64_t num4 = 1;
122     OH_HiAppEvent_AddInt64Param(list, "int64_key", num4);
123     int64_t nums4[] = {1LL, INT64_MAX, INT64_MIN};
124     OH_HiAppEvent_AddInt64ArrayParam(list, "int64_arr_key", nums4, sizeof(nums4) / sizeof(nums4[0]));
125     float num5 = 465.1234;
126     OH_HiAppEvent_AddFloatParam(list, "float_key", num5);
127     float nums5[] = {123.22f, num5, 131312.46464f};
128     OH_HiAppEvent_AddFloatArrayParam(list, "float_arr_key", nums5, sizeof(nums5) / sizeof(nums5[0]));
129     double num6 = 465.1234;
130     OH_HiAppEvent_AddDoubleParam(list, "double_key", num6);
131     double nums6[] = {123.22, num6, 131312.46464};
132     OH_HiAppEvent_AddDoubleArrayParam(list, "double_arr_key", nums6, sizeof(nums6) / sizeof(nums6[0]));
133     char str1[] = "hello";
134     OH_HiAppEvent_AddStringParam(list, "str_key", str1);
135     char str2[] = "world";
136     char* strs[] = {str1, str2};
137     OH_HiAppEvent_AddStringArrayParam(list, "str_arr_key", strs, sizeof(strs) / sizeof(strs[0]));
138 
139     int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, BEHAVIOR, list);
140     OH_HiAppEvent_DestroyParamList(list);
141     ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
142 }
143 
144 /**
145  * @tc.name: HiAppEventNDKTest002
146  * @tc.desc: check the overwriting function of the same param name.
147  * @tc.type: FUNC
148  * @tc.require: AR000GIKMA
149  */
150 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest002, TestSize.Level0)
151 {
152     /**
153      * @tc.steps: step1. create a ParamList pointer.
154      * @tc.steps: step2. add params with the same name to the ParamList.
155      * @tc.steps: step3. write event to the file.
156      * @tc.steps: step4. check the result of logging.
157      * @tc.steps: step5. destroy the ParamList pointer.
158      */
159     ParamList list = OH_HiAppEvent_CreateParamList();
160     int8_t num1 = 1;
161     OH_HiAppEvent_AddInt8Param(list, "int_key", num1);
162     int8_t nums1[] = {1, INT8_MIN, INT8_MAX};
163     OH_HiAppEvent_AddInt8ArrayParam(list, "int8_arr_key", nums1, sizeof(nums1) / sizeof(nums1[0]));
164     int16_t num2 = 1;
165     OH_HiAppEvent_AddInt16Param(list, "int16_key", num2);
166     int16_t nums2[] = {1, INT16_MAX, INT16_MIN};
167     OH_HiAppEvent_AddInt16ArrayParam(list, "int16_key", nums2, sizeof(nums2) / sizeof(nums2[0]));
168 
169     int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, BEHAVIOR, list);
170     OH_HiAppEvent_DestroyParamList(list);
171     ASSERT_EQ(res, ErrorCode::ERROR_DUPLICATE_PARAM);
172 }
173 
174 /**
175  * @tc.name: HiAppEventNDKTest003
176  * @tc.desc: check the logging function when the input value is nullptr.
177  * @tc.type: FUNC
178  * @tc.require: AR000GIKMA
179  */
180 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest003, TestSize.Level0)
181 {
182     /**
183      * @tc.steps: step1. create a ParamList pointer.
184      * @tc.steps: step2. add params with the nullptr value to the ParamList.
185      * @tc.steps: step3. write event to the file.
186      * @tc.steps: step4. check the result of logging.
187      * @tc.steps: step5. destroy the ParamList pointer.
188      */
189     int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, FAULT, nullptr);
190     ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
191 
192     ParamList list = OH_HiAppEvent_CreateParamList();
193     bool boolean = true;
194     OH_HiAppEvent_AddBoolParam(list, nullptr, boolean);
195     OH_HiAppEvent_AddBoolArrayParam(list, "bool_arr_key", nullptr, 0);
196     int8_t num1 = 1;
197     OH_HiAppEvent_AddInt8Param(list, nullptr, num1);
198     OH_HiAppEvent_AddInt8ArrayParam(list, "int8_arr_key", nullptr, 0);
199     int16_t num2 = 1;
200     OH_HiAppEvent_AddInt16Param(list, nullptr, num2);
201     OH_HiAppEvent_AddInt16ArrayParam(list, "int16_arr_key", nullptr, 1);
202     int32_t num3 = 1;
203     OH_HiAppEvent_AddInt32Param(list, nullptr, num3);
204     OH_HiAppEvent_AddInt32ArrayParam(list, "int32_arr_key", nullptr, 2);
205     int64_t num4 = 1;
206     OH_HiAppEvent_AddInt64Param(list, nullptr, num4);
207     OH_HiAppEvent_AddInt64ArrayParam(list, "int64_arr_key", nullptr, 3);
208     float num5 = 465.1234;
209     OH_HiAppEvent_AddFloatParam(list, nullptr, num5);
210     OH_HiAppEvent_AddFloatArrayParam(list, "float_arr_key", nullptr, -1);
211     double num6 = 465.1234;
212     OH_HiAppEvent_AddDoubleParam(list, nullptr, num6);
213     OH_HiAppEvent_AddDoubleArrayParam(list, "double_arr_key", nullptr, 0);
214     char str1[] = "hello";
215     OH_HiAppEvent_AddStringParam(list, nullptr, str1);
216     OH_HiAppEvent_AddStringParam(list, nullptr, nullptr);
217     OH_HiAppEvent_AddStringArrayParam(list, "str_arr_key", nullptr, 0);
218     char* strs[] = {str1, nullptr};
219     OH_HiAppEvent_AddStringArrayParam(list, "str_arr_null_key", strs, sizeof(strs) / sizeof(strs[0]));
220 
221     res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, STATISTIC, list);
222     OH_HiAppEvent_DestroyParamList(list);
223     ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
224 }
225 
226 /**
227  * @tc.name: HiAppEventNDKTest004
228  * @tc.desc: check the verification function of event logging.
229  * @tc.type: FUNC
230  * @tc.require: AR000GIKMA
231  */
232 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest004, TestSize.Level0)
233 {
234     /**
235      * @tc.steps: step1. create a ParamList pointer.
236      * @tc.steps: step2. add params with the invalid name to the ParamList.
237      * @tc.steps: step3. write event to the file.
238      * @tc.steps: step4. check the result of logging.
239      * @tc.steps: step5. destroy the ParamList pointer.
240      */
241     ParamList list = OH_HiAppEvent_CreateParamList();
242     char key1[] = "**";
243     int8_t num1 = 1;
244     OH_HiAppEvent_AddInt8Param(list, key1, num1);
245     char key2[] = "HH22";
246     int16_t num2 = 1;
247     OH_HiAppEvent_AddInt16Param(list, key2, num2);
248     char key3[] = "aa_";
249     int32_t num3 = 1;
250     OH_HiAppEvent_AddInt32Param(list, key3, num3);
251     char key4[] = "";
252     int64_t num4 = 1;
253     OH_HiAppEvent_AddInt64Param(list, key4, num4);
254 
255     int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
256     OH_HiAppEvent_DestroyParamList(list);
257     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_PARAM_NAME);
258 }
259 
260 /**
261  * @tc.name: HiAppEventNDKTest005
262  * @tc.desc: check the verification function of event logging.
263  * @tc.type: FUNC
264  * @tc.require: AR000GIKMA
265  */
266 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest005, TestSize.Level0)
267 {
268     /**
269      * @tc.steps: step1. create a ParamList pointer.
270      * @tc.steps: step2. add params with too long string length to the ParamList.
271      * @tc.steps: step3. write event to the file.
272      * @tc.steps: step4. check the result of logging.
273      * @tc.steps: step5. destroy the ParamList pointer.
274      */
275     int maxStrLen = 8 * 1024;
276     std::string longStr(maxStrLen, 'a');
277     std::string longInvalidStr(maxStrLen + 1, 'a');
278     const char* strs[] = {"hello", longStr.c_str()};
279     const char* strIns[] = {"hello", longInvalidStr.c_str()};
280 
281     ParamList list = OH_HiAppEvent_CreateParamList();
282     OH_HiAppEvent_AddStringParam(list, "long_s_key", longStr.c_str());
283     OH_HiAppEvent_AddStringArrayParam(list, "long_s_a_key", strs, sizeof(strs) / sizeof(strs[0]));
284     OH_HiAppEvent_AddStringParam(list, "long_s_i_key", longInvalidStr.c_str());
285     OH_HiAppEvent_AddStringArrayParam(list, "long_s_a_i_key", strIns, sizeof(strIns) / sizeof(strIns[0]));
286 
287     int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
288     OH_HiAppEvent_DestroyParamList(list);
289     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH);
290 }
291 
292 /**
293  * @tc.name: HiAppEventNDKTest006
294  * @tc.desc: check the verification function of event logging.
295  * @tc.type: FUNC
296  * @tc.require: AR000GIKMA
297  */
298 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest006, TestSize.Level0)
299 {
300     /**
301      * @tc.steps: step1. create a ParamList pointer.
302      * @tc.steps: step2. add too many params to the ParamList.
303      * @tc.steps: step3. write event to the file.
304      * @tc.steps: step4. check the result of logging.
305      * @tc.steps: step5. destroy the ParamList pointer.
306      */
307     // max len is 32
308     int len = 33;
309     std::vector<std::string> keys(len);
310     std::vector<std::string> values(len);
311     ParamList list = OH_HiAppEvent_CreateParamList();
312     for (int i = 0; i < len; i++) {
313         keys[i] = "key" + std::to_string(i);
314         values[i] = "value" + std::to_string(i);
315         OH_HiAppEvent_AddStringParam(list, keys[i].c_str(), values[i].c_str());
316     }
317 
318     int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
319     OH_HiAppEvent_DestroyParamList(list);
320     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_PARAM_NUM);
321 }
322 
323 /**
324  * @tc.name: HiAppEventNDKTest007
325  * @tc.desc: check the verification function of event logging.
326  * @tc.type: FUNC
327  * @tc.require: AR000GIKMA
328  */
329 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest007, TestSize.Level0)
330 {
331     /**
332      * @tc.steps: step1. create a ParamList pointer.
333      * @tc.steps: step2. add params to the ParamList.
334      * @tc.steps: step3. write event with invalid event name to the file.
335      * @tc.steps: step4. check the result of logging.
336      * @tc.steps: step5. destroy the ParamList pointer.
337      */
338     ParamList list = OH_HiAppEvent_CreateParamList();
339     OH_HiAppEvent_AddInt32Param(list, "int_key", 1);
340     int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, "verify_**", SECURITY, list);
341     OH_HiAppEvent_DestroyParamList(list);
342     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_EVENT_NAME);
343 
344     list = OH_HiAppEvent_CreateParamList();
345     OH_HiAppEvent_AddInt32Param(list, "int_key", 2);
346     res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, "verify_TEST_", SECURITY, list);
347     OH_HiAppEvent_DestroyParamList(list);
348     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_EVENT_NAME);
349 
350     list = OH_HiAppEvent_CreateParamList();
351     OH_HiAppEvent_AddInt32Param(list, "int_key", 3);
352     res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, "", SECURITY, list);
353     OH_HiAppEvent_DestroyParamList(list);
354     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_EVENT_NAME);
355 }
356 
357 /**
358  * @tc.name: HiAppEventNDKTest008
359  * @tc.desc: check the verification function of event logging.
360  * @tc.type: FUNC
361  * @tc.require: AR000GIKMA
362  */
363 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest008, TestSize.Level0)
364 {
365     /**
366      * @tc.steps: step1. create a ParamList pointer.
367      * @tc.steps: step2. add params to the ParamList.
368      * @tc.steps: step3. write event with nullptr event name to the file.
369      * @tc.steps: step4. check the result of logging.
370      * @tc.steps: step5. destroy the ParamList pointer.
371      */
372     ParamList list = OH_HiAppEvent_CreateParamList();
373     OH_HiAppEvent_AddInt32Param(list, "int_key", 1);
374 
375     int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, nullptr, SECURITY, list);
376     OH_HiAppEvent_DestroyParamList(list);
377     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_EVENT_NAME);
378 }
379 
380 /**
381  * @tc.name: HiAppEventNDKTest009
382  * @tc.desc: check the verification function of event logging.
383  * @tc.type: FUNC
384  * @tc.require: AR000GIKMA
385  */
386 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest009, TestSize.Level0)
387 {
388     /**
389      * @tc.steps: step1. disable the logging function.
390      * @tc.steps: step2. create a ParamList pointer.
391      * @tc.steps: step3. add params to the ParamList.
392      * @tc.steps: step4. write event to the file.
393      * @tc.steps: step5. check the result of logging.
394      * @tc.steps: step6. destroy the ParamList pointer.
395      */
396     OH_HiAppEvent_Configure(DISABLE, "true");
397     ParamList list = OH_HiAppEvent_CreateParamList();
398     OH_HiAppEvent_AddInt32Param(list, "int_key", 1);
399     int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
400     OH_HiAppEvent_DestroyParamList(list);
401     ASSERT_EQ(res,  ErrorCode::ERROR_HIAPPEVENT_DISABLE);
402 
403     OH_HiAppEvent_Configure(DISABLE, "false");
404     list = OH_HiAppEvent_CreateParamList();
405     OH_HiAppEvent_AddStringParam(list, "str_key", "test");
406     res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
407     OH_HiAppEvent_DestroyParamList(list);
408     ASSERT_EQ(res,  ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
409 }
410 
411 /**
412  * @tc.name: HiAppEventNDKTest010
413  * @tc.desc: check the configuration function of event logging.
414  * @tc.type: FUNC
415  * @tc.require: AR000GIKMA
416  */
417 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest010, TestSize.Level0)
418 {
419     /**
420      * @tc.steps: step1. config with invalid params.
421      * @tc.steps: step2. check the result of config.
422      */
423     bool res = OH_HiAppEvent_Configure(nullptr, nullptr);
424     ASSERT_FALSE(res);
425 
426     res = OH_HiAppEvent_Configure("key", "true");
427     ASSERT_FALSE(res);
428 
429     res = OH_HiAppEvent_Configure(DISABLE, "xxx");
430     ASSERT_FALSE(res);
431 
432     res = OH_HiAppEvent_Configure(MAX_STORAGE, "xxx");
433     ASSERT_FALSE(res);
434 
435     res = OH_HiAppEvent_Configure("", "100M");
436     ASSERT_FALSE(res);
437 }
438 
439 /**
440  * @tc.name: HiAppEventNDKTest011
441  * @tc.desc: check the configuration function of event logging.
442  * @tc.type: FUNC
443  * @tc.require: AR000GIKMA
444  */
445 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest011, TestSize.Level0)
446 {
447     /**
448      * @tc.steps: step1. config the storage directory quota of the logging function.
449      * @tc.steps: step2. check the result of config.
450      * @tc.steps: step3. write event to the file.
451      * @tc.steps: step4. check the result of logging.
452      */
453     bool res = OH_HiAppEvent_Configure(MAX_STORAGE, "1k");
454     ASSERT_TRUE(res);
455 
456     ParamList list = OH_HiAppEvent_CreateParamList();
457     OH_HiAppEvent_AddInt32Param(list, "int_key", 1);
458     int result = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
459     OH_HiAppEvent_DestroyParamList(list);
460     ASSERT_EQ(result,  ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
461 
462     res = OH_HiAppEvent_Configure(MAX_STORAGE, "100M");
463     ASSERT_TRUE(res);
464 }
465 
466 /**
467  * @tc.name: HiAppEventNDKTest012
468  * @tc.desc: check the event logging function with predefined events.
469  * @tc.type: FUNC
470  * @tc.require: AR000GIKMA
471  */
472 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest012, TestSize.Level0)
473 {
474     /**
475      * @tc.steps: step1. create a ParamList pointer.
476      * @tc.steps: step2. add params with predefined param name to the ParamList.
477      * @tc.steps: step3. write event with predefined event name to the file.
478      * @tc.steps: step4. check the result of logging.
479      * @tc.steps: step5. destroy the ParamList pointer.
480      */
481     ParamList list = OH_HiAppEvent_CreateParamList();
482     OH_HiAppEvent_AddInt32Param(list, PARAM_USER_ID, 123);
483     int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, EVENT_USER_LOGIN, BEHAVIOR, list);
484     OH_HiAppEvent_DestroyParamList(list);
485     ASSERT_EQ(res,  ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
486 
487     list = OH_HiAppEvent_CreateParamList();
488     OH_HiAppEvent_AddStringParam(list, PARAM_USER_ID, "123456");
489     res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, EVENT_USER_LOGOUT, SECURITY, list);
490     OH_HiAppEvent_DestroyParamList(list);
491     ASSERT_EQ(res,  ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
492 
493     list = OH_HiAppEvent_CreateParamList();
494     OH_HiAppEvent_AddStringParam(list, PARAM_DISTRIBUTED_SERVICE_NAME, "hiview");
495     OH_HiAppEvent_AddStringParam(list, PARAM_DISTRIBUTED_SERVICE_INSTANCE_ID, "123");
496     res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, EVENT_DISTRIBUTED_SERVICE_START, SECURITY, list);
497     OH_HiAppEvent_DestroyParamList(list);
498     ASSERT_EQ(res,  ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
499 }
500 
501 /**
502  * @tc.name: HiAppEventNDKTest013
503  * @tc.desc: check the local file.
504  * @tc.type: FUNC
505  * @tc.require: AR000GIKMA
506  */
507 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest013, TestSize.Level0)
508 {
509     /**
510      * @tc.steps: step1. create a ParamList pointer.
511      * @tc.steps: step2. add params to the ParamList.
512      * @tc.steps: step3. write event to the file.
513      * @tc.steps: step4. check the result of logging.
514      * @tc.steps: step5. check the file.
515      */
516     ParamList list = OH_HiAppEvent_CreateParamList();
517     OH_HiAppEvent_AddInt32Param(list, "int_key", 123);
518     int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, BEHAVIOR, list);
519     OH_HiAppEvent_DestroyParamList(list);
520     ASSERT_EQ(res,  ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
521 
522     sleep(1); // wait 1s for WriteEvent complete
523     std::string filePath = TEST_STORAGE_PATH + GetStorageFilePath();
524     ASSERT_EQ(access(filePath.c_str(), F_OK), 0);
525 }
526 
527 /**
528  * @tc.name: HiAppEventNDKTest014
529  * @tc.desc: check the domain verification function of event logging.
530  * @tc.type: FUNC
531  * @tc.require: issueI8OY2U
532  */
533 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest014, TestSize.Level0)
534 {
535     int res = OH_HiAppEvent_Write(nullptr, TEST_EVENT_NAME, SECURITY, nullptr);
536     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
537 
538     res = OH_HiAppEvent_Write("", TEST_EVENT_NAME, SECURITY, nullptr);
539     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
540 
541     constexpr size_t limitLen = 32;
542     res = OH_HiAppEvent_Write(std::string(limitLen, 'a').c_str(), TEST_EVENT_NAME, SECURITY, nullptr);
543     ASSERT_EQ(res,  ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
544     res = OH_HiAppEvent_Write(std::string(limitLen + 1, 'a').c_str(), TEST_EVENT_NAME, SECURITY, nullptr);
545     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
546 
547     std::string invalidDomain = std::string(limitLen - 1, 'a') + "_";
548     res = OH_HiAppEvent_Write(invalidDomain.c_str(), TEST_EVENT_NAME, SECURITY, nullptr);
549     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
550 
551     res = OH_HiAppEvent_Write("AAAaaa", TEST_EVENT_NAME, SECURITY, nullptr);
552     ASSERT_EQ(res,  ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
553 
554     res = OH_HiAppEvent_Write("abc***", TEST_EVENT_NAME, SECURITY, nullptr);
555     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
556 
557     res = OH_HiAppEvent_Write("domain_", TEST_EVENT_NAME, SECURITY, nullptr);
558     ASSERT_EQ(res,  ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
559 
560     res = OH_HiAppEvent_Write("a", TEST_EVENT_NAME, SECURITY, nullptr);
561     ASSERT_EQ(res,  ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
562 
563     res = OH_HiAppEvent_Write("a1", TEST_EVENT_NAME, SECURITY, nullptr);
564     ASSERT_EQ(res,  ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
565 }
566 
567 /**
568  * @tc.name: HiAppEventNDKTest015
569  * @tc.desc: check the function of OH_HiAppEvent_CreateWatcher.
570  * @tc.type: FUNC
571  * @tc.require: issueI8OY2U
572  */
573 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest015, TestSize.Level0)
574 {
575     ASSERT_TRUE(OH_HiAppEvent_CreateWatcher(nullptr) == nullptr);
576     g_onReceiveWatcher = OH_HiAppEvent_CreateWatcher("test_onReceiver_watcher");
577     ASSERT_TRUE(g_onReceiveWatcher != nullptr);
578     g_onTriggerWatcher = OH_HiAppEvent_CreateWatcher("test_onTrigger_watcher");
579     ASSERT_TRUE(g_onTriggerWatcher != nullptr);
580 }
581 
582 /**
583  * @tc.name: HiAppEventNDKTest016
584  * @tc.desc:  check the function of OH_HiAppEvent_SetAppEventFilter.
585  * @tc.type: FUNC
586  * @tc.require: issueI8OY2U
587  */
588 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest016, TestSize.Level0)
589 {
590     const char* filterNames[] =  {TEST_EVENT_NAME};
591     const char* filterNamesWithNullptr[] =  {nullptr};
592     constexpr int namsLen = 1;
593     ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(nullptr, TEST_DOMAIN_NAME, 0, filterNames, namsLen),
594               ErrorCode::ERROR_INVALID_WATCHER);
595     ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(g_onReceiveWatcher, nullptr, 0, filterNames, namsLen),
596               ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
597     ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(g_onReceiveWatcher, TEST_DOMAIN_NAME, 0, nullptr, namsLen),
598               ErrorCode::ERROR_INVALID_EVENT_NAME);
599     ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(g_onReceiveWatcher, TEST_DOMAIN_NAME, 0, filterNamesWithNullptr, namsLen),
600               ErrorCode::ERROR_INVALID_EVENT_NAME);
601     ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(g_onReceiveWatcher, TEST_DOMAIN_NAME, 0, filterNames, namsLen), 0);
602     ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(g_onTriggerWatcher, TEST_DOMAIN_NAME, 0, filterNames, namsLen), 0);
603 }
604 
605 /**
606  * @tc.name: HiAppEventNDKTest017
607  * @tc.desc:  check the function of OH_HiAppEvent_SetWatcherOnReceiver.
608  * @tc.type: FUNC
609  * @tc.require: issueI8OY2U
610  */
611 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest017, TestSize.Level0)
612 {
613     ASSERT_EQ(OH_HiAppEvent_SetWatcherOnReceive(nullptr, OnReceive), ErrorCode::ERROR_INVALID_WATCHER);
614     ASSERT_EQ(OH_HiAppEvent_SetWatcherOnReceive(g_onReceiveWatcher, OnReceive), 0);
615 }
616 
617 /**
618  * @tc.name: HiAppEventNDKTest018
619  * @tc.desc: check the function of OH_HiAppEvent_SetTriggerCondition.
620  * @tc.type: FUNC
621  * @tc.require: issueI8OY2U
622  */
623 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest018, TestSize.Level0)
624 {
625     ASSERT_EQ(OH_HiAppEvent_SetTriggerCondition(nullptr, TEST_EVENT_NUM, 0, 0), ErrorCode::ERROR_INVALID_WATCHER);
626     ASSERT_EQ(OH_HiAppEvent_SetTriggerCondition(g_onTriggerWatcher, TEST_EVENT_NUM, 0, 0), 0);
627 }
628 
629 /**
630  * @tc.name: HiAppEventNDKTest019
631  * @tc.desc:  check the function of OH_HiAppEvent_SetWatcherOnTrigger.
632  * @tc.type: FUNC
633  * @tc.require: issueI8OY2U
634  */
635 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest019, TestSize.Level0)
636 {
637     ASSERT_EQ(OH_HiAppEvent_SetWatcherOnTrigger(nullptr, OnTrigger), ErrorCode::ERROR_INVALID_WATCHER);
638     ASSERT_EQ(OH_HiAppEvent_SetWatcherOnTrigger(g_onTriggerWatcher, OnTrigger), 0);
639 }
640 
641 /**
642  * @tc.name: HiAppEventNDKTest020
643  * @tc.desc:  check the function of OH_HiAppEvent_AddWatcher.
644  * @tc.type: FUNC
645  * @tc.require: issueI8OY2U
646  */
647 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest020, TestSize.Level0)
648 {
649     ASSERT_EQ(OH_HiAppEvent_AddWatcher(nullptr), ErrorCode::ERROR_INVALID_WATCHER);
650     ASSERT_EQ(OH_HiAppEvent_AddWatcher(g_onTriggerWatcher), 0);
651     ASSERT_EQ(OH_HiAppEvent_AddWatcher(g_onReceiveWatcher), 0);
652     for (int i = 0; i < TEST_EVENT_NUM; ++i) {
653         WriteEvent();
654     }
655 }
656 
657 /**
658  * @tc.name: HiAppEventNDKTest021
659  * @tc.desc:  check the function of OH_HiAppEvent_TakeWatcherData.
660  * @tc.type: FUNC
661  * @tc.require: issueI8OY2U
662  */
663 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest021, TestSize.Level0)
664 {
665     constexpr uint32_t takeNum = 10;
666     sleep(1); // wait 1s for WriteEvent complete
667     ASSERT_EQ(OH_HiAppEvent_TakeWatcherData(nullptr, takeNum, OnTake), ErrorCode::ERROR_INVALID_WATCHER);
668     ASSERT_EQ(OH_HiAppEvent_TakeWatcherData(g_onTriggerWatcher, takeNum, OnTake), 0);
669 }
670 
671 /**
672  * @tc.name: HiAppEventNDKTest022
673  * @tc.desc: check the function of OH_HiAppEvent_ClearData.
674  * @tc.type: FUNC
675  * @tc.require: issueI8OY2U
676  */
677 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest022, TestSize.Level0)
678 {
679     OH_HiAppEvent_ClearData();
680     ASSERT_EQ(HiAppEvent::UserInfo::GetInstance().GetUserIdVersion(), 0);
681     ASSERT_EQ(HiAppEvent::UserInfo::GetInstance().GetUserPropertyVersion(), 0);
682 }
683 
684 /**
685  * @tc.name: HiAppEventNDKTest023
686  * @tc.desc: check the function of OH_HiAppEvent_RemoveWatcher.
687  * @tc.type: FUNC
688  * @tc.require: issueI8OY2U
689  */
690 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest023, TestSize.Level0)
691 {
692     ASSERT_EQ(OH_HiAppEvent_RemoveWatcher(g_onTriggerWatcher), 0);
693     ASSERT_EQ(OH_HiAppEvent_RemoveWatcher(g_onTriggerWatcher), ErrorCode::ERROR_WATCHER_NOT_ADDED);
694     ASSERT_EQ(OH_HiAppEvent_RemoveWatcher(g_onReceiveWatcher), 0);
695     ASSERT_EQ(OH_HiAppEvent_RemoveWatcher(g_onReceiveWatcher), ErrorCode::ERROR_WATCHER_NOT_ADDED);
696     OH_HiAppEvent_DestroyWatcher(g_onTriggerWatcher);
697     g_onTriggerWatcher = nullptr;
698     OH_HiAppEvent_DestroyWatcher(g_onReceiveWatcher);
699     g_onReceiveWatcher = nullptr;
700 }
701 
702 /**
703  * @tc.name: HiAppEventNDKTest024
704  * @tc.desc: check ndk interface of AddProcessor.
705  * @tc.type: FUNC
706  */
707 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest024, TestSize.Level0)
708 {
709     setuid(0); // 0 means root uid
710     ASSERT_EQ(CreateProcessor(TEST_PROCESSOR_NAME), nullptr);
711     ASSERT_EQ(SetReportRoute(nullptr, nullptr, nullptr), ErrorCode::ERROR_NOT_APP);
712     ASSERT_EQ(SetReportPolicy(nullptr, 0, 0, false, false), ErrorCode::ERROR_NOT_APP);
713     ASSERT_EQ(SetReportEvent(nullptr, nullptr, nullptr, false), ErrorCode::ERROR_NOT_APP);
714     ASSERT_EQ(SetCustomConfig(nullptr, nullptr, nullptr), ErrorCode::ERROR_NOT_APP);
715     ASSERT_EQ(SetConfigId(nullptr, 0), ErrorCode::ERROR_NOT_APP);
716     ASSERT_EQ(SetReportUserId(nullptr, nullptr, 0), ErrorCode::ERROR_NOT_APP);
717     ASSERT_EQ(SetReportUserProperty(nullptr, nullptr, 0), ErrorCode::ERROR_NOT_APP);
718     ASSERT_EQ(AddProcessor(nullptr), ErrorCode::ERROR_NOT_APP);
719     DestoryProcessor(nullptr);
720     ASSERT_EQ(RemoveProcessor(0), ErrorCode::ERROR_NOT_APP);
721 
722     // set app uid
723     setuid(TEST_UID);
724 
725     ASSERT_EQ(CreateProcessor(nullptr), nullptr);
726     ASSERT_EQ(SetReportRoute(nullptr, nullptr, nullptr), ErrorCode::ERROR_INVALID_PROCESSOR);
727     ASSERT_EQ(SetReportPolicy(nullptr, 0, 0, false, false), ErrorCode::ERROR_INVALID_PROCESSOR);
728     ASSERT_EQ(SetReportEvent(nullptr, nullptr, nullptr, false), ErrorCode::ERROR_INVALID_PROCESSOR);
729     ASSERT_EQ(SetCustomConfig(nullptr, nullptr, nullptr), ErrorCode::ERROR_INVALID_PROCESSOR);
730     ASSERT_EQ(SetConfigId(nullptr, 0), ErrorCode::ERROR_INVALID_PROCESSOR);
731     ASSERT_EQ(SetReportUserId(nullptr, nullptr, 0), ErrorCode::ERROR_INVALID_PROCESSOR);
732     ASSERT_EQ(SetReportUserProperty(nullptr, nullptr, 0), ErrorCode::ERROR_INVALID_PROCESSOR);
733     ASSERT_EQ(AddProcessor(nullptr), ErrorCode::ERROR_INVALID_PROCESSOR);
734     DestoryProcessor(nullptr);
735     ASSERT_EQ(RemoveProcessor(0), ErrorCode::ERROR_PROCESSOR_NOT_ADDED);
736 }
737 
738 /**
739  * @tc.name: HiAppEventNDKTest025
740  * @tc.desc: check ndk interface of AddProcessor.
741  * @tc.type: FUNC
742  */
743 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest025, TestSize.Level0)
744 {
745     ASSERT_EQ(CreateProcessor(""), nullptr);
746     auto processor = CreateProcessor(TEST_PROCESSOR_NAME);
747     ASSERT_TRUE(processor != nullptr);
748     ASSERT_EQ(SetReportRoute(processor, nullptr, nullptr), ErrorCode::ERROR_INVALID_PARAM_VALUE);
749     ASSERT_EQ(SetReportRoute(processor, "", ""), 0);
750     ASSERT_EQ(SetReportPolicy(processor, -1, 0, false, false), ErrorCode::ERROR_INVALID_PARAM_VALUE);
751     ASSERT_EQ(SetReportEvent(processor, nullptr, nullptr, false), ErrorCode::ERROR_INVALID_PARAM_VALUE);
752     ASSERT_EQ(SetCustomConfig(processor, nullptr, nullptr), ErrorCode::ERROR_INVALID_PARAM_VALUE);
753     ASSERT_EQ(SetCustomConfig(processor, "", ""), ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH);
754     ASSERT_EQ(SetConfigId(processor, -1), ErrorCode::ERROR_INVALID_PARAM_VALUE);
755     ASSERT_EQ(SetReportUserId(processor, nullptr, 0), ErrorCode::ERROR_INVALID_PARAM_VALUE);
756     const char* userStrs[] = {"aaa", ""};
757     ASSERT_EQ(SetReportUserId(processor, userStrs, 0), 0);
758     ASSERT_EQ(SetReportUserProperty(processor, nullptr, 0), ErrorCode::ERROR_INVALID_PARAM_VALUE);
759     ASSERT_EQ(SetReportUserProperty(processor, userStrs, 0), 0);
760     int seq = AddProcessor(processor);
761     ASSERT_GT(seq, 0);
762     DestoryProcessor(processor);
763     ASSERT_EQ(RemoveProcessor(seq), 0);
764 }
765 
766 /**
767  * @tc.name: HiAppEventNDKTest026
768  * @tc.desc: check ndk interface of AddProcessor.
769  * @tc.type: FUNC
770  */
771 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest026, TestSize.Level0)
772 {
773     auto processor = CreateProcessor(TEST_PROCESSOR_NAME);
774     ASSERT_TRUE(processor != nullptr);
775     ASSERT_EQ(SetReportRoute(processor, "test_appid", "test_routeInfo"), 0);
776     ASSERT_EQ(SetReportPolicy(processor, 2, 2, false, false), 0);
777     ASSERT_EQ(SetReportEvent(processor, "test_domain", "test_name", false), 0);
778     ASSERT_EQ(SetCustomConfig(processor, "str_key", "str_value"), 0);
779     ASSERT_EQ(SetConfigId(processor, 1), 0);
780     const char* userIds[] = {"test_id"};
781     ASSERT_EQ(SetReportUserId(processor, userIds, 1), 0);
782     const char* userProperties[] = {"test_property"};
783     ASSERT_EQ(SetReportUserProperty(processor, userProperties, 1), 0);
784     int seq = AddProcessor(processor);
785     ASSERT_GT(seq, 0);
786     DestoryProcessor(processor);
787     ASSERT_EQ(RemoveProcessor(seq), 0);
788 }