1 /*
2 * Copyright (c) 2021 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 "sys_event_test.h"
16
17 #include <ctime>
18 #include <iostream>
19 #include <limits>
20 #include <memory>
21 #include <regex>
22 #include <vector>
23
24 #include "sys_event.h"
25
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace {
29 constexpr int SCALE_FACTOR = 2;
30 const std::vector<int64_t> ORIGIN_INT_VEC = {1, 2, 3};
31 const std::vector<uint64_t> ORIGIN_UINT_VEC = {1, 2, 3};
32 const std::vector<double> ORIGIN_INT_TO_DOUBLE_VEC = {1.0, 2.0, 3.0};
33 const std::vector<double> ORIGIN_DOUBLE_VEC = {1.0, 2.2, 4.8};
34 const std::vector<int64_t> ORIGIN_DOUBLE_TO_INT_VEC = {1, 2, 4};
35 const std::vector<uint64_t> ORIGIN_DOUBLE_TO_UINT_VEC = {1, 2, 4};
36
37 template<typename T>
TransNumberVecToStr(const std::vector<T> & nums)38 std::string TransNumberVecToStr(const std::vector<T>& nums)
39 {
40 std::string jsonStr = "[";
41 if (nums.empty()) {
42 jsonStr.append("]");
43 return jsonStr;
44 }
45 for (auto num : nums) {
46 jsonStr.append(std::to_string(num));
47 jsonStr.append(",");
48 }
49 jsonStr.erase(jsonStr.length() - 1);
50 jsonStr.append("]");
51 return jsonStr;
52 }
53
GetOriginTestString()54 std::string GetOriginTestString()
55 {
56 std::string jsonStr = R"~({"domain_":"DEMO","name_":"NAME1","type_":1,"tz_":"+0800","time_":1620271291188,
57 "pid_":6527, "tid_":6527, "traceid_":"f0ed6160bb2df4b", "spanid_":"10", "pspanid_":"20", "trace_flag_":4,)~";
58 jsonStr.append(R"~("EMPTY_ARRAY":[],"INT_ARRAY1":[)~");
59 jsonStr.append(std::to_string(std::numeric_limits<int64_t>::min()));
60 jsonStr.append(R"~(,)~");
61 jsonStr.append(std::to_string(std::numeric_limits<int64_t>::max()));
62 jsonStr.append(R"~(],"INT_ARRAY2":)~");
63 jsonStr.append(TransNumberVecToStr(ORIGIN_INT_VEC));
64 jsonStr.append(R"~(,"UINT_ARRAY1":[)~");
65 jsonStr.append(std::to_string(std::numeric_limits<uint64_t>::min()));
66 jsonStr.append(R"~(,)~");
67 jsonStr.append(std::to_string(std::numeric_limits<uint64_t>::max()));
68 jsonStr.append(R"~(],"UINT_ARRAY2":)~");
69 jsonStr.append(TransNumberVecToStr(ORIGIN_UINT_VEC));
70 jsonStr.append(R"~(,"FLOAT_ARRAY1":[)~");
71 jsonStr.append(std::to_string((static_cast<double>(std::numeric_limits<int64_t>::min()) * SCALE_FACTOR)));
72 jsonStr.append(R"~(,)~");
73 jsonStr.append(std::to_string((static_cast<double>(std::numeric_limits<uint64_t>::max()) * SCALE_FACTOR)));
74 jsonStr.append(R"~(],"FLOAT_ARRAY2":)~");
75 jsonStr.append(TransNumberVecToStr(ORIGIN_DOUBLE_VEC));
76 jsonStr.append(R"~(,"STR_ARRAY":["STR1","STR2","STR3"]})~");
77 return jsonStr;
78 }
79 }
SetUpTestCase()80 void SysEventTest::SetUpTestCase()
81 {
82 }
83
TearDownTestCase()84 void SysEventTest::TearDownTestCase()
85 {
86 }
87
SetUp()88 void SysEventTest::SetUp()
89 {
90 }
91
TearDown()92 void SysEventTest::TearDown()
93 {
94 }
95
96 /**
97 * @tc.name: TestSendBaseType001
98 * @tc.desc: create base sys event
99 * @tc.type: FUNC
100 * @tc.require: AR000FT2Q2 AR000FT2Q3
101 */
102 HWTEST_F(SysEventTest, TestSendBaseType001, testing::ext::TestSize.Level3)
103 {
104 /**
105 * @tc.steps: step1. create base sys event
106 */
107 std::cout << "TestSendBaseType001 test base type" << std::endl;
108 SysEventCreator sysEventCreator("DEMO", "EVENT_NAME", SysEventCreator::FAULT);
109 sysEventCreator.SetKeyValue("KEY", 1);
110 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("test", nullptr, sysEventCreator);
111 std::regex expValue(R"~(\{"domain_":"DEMO","name_":"EVENT_NAME","type_":1,"time_":\d+,"tz_":"[\+\-]\d+",)~"
112 R"~("pid_":\d+,"tid_":\d+,"uid_":\d+,"log_":0,"id_":"\d+","KEY":1\})~");
113 std::cout << "size=" << sysEvent->AsJsonStr().size() << ", jsonStr:" << sysEvent->AsJsonStr() << std::endl;
114 std::smatch baseMatch;
115 auto eventJsonStr = sysEvent->AsJsonStr();
116 bool isMatch = std::regex_match(eventJsonStr, baseMatch, expValue);
117 ASSERT_TRUE(isMatch);
118 }
119
120 /**
121 * @tc.name: TestSendIntVectorType002
122 * @tc.desc: create base sys event
123 * @tc.type: FUNC
124 * @tc.require: AR000FT2Q2 AR000FT2Q3
125 */
126 HWTEST_F(SysEventTest, TestSendIntVectorType002, testing::ext::TestSize.Level3)
127 {
128 /**
129 * @tc.steps: step1. create base sys event
130 */
131 std::cout << "TestSendIntVectorType002 test vector<int> type" << std::endl;
132 SysEventCreator sysEventCreator("DEMO", "EVENT_NAME", SysEventCreator::FAULT);
133 std::vector<int> values = {1, 2, 3};
134 sysEventCreator.SetKeyValue("KEY", values);
135 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("test", nullptr, sysEventCreator);
136 std::regex expValue(R"~(\{"domain_":"DEMO","name_":"EVENT_NAME","type_":1,"time_":\d+,"tz_":"[\+\-]\d+",)~"
137 R"~("pid_":\d+,"tid_":\d+,"uid_":\d+,"log_":0,"id_":"\d+","KEY":\[1,2,3\]\})~");
138 std::cout << "size=" << sysEvent->AsJsonStr().size() << ", jsonStr:" << sysEvent->AsJsonStr() << std::endl;
139 std::smatch baseMatch;
140 auto eventJsonStr = sysEvent->AsJsonStr();
141 bool isMatch = std::regex_match(eventJsonStr, baseMatch, expValue);
142 ASSERT_TRUE(isMatch);
143 }
144
145 /**
146 * @tc.name: TestSysEventValueParse001
147 * @tc.desc: Parse customized value as int64_t type from sys event
148 * @tc.type: FUNC
149 * @tc.require: issueI7V7ZA
150 */
151 HWTEST_F(SysEventTest, TestSysEventValueParse001, testing::ext::TestSize.Level3)
152 {
153 /**
154 * @tc.steps: step1. create base sys event
155 */
156 std::string jsonStr = R"~({"domain_":"DEMO","name_":"VALUE_PARSE001","type_":1,"tz_":"+0800","time_":1620271291188,
157 "pid_":6527,"tid_":6527,"traceid_":"f0ed5160bb2df4b","spanid_":"10","pspanid_":"20","trace_flag_":4,)~";
158 jsonStr.append(R"~("INT_VAL1":-1,"INT_VAL2":1,"INT_VAL3":)~");
159 jsonStr.append(std::to_string(std::numeric_limits<int64_t>::min()));
160 jsonStr.append(R"~(,"INT_VAL4":)~");
161 jsonStr.append(std::to_string(std::numeric_limits<int64_t>::max()));
162 jsonStr.append(R"~(,"UINT_VAL1":10,"UINT_VAL2":1000,"UINT_VAL3":)~");
163 jsonStr.append(std::to_string(std::numeric_limits<uint64_t>::min()));
164 jsonStr.append(R"~(,"UINT_VAL4":)~");
165 jsonStr.append(std::to_string(std::numeric_limits<uint64_t>::max()));
166 jsonStr.append("}");
167
168 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, jsonStr);
169 ASSERT_TRUE(sysEvent != nullptr);
170 int64_t dest = sysEvent->GetEventIntValue("INT_VAL1");
171 ASSERT_EQ(dest, -1); // test value
172 dest = sysEvent->GetEventIntValue("INT_VAL2");
173 ASSERT_EQ(dest, 1); // test value
174 dest = sysEvent->GetEventIntValue("INT_VAL3");
175 ASSERT_EQ(dest, std::numeric_limits<int64_t>::min());
176 dest = sysEvent->GetEventIntValue("INT_VAL4");
177 ASSERT_EQ(dest, std::numeric_limits<int64_t>::max());
178 dest = sysEvent->GetEventIntValue("UINT_VAL1");
179 ASSERT_EQ(dest, 10); // test value
180 dest = sysEvent->GetEventIntValue("UINT_VAL2");
181 ASSERT_EQ(dest, 1000); // test value
182 dest = sysEvent->GetEventIntValue("UINT_VAL3");
183 ASSERT_EQ(dest, static_cast<int64_t>(std::numeric_limits<uint64_t>::min()));
184 dest = sysEvent->GetEventIntValue("UINT_VAL4");
185 ASSERT_EQ(dest, 0); // test value
186 }
187
188 /**
189 * @tc.name: TestSysEventValueParse002
190 * @tc.desc: Parse customized value as uint64_t type from sys event
191 * @tc.type: FUNC
192 * @tc.require: issueI7V7ZA
193 */
194 HWTEST_F(SysEventTest, TestSysEventValueParse002, testing::ext::TestSize.Level3)
195 {
196 /**
197 * @tc.steps: step1. create base sys event
198 */
199 std::string jsonStr = R"~({"domain_":"DEMO","name_":"VALUE_PARSE001","type_":1,"tz_":"+0800","time_":1620271291188,
200 "pid_":6527,"tid_":6527,"traceid_":"f0ed5160bb2df4b","spanid_":"10","pspanid_":"20","trace_flag_":4,)~";
201 jsonStr.append(R"~("INT_VAL1":-1,"INT_VAL2":1,"INT_VAL3":)~");
202 jsonStr.append(std::to_string(std::numeric_limits<int64_t>::min()));
203 jsonStr.append(R"~(,"INT_VAL4":)~");
204 jsonStr.append(std::to_string(std::numeric_limits<int64_t>::max()));
205 jsonStr.append(R"~(,"UINT_VAL1":10,"UINT_VAL2":1000,"UINT_VAL3":)~");
206 jsonStr.append(std::to_string(std::numeric_limits<uint64_t>::min()));
207 jsonStr.append(R"~(,"UINT_VAL4":)~");
208 jsonStr.append(std::to_string(std::numeric_limits<uint64_t>::max()));
209 jsonStr.append("}");
210
211 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, jsonStr);
212 ASSERT_TRUE(sysEvent != nullptr);
213 uint64_t dest = sysEvent->GetEventUintValue("INT_VAL1");
214 ASSERT_EQ(dest, 0); // test value
215 dest = sysEvent->GetEventUintValue("INT_VAL2");
216 ASSERT_EQ(dest, 1); // test value
217 dest = sysEvent->GetEventUintValue("INT_VAL3");
218 ASSERT_EQ(dest, 0); // test value
219 dest = sysEvent->GetEventUintValue("INT_VAL4");
220 ASSERT_EQ(dest, static_cast<uint64_t>(std::numeric_limits<int64_t>::max()));
221 dest = sysEvent->GetEventUintValue("UINT_VAL1");
222 ASSERT_EQ(dest, 10); // test value
223 dest = sysEvent->GetEventUintValue("UINT_VAL2");
224 ASSERT_EQ(dest, 1000); // test value
225 dest = sysEvent->GetEventUintValue("UINT_VAL3");
226 ASSERT_EQ(dest, std::numeric_limits<uint64_t>::min());
227 dest = sysEvent->GetEventUintValue("UINT_VAL4");
228 ASSERT_EQ(dest, std::numeric_limits<uint64_t>::max());
229 }
230
231 /**
232 * @tc.name: TestSysEventValueParse003
233 * @tc.desc: Parse customized value as double type from sys event
234 * @tc.type: FUNC
235 * @tc.require: issueI7V7ZA
236 */
237 HWTEST_F(SysEventTest, TestSysEventValueParse003, testing::ext::TestSize.Level3)
238 {
239 /**
240 * @tc.steps: step1. create base sys event
241 */
242 std::string jsonStr = R"~({"domain_":"DEMO", "name_":"VALUE_PARSE001", "type_":1, "tz_":"+0800",
243 "time_":1620271291188, "pid_":6527, "tid_":6527, "traceid_":"f0ed5160bb2df4b", "spanid_":"10",
244 "pspanid_":"20", "trace_flag_":4, "FLOAT_VAL1":-1.0, "FLOAT_VAL2":1.0})~";
245
246 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, jsonStr);
247 ASSERT_TRUE(sysEvent != nullptr);
248 double dest = sysEvent->GetEventDoubleValue("FLOAT_VAL1");
249 ASSERT_EQ(dest, -1); // test value
250 dest = sysEvent->GetEventDoubleValue("FLOAT_VAL2");
251 ASSERT_EQ(dest, 1); // test value
252 }
253
254 /**
255 * @tc.name: TestSysEventValueParse004
256 * @tc.desc: Parse base value as int64_t type from sys event
257 * @tc.type: FUNC
258 * @tc.require: issueI7V7ZA
259 */
260 HWTEST_F(SysEventTest, TestSysEventValueParse004, testing::ext::TestSize.Level3)
261 {
262 /**
263 * @tc.steps: step1. create base sys event
264 */
265 std::string jsonStr = R"~({"domain_":"DEMO", "name_":"VALUE_PARSE001", "type_":1, "tz_":"+0800",
266 "time_":1620271291188, "pid_":6527, "tid_":-6527, "traceid_":"f0ed5160bb2df4b", "spanid_":"10",
267 "pspanid_":"20", "trace_flag_":4, "FLOAT_VAL1":-1.0, "FLOAT_VAL2":1.0,)~";
268 jsonStr.append(R"~("FLOAT_VAL3":)~");
269 jsonStr.append(std::to_string((static_cast<double>(std::numeric_limits<int64_t>::min()) * SCALE_FACTOR)));
270 jsonStr.append(R"~("FLOAT_VAL4":)~");
271 jsonStr.append(std::to_string((static_cast<double>(std::numeric_limits<int64_t>::max()) * SCALE_FACTOR)));
272 jsonStr.append("}");
273
274 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, jsonStr);
275 ASSERT_TRUE(sysEvent != nullptr);
276 int64_t dest = sysEvent->GetEventIntValue("domain_");
277 ASSERT_EQ(dest, 0); // test value
278 dest = sysEvent->GetEventIntValue("type_");
279 ASSERT_EQ(dest, 1); // test value
280 dest = sysEvent->GetEventIntValue("time_");
281 ASSERT_EQ(dest, 1620271291188); // test value
282 dest = sysEvent->GetEventIntValue("tz_");
283 ASSERT_EQ(dest, 27); // test value
284 dest = sysEvent->GetEventIntValue("pid_");
285 ASSERT_EQ(dest, 6527); // test value
286 dest = sysEvent->GetEventIntValue("traceid_");
287 ASSERT_EQ(dest, 1085038850905136971); // test value: f0ed5160bb2df4b
288 dest = sysEvent->GetEventIntValue("trace_flag_");
289 ASSERT_EQ(dest, 4); // test value
290 dest = sysEvent->GetEventIntValue("FLOAT_VAL1");
291 ASSERT_EQ(dest, -1); // test value
292 dest = sysEvent->GetEventIntValue("FLOAT_VAL2");
293 ASSERT_EQ(dest, 1); // test value
294 dest = sysEvent->GetEventIntValue("FLOAT_VAL3");
295 ASSERT_EQ(dest, 0); // test value
296 dest = sysEvent->GetEventIntValue("FLOAT_VAL4");
297 ASSERT_EQ(dest, 0); // test value
298 }
299
300 /**
301 * @tc.name: TestSysEventValueParse005
302 * @tc.desc: Parse base value as uint64_t type from sys event
303 * @tc.type: FUNC
304 * @tc.require: issueI7V7ZA
305 */
306 HWTEST_F(SysEventTest, TestSysEventValueParse005, testing::ext::TestSize.Level3)
307 {
308 /**
309 * @tc.steps: step1. create base sys event
310 */
311 std::string jsonStr = R"~({"domain_":"DEMO", "name_":"VALUE_PARSE001", "type_":1, "tz_":"+0800",
312 "time_":1620271291188, "pid_":6527, "tid_":6527, "traceid_":"f0ed5160bb2df4b", "spanid_":"10",
313 "pspanid_":"20", "trace_flag_":4, "FLOAT_VAL1":-1.0, "FLOAT_VAL2":1.0,)~";
314 jsonStr.append(R"~("FLOAT_VAL3":)~");
315 jsonStr.append(std::to_string((static_cast<double>(std::numeric_limits<uint64_t>::min()) * SCALE_FACTOR)));
316 jsonStr.append(R"~("FLOAT_VAL4":)~");
317 jsonStr.append(std::to_string((static_cast<double>(std::numeric_limits<uint64_t>::max()) * SCALE_FACTOR)));
318 jsonStr.append("}");
319
320 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, jsonStr);
321 ASSERT_TRUE(sysEvent != nullptr);
322 uint64_t dest = sysEvent->GetEventUintValue("domain_");
323 ASSERT_EQ(dest, 0); // test value
324 dest = sysEvent->GetEventUintValue("type_");
325 ASSERT_EQ(dest, 1); // test value
326 dest = sysEvent->GetEventUintValue("time_");
327 ASSERT_EQ(dest, 1620271291188); // test value
328 dest = sysEvent->GetEventUintValue("tz_");
329 ASSERT_EQ(dest, 27); // test value
330 dest = sysEvent->GetEventUintValue("pid_");
331 ASSERT_EQ(dest, 6527); // test value
332 dest = sysEvent->GetEventUintValue("traceid_");
333 ASSERT_EQ(dest, 1085038850905136971); // test value: f0ed5160bb2df4b
334 dest = sysEvent->GetEventUintValue("trace_flag_");
335 ASSERT_EQ(dest, 4); // test value
336 dest = sysEvent->GetEventUintValue("FLOAT_VAL1");
337 ASSERT_EQ(dest, 0); // test value
338 dest = sysEvent->GetEventUintValue("FLOAT_VAL2");
339 ASSERT_EQ(dest, 1); // test value
340 dest = sysEvent->GetEventUintValue("FLOAT_VAL3");
341 ASSERT_EQ(dest, 0); // test value
342 dest = sysEvent->GetEventUintValue("FLOAT_VAL4");
343 ASSERT_EQ(dest, 0); // test value
344 }
345
346 /**
347 * @tc.name: TestSysEventValueParse006
348 * @tc.desc: Parse base value as double type from sys event
349 * @tc.type: FUNC
350 * @tc.require: issueI7V7ZA
351 */
352 HWTEST_F(SysEventTest, TestSysEventValueParse006, testing::ext::TestSize.Level3)
353 {
354 /**
355 * @tc.steps: step1. create base sys event
356 */
357 std::string jsonStr = R"~({"domain_":"DEMO", "name_":"VALUE_PARSE001", "type_":1, "tz_":"+0800",
358 "time_":1620271291188, "pid_":6527, "tid_":6527, "traceid_":"f0ed5160bb2df4b", "spanid_":"10",
359 "pspanid_":"20", "trace_flag_":4, "FLOAT_VAL1":-1.0, "FLOAT_VAL2":1.0})~";
360
361 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, jsonStr);
362 ASSERT_TRUE(sysEvent != nullptr);
363 double dest = sysEvent->GetEventDoubleValue("domain_");
364 ASSERT_EQ(dest, 0.0); // test value
365 dest = sysEvent->GetEventDoubleValue("type_");
366 ASSERT_EQ(dest, 1); // test value
367 dest = sysEvent->GetEventDoubleValue("time_");
368 ASSERT_EQ(dest, 1620271291188); // test value
369 dest = sysEvent->GetEventDoubleValue("tz_");
370 ASSERT_EQ(dest, 27); // test value
371 dest = sysEvent->GetEventDoubleValue("pid_");
372 ASSERT_EQ(dest, 6527); // test value
373 dest = sysEvent->GetEventDoubleValue("trace_flag_");
374 ASSERT_EQ(dest, 4); // test value
375 dest = sysEvent->GetEventDoubleValue("FLOAT_VAL1");
376 ASSERT_EQ(dest, -1); // test value
377 dest = sysEvent->GetEventDoubleValue("FLOAT_VAL2");
378 ASSERT_EQ(dest, 1); // test value
379 }
380
381 /**
382 * @tc.name: TestSysEventValueParse007
383 * @tc.desc: Parse base value as string type from sys event
384 * @tc.type: FUNC
385 * @tc.require: issueI7V7ZA
386 */
387 HWTEST_F(SysEventTest, TestSysEventValueParse007, testing::ext::TestSize.Level3)
388 {
389 /**
390 * @tc.steps: step1. create base sys event
391 */
392 std::string jsonStr = R"~({"domain_":"DEMO", "name_":"VALUE_PARSE001", "type_":1, "tz_":"+0800",
393 "time_":1620271291188, "pid_":6527, "tid_":6527, "traceid_":"f0ed5160bb2df4b", "spanid_":"10",
394 "pspanid_":"20", "trace_flag_":4, "FLOAT_VAL1":-1.0, "FLOAT_VAL2":1.0})~";
395
396 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, jsonStr);
397 ASSERT_TRUE(sysEvent != nullptr);
398 std::string dest = sysEvent->GetEventValue("domain_");
399 ASSERT_EQ(dest, "DEMO"); // test value
400 dest = sysEvent->GetEventValue("type_");
401 ASSERT_EQ(dest, ""); // test value
402 dest = sysEvent->GetEventValue("time_");
403 ASSERT_EQ(dest, ""); // test value
404 dest = sysEvent->GetEventValue("tz_");
405 ASSERT_EQ(dest, "+0800"); // test value
406 dest = sysEvent->GetEventValue("pid_");
407 ASSERT_EQ(dest, ""); // test value
408 dest = sysEvent->GetEventValue("traceid_");
409 ASSERT_EQ(dest, "f0ed5160bb2df4b"); // test value
410 dest = sysEvent->GetEventValue("trace_flag_");
411 ASSERT_EQ(dest, ""); // test value
412 dest = sysEvent->GetEventValue("FLOAT_VAL1");
413 ASSERT_EQ(dest, ""); // test value
414 dest = sysEvent->GetEventValue("FLOAT_VAL2");
415 ASSERT_EQ(dest, ""); // test value
416 }
417
418 /**
419 * @tc.name: TestSysEventValueParse008
420 * @tc.desc: Parse base value as int64_t array from sys event
421 * @tc.type: FUNC
422 * @tc.require: issueI7V7ZA
423 */
424 HWTEST_F(SysEventTest, TestSysEventValueParse008, testing::ext::TestSize.Level3)
425 {
426 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, GetOriginTestString());
427 ASSERT_TRUE(sysEvent != nullptr);
428 std::vector<int64_t> dest1;
429 ASSERT_TRUE(sysEvent->GetEventIntArrayValue("INT_ARRAY1", dest1));
430 ASSERT_EQ(dest1.size(), 2); // test value
431 ASSERT_TRUE((dest1[0] == std::numeric_limits<int64_t>::min()) &&
432 (dest1[1] == std::numeric_limits<int64_t>::max()));
433 std::vector<int64_t> dest2;
434 ASSERT_TRUE(sysEvent->GetEventIntArrayValue("INT_ARRAY2", dest2));
435 ASSERT_EQ(dest2, ORIGIN_INT_VEC);
436 std::vector<int64_t> dest3;
437 ASSERT_TRUE(!sysEvent->GetEventIntArrayValue("UINT_ARRAY1", dest3));
438 ASSERT_TRUE(dest3.empty());
439 std::vector<int64_t> dest4;
440 ASSERT_TRUE(sysEvent->GetEventIntArrayValue("UINT_ARRAY2", dest4));
441 ASSERT_EQ(dest4, ORIGIN_INT_VEC);
442 std::vector<int64_t> dest5;
443 ASSERT_TRUE(!sysEvent->GetEventIntArrayValue("FLOAT_ARRAY1", dest5));
444 ASSERT_TRUE(dest5.empty());
445 std::vector<int64_t> dest6;
446 ASSERT_TRUE(sysEvent->GetEventIntArrayValue("FLOAT_ARRAY2", dest6));
447 ASSERT_EQ(dest6, ORIGIN_DOUBLE_TO_INT_VEC);
448 std::vector<int64_t> dest7;
449 ASSERT_TRUE(!sysEvent->GetEventIntArrayValue("STR_ARRAY", dest7));
450 ASSERT_TRUE(dest7.empty());
451 std::vector<int64_t> dest8;
452 ASSERT_TRUE(sysEvent->GetEventIntArrayValue("EMPTY_ARRAY", dest8));
453 ASSERT_TRUE(dest8.empty());
454 }
455
456 /**
457 * @tc.name: TestSysEventValueParse009
458 * @tc.desc: Parse base value as uint64_t array from sys event
459 * @tc.type: FUNC
460 * @tc.require: issueI7V7ZA
461 */
462 HWTEST_F(SysEventTest, TestSysEventValueParse009, testing::ext::TestSize.Level3)
463 {
464 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, GetOriginTestString());
465 ASSERT_TRUE(sysEvent != nullptr);
466 std::vector<uint64_t> dest1;
467 ASSERT_TRUE(!sysEvent->GetEventUintArrayValue("INT_ARRAY1", dest1));
468 ASSERT_TRUE(dest1.empty());
469 std::vector<uint64_t> dest2;
470 ASSERT_TRUE(sysEvent->GetEventUintArrayValue("INT_ARRAY2", dest2));
471 ASSERT_EQ(dest2, ORIGIN_UINT_VEC);
472 std::vector<uint64_t> dest3;
473 ASSERT_TRUE(sysEvent->GetEventUintArrayValue("UINT_ARRAY1", dest3));
474 ASSERT_EQ(dest3.size(), 2); // test value
475 ASSERT_TRUE((dest3[0] == std::numeric_limits<uint64_t>::min()) &&
476 (dest3[1] == std::numeric_limits<uint64_t>::max()));
477 std::vector<uint64_t> dest4;
478 ASSERT_TRUE(sysEvent->GetEventUintArrayValue("UINT_ARRAY2", dest4));
479 ASSERT_EQ(dest4, ORIGIN_UINT_VEC);
480 std::vector<uint64_t> dest5;
481 ASSERT_TRUE(!sysEvent->GetEventUintArrayValue("FLOAT_ARRAY1", dest5));
482 ASSERT_TRUE(dest5.empty());
483 std::vector<uint64_t> dest6;
484 ASSERT_TRUE(sysEvent->GetEventUintArrayValue("FLOAT_ARRAY2", dest6));
485 ASSERT_EQ(dest6, ORIGIN_DOUBLE_TO_UINT_VEC);
486 std::vector<uint64_t> dest7;
487 ASSERT_TRUE(!sysEvent->GetEventUintArrayValue("STR_ARRAY", dest7));
488 ASSERT_TRUE(dest7.empty());
489 std::vector<uint64_t> dest8;
490 ASSERT_TRUE(sysEvent->GetEventUintArrayValue("EMPTY_ARRAY", dest8));
491 ASSERT_TRUE(dest8.empty());
492 }
493
494 /**
495 * @tc.name: TestSysEventValueParse010
496 * @tc.desc: Parse base value as double array from sys event
497 * @tc.type: FUNC
498 * @tc.require: issueI7V7ZA
499 */
500 HWTEST_F(SysEventTest, TestSysEventValueParse010, testing::ext::TestSize.Level3)
501 {
502 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, GetOriginTestString());
503 ASSERT_TRUE(sysEvent != nullptr);
504 std::vector<double> dest1;
505 ASSERT_TRUE(sysEvent->GetEventDoubleArrayValue("INT_ARRAY1", dest1));
506 ASSERT_EQ(dest1.size(), 2); // test value
507 ASSERT_EQ(dest1[0], std::numeric_limits<int64_t>::min());
508 ASSERT_EQ(dest1[1], std::numeric_limits<int64_t>::max());
509 std::vector<double> dest2;
510 ASSERT_TRUE(sysEvent->GetEventDoubleArrayValue("INT_ARRAY2", dest2));
511 ASSERT_EQ(dest2, ORIGIN_INT_TO_DOUBLE_VEC);
512 std::vector<double> dest3;
513 ASSERT_TRUE(sysEvent->GetEventDoubleArrayValue("UINT_ARRAY1", dest3));
514 ASSERT_EQ(dest3.size(), 2); // test value
515 ASSERT_EQ(dest3[0], std::numeric_limits<uint64_t>::min());
516 ASSERT_EQ(dest3[1], std::numeric_limits<uint64_t>::max());
517 std::vector<double> dest4;
518 ASSERT_TRUE(sysEvent->GetEventDoubleArrayValue("UINT_ARRAY2", dest4));
519 ASSERT_EQ(dest4, ORIGIN_INT_TO_DOUBLE_VEC);
520 std::vector<double> dest5;
521 ASSERT_TRUE(sysEvent->GetEventDoubleArrayValue("FLOAT_ARRAY1", dest5));
522 ASSERT_EQ(dest5.size(), 2); // test value
523 ASSERT_EQ(dest5[0], (static_cast<double>(std::numeric_limits<int64_t>::min()) * SCALE_FACTOR));
524 ASSERT_EQ(dest5[1], (static_cast<double>(std::numeric_limits<uint64_t>::max()) * SCALE_FACTOR));
525 std::vector<double> dest6;
526 ASSERT_TRUE(sysEvent->GetEventDoubleArrayValue("FLOAT_ARRAY2", dest6));
527 ASSERT_EQ(dest6, ORIGIN_DOUBLE_VEC);
528 std::vector<double> dest7;
529 ASSERT_TRUE(!sysEvent->GetEventDoubleArrayValue("STR_ARRAY", dest7));
530 ASSERT_TRUE(dest7.empty());
531 std::vector<double> dest8;
532 ASSERT_TRUE(sysEvent->GetEventDoubleArrayValue("EMPTY_ARRAY", dest8));
533 ASSERT_TRUE(dest8.empty());
534 }
535
536 /**
537 * @tc.name: TestSysEventValueParse011
538 * @tc.desc: Parse base value as string array from sys event
539 * @tc.type: FUNC
540 * @tc.require: issueI7V7ZA
541 */
542 HWTEST_F(SysEventTest, TestSysEventValueParse011, testing::ext::TestSize.Level3)
543 {
544 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, GetOriginTestString());
545 ASSERT_TRUE(sysEvent != nullptr);
546 std::vector<std::string> dest1;
547 ASSERT_TRUE(!sysEvent->GetEventStringArrayValue("INT_ARRAY1", dest1));
548 ASSERT_TRUE(dest1.empty());
549 std::vector<std::string> dest2;
550 ASSERT_TRUE(!sysEvent->GetEventStringArrayValue("INT_ARRAY2", dest2));
551 ASSERT_TRUE(dest2.empty());
552 std::vector<std::string> dest3;
553 ASSERT_TRUE(!sysEvent->GetEventStringArrayValue("UINT_ARRAY1", dest3));
554 ASSERT_TRUE(dest3.empty());
555 std::vector<std::string> dest4;
556 ASSERT_TRUE(!sysEvent->GetEventStringArrayValue("UINT_ARRAY2", dest4));
557 ASSERT_TRUE(dest4.empty());
558 std::vector<std::string> dest5;
559 ASSERT_TRUE(!sysEvent->GetEventStringArrayValue("FLOAT_ARRAY1", dest5));
560 ASSERT_TRUE(dest5.empty());
561 std::vector<std::string> dest6;
562 ASSERT_TRUE(!sysEvent->GetEventStringArrayValue("FLOAT_ARRAY2", dest6));
563 ASSERT_TRUE(dest6.empty());
564 std::vector<std::string> dest7;
565 ASSERT_TRUE(sysEvent->GetEventStringArrayValue("STR_ARRAY", dest7));
566 std::vector<std::string> origin = {"STR1", "STR2", "STR3"};
567 ASSERT_EQ(dest7, origin);
568 std::vector<std::string> dest8;
569 ASSERT_TRUE(sysEvent->GetEventStringArrayValue("EMPTY_ARRAY", dest8));
570 ASSERT_TRUE(dest8.empty());
571 }
572
573 /**
574 * @tc.name: TestSysEventTranslation01
575 * @tc.desc: Test translation from sys event to json string
576 * @tc.type: FUNC
577 * @tc.require: issueI91RBZ
578 */
579 HWTEST_F(SysEventTest, TestSysEventTranslation01, testing::ext::TestSize.Level3)
580 {
581 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, GetOriginTestString());
582 ASSERT_TRUE(sysEvent != nullptr);
583 auto jsonStrOrigin = sysEvent->AsJsonStr();
584 auto jsonStrBeforeValueAppend = sysEvent->AsJsonStr();
585 ASSERT_EQ(jsonStrOrigin, jsonStrBeforeValueAppend);
586 sysEvent->SetEventValue("TEST_KEY", "test value1");
587 auto jsonStrAfterValueAppend = sysEvent->AsJsonStr();
588 auto foundRet = jsonStrBeforeValueAppend.find("\"TEST_KEY\":\"test value1\"");
589 ASSERT_TRUE(foundRet == std::string::npos);
590 foundRet = jsonStrAfterValueAppend.find("\"TEST_KEY\":\"test value1\"");
591 ASSERT_TRUE(foundRet != std::string::npos);
592 sysEvent->SetEventValue("TEST_KEY", "test value2");
593 jsonStrAfterValueAppend = sysEvent->AsJsonStr();
594 foundRet = jsonStrAfterValueAppend.find("\"TEST_KEY\":\"test value1\"");
595 ASSERT_TRUE(foundRet == std::string::npos);
596 foundRet = jsonStrAfterValueAppend.find("\"TEST_KEY\":\"test value2\"");
597 ASSERT_TRUE(foundRet != std::string::npos);
598 }
599
600 /**
601 * @tc.name: TestSysEventTranslation02
602 * @tc.desc: Test translation from sys event to binary raw data
603 * @tc.type: FUNC
604 * @tc.require: issueI91RBZ
605 */
606 HWTEST_F(SysEventTest, TestSysEventTranslation02, testing::ext::TestSize.Level3)
607 {
608 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, GetOriginTestString());
609 ASSERT_TRUE(sysEvent != nullptr);
610 auto rawData = sysEvent->AsRawData();
611 int32_t dataLength = *(reinterpret_cast<int32_t*>(rawData));
612 ASSERT_TRUE(rawData != nullptr);
613 auto rawDataBeforeValueAppend = sysEvent->AsRawData();
614 ASSERT_TRUE(rawDataBeforeValueAppend != nullptr);
615 ASSERT_EQ(dataLength, *(reinterpret_cast<int32_t*>(rawDataBeforeValueAppend)));
616 sysEvent->SetEventValue("TEST_KEY", "test value1");
617 auto rawDataAfterValueAppend = sysEvent->AsRawData();
618 ASSERT_TRUE(rawDataAfterValueAppend != nullptr);
619 int32_t dataLengthAfterValueAppend = *(reinterpret_cast<int32_t*>(rawDataAfterValueAppend));
620 ASSERT_NE(dataLength, dataLengthAfterValueAppend);
621 sysEvent->SetEventValue("TEST_KEY", "test value123");
622 auto rawDataAfterValueAppendNew = sysEvent->AsRawData();
623 int32_t dataLengthAfterValueAppendNew = *(reinterpret_cast<int32_t*>(rawDataAfterValueAppendNew));
624 ASSERT_NE(dataLengthAfterValueAppend, dataLengthAfterValueAppendNew);
625 }
626
627 /**
628 * @tc.name: TestSetIdAndSetLog001
629 * @tc.desc: Test SetId & SetLog apis of SysEvent
630 * @tc.type: FUNC
631 * @tc.require: issueIAH9IC
632 */
633 HWTEST_F(SysEventTest, TestSetIdAndSetLog001, testing::ext::TestSize.Level3)
634 {
635 auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, GetOriginTestString());
636 ASSERT_TRUE(sysEvent != nullptr);
637 uint64_t id = 1; // 1 is a test id value
638 sysEvent->SetId(id);
639 uint8_t log = 0; // 0 is a test log value
640 sysEvent->SetLog(log);
641 auto eventStr = sysEvent->AsJsonStr();
642 ASSERT_NE(eventStr, "");
643 std::string matchedIdContent = std::string("\"id_\":\"00000000000000000001\"");
644 ASSERT_NE(eventStr.find(matchedIdContent), std::string::npos);
645 std::string matchedLogContent = std::string("\"log_\":") + std::to_string(log);
646 ASSERT_NE(eventStr.find(matchedLogContent), std::string::npos);
647 }
648 } // HiviewDFX
649 } // OHOS