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