1 /*
2 * Copyright (c) 2021-2025 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 "application_context.h"
23 #include "hiappevent/hiappevent.h"
24 #include "hiappevent_base.h"
25 #include "hiappevent_config.h"
26 #include "hiappevent_test_common.h"
27 #include "hiappevent_userinfo.h"
28 #include "ndk_app_event_processor_service.h"
29 #include "time_util.h"
30 #include "processor/test_processor.h"
31 #include "app_event_processor_mgr.h"
32
33 using namespace testing::ext;
34 using namespace OHOS::HiviewDFX;
35
36 namespace {
37 const std::string TEST_STORAGE_PATH = "/data/test/hiappevent/";
38 const char* TEST_DOMAIN_NAME = "test_domain";
39 const char* TEST_EVENT_NAME = "test_event";
40 const char* TEST_EVENT_PARAM_KEY = "test_param_key";
41 const char* TEST_EVENT_PARAM = "{\"test_param_key\":1}";
42 constexpr int TEST_EVENT_PARAM_LENGTH = 20;
43 constexpr int TEST_EVENT_NUM = 2;
44
45 const char* TEST_PROCESSOR_NAME = "test_processor";
46 constexpr int32_t TEST_UID = 200000 * 100;
47
WriteEvent()48 static void WriteEvent()
49 {
50 ParamList list = OH_HiAppEvent_CreateParamList();
51 OH_HiAppEvent_AddInt16Param(list, TEST_EVENT_PARAM_KEY, 1);
52 OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
53 }
54
OnReceive(const char * domain,const struct HiAppEvent_AppEventGroup * appEventGroups,uint32_t groupSize)55 static void OnReceive(const char* domain, const struct HiAppEvent_AppEventGroup* appEventGroups, uint32_t groupSize)
56 {
57 ASSERT_EQ(strcmp(domain, TEST_DOMAIN_NAME), 0);
58 ASSERT_EQ(groupSize, 1);
59 ASSERT_TRUE(appEventGroups);
60 ASSERT_EQ(appEventGroups[0].infoLen, 1);
61 ASSERT_TRUE(appEventGroups[0].appEventInfos);
62 ASSERT_EQ(strcmp(appEventGroups[0].appEventInfos[0].name, TEST_EVENT_NAME), 0);
63 ASSERT_EQ(strcmp(appEventGroups[0].appEventInfos[0].domain, TEST_DOMAIN_NAME), 0);
64 ASSERT_EQ(appEventGroups[0].appEventInfos[0].type, SECURITY);
65 ASSERT_EQ(strncmp(appEventGroups[0].appEventInfos[0].params, TEST_EVENT_PARAM, TEST_EVENT_PARAM_LENGTH), 0);
66 }
67
OnTrigger(int32_t row,int32_t size)68 static void OnTrigger(int32_t row, int32_t size)
69 {
70 ASSERT_EQ(row, TEST_EVENT_NUM);
71 ASSERT_GT(size, 0);
72 }
73
OnTake(const char * const * events,uint32_t eventLen)74 static void OnTake(const char* const *events, uint32_t eventLen)
75 {
76 ASSERT_TRUE(events != nullptr);
77 ASSERT_EQ(eventLen, TEST_EVENT_NUM);
78 }
79
GetStorageFilePath()80 std::string GetStorageFilePath()
81 {
82 return "app_event_" + TimeUtil::GetDate() + ".log";
83 }
84 }
85
SetUpTestCase()86 void HiAppEventNativeTest::SetUpTestCase()
87 {
88 // set app uid
89 setuid(TEST_UID);
90 HiAppEventConfig::GetInstance().SetStorageDir(TEST_STORAGE_PATH);
91 // set context bundle name
92 auto context = OHOS::AbilityRuntime::ApplicationContext::GetInstance();
93 if (context == nullptr) {
94 std::cout << "context is null." << std::endl;
95 return;
96 }
97 auto contextImpl = std::make_shared<TestContextImpl>("ohos.hiappevent.native.test");
98 context->AttachContextImpl(contextImpl);
99 std::cout << "set bundle name." << std::endl;
100 auto processor = std::make_shared<HiAppEvent::TestProcessor>();
101 int ret = HiAppEvent::AppEventProcessorMgr::RegisterProcessor("test_processor", processor);
102 std::cout << "register observer ret = " << ret << std::endl;
103 }
104
105 /**
106 * @tc.name: HiAppEventNDKTest001
107 * @tc.desc: check the logging function
108 * @tc.type: FUNC
109 * @tc.require: AR000GIKMA
110 */
111 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest001, TestSize.Level0)
112 {
113 /**
114 * @tc.steps: step1. create a ParamList pointer.
115 * @tc.steps: step2. add params to the ParamList.
116 * @tc.steps: step3. write event to the file.
117 * @tc.steps: step4. check the result of logging.
118 * @tc.steps: step5. destroy the ParamList pointer.
119 */
120 ParamList list = OH_HiAppEvent_CreateParamList();
121 bool boolean = true;
122 OH_HiAppEvent_AddBoolParam(list, "bool_key", boolean);
123 bool booleans[] = {true, false, true};
124 OH_HiAppEvent_AddBoolArrayParam(list, "bool_arr_key", booleans, sizeof(booleans) / sizeof(booleans[0]));
125 int8_t num1 = 1;
126 OH_HiAppEvent_AddInt8Param(list, "int8_key", num1);
127 int8_t nums1[] = {1, INT8_MIN, INT8_MAX};
128 OH_HiAppEvent_AddInt8ArrayParam(list, "int8_arr_key", nums1, sizeof(nums1) / sizeof(nums1[0]));
129 int16_t num2 = 1;
130 OH_HiAppEvent_AddInt16Param(list, "int16_key", num2);
131 int16_t nums2[] = {1, INT16_MAX, INT16_MIN};
132 OH_HiAppEvent_AddInt16ArrayParam(list, "int16_arr_key", nums2, sizeof(nums2) / sizeof(nums2[0]));
133 int32_t num3 = 1;
134 OH_HiAppEvent_AddInt32Param(list, "int32_key", num3);
135 int32_t nums3[] = {1, INT32_MAX, INT32_MIN};
136 OH_HiAppEvent_AddInt32ArrayParam(list, "int32_arr_key", nums3, sizeof(nums3) / sizeof(nums3[0]));
137 int64_t num4 = 1;
138 OH_HiAppEvent_AddInt64Param(list, "int64_key", num4);
139 int64_t nums4[] = {1LL, INT64_MAX, INT64_MIN};
140 OH_HiAppEvent_AddInt64ArrayParam(list, "int64_arr_key", nums4, sizeof(nums4) / sizeof(nums4[0]));
141 float num5 = 465.1234;
142 OH_HiAppEvent_AddFloatParam(list, "float_key", num5);
143 float nums5[] = {123.22f, num5, 131312.46464f};
144 OH_HiAppEvent_AddFloatArrayParam(list, "float_arr_key", nums5, sizeof(nums5) / sizeof(nums5[0]));
145 double num6 = 465.1234;
146 OH_HiAppEvent_AddDoubleParam(list, "double_key", num6);
147 double nums6[] = {123.22, num6, 131312.46464};
148 OH_HiAppEvent_AddDoubleArrayParam(list, "double_arr_key", nums6, sizeof(nums6) / sizeof(nums6[0]));
149 char str1[] = "hello";
150 OH_HiAppEvent_AddStringParam(list, "str_key", str1);
151 char str2[] = "world";
152 char* strs[] = {str1, str2};
153 OH_HiAppEvent_AddStringArrayParam(list, "str_arr_key", strs, sizeof(strs) / sizeof(strs[0]));
154
155 int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, BEHAVIOR, list);
156 OH_HiAppEvent_DestroyParamList(list);
157 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
158 }
159
160 /**
161 * @tc.name: HiAppEventNDKTest002
162 * @tc.desc: check the overwriting function of the same param name.
163 * @tc.type: FUNC
164 * @tc.require: AR000GIKMA
165 */
166 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest002, TestSize.Level0)
167 {
168 /**
169 * @tc.steps: step1. create a ParamList pointer.
170 * @tc.steps: step2. add params with the same name to the ParamList.
171 * @tc.steps: step3. write event to the file.
172 * @tc.steps: step4. check the result of logging.
173 * @tc.steps: step5. destroy the ParamList pointer.
174 */
175 ParamList list = OH_HiAppEvent_CreateParamList();
176 int8_t num1 = 1;
177 OH_HiAppEvent_AddInt8Param(list, "int_key", num1);
178 int8_t nums1[] = {1, INT8_MIN, INT8_MAX};
179 OH_HiAppEvent_AddInt8ArrayParam(list, "int8_arr_key", nums1, sizeof(nums1) / sizeof(nums1[0]));
180 int16_t num2 = 1;
181 OH_HiAppEvent_AddInt16Param(list, "int16_key", num2);
182 int16_t nums2[] = {1, INT16_MAX, INT16_MIN};
183 OH_HiAppEvent_AddInt16ArrayParam(list, "int16_key", nums2, sizeof(nums2) / sizeof(nums2[0]));
184
185 int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, BEHAVIOR, list);
186 OH_HiAppEvent_DestroyParamList(list);
187 ASSERT_EQ(res, ErrorCode::ERROR_DUPLICATE_PARAM);
188 }
189
190 /**
191 * @tc.name: HiAppEventNDKTest003
192 * @tc.desc: check the logging function when the input value is nullptr.
193 * @tc.type: FUNC
194 * @tc.require: AR000GIKMA
195 */
196 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest003, TestSize.Level0)
197 {
198 /**
199 * @tc.steps: step1. create a ParamList pointer.
200 * @tc.steps: step2. add params with the nullptr value to the ParamList.
201 * @tc.steps: step3. write event to the file.
202 * @tc.steps: step4. check the result of logging.
203 * @tc.steps: step5. destroy the ParamList pointer.
204 */
205 int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, FAULT, nullptr);
206 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
207
208 ParamList list = OH_HiAppEvent_CreateParamList();
209 bool boolean = true;
210 OH_HiAppEvent_AddBoolParam(list, nullptr, boolean);
211 OH_HiAppEvent_AddBoolArrayParam(list, "bool_arr_key", nullptr, 0);
212 int8_t num1 = 1;
213 OH_HiAppEvent_AddInt8Param(list, nullptr, num1);
214 OH_HiAppEvent_AddInt8ArrayParam(list, "int8_arr_key", nullptr, 0);
215 int16_t num2 = 1;
216 OH_HiAppEvent_AddInt16Param(list, nullptr, num2);
217 OH_HiAppEvent_AddInt16ArrayParam(list, "int16_arr_key", nullptr, 1);
218 int32_t num3 = 1;
219 OH_HiAppEvent_AddInt32Param(list, nullptr, num3);
220 OH_HiAppEvent_AddInt32ArrayParam(list, "int32_arr_key", nullptr, 2);
221 int64_t num4 = 1;
222 OH_HiAppEvent_AddInt64Param(list, nullptr, num4);
223 OH_HiAppEvent_AddInt64ArrayParam(list, "int64_arr_key", nullptr, 3);
224 float num5 = 465.1234;
225 OH_HiAppEvent_AddFloatParam(list, nullptr, num5);
226 OH_HiAppEvent_AddFloatArrayParam(list, "float_arr_key", nullptr, -1);
227 double num6 = 465.1234;
228 OH_HiAppEvent_AddDoubleParam(list, nullptr, num6);
229 OH_HiAppEvent_AddDoubleArrayParam(list, "double_arr_key", nullptr, 0);
230 char str1[] = "hello";
231 OH_HiAppEvent_AddStringParam(list, nullptr, str1);
232 OH_HiAppEvent_AddStringParam(list, nullptr, nullptr);
233 OH_HiAppEvent_AddStringArrayParam(list, "str_arr_key", nullptr, 0);
234 char* strs[] = {str1, nullptr};
235 OH_HiAppEvent_AddStringArrayParam(list, "str_arr_null_key", strs, sizeof(strs) / sizeof(strs[0]));
236
237 res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, STATISTIC, list);
238 OH_HiAppEvent_DestroyParamList(list);
239 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
240 }
241
242 /**
243 * @tc.name: HiAppEventNDKTest004
244 * @tc.desc: check the verification function of event logging.
245 * @tc.type: FUNC
246 * @tc.require: AR000GIKMA
247 */
248 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest004, TestSize.Level0)
249 {
250 /**
251 * @tc.steps: step1. create a ParamList pointer.
252 * @tc.steps: step2. add params with the invalid name to the ParamList.
253 * @tc.steps: step3. write event to the file.
254 * @tc.steps: step4. check the result of logging.
255 * @tc.steps: step5. destroy the ParamList pointer.
256 */
257 ParamList list = OH_HiAppEvent_CreateParamList();
258 char key1[] = "**";
259 int8_t num1 = 1;
260 OH_HiAppEvent_AddInt8Param(list, key1, num1);
261 char key2[] = "HH22";
262 int16_t num2 = 1;
263 OH_HiAppEvent_AddInt16Param(list, key2, num2);
264 char key3[] = "aa_";
265 int32_t num3 = 1;
266 OH_HiAppEvent_AddInt32Param(list, key3, num3);
267 char key4[] = "";
268 int64_t num4 = 1;
269 OH_HiAppEvent_AddInt64Param(list, key4, num4);
270
271 int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
272 OH_HiAppEvent_DestroyParamList(list);
273 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_PARAM_NAME);
274 }
275
276 /**
277 * @tc.name: HiAppEventNDKTest005
278 * @tc.desc: check the verification function of event logging.
279 * @tc.type: FUNC
280 * @tc.require: AR000GIKMA
281 */
282 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest005, TestSize.Level0)
283 {
284 /**
285 * @tc.steps: step1. create a ParamList pointer.
286 * @tc.steps: step2. add params with too long string length to the ParamList.
287 * @tc.steps: step3. write event to the file.
288 * @tc.steps: step4. check the result of logging.
289 * @tc.steps: step5. destroy the ParamList pointer.
290 */
291 int maxStrLen = 8 * 1024;
292 std::string longStr(maxStrLen, 'a');
293 std::string longInvalidStr(maxStrLen + 1, 'a');
294 const char* strs[] = {"hello", longStr.c_str()};
295 const char* strIns[] = {"hello", longInvalidStr.c_str()};
296
297 ParamList list = OH_HiAppEvent_CreateParamList();
298 OH_HiAppEvent_AddStringParam(list, "long_s_key", longStr.c_str());
299 OH_HiAppEvent_AddStringArrayParam(list, "long_s_a_key", strs, sizeof(strs) / sizeof(strs[0]));
300 OH_HiAppEvent_AddStringParam(list, "long_s_i_key", longInvalidStr.c_str());
301 OH_HiAppEvent_AddStringArrayParam(list, "long_s_a_i_key", strIns, sizeof(strIns) / sizeof(strIns[0]));
302
303 int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
304 OH_HiAppEvent_DestroyParamList(list);
305 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH);
306 }
307
308 /**
309 * @tc.name: HiAppEventNDKTest006
310 * @tc.desc: check the verification function of event logging.
311 * @tc.type: FUNC
312 * @tc.require: AR000GIKMA
313 */
314 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest006, TestSize.Level0)
315 {
316 /**
317 * @tc.steps: step1. create a ParamList pointer.
318 * @tc.steps: step2. add too many params to the ParamList.
319 * @tc.steps: step3. write event to the file.
320 * @tc.steps: step4. check the result of logging.
321 * @tc.steps: step5. destroy the ParamList pointer.
322 */
323 // max len is 32
324 int len = 33;
325 std::vector<std::string> keys(len);
326 std::vector<std::string> values(len);
327 ParamList list = OH_HiAppEvent_CreateParamList();
328 for (int i = 0; i < len; i++) {
329 keys[i] = "key" + std::to_string(i);
330 values[i] = "value" + std::to_string(i);
331 OH_HiAppEvent_AddStringParam(list, keys[i].c_str(), values[i].c_str());
332 }
333
334 int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
335 OH_HiAppEvent_DestroyParamList(list);
336 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_PARAM_NUM);
337 }
338
339 /**
340 * @tc.name: HiAppEventNDKTest007
341 * @tc.desc: check the verification function of event logging.
342 * @tc.type: FUNC
343 * @tc.require: AR000GIKMA
344 */
345 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest007, TestSize.Level0)
346 {
347 /**
348 * @tc.steps: step1. create a ParamList pointer.
349 * @tc.steps: step2. add params to the ParamList.
350 * @tc.steps: step3. write event with invalid event name to the file.
351 * @tc.steps: step4. check the result of logging.
352 * @tc.steps: step5. destroy the ParamList pointer.
353 */
354 ParamList list = OH_HiAppEvent_CreateParamList();
355 OH_HiAppEvent_AddInt32Param(list, "int_key", 1);
356 int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, "verify_**", SECURITY, list);
357 OH_HiAppEvent_DestroyParamList(list);
358 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_EVENT_NAME);
359
360 list = OH_HiAppEvent_CreateParamList();
361 OH_HiAppEvent_AddInt32Param(list, "int_key", 2);
362 res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, "verify_TEST_", SECURITY, list);
363 OH_HiAppEvent_DestroyParamList(list);
364 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_EVENT_NAME);
365
366 list = OH_HiAppEvent_CreateParamList();
367 OH_HiAppEvent_AddInt32Param(list, "int_key", 3);
368 res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, "", SECURITY, list);
369 OH_HiAppEvent_DestroyParamList(list);
370 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_EVENT_NAME);
371 }
372
373 /**
374 * @tc.name: HiAppEventNDKTest008
375 * @tc.desc: check the verification function of event logging.
376 * @tc.type: FUNC
377 * @tc.require: AR000GIKMA
378 */
379 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest008, TestSize.Level0)
380 {
381 /**
382 * @tc.steps: step1. create a ParamList pointer.
383 * @tc.steps: step2. add params to the ParamList.
384 * @tc.steps: step3. write event with nullptr event name to the file.
385 * @tc.steps: step4. check the result of logging.
386 * @tc.steps: step5. destroy the ParamList pointer.
387 */
388 ParamList list = OH_HiAppEvent_CreateParamList();
389 OH_HiAppEvent_AddInt32Param(list, "int_key", 1);
390
391 int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, nullptr, SECURITY, list);
392 OH_HiAppEvent_DestroyParamList(list);
393 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_EVENT_NAME);
394 }
395
396 /**
397 * @tc.name: HiAppEventNDKTest009
398 * @tc.desc: check the verification function of event logging.
399 * @tc.type: FUNC
400 * @tc.require: AR000GIKMA
401 */
402 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest009, TestSize.Level0)
403 {
404 /**
405 * @tc.steps: step1. disable the logging function.
406 * @tc.steps: step2. create a ParamList pointer.
407 * @tc.steps: step3. add params to the ParamList.
408 * @tc.steps: step4. write event to the file.
409 * @tc.steps: step5. check the result of logging.
410 * @tc.steps: step6. destroy the ParamList pointer.
411 */
412 OH_HiAppEvent_Configure(DISABLE, "true");
413 ParamList list = OH_HiAppEvent_CreateParamList();
414 OH_HiAppEvent_AddInt32Param(list, "int_key", 1);
415 int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
416 OH_HiAppEvent_DestroyParamList(list);
417 ASSERT_EQ(res, ErrorCode::ERROR_HIAPPEVENT_DISABLE);
418
419 OH_HiAppEvent_Configure(DISABLE, "false");
420 list = OH_HiAppEvent_CreateParamList();
421 OH_HiAppEvent_AddStringParam(list, "str_key", "test");
422 res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
423 OH_HiAppEvent_DestroyParamList(list);
424 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
425 }
426
427 /**
428 * @tc.name: HiAppEventNDKTest010
429 * @tc.desc: check the configuration function of event logging.
430 * @tc.type: FUNC
431 * @tc.require: AR000GIKMA
432 */
433 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest010, TestSize.Level0)
434 {
435 /**
436 * @tc.steps: step1. config with invalid params.
437 * @tc.steps: step2. check the result of config.
438 */
439 bool res = OH_HiAppEvent_Configure(nullptr, nullptr);
440 ASSERT_FALSE(res);
441
442 res = OH_HiAppEvent_Configure("key", "true");
443 ASSERT_FALSE(res);
444
445 res = OH_HiAppEvent_Configure(DISABLE, "xxx");
446 ASSERT_FALSE(res);
447
448 res = OH_HiAppEvent_Configure(MAX_STORAGE, "xxx");
449 ASSERT_FALSE(res);
450
451 res = OH_HiAppEvent_Configure("", "100M");
452 ASSERT_FALSE(res);
453 }
454
455 /**
456 * @tc.name: HiAppEventNDKTest011
457 * @tc.desc: check the configuration function of event logging.
458 * @tc.type: FUNC
459 * @tc.require: AR000GIKMA
460 */
461 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest011, TestSize.Level0)
462 {
463 /**
464 * @tc.steps: step1. config the storage directory quota of the logging function.
465 * @tc.steps: step2. check the result of config.
466 * @tc.steps: step3. write event to the file.
467 * @tc.steps: step4. check the result of logging.
468 */
469 bool res = OH_HiAppEvent_Configure(MAX_STORAGE, "1k");
470 ASSERT_TRUE(res);
471
472 ParamList list = OH_HiAppEvent_CreateParamList();
473 OH_HiAppEvent_AddInt32Param(list, "int_key", 1);
474 int result = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, SECURITY, list);
475 OH_HiAppEvent_DestroyParamList(list);
476 ASSERT_EQ(result, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
477
478 res = OH_HiAppEvent_Configure(MAX_STORAGE, "100M");
479 ASSERT_TRUE(res);
480 }
481
482 /**
483 * @tc.name: HiAppEventNDKTest012
484 * @tc.desc: check the event logging function with predefined events.
485 * @tc.type: FUNC
486 * @tc.require: AR000GIKMA
487 */
488 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest012, TestSize.Level0)
489 {
490 /**
491 * @tc.steps: step1. create a ParamList pointer.
492 * @tc.steps: step2. add params with predefined param name to the ParamList.
493 * @tc.steps: step3. write event with predefined event name to the file.
494 * @tc.steps: step4. check the result of logging.
495 * @tc.steps: step5. destroy the ParamList pointer.
496 */
497 ParamList list = OH_HiAppEvent_CreateParamList();
498 OH_HiAppEvent_AddInt32Param(list, PARAM_USER_ID, 123);
499 int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, EVENT_USER_LOGIN, BEHAVIOR, list);
500 OH_HiAppEvent_DestroyParamList(list);
501 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
502
503 list = OH_HiAppEvent_CreateParamList();
504 OH_HiAppEvent_AddStringParam(list, PARAM_USER_ID, "123456");
505 res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, EVENT_USER_LOGOUT, SECURITY, list);
506 OH_HiAppEvent_DestroyParamList(list);
507 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
508
509 list = OH_HiAppEvent_CreateParamList();
510 OH_HiAppEvent_AddStringParam(list, PARAM_DISTRIBUTED_SERVICE_NAME, "hiview");
511 OH_HiAppEvent_AddStringParam(list, PARAM_DISTRIBUTED_SERVICE_INSTANCE_ID, "123");
512 res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, EVENT_DISTRIBUTED_SERVICE_START, SECURITY, list);
513 OH_HiAppEvent_DestroyParamList(list);
514 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
515 }
516
517 /**
518 * @tc.name: HiAppEventNDKTest013
519 * @tc.desc: check the local file.
520 * @tc.type: FUNC
521 * @tc.require: AR000GIKMA
522 */
523 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest013, TestSize.Level0)
524 {
525 /**
526 * @tc.steps: step1. create a ParamList pointer.
527 * @tc.steps: step2. add params to the ParamList.
528 * @tc.steps: step3. write event to the file.
529 * @tc.steps: step4. check the result of logging.
530 * @tc.steps: step5. check the file.
531 */
532 ParamList list = OH_HiAppEvent_CreateParamList();
533 OH_HiAppEvent_AddInt32Param(list, "int_key", 123);
534 int res = OH_HiAppEvent_Write(TEST_DOMAIN_NAME, TEST_EVENT_NAME, BEHAVIOR, list);
535 OH_HiAppEvent_DestroyParamList(list);
536 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
537
538 sleep(1); // wait 1s for WriteEvent complete
539 std::string filePath = TEST_STORAGE_PATH + GetStorageFilePath();
540 ASSERT_EQ(access(filePath.c_str(), F_OK), 0);
541 }
542
543 /**
544 * @tc.name: HiAppEventNDKTest014
545 * @tc.desc: check the domain verification function of event logging.
546 * @tc.type: FUNC
547 * @tc.require: issueI8OY2U
548 */
549 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest014, TestSize.Level0)
550 {
551 int res = OH_HiAppEvent_Write(nullptr, TEST_EVENT_NAME, SECURITY, nullptr);
552 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
553
554 res = OH_HiAppEvent_Write("", TEST_EVENT_NAME, SECURITY, nullptr);
555 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
556
557 constexpr size_t limitLen = 32;
558 res = OH_HiAppEvent_Write(std::string(limitLen, 'a').c_str(), TEST_EVENT_NAME, SECURITY, nullptr);
559 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
560 res = OH_HiAppEvent_Write(std::string(limitLen + 1, 'a').c_str(), TEST_EVENT_NAME, SECURITY, nullptr);
561 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
562
563 std::string invalidDomain = std::string(limitLen - 1, 'a') + "_";
564 res = OH_HiAppEvent_Write(invalidDomain.c_str(), TEST_EVENT_NAME, SECURITY, nullptr);
565 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
566
567 res = OH_HiAppEvent_Write("AAAaaa", TEST_EVENT_NAME, SECURITY, nullptr);
568 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
569
570 res = OH_HiAppEvent_Write("abc***", TEST_EVENT_NAME, SECURITY, nullptr);
571 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
572
573 res = OH_HiAppEvent_Write("domain_", TEST_EVENT_NAME, SECURITY, nullptr);
574 ASSERT_EQ(res, ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
575
576 res = OH_HiAppEvent_Write("a", TEST_EVENT_NAME, SECURITY, nullptr);
577 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
578
579 res = OH_HiAppEvent_Write("a1", TEST_EVENT_NAME, SECURITY, nullptr);
580 ASSERT_EQ(res, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
581 }
582
583 /**
584 * @tc.name: HiAppEventNDKTest015
585 * @tc.desc: check the function of OH_HiAppEvent_CreateWatcher.
586 * @tc.type: FUNC
587 * @tc.require: issueI8OY2U
588 */
589 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest015, TestSize.Level0)
590 {
591 ASSERT_TRUE(OH_HiAppEvent_CreateWatcher(nullptr) == nullptr);
592 g_onReceiveWatcher = OH_HiAppEvent_CreateWatcher("test_onReceiver_watcher");
593 ASSERT_TRUE(g_onReceiveWatcher != nullptr);
594 g_onTriggerWatcher = OH_HiAppEvent_CreateWatcher("test_onTrigger_watcher");
595 ASSERT_TRUE(g_onTriggerWatcher != nullptr);
596 }
597
598 /**
599 * @tc.name: HiAppEventNDKTest016
600 * @tc.desc: check the function of OH_HiAppEvent_SetAppEventFilter.
601 * @tc.type: FUNC
602 * @tc.require: issueI8OY2U
603 */
604 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest016, TestSize.Level0)
605 {
606 const char* filterNames[] = {TEST_EVENT_NAME};
607 const char* filterNamesWithNullptr[] = {nullptr};
608 constexpr int namsLen = 1;
609 ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(nullptr, TEST_DOMAIN_NAME, 0, filterNames, namsLen),
610 ErrorCode::ERROR_INVALID_WATCHER);
611 ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(g_onReceiveWatcher, nullptr, 0, filterNames, namsLen),
612 ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
613 ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(g_onReceiveWatcher, TEST_DOMAIN_NAME, 0, nullptr, namsLen),
614 ErrorCode::ERROR_INVALID_EVENT_NAME);
615 ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(g_onReceiveWatcher, TEST_DOMAIN_NAME, 0, filterNamesWithNullptr, namsLen),
616 ErrorCode::ERROR_INVALID_EVENT_NAME);
617 ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(g_onReceiveWatcher, TEST_DOMAIN_NAME, 0, filterNames, namsLen), 0);
618 ASSERT_EQ(OH_HiAppEvent_SetAppEventFilter(g_onTriggerWatcher, TEST_DOMAIN_NAME, 0, filterNames, namsLen), 0);
619 }
620
621 /**
622 * @tc.name: HiAppEventNDKTest017
623 * @tc.desc: check the function of OH_HiAppEvent_SetWatcherOnReceiver.
624 * @tc.type: FUNC
625 * @tc.require: issueI8OY2U
626 */
627 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest017, TestSize.Level0)
628 {
629 ASSERT_EQ(OH_HiAppEvent_SetWatcherOnReceive(nullptr, OnReceive), ErrorCode::ERROR_INVALID_WATCHER);
630 ASSERT_EQ(OH_HiAppEvent_SetWatcherOnReceive(g_onReceiveWatcher, OnReceive), 0);
631 }
632
633 /**
634 * @tc.name: HiAppEventNDKTest018
635 * @tc.desc: check the function of OH_HiAppEvent_SetTriggerCondition.
636 * @tc.type: FUNC
637 * @tc.require: issueI8OY2U
638 */
639 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest018, TestSize.Level0)
640 {
641 ASSERT_EQ(OH_HiAppEvent_SetTriggerCondition(nullptr, TEST_EVENT_NUM, 0, 0), ErrorCode::ERROR_INVALID_WATCHER);
642 ASSERT_EQ(OH_HiAppEvent_SetTriggerCondition(g_onTriggerWatcher, TEST_EVENT_NUM, 0, 0), 0);
643 }
644
645 /**
646 * @tc.name: HiAppEventNDKTest019
647 * @tc.desc: check the function of OH_HiAppEvent_SetWatcherOnTrigger.
648 * @tc.type: FUNC
649 * @tc.require: issueI8OY2U
650 */
651 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest019, TestSize.Level0)
652 {
653 ASSERT_EQ(OH_HiAppEvent_SetWatcherOnTrigger(nullptr, OnTrigger), ErrorCode::ERROR_INVALID_WATCHER);
654 ASSERT_EQ(OH_HiAppEvent_SetWatcherOnTrigger(g_onTriggerWatcher, OnTrigger), 0);
655 }
656
657 /**
658 * @tc.name: HiAppEventNDKTest020
659 * @tc.desc: check the function of OH_HiAppEvent_AddWatcher.
660 * @tc.type: FUNC
661 * @tc.require: issueI8OY2U
662 */
663 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest020, TestSize.Level0)
664 {
665 ASSERT_EQ(OH_HiAppEvent_AddWatcher(nullptr), ErrorCode::ERROR_INVALID_WATCHER);
666 ASSERT_EQ(OH_HiAppEvent_AddWatcher(g_onTriggerWatcher), 0);
667 ASSERT_EQ(OH_HiAppEvent_AddWatcher(g_onReceiveWatcher), 0);
668 for (int i = 0; i < TEST_EVENT_NUM; ++i) {
669 WriteEvent();
670 }
671 }
672
673 /**
674 * @tc.name: HiAppEventNDKTest021
675 * @tc.desc: check the function of OH_HiAppEvent_TakeWatcherData.
676 * @tc.type: FUNC
677 * @tc.require: issueI8OY2U
678 */
679 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest021, TestSize.Level0)
680 {
681 constexpr uint32_t takeNum = 10;
682 sleep(1); // wait 1s for WriteEvent complete
683 ASSERT_EQ(OH_HiAppEvent_TakeWatcherData(nullptr, takeNum, OnTake), ErrorCode::ERROR_INVALID_WATCHER);
684 ASSERT_EQ(OH_HiAppEvent_TakeWatcherData(g_onTriggerWatcher, takeNum, OnTake), 0);
685 }
686
687 /**
688 * @tc.name: HiAppEventNDKTest022
689 * @tc.desc: check the function of OH_HiAppEvent_ClearData.
690 * @tc.type: FUNC
691 * @tc.require: issueI8OY2U
692 */
693 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest022, TestSize.Level0)
694 {
695 OH_HiAppEvent_ClearData();
696 ASSERT_EQ(HiAppEvent::UserInfo::GetInstance().GetUserIdVersion(), 0);
697 ASSERT_EQ(HiAppEvent::UserInfo::GetInstance().GetUserPropertyVersion(), 0);
698 }
699
700 /**
701 * @tc.name: HiAppEventNDKTest023
702 * @tc.desc: check the function of OH_HiAppEvent_RemoveWatcher.
703 * @tc.type: FUNC
704 * @tc.require: issueI8OY2U
705 */
706 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest023, TestSize.Level0)
707 {
708 ASSERT_EQ(OH_HiAppEvent_RemoveWatcher(g_onTriggerWatcher), 0);
709 ASSERT_EQ(OH_HiAppEvent_RemoveWatcher(g_onTriggerWatcher), ErrorCode::ERROR_WATCHER_NOT_ADDED);
710 ASSERT_EQ(OH_HiAppEvent_RemoveWatcher(g_onReceiveWatcher), 0);
711 ASSERT_EQ(OH_HiAppEvent_RemoveWatcher(g_onReceiveWatcher), ErrorCode::ERROR_WATCHER_NOT_ADDED);
712 OH_HiAppEvent_DestroyWatcher(g_onTriggerWatcher);
713 g_onTriggerWatcher = nullptr;
714 OH_HiAppEvent_DestroyWatcher(g_onReceiveWatcher);
715 g_onReceiveWatcher = nullptr;
716 }
717
718 /**
719 * @tc.name: HiAppEventNDKTest024
720 * @tc.desc: check ndk interface of AddProcessor.
721 * @tc.type: FUNC
722 */
723 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest024, TestSize.Level0)
724 {
725 setuid(0); // 0 means root uid
726 ASSERT_EQ(CreateProcessor(TEST_PROCESSOR_NAME), nullptr);
727 ASSERT_EQ(SetReportRoute(nullptr, nullptr, nullptr), ErrorCode::ERROR_NOT_APP);
728 ASSERT_EQ(SetReportPolicy(nullptr, 0, 0, false, false), ErrorCode::ERROR_NOT_APP);
729 ASSERT_EQ(SetReportEvent(nullptr, nullptr, nullptr, false), ErrorCode::ERROR_NOT_APP);
730 ASSERT_EQ(SetCustomConfig(nullptr, nullptr, nullptr), ErrorCode::ERROR_NOT_APP);
731 ASSERT_EQ(SetConfigId(nullptr, 0), ErrorCode::ERROR_NOT_APP);
732 ASSERT_EQ(SetReportUserId(nullptr, nullptr, 0), ErrorCode::ERROR_NOT_APP);
733 ASSERT_EQ(SetReportUserProperty(nullptr, nullptr, 0), ErrorCode::ERROR_NOT_APP);
734 ASSERT_EQ(AddProcessor(nullptr), ErrorCode::ERROR_NOT_APP);
735 DestroyProcessor(nullptr);
736 ASSERT_EQ(RemoveProcessor(0), ErrorCode::ERROR_NOT_APP);
737
738 // set app uid
739 setuid(TEST_UID);
740
741 ASSERT_EQ(CreateProcessor(nullptr), nullptr);
742 ASSERT_EQ(SetReportRoute(nullptr, nullptr, nullptr), ErrorCode::ERROR_INVALID_PROCESSOR);
743 ASSERT_EQ(SetReportPolicy(nullptr, 0, 0, false, false), ErrorCode::ERROR_INVALID_PROCESSOR);
744 ASSERT_EQ(SetReportEvent(nullptr, nullptr, nullptr, false), ErrorCode::ERROR_INVALID_PROCESSOR);
745 ASSERT_EQ(SetCustomConfig(nullptr, nullptr, nullptr), ErrorCode::ERROR_INVALID_PROCESSOR);
746 ASSERT_EQ(SetConfigId(nullptr, 0), ErrorCode::ERROR_INVALID_PROCESSOR);
747 ASSERT_EQ(SetReportUserId(nullptr, nullptr, 0), ErrorCode::ERROR_INVALID_PROCESSOR);
748 ASSERT_EQ(SetReportUserProperty(nullptr, nullptr, 0), ErrorCode::ERROR_INVALID_PROCESSOR);
749 ASSERT_EQ(AddProcessor(nullptr), ErrorCode::ERROR_INVALID_PROCESSOR);
750 DestroyProcessor(nullptr);
751 ASSERT_EQ(RemoveProcessor(0), ErrorCode::ERROR_PROCESSOR_NOT_ADDED);
752 }
753
754 /**
755 * @tc.name: HiAppEventNDKTest025
756 * @tc.desc: check ndk interface of AddProcessor.
757 * @tc.type: FUNC
758 */
759 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest025, TestSize.Level0)
760 {
761 ASSERT_EQ(CreateProcessor(""), nullptr);
762 auto processor = CreateProcessor(TEST_PROCESSOR_NAME);
763 ASSERT_TRUE(processor != nullptr);
764 ASSERT_EQ(SetReportRoute(processor, nullptr, nullptr), ErrorCode::ERROR_INVALID_PARAM_VALUE);
765 ASSERT_EQ(SetReportRoute(processor, "", ""), 0);
766 ASSERT_EQ(SetReportPolicy(processor, -1, 0, false, false), ErrorCode::ERROR_INVALID_PARAM_VALUE);
767 ASSERT_EQ(SetReportEvent(processor, nullptr, nullptr, false), ErrorCode::ERROR_INVALID_PARAM_VALUE);
768 ASSERT_EQ(SetCustomConfig(processor, nullptr, nullptr), ErrorCode::ERROR_INVALID_PARAM_VALUE);
769 ASSERT_EQ(SetCustomConfig(processor, "", ""), ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH);
770 ASSERT_EQ(SetConfigId(processor, -1), ErrorCode::ERROR_INVALID_PARAM_VALUE);
771 ASSERT_EQ(SetReportUserId(processor, nullptr, 0), ErrorCode::ERROR_INVALID_PARAM_VALUE);
772 const char* userStrs[] = {"aaa", ""};
773 ASSERT_EQ(SetReportUserId(processor, userStrs, 0), 0);
774 ASSERT_EQ(SetReportUserProperty(processor, nullptr, 0), ErrorCode::ERROR_INVALID_PARAM_VALUE);
775 ASSERT_EQ(SetReportUserProperty(processor, userStrs, 0), 0);
776 int seq = AddProcessor(processor);
777 ASSERT_GT(seq, 0);
778 DestroyProcessor(processor);
779 ASSERT_EQ(RemoveProcessor(seq), 0);
780 }
781
782 /**
783 * @tc.name: HiAppEventNDKTest026
784 * @tc.desc: check ndk interface of AddProcessor.
785 * @tc.type: FUNC
786 */
787 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest026, TestSize.Level0)
788 {
789 auto processor = CreateProcessor(TEST_PROCESSOR_NAME);
790 ASSERT_TRUE(processor != nullptr);
791 ASSERT_EQ(SetReportRoute(processor, "test_appid", "test_routeInfo"), 0);
792 ASSERT_EQ(SetReportPolicy(processor, 2, 2, false, false), 0);
793 ASSERT_EQ(SetReportEvent(processor, "test_domain", "test_name", false), 0);
794 ASSERT_EQ(SetCustomConfig(processor, "str_key", "str_value"), 0);
795 ASSERT_EQ(SetConfigId(processor, 1), 0);
796 const char* userIds[] = {"test_id"};
797 ASSERT_EQ(SetReportUserId(processor, userIds, 1), 0);
798 const char* userProperties[] = {"test_property"};
799 ASSERT_EQ(SetReportUserProperty(processor, userProperties, 1), 0);
800 int seq = AddProcessor(processor);
801 ASSERT_GT(seq, 0);
802 DestroyProcessor(processor);
803 ASSERT_EQ(RemoveProcessor(seq), 0);
804 }
805
806 /**
807 * @tc.name: HiAppEventNDKTest027
808 * @tc.desc: check the interface of SetEventItem.
809 * @tc.type: FUNC
810 */
811 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest027, TestSize.Level0)
812 {
813 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(nullptr, "testName", "testValue"), ErrorCode::ERROR_EVENT_CONFIG_IS_NULL);
814
815 int maxStrLen = 1024;
816 std::string longStr(maxStrLen, 'a');
817 std::string longInvalidStr(maxStrLen + 1, 'a');
818 HiAppEvent_Config* configDemo = OH_HiAppEvent_CreateConfig();
819
820 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "", "testValue"), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
821 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, longStr.c_str(), "testValue"),
822 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
823 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, nullptr, "testValue"), ErrorCode::ERROR_INVALID_PARAM_VALUE);
824 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, longInvalidStr.c_str(), "testValue"),
825 ErrorCode::ERROR_INVALID_PARAM_VALUE);
826
827 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "testName", ""), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
828 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "testName", longStr.c_str()),
829 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
830 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "testName", nullptr), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
831 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "testName", longInvalidStr.c_str()),
832 ErrorCode::ERROR_INVALID_PARAM_VALUE);
833
834 OH_HiAppEvent_DestroyConfig(configDemo);
835 }
836
837 /**
838 * @tc.name: HiAppEventNDKTest028
839 * @tc.desc: check the interface of SetEventConfig when name is invalid.
840 * @tc.type: FUNC
841 */
842 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest028, TestSize.Level0)
843 {
844 HiAppEvent_Config* configDemo = OH_HiAppEvent_CreateConfig();
845 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "log_type", "0"), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
846
847 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
848 ASSERT_EQ(OH_HiAppEvent_SetEventConfig(nullptr, configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
849 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("testName", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
850 int maxStrLen = 1024;
851 std::string longInvalidStr(maxStrLen + 1, 'a');
852 ASSERT_EQ(OH_HiAppEvent_SetEventConfig(longInvalidStr.c_str(), configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
853
854 OH_HiAppEvent_DestroyConfig(configDemo);
855 }
856
857 /**
858 * @tc.name: HiAppEventNDKTest029
859 * @tc.desc: check the interface of SetEventConfig for MAIN_THREAD_JANK when the log type is not customizable.
860 * @tc.type: FUNC
861 */
862 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest029, TestSize.Level0)
863 {
864 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", nullptr), ErrorCode::ERROR_INVALID_PARAM_VALUE);
865
866 HiAppEvent_Config* configDemo = OH_HiAppEvent_CreateConfig();
867 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
868
869 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "log_type", "-1"), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
870 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
871
872 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "log_type", "abc"), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
873 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
874
875 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "log_type", ""), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
876 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
877
878 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "log_type", nullptr), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
879 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
880
881 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "log_type", "100"), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
882 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
883
884 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "log_type", "0"), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
885 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
886
887 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "log_type", "2"), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
888 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
889
890 OH_HiAppEvent_DestroyConfig(configDemo);
891 }
892
893 /**
894 * @tc.name: HiAppEventNDKTest030
895 * @tc.desc: check the interface of SetEventConfig for MAIN_THREAD_JANK when the log type is customizable.
896 * @tc.type: FUNC
897 */
898 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest030, TestSize.Level0)
899 {
900 HiAppEvent_Config* configDemo = OH_HiAppEvent_CreateConfig();
901 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "log_type", "1"), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
902 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
903
904 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "ignore_startup_time", "10"),
905 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
906 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "sample_interval", "100"),
907 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
908 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "report_times_per_app", "3"),
909 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
910 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "sample_count", "21"), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
911 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
912
913 OH_HiAppEvent_DestroyConfig(configDemo);
914 }
915
916 /**
917 * @tc.name: HiAppEventNDKTest031
918 * @tc.desc: check the interface of SetEventConfig for MAIN_THREAD_JANK when event params value is invalid.
919 * @tc.type: FUNC
920 */
921 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest031, TestSize.Level0)
922 {
923 HiAppEvent_Config* configDemo = OH_HiAppEvent_CreateConfig();
924 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "log_type", "1"), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
925
926 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "sample_count", "21"), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
927 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "ignore_startup_time", "10"),
928 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
929 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "report_times_per_app", "3"),
930 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
931 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "sample_interval", "50"),
932 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL); // sample_interval range is [50, 1000]
933
934 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
935
936 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "sample_interval", "-1"),
937 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
938 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
939
940 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "sample_interval", "49"),
941 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
942 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
943
944 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "sample_interval", "aa"),
945 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
946 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
947
948 std::string maxValue = "92233720368547758079223372036854775807";
949 ASSERT_EQ(OH_HiAppEvent_SetConfigItem(configDemo, "sample_interval", maxValue.c_str()),
950 ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
951 ASSERT_EQ(OH_HiAppEvent_SetEventConfig("MAIN_THREAD_JANK", configDemo), ErrorCode::ERROR_INVALID_PARAM_VALUE);
952
953 OH_HiAppEvent_DestroyConfig(configDemo);
954 }
955
956 /**
957 * @tc.name: HiAppEventNDKTest032
958 * @tc.desc: check the AddIntParam function when the input value is nullptr.
959 * @tc.type: FUNC
960 */
961 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest032, TestSize.Level0)
962 {
963 ParamList list = OH_HiAppEvent_CreateParamList();
964
965 int8_t num1 = 1;
966 ParamList listRes = OH_HiAppEvent_AddInt8Param(nullptr, nullptr, num1);
967 EXPECT_EQ(listRes, nullptr);
968 listRes = OH_HiAppEvent_AddInt8ArrayParam(nullptr, nullptr, nullptr, 0);
969 EXPECT_EQ(listRes, nullptr);
970 listRes = OH_HiAppEvent_AddInt8ArrayParam(list, nullptr, nullptr, 0);
971 EXPECT_EQ(listRes, list);
972
973 int16_t num2 = 1;
974 listRes = OH_HiAppEvent_AddInt16Param(nullptr, nullptr, num2);
975 EXPECT_EQ(listRes, nullptr);
976 listRes = OH_HiAppEvent_AddInt16ArrayParam(nullptr, nullptr, nullptr, 0);
977 EXPECT_EQ(listRes, nullptr);
978 listRes = OH_HiAppEvent_AddInt16ArrayParam(list, nullptr, nullptr, 0);
979 EXPECT_EQ(listRes, list);
980
981 int32_t num3 = 1;
982 listRes = OH_HiAppEvent_AddInt32Param(nullptr, nullptr, num3);
983 EXPECT_EQ(listRes, nullptr);
984 listRes = OH_HiAppEvent_AddInt32ArrayParam(nullptr, nullptr, nullptr, 0);
985 EXPECT_EQ(listRes, nullptr);
986 listRes = OH_HiAppEvent_AddInt32ArrayParam(list, nullptr, nullptr, 0);
987 EXPECT_EQ(listRes, list);
988
989 int64_t num4 = 1;
990 listRes = OH_HiAppEvent_AddInt64Param(nullptr, nullptr, num4);
991 EXPECT_EQ(listRes, nullptr);
992 listRes = OH_HiAppEvent_AddInt64ArrayParam(nullptr, nullptr, nullptr, 0);
993 EXPECT_EQ(listRes, nullptr);
994 listRes = OH_HiAppEvent_AddInt64ArrayParam(list, nullptr, nullptr, 0);
995 EXPECT_EQ(listRes, list);
996
997 OH_HiAppEvent_DestroyParamList(list);
998 }
999
1000 /**
1001 * @tc.name: HiAppEventNDKTest033
1002 * @tc.desc: check the Add except int Param function when the input value is nullptr.
1003 * @tc.type: FUNC
1004 */
1005 HWTEST_F(HiAppEventNativeTest, HiAppEventNDKTest033, TestSize.Level0)
1006 {
1007 ParamList list = OH_HiAppEvent_CreateParamList();
1008 ParamList listRes = OH_HiAppEvent_AddBoolParam(nullptr, nullptr, true);
1009 EXPECT_EQ(listRes, nullptr);
1010 listRes = OH_HiAppEvent_AddBoolArrayParam(nullptr, nullptr, nullptr, 0);
1011 EXPECT_EQ(listRes, nullptr);
1012 listRes = OH_HiAppEvent_AddBoolArrayParam(list, nullptr, nullptr, 0);
1013 EXPECT_EQ(listRes, list);
1014
1015 float num1 = 456.1234;
1016 listRes = OH_HiAppEvent_AddFloatParam(nullptr, nullptr, num1);
1017 EXPECT_EQ(listRes, nullptr);
1018 listRes = OH_HiAppEvent_AddFloatArrayParam(nullptr, nullptr, nullptr, 0);
1019 EXPECT_EQ(listRes, nullptr);
1020 listRes = OH_HiAppEvent_AddFloatArrayParam(list, nullptr, nullptr, 0);
1021 EXPECT_EQ(listRes, list);
1022
1023 double num2 = 456.1234;
1024 listRes = OH_HiAppEvent_AddDoubleParam(nullptr, nullptr, num2);
1025 EXPECT_EQ(listRes, nullptr);
1026 listRes = OH_HiAppEvent_AddDoubleArrayParam(nullptr, nullptr, nullptr, 0);
1027 EXPECT_EQ(listRes, nullptr);
1028 listRes = OH_HiAppEvent_AddDoubleArrayParam(list, nullptr, nullptr, 0);
1029 EXPECT_EQ(listRes, list);
1030
1031 char str1[] = "hello";
1032 listRes = OH_HiAppEvent_AddStringParam(nullptr, nullptr, str1);
1033 EXPECT_EQ(listRes, nullptr);
1034 listRes = OH_HiAppEvent_AddStringArrayParam(nullptr, nullptr, nullptr, 0);
1035 EXPECT_EQ(listRes, nullptr);
1036 listRes = OH_HiAppEvent_AddStringArrayParam(list, nullptr, nullptr, 0);
1037 EXPECT_EQ(listRes, list);
1038
1039 OH_HiAppEvent_DestroyParamList(list);
1040 }
1041