1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <iostream>
16
17 #include <gtest/gtest.h>
18
19 #include "app_event.h"
20 #include "hiappevent_base.h"
21 #include "hiappevent_config.h"
22
23 using namespace testing::ext;
24 using namespace OHOS::HiviewDFX;
25 using namespace OHOS::HiviewDFX::ErrorCode;
26 using namespace OHOS::HiviewDFX::HiAppEvent;
27
28 namespace {
29 const std::string TEST_DIR = "/data/test/hiappevent/";
30 const std::string TEST_DOMAIN = "test_domain";
31 const std::string TEST_NAME = "test_domain";
32 const EventType TEST_TYPE = FAULT;
33 constexpr int32_t TEST_INT_VALUE = 1;
34 const std::string TEST_STR_VALUE = "test_value";
35
36 class HiAppEventAppEventTest : public testing::Test {
37 public:
38 void SetUp();
39 void TearDown();
40 };
41
SetUp()42 void HiAppEventAppEventTest::SetUp()
43 {
44 HiAppEventConfig::GetInstance().SetStorageDir(TEST_DIR);
45 }
46
TearDown()47 void HiAppEventAppEventTest::TearDown()
48 {}
49 }
50
51 /**
52 * @tc.name: HiAppEventAppEventTest001
53 * @tc.desc: Test the writing of normal data.
54 * @tc.type: FUNC
55 */
56 HWTEST_F(HiAppEventAppEventTest, HiAppEventAppEventTest001, TestSize.Level1)
57 {
58 std::cout << "HiAppEventAppEventTest001 start" << std::endl;
59
60 Event event(TEST_DOMAIN, TEST_NAME, FAULT);
61 event.AddParam("Bool_key", true);
62 constexpr int32_t num1 = 1;
63 event.AddParam("int32_key", num1);
64 constexpr int64_t num2 = 1;
65 event.AddParam("$int64_key", num2);
66 constexpr double num3 = 123.456;
67 event.AddParam("dou_key1", num3);
68 event.AddParam("str_key", TEST_STR_VALUE);
69
70 const std::vector<bool> bs = {false, true};
71 event.AddParam("bools_key", bs);
72 const std::vector<int32_t> num1s = {INT32_MAX, INT32_MIN};
73 event.AddParam("int32s_key", num1s);
74 const std::vector<int64_t> num2s = {INT64_MAX, INT64_MIN};
75 event.AddParam("$int64s_key", num2s);
76 const std::vector<double> num3s = {123.456, 234.67890123};
77 event.AddParam("double_key", num3s);
78 const std::vector<std::string> strs = {TEST_STR_VALUE, TEST_STR_VALUE};
79 event.AddParam("strs_key", strs);
80
81 int ret = Write(event);
82 ASSERT_EQ(ret, HIAPPEVENT_VERIFY_SUCCESSFUL);
83
84 std::cout << "HiAppEventAppEventTest001 end" << std::endl;
85 }
86
87 /**
88 * @tc.name: HiAppEventAppEventTest002
89 * @tc.desc: Test the writing of invalid param name.
90 * @tc.type: FUNC
91 */
92 HWTEST_F(HiAppEventAppEventTest, HiAppEventAppEventTest002, TestSize.Level1)
93 {
94 std::cout << "HiAppEventAppEventTest001 start" << std::endl;
95
96 Event event1(TEST_DOMAIN, TEST_NAME, FAULT);
97 event1.AddParam("**", TEST_INT_VALUE);
98 ASSERT_EQ(Write(event1), ERROR_INVALID_PARAM_NAME);
99 Event event2(TEST_DOMAIN, TEST_NAME, STATISTIC);
100 event2.AddParam("aa_", TEST_INT_VALUE);
101 ASSERT_EQ(Write(event2), ERROR_INVALID_PARAM_NAME);
102 Event event3(TEST_DOMAIN, TEST_NAME, SECURITY);
103 event3.AddParam("", TEST_INT_VALUE);
104 ASSERT_EQ(Write(event3), ERROR_INVALID_PARAM_NAME);
105 Event event4(TEST_DOMAIN, TEST_NAME, BEHAVIOR);
106 event4.AddParam("123AAA", TEST_INT_VALUE);
107 ASSERT_EQ(Write(event4), ERROR_INVALID_PARAM_NAME);
108
109 constexpr size_t limitLen = 32;
110 Event event5(TEST_DOMAIN, TEST_NAME, BEHAVIOR);
111 event5.AddParam(std::string(limitLen, 'a'), TEST_INT_VALUE);
112 ASSERT_EQ(Write(event5), HIAPPEVENT_VERIFY_SUCCESSFUL);
113 Event event6(TEST_DOMAIN, TEST_NAME, BEHAVIOR);
114 event6.AddParam(std::string(limitLen + 1, 'a'), TEST_INT_VALUE);
115 ASSERT_EQ(Write(event6), ERROR_INVALID_PARAM_NAME);
116 Event event7(TEST_DOMAIN, TEST_NAME, BEHAVIOR);
117 event7.AddParam(std::string(limitLen - 1, 'a') + "_", TEST_INT_VALUE);
118 ASSERT_EQ(Write(event7), ERROR_INVALID_PARAM_NAME);
119
120 std::cout << "HiAppEventAppEventTest002 end" << std::endl;
121 }
122
123 /**
124 * @tc.name: HiAppEventAppEventTest003
125 * @tc.desc: Test the writing of invalid length string.
126 * @tc.type: FUNC
127 */
128 HWTEST_F(HiAppEventAppEventTest, HiAppEventAppEventTest003, TestSize.Level1)
129 {
130 std::cout << "HiAppEventAppEventTest003 start" << std::endl;
131
132 constexpr size_t maxStrLen = 8 * 1024;
133 std::string longStr(maxStrLen, 'a');
134 Event event1(TEST_DOMAIN, TEST_NAME, TEST_TYPE);
135 event1.AddParam("str_key", longStr);
136 ASSERT_EQ(Write(event1), HIAPPEVENT_VERIFY_SUCCESSFUL);
137
138 std::string longInvalidStr(maxStrLen + 1, 'a');
139 Event event2(TEST_DOMAIN, TEST_NAME, TEST_TYPE);
140 event2.AddParam("str_key", longInvalidStr);
141 ASSERT_EQ(Write(event2), ERROR_INVALID_PARAM_VALUE_LENGTH);
142
143 Event event3(TEST_DOMAIN, TEST_NAME, TEST_TYPE);
144 const std::vector<std::string> strs1 = {longStr};
145 event3.AddParam("strs_key", strs1);
146 ASSERT_EQ(Write(event3), HIAPPEVENT_VERIFY_SUCCESSFUL);
147
148 Event event4(TEST_DOMAIN, TEST_NAME, TEST_TYPE);
149 const std::vector<std::string> strs2 = {longInvalidStr};
150 event4.AddParam("strs_key", strs2);
151 ASSERT_EQ(Write(event4), ERROR_INVALID_PARAM_VALUE_LENGTH);
152
153 std::cout << "HiAppEventAppEventTest003 end" << std::endl;
154 }
155
156 /**
157 * @tc.name: HiAppEventAppEventTest004
158 * @tc.desc: Test the writing of invalid number of params.
159 * @tc.type: FUNC
160 */
161 HWTEST_F(HiAppEventAppEventTest, HiAppEventAppEventTest004, TestSize.Level1)
162 {
163 std::cout << "HiAppEventAppEventTest004 start" << std::endl;
164
165 constexpr size_t limitLen = 32;
166 std::vector<std::string> keys(limitLen + 1);
167 std::vector<std::string> values(limitLen + 1);
168 Event event1(TEST_DOMAIN, TEST_NAME, TEST_TYPE);
169 for (size_t i = 0; i <= limitLen; i++) {
170 keys[i] = "key" + std::to_string(i);
171 values[i] = "value" + std::to_string(i);
172 event1.AddParam(keys[i], values[i]);
173 }
174 ASSERT_EQ(Write(event1), ERROR_INVALID_PARAM_NUM);
175
176 Event event2(TEST_DOMAIN, TEST_NAME, TEST_TYPE);
177 ASSERT_EQ(Write(event2), HIAPPEVENT_VERIFY_SUCCESSFUL);
178
179 Event event3(TEST_DOMAIN, TEST_NAME, TEST_TYPE);
180 for (size_t i = 0; i < limitLen; i++) {
181 event3.AddParam(keys[i], values[i]);
182 }
183 ASSERT_EQ(Write(event3), HIAPPEVENT_VERIFY_SUCCESSFUL);
184
185 std::cout << "HiAppEventAppEventTest004 end" << std::endl;
186 }
187
188 /**
189 * @tc.name: HiAppEventAppEventTest005
190 * @tc.desc: Test the writing of invalid event name.
191 * @tc.type: FUNC
192 */
193 HWTEST_F(HiAppEventAppEventTest, HiAppEventAppEventTest005, TestSize.Level1)
194 {
195 std::cout << "HiAppEventAppEventTest005 start" << std::endl;
196
197 Event event(TEST_DOMAIN, "$event", TEST_TYPE);
198 ASSERT_EQ(Write(event), HIAPPEVENT_VERIFY_SUCCESSFUL);
199
200 Event event1(TEST_DOMAIN, "verify_**", TEST_TYPE);
201 ASSERT_EQ(Write(event1), ERROR_INVALID_EVENT_NAME);
202 Event event2(TEST_DOMAIN, "aa_", TEST_TYPE);
203 ASSERT_EQ(Write(event1), ERROR_INVALID_EVENT_NAME);
204 Event event3(TEST_DOMAIN, "123AAA", TEST_TYPE);
205 ASSERT_EQ(Write(event3), ERROR_INVALID_EVENT_NAME);
206 Event event4(TEST_DOMAIN, "", TEST_TYPE);
207 ASSERT_EQ(Write(event4), ERROR_INVALID_EVENT_NAME);
208
209 constexpr size_t limitLen = 48;
210 Event event5(TEST_DOMAIN, std::string(limitLen, 'a'), BEHAVIOR);
211 ASSERT_EQ(Write(event5), HIAPPEVENT_VERIFY_SUCCESSFUL);
212 Event event6(TEST_DOMAIN, std::string(limitLen + 1, 'a'), BEHAVIOR);
213 ASSERT_EQ(Write(event6), ERROR_INVALID_EVENT_NAME);
214
215 std::cout << "HiAppEventAppEventTest005 end" << std::endl;
216 }
217
218 /**
219 * @tc.name: HiAppEventAppEventTest006
220 * @tc.desc: Test the writing of invalid domain name.
221 * @tc.type: FUNC
222 */
223 HWTEST_F(HiAppEventAppEventTest, HiAppEventAppEventTest006, TestSize.Level1)
224 {
225 std::cout << "HiAppEventAppEventTest006 start" << std::endl;
226
227 Event event("domain_123_a", TEST_NAME, TEST_TYPE);
228 ASSERT_EQ(Write(event), HIAPPEVENT_VERIFY_SUCCESSFUL);
229
230 Event event1("Domain", TEST_NAME, TEST_TYPE);
231 ASSERT_EQ(Write(event1), HIAPPEVENT_VERIFY_SUCCESSFUL);
232 Event event2("123_domain", TEST_NAME, TEST_TYPE);
233 ASSERT_EQ(Write(event2), ERROR_INVALID_EVENT_DOMAIN);
234 Event event3("domain**", TEST_NAME, TEST_TYPE);
235 ASSERT_EQ(Write(event3), ERROR_INVALID_EVENT_DOMAIN);
236 Event event4("domain_", TEST_NAME, TEST_TYPE);
237 ASSERT_EQ(Write(event4), ERROR_INVALID_EVENT_DOMAIN);
238
239 constexpr size_t limitLen = 32;
240 Event event5(std::string(limitLen, 'a'), TEST_NAME, BEHAVIOR);
241 ASSERT_EQ(Write(event5), HIAPPEVENT_VERIFY_SUCCESSFUL);
242 Event event6(std::string(limitLen + 1, 'a'), TEST_NAME, BEHAVIOR);
243 ASSERT_EQ(Write(event6), ERROR_INVALID_EVENT_DOMAIN);
244 Event event7("", TEST_NAME, BEHAVIOR);
245 ASSERT_EQ(Write(event7), ERROR_INVALID_EVENT_DOMAIN);
246 Event event8(std::string(limitLen - 1, 'a') + "_", TEST_NAME, BEHAVIOR);
247 ASSERT_EQ(Write(event8), ERROR_INVALID_EVENT_DOMAIN);
248
249 std::cout << "HiAppEventAppEventTest006 end" << std::endl;
250 }
251
252 /**
253 * @tc.name: HiAppEventAppEventTest007
254 * @tc.desc: Test the writing of invalid vector size.
255 * @tc.type: FUNC
256 */
257 HWTEST_F(HiAppEventAppEventTest, HiAppEventAppEventTest007, TestSize.Level1)
258 {
259 std::cout << "HiAppEventAppEventTest007 start" << std::endl;
260
261 Event event1(TEST_DOMAIN, TEST_NAME, TEST_TYPE);
262 constexpr size_t limitSize = 100;
263 std::vector<int32_t> nums1(limitSize, TEST_INT_VALUE);
264 event1.AddParam("valid_vec", nums1);
265 ASSERT_EQ(Write(event1), HIAPPEVENT_VERIFY_SUCCESSFUL);
266
267 Event event2(TEST_DOMAIN, TEST_NAME, TEST_TYPE);
268 std::vector<int32_t> nums2(limitSize + 1, TEST_INT_VALUE);
269 event2.AddParam("invalid_vec", nums2);
270 ASSERT_EQ(Write(event2), ERROR_INVALID_LIST_PARAM_SIZE);
271
272 std::cout << "HiAppEventAppEventTest007 end" << std::endl;
273 }
274
275 /**
276 * @tc.name: HiAppEventAppEventTest008
277 * @tc.desc: Test the writing of logging function is disabled.
278 * @tc.type: FUNC
279 */
280 HWTEST_F(HiAppEventAppEventTest, HiAppEventAppEventTest008, TestSize.Level1)
281 {
282 std::cout << "HiAppEventAppEventTest008 start" << std::endl;
283
284 HiAppEventConfig::GetInstance().SetConfigurationItem("disable", "false");
285 Event event(TEST_DOMAIN, TEST_NAME, TEST_TYPE);
286 ASSERT_EQ(Write(event), HIAPPEVENT_VERIFY_SUCCESSFUL);
287
288 HiAppEventConfig::GetInstance().SetConfigurationItem("disable", "true");
289 ASSERT_EQ(Write(event), ERROR_HIAPPEVENT_DISABLE);
290
291 HiAppEventConfig::GetInstance().SetConfigurationItem("disable", "false");
292 ASSERT_EQ(Write(event), HIAPPEVENT_VERIFY_SUCCESSFUL);
293
294 std::cout << "HiAppEventAppEventTest008 end" << std::endl;
295 }
296
297 /**
298 * @tc.name: HiAppEventAppEventTest009
299 * @tc.desc: Test the writing of duplicate param.
300 * @tc.type: FUNC
301 */
302 HWTEST_F(HiAppEventAppEventTest, HiAppEventAppEventTest009, TestSize.Level1)
303 {
304 std::cout << "HiAppEventAppEventTest009 start" << std::endl;
305
306 Event event1(TEST_DOMAIN, TEST_NAME, TEST_TYPE);
307 std::vector<std::string> strs = {TEST_STR_VALUE};
308 event1.AddParam("str_key", strs);
309 ASSERT_EQ(Write(event1), HIAPPEVENT_VERIFY_SUCCESSFUL);
310 event1.AddParam("str_key", TEST_STR_VALUE);
311 ASSERT_EQ(Write(event1), ERROR_DUPLICATE_PARAM);
312
313 std::cout << "HiAppEventAppEventTest009 end" << std::endl;
314 }
315