1/* 2 * Copyright (C) 2021-2022 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 16import hiSysEvent from "@ohos.hiSysEvent" 17 18import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 19 20describe('hiSysEventJsUnitTest', function () { 21 beforeAll(function() { 22 23 /** 24 * @tc.setup: setup invoked before all test cases 25 */ 26 console.info('hiSysEventJsUnitTest beforeAll called') 27 }) 28 29 afterAll(function() { 30 31 /** 32 * @tc.teardown: teardown invoked after all test cases 33 */ 34 console.info('hiSysEventJsUnitTest afterAll called') 35 }) 36 37 beforeEach(function() { 38 39 /** 40 * @tc.setup: setup invoked before each test case 41 */ 42 console.info('hiSysEventJsUnitTest beforeEach called') 43 }) 44 45 afterEach(function() { 46 47 /** 48 * @tc.teardown: teardown invoked after each test case 49 */ 50 console.info('hiSysEventJsUnitTest afterEach called') 51 }) 52 53 const DEFAULT_QUERY_TIME = -1 54 55 const DEFAULT_ID = 1 56 57 const PERMISSION_ERR = 201 58 59 const PARAM_CHECK_ERR = 401 60 61 const INVALID_QUERY_RULE_ERR = 11200302 62 63 const QUERY_TOO_FREQUENTLY_ERR = 11200304 64 65 const DELAY_1500_MS = 1500 66 67 const DELAY_2000_MS = 2000 68 69 const QUERY_1_ITEM = 1 70 71 const QUERY_2_ITEMS = 2 72 73 const defaultEventInfo = { 74 PID: DEFAULT_ID, 75 UID: DEFAULT_ID, 76 PACKAGE_NAME: "com.ohos.testHiSysEvent", 77 PROCESS_NAME: "hiview js test suite", 78 MSG: "no msg." 79 } 80 81 const defaultQueryArg = { 82 beginTime: DEFAULT_QUERY_TIME, 83 endTime: DEFAULT_QUERY_TIME, 84 maxEvents: -1, // -1 means query cnt with no limit 85 } 86 87 const queryArgWithOneCnt = { 88 beginTime: DEFAULT_QUERY_TIME, 89 endTime: DEFAULT_QUERY_TIME, 90 maxEvents: QUERY_1_ITEM, // 1 is query count 91 } 92 93 const queryArgWithTwoCnt = { 94 beginTime: DEFAULT_QUERY_TIME, 95 endTime: DEFAULT_QUERY_TIME, 96 maxEvents: QUERY_2_ITEMS, 97 } 98 99 const queryArgWithFiveCnt = { 100 beginTime: DEFAULT_QUERY_TIME, 101 endTime: DEFAULT_QUERY_TIME, 102 maxEvents: 5, // 5 is query count 103 } 104 105 function GetParam(src, key) { 106 return src["params"][key] 107 } 108 109 function GetArrayIemParamByIndex(src, key, index) { 110 let arrayInSrc = src["params"][key] 111 if (index > arrayInSrc.length) { 112 return undefined 113 } 114 return arrayInSrc[index] 115 } 116 117 function writeEventWithAsyncWork(domain, name, eventType, params, errCallback, normalCallback, done) { 118 try { 119 hiSysEvent.write({ 120 domain: domain, 121 name: name, 122 eventType: eventType, 123 params: params 124 }, (err, val) => { 125 if (err) { 126 errCallback(err) 127 } else { 128 normalCallback(val) 129 } 130 done() 131 }) 132 } catch (err) { 133 expect(false).assertTrue() 134 done() 135 } 136 } 137 138 function writeEventWithPromise(domain, name, eventType, params, errCallback, normalCallback, done) { 139 try { 140 hiSysEvent.write({ 141 domain: domain, 142 name: name, 143 eventType: eventType, 144 params: params 145 }).then((val) => { 146 normalCallback(val) 147 done() 148 }).catch((err) => { 149 errCallback(err) 150 done() 151 }) 152 } catch (err) { 153 expect(false).assertTrue() 154 done() 155 } 156 } 157 158 function writeCustomizedSysEvent(customized) { 159 try { 160 hiSysEvent.write({ 161 domain: "RELIABILITY", 162 name: "STACK", 163 eventType: hiSysEvent.EventType.FAULT, 164 params: customized 165 }, (err, val) => { 166 if (err) { 167 console.error(`callback: err.code = ${err.code}, error msg is ${err.message}`) 168 expect(false).assertTrue() 169 done() 170 } 171 }) 172 } catch (err) { 173 expect(false).assertTrue() 174 done() 175 } 176 } 177 178 function writeDefaultSysEvent() { 179 writeCustomizedSysEvent(defaultEventInfo) 180 } 181 182 function querySysEvent(queryArgs, querRules, onQueryCallback, onCompleteCallback, 183 errCallback, done) { 184 try { 185 hiSysEvent.query(queryArgs, querRules, { 186 onQuery: function (infos) { 187 onQueryCallback(infos) 188 }, 189 onComplete: function(reason, total) { 190 onCompleteCallback(reason, total) 191 done() 192 } 193 }) 194 } catch (err) { 195 errCallback(err) 196 done() 197 } 198 } 199 200 /** 201 * @tc.desc: Test hisysevent writing with calling AsyncCallback. 202 * @tc.level: Level 0 203 * @tc.name: hiSysEventJsUnitTest001 204 * @tc.number: hiSysEventJsUnitTest001 205 * @tc.type: Function 206 * @tc.size: MediumTest 207 */ 208 it('hiSysEventJsUnitTest001', 0, async function (done) { 209 writeEventWithAsyncWork("RELIABILITY", "STACK", hiSysEvent.EventType.FAULT, defaultEventInfo, 210 (err) => { 211 expect(false).assertTrue() 212 }, 213 (val) => { 214 expect(val).assertEqual(0) 215 }, done) 216 }) 217 218 /** 219 * @tc.desc: Test hisysevent writing with returning Promise. 220 * @tc.level: Level 0 221 * @tc.name: hiSysEventJsUnitTest002 222 * @tc.number: hiSysEventJsUnitTest002 223 * @tc.type: Function 224 * @tc.size: MediumTest 225 */ 226 it('hiSysEventJsUnitTest002', 0, async function (done) { 227 writeEventWithPromise("RELIABILITY", "STACK", hiSysEvent.EventType.FAULT, defaultEventInfo, 228 (err) => { 229 expect(false).assertTrue() 230 }, 231 (val) => { 232 expect(val).assertEqual(0) 233 }, done) 234 }) 235 236 /** 237 * @tc.desc: Test function return of adding/remove hisysevent watcher result. 238 * @tc.level: Level 0 239 * @tc.name: hiSysEventJsUnitTest003 240 * @tc.number: hiSysEventJsUnitTest003 241 * @tc.type: Function 242 * @tc.size: MediumTest 243 */ 244 it('hiSysEventJsUnitTest003', 0, async function (done) { 245 let watcher = { 246 rules: [{ 247 domain: "RELIABILITY", 248 name: "STACK", 249 ruleType: hiSysEvent.RuleType.WHOLE_WORD, 250 }], 251 onEvent: (info) => { 252 }, 253 onServiceDied: () => { 254 } 255 } 256 try { 257 hiSysEvent.addWatcher(watcher) 258 hiSysEvent.removeWatcher(watcher) 259 expect(true).assertTrue() 260 done(); 261 } catch (err) { 262 expect(err.code == PERMISSION_ERR).assertTrue() 263 done() 264 } 265 }) 266 267 /** 268 * @tc.desc: Test watcher callback. 269 * @tc.level: Level 0 270 * @tc.name: hiSysEventJsUnitTest004 271 * @tc.number: hiSysEventJsUnitTest004 272 * @tc.type: Function 273 * @tc.size: MediumTest 274 */ 275 it('hiSysEventJsUnitTest004', 0, async function (done) { 276 let watcher = { 277 rules: [{ 278 domain: "RELIABILITY", 279 name: "STACK", 280 tag: "STABILITY", 281 ruleType: hiSysEvent.RuleType.WHOLE_WORD, 282 }], 283 onEvent: (info) => { 284 expect(Object.keys(info).length > 0).assertTrue() 285 }, 286 onServiceDied: () => { 287 } 288 } 289 try { 290 hiSysEvent.addWatcher(watcher) 291 writeDefaultSysEvent() 292 setTimeout(() => { 293 try { 294 hiSysEvent.removeWatcher(watcher) 295 expect(true).assertTrue() 296 done() 297 } catch (err) { 298 expect(err.code == PERMISSION_ERR).assertTrue() 299 done() 300 } 301 }, 1000) 302 } catch (err) { 303 expect(err.code == PERMISSION_ERR).assertTrue() 304 done() 305 } 306 }) 307 308 /** 309 * @tc.desc: Test query callback. 310 * @tc.level: Level 0 311 * @tc.name: hiSysEventJsUnitTest005 312 * @tc.number: hiSysEventJsUnitTest005 313 * @tc.type: Function 314 * @tc.size: MediumTest 315 */ 316 it('hiSysEventJsUnitTest005', 0, async function (done) { 317 try { 318 writeDefaultSysEvent() 319 setTimeout(() => { 320 querySysEvent(queryArgWithTwoCnt, 321 [{ 322 domain: "RELIABILITY", 323 names: ["STACK"], 324 }], (infos) => { 325 expect(infos.length >= 0).assertTrue() 326 }, (reason, total) => { 327 expect(true).assertTrue() 328 }, (err) => { 329 expect(err.code == PERMISSION_ERR).assertTrue() 330 }, done) 331 }, 1000); 332 } catch (err) { 333 expect(err.code == PERMISSION_ERR).assertTrue() 334 done() 335 } 336 }) 337 338 /** 339 * @tc.desc: Test query callback with domain which length is over 16. 340 * @tc.level: Level 0 341 * @tc.name: hiSysEventJsUnitTest006 342 * @tc.number: hiSysEventJsUnitTest006 343 * @tc.type: Function 344 * @tc.size: MediumTest 345 */ 346 it('hiSysEventJsUnitTest006', 0, async function (done) { 347 console.info('hiSysEventJsUnitTest006 start') 348 try { 349 writeDefaultSysEvent() 350 setTimeout(() => { 351 querySysEvent(queryArgWithTwoCnt, 352 [{ 353 domain: "RELIABILITY_RELIABILITY", 354 names: ["STACK"], 355 }], (infos) => { 356 expect(infos.length >= 0).assertTrue() 357 }, (reason, total) => { 358 expect(true).assertTrue() 359 }, (err) => { 360 expect(err.code == INVALID_QUERY_RULE_ERR || err.code == QUERY_TOO_FREQUENTLY_ERR).assertTrue() 361 }, done) 362 }, 1000); 363 } catch (err) { 364 expect(false).assertTrue() 365 done() 366 } 367 }) 368 369 /** 370 * @tc.desc: Test query callback with domain which length is over 32. 371 * @tc.level: Level 0 372 * @tc.name: hiSysEventJsUnitTest007 373 * @tc.number: hiSysEventJsUnitTest007 374 * @tc.type: Function 375 * @tc.size: MediumTest 376 */ 377 it('hiSysEventJsUnitTest007', 0, async function (done) { 378 try { 379 writeDefaultSysEvent() 380 setTimeout(() => { 381 querySysEvent(queryArgWithTwoCnt, 382 [{ 383 domain: "RELIABILITY", 384 names: ["STACK_STACK_STACK_STACK_STACK_STACK"], 385 }], (infos) => { 386 expect(infos.length >= 0).assertTrue() 387 }, (reason, total) => { 388 expect(true).assertTrue() 389 }, (err) => { 390 expect(err.code == INVALID_QUERY_RULE_ERR || err.code == QUERY_TOO_FREQUENTLY_ERR).assertTrue() 391 }, done) 392 }, 1000); 393 } catch (err) { 394 expect(false).assertTrue() 395 done() 396 } 397 }) 398 399 /** 400 * @tc.desc: Test hisysevent of invalid domain writing with calling AsyncCallback. 401 * @tc.level: Level 0 402 * @tc.name: hiSysEventJsUnitTest008 403 * @tc.number: hiSysEventJsUnitTest008 404 * @tc.type: Function 405 * @tc.size: MediumTest 406 */ 407 it('hiSysEventJsUnitTest008', 0, async function (done) { 408 writeEventWithAsyncWork("RELIABILITY_RELIABILITY", "STACK", hiSysEvent.EventType.FAULT, defaultEventInfo, 409 (err) => { 410 expect(err.code == 11200001).assertTrue() // 11200001: error code for invalid domain 411 }, (val) => { 412 expect(false).assertTrue() 413 }, done) 414 }) 415 416 /** 417 * @tc.desc: Test hisysevent of invalid event name writing with calling AsyncCallback. 418 * @tc.level: Level 0 419 * @tc.name: hiSysEventJsUnitTest009 420 * @tc.number: hiSysEventJsUnitTest009 421 * @tc.type: Function 422 * @tc.size: MediumTest 423 */ 424 it('hiSysEventJsUnitTest009', 0, async function (done) { 425 writeEventWithAsyncWork("RELIABILITY", "STACK_STACK_STACK_STACK_STACK_STACK", hiSysEvent.EventType.FAULT, 426 defaultEventInfo, 427 (err) => { 428 expect(err.code == 11200002).assertTrue() // 11200002: error code for invalid event name 429 }, (val) => { 430 expect(false).assertTrue() 431 }, done) 432 }) 433 434 /** 435 * @tc.desc: Test hisysevent which is over size writing with calling AsyncCallback. 436 * @tc.level: Level 0 437 * @tc.name: hiSysEventJsUnitTest010 438 * @tc.number: hiSysEventJsUnitTest010 439 * @tc.type: Function 440 * @tc.size: MediumTest 441 */ 442 it('hiSysEventJsUnitTest010', 0, async function (done) { 443 for (let i = 0; i < 40; i++) { // 40 is a test limiting value 444 defaultEventInfo[`bundle${i}`] = Array.from({length: 10 * 1024}).join("ohos") 445 } 446 writeEventWithAsyncWork("RELIABILITY", "STACK", hiSysEvent.EventType.FAULT, 447 defaultEventInfo, 448 (err) => { 449 expect(err.code == 11200004).assertTrue() // 11200004: error code for content over limit 450 }, (val) => { 451 expect(false).assertTrue() 452 }, done) 453 }) 454 455 /** 456 * @tc.desc: Test hisysevent of invalid param name writing with calling AsyncCallback. 457 * @tc.level: Level 0 458 * @tc.name: hiSysEventJsUnitTest011 459 * @tc.number: hiSysEventJsUnitTest011 460 * @tc.type: Function 461 * @tc.size: MediumTest 462 */ 463 it('hiSysEventJsUnitTest011', 0, async function (done) { 464 writeEventWithAsyncWork("RELIABILITY", "STACK", hiSysEvent.EventType.FAULT, 465 { 466 PID: DEFAULT_ID, 467 UID: DEFAULT_ID, 468 STACK_STACK_STACK_STACK_STACK_STACK_STACK_STACK_STACK_STACK_STACK_STACK: "com.ohos.testHiSysEvent", 469 PROCESS_NAME: "hiview js test suite", 470 MSG: "no msg." 471 }, (err) => { 472 expect(err.code == 11200051).assertTrue() // 11200051: error code for invalid parameter name 473 }, (val) => { 474 expect(false).assertTrue() 475 }, done) 476 }) 477 478 /** 479 * @tc.desc: Test hisysevent with string over limit writing with calling AsyncCallback. 480 * @tc.level: Level 0 481 * @tc.name: hiSysEventJsUnitTest012 482 * @tc.number: hiSysEventJsUnitTest012 483 * @tc.type: Function 484 * @tc.size: MediumTest 485 */ 486 it('hiSysEventJsUnitTest012', 0, async function (done) { 487 writeEventWithAsyncWork("RELIABILITY", "STACK", hiSysEvent.EventType.FAULT, 488 { 489 PID: DEFAULT_ID, 490 UID: DEFAULT_ID, 491 PACKAGE_NAME: "com.ohos.testHiSysEvent", 492 PROCESS_NAME: Array.from({length: 10 * 1024 + 10}).join("ohos"), // 10 * 1024 + 10 is a threshold value 493 MSG: "no msg." 494 }, (err) => { 495 expect(err.code == 11200052).assertTrue() // 11200052: error code for length of string over limit 496 }, (val) => { 497 expect(false).assertTrue() 498 }, done) 499 }) 500 501 /** 502 * @tc.desc: Test hisysevent with param count over limit writing with calling AsyncCallback. 503 * @tc.level: Level 0 504 * @tc.name: hiSysEventJsUnitTest013 505 * @tc.number: hiSysEventJsUnitTest013 506 * @tc.type: Function 507 * @tc.size: MediumTest 508 */ 509 it('hiSysEventJsUnitTest013', 0, async function (done) { 510 let largeParams = {} 511 for (let i = 0; i < 200; i++) { // 200 is a test limiting value 512 largeParams["name" + i] = i 513 } 514 writeEventWithAsyncWork("RELIABILITY", "STACK", hiSysEvent.EventType.FAULT, 515 largeParams, (err) => { 516 expect(err.code == 11200053).assertTrue() // 11200053: error code for parameter count over limit 517 }, (val) => { 518 expect(false).assertTrue() 519 }, done) 520 }) 521 522 /** 523 * @tc.desc: Test hisysevent with array size over limit writing with calling AsyncCallback. 524 * @tc.level: Level 0 525 * @tc.name: hiSysEventJsUnitTest014 526 * @tc.number: hiSysEventJsUnitTest014 527 * @tc.type: Function 528 * @tc.size: MediumTest 529 */ 530 it('hiSysEventJsUnitTest014', 0, async function (done) { 531 let msgArray = [] 532 for (let i = 0; i < 200; i++) { // 200 is a test limiting value 533 msgArray[i] = i 534 } 535 writeEventWithAsyncWork("RELIABILITY", "STACK", hiSysEvent.EventType.FAULT, 536 { 537 PID: DEFAULT_ID, 538 UID: DEFAULT_ID, 539 PACKAGE_NAME: "com.ohos.testHiSysEvent", 540 PROCESS_NAME: "hiview js test suite", 541 MSG: msgArray 542 }, (err) => { 543 expect(err.code == 11200054).assertTrue() // 11200054: error code for array size over limit 544 }, (val) => { 545 expect(false).assertTrue() 546 }, done) 547 }) 548 549 /** 550 * @tc.desc: Test hisysevent query with sequence. 551 * @tc.level: Level 0 552 * @tc.name: hiSysEventJsUnitTest015 553 * @tc.number: hiSysEventJsUnitTest015 554 * @tc.type: Function 555 * @tc.size: MediumTest 556 */ 557 it('hiSysEventJsUnitTest015', 0, async function (done) { 558 querySysEvent({ 559 maxEvents: 10000, // 10000 is a test value 560 fromSeq: 100, // 100 is a test value 561 toSeq: 1000, // 1000 is a test value 562 }, [{ 563 domain: "AAFWK", 564 names: ["CONNECT_SERVICE"], 565 }], (infos) => { 566 expect(infos.length >= 0).assertTrue() 567 }, (reason, total) => { 568 expect(true).assertTrue() 569 }, (err) => { 570 expect(err.code == PERMISSION_ERR).assertTrue() 571 }, done) 572 }) 573 574 /** 575 * @tc.desc: Test hisysevent get max sequence. 576 * @tc.level: Level 0 577 * @tc.name: hiSysEventJsUnitTest016 578 * @tc.number: hiSysEventJsUnitTest016 579 * @tc.type: Function 580 * @tc.size: MediumTest 581 */ 582 it('hiSysEventJsUnitTest016', 0, async function (done) { 583 querySysEvent({ 584 maxEvents: 0, // 0 is a test value 585 fromSeq: 0, // 0 is a test value 586 toSeq: 1000, // 1000 is a test value 587 }, [{ 588 domain: "AAFWK", 589 names: ["CONNECT_SERVICE"], 590 }], (infos) => { 591 expect(infos.length >= 0).assertTrue() 592 }, (reason, total) => { 593 expect(true).assertTrue() 594 }, (err) => { 595 expect(err.code == PERMISSION_ERR).assertTrue() 596 }, done) 597 }) 598 599 /** 600 * @tc.desc: Test writing sysevents more than 100 times in 5 seconds. 601 * @tc.level: Level 0 602 * @tc.name: hiSysEventJsUnitTest017 603 * @tc.number: hiSysEventJsUnitTest017 604 * @tc.type: Function 605 * @tc.size: MediumTest 606 */ 607 it('hiSysEventJsUnitTest017', 0, async function (done) { 608 try { 609 for (let index = 0; index < 102; index++) { // 102 is a test limiting value 610 writeEventWithAsyncWork("USERIAM_PIN", "USERIAM_TEMPLATE_CHANGE", 611 hiSysEvent.EventType.SECURITY, 612 { 613 PID: DEFAULT_ID, 614 UID: DEFAULT_ID, 615 PACKAGE_NAME: "com.ohos.testHiSysEvent", 616 PROCESS_NAME: "hiview js test suite", 617 }, (err) => { 618 expect(err.code == 11200003).assertTrue() // 11200003: error code for abnormnal environment 619 }, (val) => {}, done) 620 } 621 } catch (err) { 622 expect(false).assertTrue() 623 done() 624 } 625 }) 626 627 /** 628 * @tc.desc: Test query sysevent with 2 conditions: == & ==. 629 * @tc.level: Level 0 630 * @tc.name: hiSysEventJsUnitTest018 631 * @tc.number: hiSysEventJsUnitTest018 632 * @tc.type: Function 633 * @tc.size: MediumTest 634 */ 635 it('hiSysEventJsUnitTest018', 0, async function (done) { 636 writeCustomizedSysEvent({ 637 PID: 323232388, // 323232388 is a test pid 638 UID: DEFAULT_ID, 639 PACKAGE_NAME: "com.ohos.hiSysEventJsUnitTest018", 640 PROCESS_NAME: "hiview js test suite" 641 }) 642 writeCustomizedSysEvent({ 643 PID: 1000, // 1000 is a test pid 644 UID: DEFAULT_ID, 645 PACKAGE_NAME: "com.ohos.hiSysEventJsUnitTest018", 646 PROCESS_NAME: "hiview js test suite" 647 }) 648 writeCustomizedSysEvent({ 649 PID: 1000, // 1000 is a test pid 650 UID: DEFAULT_ID, 651 PACKAGE_NAME: "com.ohos.testHiSysEvent2", 652 PROCESS_NAME: "hiview js test suite" 653 }) 654 setTimeout(() => { 655 querySysEvent(queryArgWithFiveCnt, 656 [{ 657 domain: "RELIABILITY", 658 names: ["STACK"], 659 condition: '{"version":"V1","condition":{"and":[{"param":"PID","op":"=","value":1000},' + 660 '{"param":"PACKAGE_NAME","op":"=","value":"com.ohos.testHiSysEvent2"}]}}' 661 }], (infos) => { 662 expect(infos.length >= 0).assertTrue() 663 }, (reason, total) => { 664 expect(total >= QUERY_1_ITEM).assertTrue() 665 }, (err) => { 666 expect(false).assertTrue() 667 }, done) 668 }, 1000) 669 }) 670 671 /** 672 * @tc.desc: Test query sysevent with conditions: <= & >=. 673 * @tc.level: Level 0 674 * @tc.name: hiSysEventJsUnitTest019 675 * @tc.number: hiSysEventJsUnitTest019 676 * @tc.type: Function 677 * @tc.size: MediumTest 678 */ 679 it('hiSysEventJsUnitTest019', 0, async function (done) { 680 const PID = 222 681 writeCustomizedSysEvent({ 682 PID, 683 UID: 10, // 10 is a test value 684 PACKAGE_NAME: "com.ohos.hiSysEventJsUnitTest019", 685 PROCESS_NAME: "hiview js test suite" 686 }) 687 writeCustomizedSysEvent({ 688 PID, 689 UID: 20, // 20 is a test value 690 PACKAGE_NAME: "com.ohos.hiSysEventJsUnitTest019", 691 PROCESS_NAME: "hiview js test suite" 692 }) 693 writeCustomizedSysEvent({ 694 PID, 695 UID: 23, // 23 is a test value 696 PACKAGE_NAME: "com.ohos.hiSysEventJsUnitTest019", 697 PROCESS_NAME: "hiview js test suite" 698 }) 699 setTimeout(() => { 700 querySysEvent(queryArgWithFiveCnt, 701 [{ 702 domain: "RELIABILITY", 703 names: ["STACK"], 704 condition: '{"version":"V1","condition":{"and":[{"param":"PID","op":"<=","value":222},' + 705 '{"param":"UID","op":">=","value":19.0}]}}' 706 }], (infos) => { 707 expect(infos.length >= 0).assertTrue() 708 }, (reason, total) => { 709 expect(total >= QUERY_2_ITEMS).assertTrue() // 2 is a check value 710 }, (err) => { 711 expect(false).assertTrue() 712 }, done) 713 }, DELAY_1500_MS) 714 }) 715 716 /** 717 * @tc.desc: Test query sysevent with conditions: > & <. 718 * @tc.level: Level 0 719 * @tc.name: hiSysEventJsUnitTest020 720 * @tc.number: hiSysEventJsUnitTest020 721 * @tc.type: Function 722 * @tc.size: MediumTest 723 */ 724 it('hiSysEventJsUnitTest020', 0, async function (done) { 725 writeCustomizedSysEvent({ 726 PID: 2009, // 2009 is a test value 727 UID: 20001, // 20001 is a test value 728 PACKAGE_NAME: "com.ohos.hiSysEventJsUnitTest020", 729 PROCESS_NAME: "hiview js test suite" 730 }) 731 writeCustomizedSysEvent({ 732 PID: 2010, // 2010 is a test value 733 UID: 20002, // 20002 is a test value 734 PACKAGE_NAME: "com.ohos.hiSysEventJsUnitTest020", 735 PROCESS_NAME: "hiview js test suite" 736 }) 737 writeCustomizedSysEvent({ 738 PID: 2020, // 2020 is a test value 739 UID: 20003, // 20003 is a test value 740 PACKAGE_NAME: "com.ohos.hiSysEventJsUnitTest020", 741 PROCESS_NAME: "hiview js test suite" 742 }) 743 setTimeout(() => { 744 querySysEvent(queryArgWithFiveCnt, 745 [{ 746 domain: "RELIABILITY", 747 names: ["STACK"], 748 condition: '{"version":"V1","condition":{"and":[{"param":"PID","op":">","value":2000},' + 749 '{"param":"UID","op":"<","value":20003}]}}' 750 }], (infos) => { 751 expect(infos.length >= 0).assertTrue() 752 }, (reason, total) => { 753 expect(total >= QUERY_2_ITEMS).assertTrue() // 2 is a test limit 754 }, (err) => { 755 expect(false).assertTrue() 756 }, done) 757 }, DELAY_2000_MS) 758 }) 759 760 /** 761 * @tc.desc: Test query sysevent with 2 conditions: != & ==. 762 * @tc.level: Level 0 763 * @tc.name: hiSysEventJsUnitTest021 764 * @tc.number: hiSysEventJsUnitTest021 765 * @tc.type: Function 766 * @tc.size: MediumTest 767 */ 768 it('hiSysEventJsUnitTest021', 0, async function (done) { 769 const UID = 88888 770 writeCustomizedSysEvent({ 771 PID: 22, // 22 is a test value 772 UID, 773 PACKAGE_NAME: "com.ohos.hiSysEventJsUnitTest021", 774 PROCESS_NAME: "hiview js test suite" 775 }) 776 writeCustomizedSysEvent({ 777 PID: 23, // 23 is a test value 778 UID, 779 PACKAGE_NAME: "com.ohos.hiSysEventJsUnitTest021", 780 PROCESS_NAME: "hiview js test suite" 781 }) 782 writeCustomizedSysEvent({ 783 PID: 24, // 24 is a test value 784 UID, 785 PACKAGE_NAME: "com.ohos.hiSysEventJsUnitTest021", 786 PROCESS_NAME: "hiview js test suite" 787 }) 788 setTimeout(() => { 789 querySysEvent(queryArgWithFiveCnt, 790 [{ 791 domain: "RELIABILITY", 792 names: ["STACK"], 793 condition: '{"version":"V1","condition":{"and":[{"param":"PID","op":"!=","value":22}, ' + 794 '{"param":"UID","op":"=","value":88888}]}}' 795 }], (infos) => { 796 expect(infos.length >= 0).assertTrue() 797 }, (reason, total) => { 798 expect(total >= 2).assertTrue() 799 }, (err) => { 800 expect(false).assertTrue() 801 }, done) 802 }, 2500) 803 }) 804 805 /** 806 * @tc.desc: Test query sysevent with null condition. 807 * @tc.level: Level 0 808 * @tc.name: hiSysEventJsUnitTest022 809 * @tc.number: hiSysEventJsUnitTest022 810 * @tc.type: Function 811 * @tc.size: MediumTest 812 */ 813 it('hiSysEventJsUnitTest022', 0, async function (done) { 814 setTimeout(() => { 815 querySysEvent(queryArgWithFiveCnt, 816 [{ 817 domain: "RELIABILITY", 818 names: ["STACK"], 819 condition: null 820 }], (infos) => { 821 expect(infos.length >= 0).assertTrue() 822 }, (reason, total) => { 823 expect(total > 0).assertTrue() 824 }, (err) => { 825 expect(false).assertTrue() 826 }, done) 827 }, 2500) 828 }) 829 830 /** 831 * @tc.desc: Test query sysevent with default query argument. 832 * @tc.level: Level 0 833 * @tc.name: hiSysEventJsUnitTest023 834 * @tc.number: hiSysEventJsUnitTest023 835 * @tc.type: Function 836 * @tc.size: MediumTest 837 */ 838 it('hiSysEventJsUnitTest023', 0, async function (done) { 839 setTimeout(() => { 840 querySysEvent(defaultQueryArg, 841 [{ 842 domain: "RELIABILITY", 843 names: ["STACK"], 844 condition: null 845 }], (infos) => { 846 expect(infos.length >= 0).assertTrue() 847 }, (reason, total) => { 848 expect(total > 0).assertTrue() 849 }, (err) => { 850 expect(false).assertTrue() 851 }, done) 852 }, 2500) 853 }) 854 855 /** 856 * @tc.desc: Test write with integer number 857 * @tc.level: Level 0 858 * @tc.name: hiSysEventJsUnitTest024 859 * @tc.number: hiSysEventJsUnitTest024 860 * @tc.type: Function 861 * @tc.size: MediumTest 862 */ 863 it('hiSysEventJsUnitTest024', 0, async function (done) { 864 const FIRST_INT_VAL = 1 865 const SECOND_INT_VAL = -1 866 const THIRD_INT_VAL = 123456789 867 const FORTH_INT_VAL = -123456789 868 const TEST_PID_UID = 2222222 869 writeCustomizedSysEvent({ 870 PID: TEST_PID_UID, 871 UID: TEST_PID_UID, 872 FIRST_INT_VAL, SECOND_INT_VAL, THIRD_INT_VAL, FORTH_INT_VAL, 873 }) 874 setTimeout(() => { 875 querySysEvent(queryArgWithOneCnt, 876 [{ 877 domain: "RELIABILITY", 878 names: ["STACK"], 879 condition: '{"version":"V1","condition":{"and":[{"param":"PID","op":"=","value":2222222},' + 880 '{"param":"UID","op":"=","value":2222222}]}}' 881 }], (infos) => { 882 expect(infos.length >= 0).assertTrue() 883 expect(GetParam(infos[0], "FIRST_INT_VAL")).assertEqual(FIRST_INT_VAL) 884 expect(GetParam(infos[0], "SECOND_INT_VAL")).assertEqual(SECOND_INT_VAL) 885 expect(GetParam(infos[0], "THIRD_INT_VAL")).assertEqual(THIRD_INT_VAL) 886 expect(GetParam(infos[0], "FORTH_INT_VAL")).assertEqual(FORTH_INT_VAL) 887 }, (reason, total) => { 888 expect(total == QUERY_1_ITEM).assertTrue() 889 }, (err) => { 890 expect(false).assertTrue() 891 }, done) 892 }, DELAY_1500_MS) 893 }) 894 895 /** 896 * @tc.desc: Test write with big integer number 897 * @tc.level: Level 0 898 * @tc.name: hiSysEventJsUnitTest025 899 * @tc.number: hiSysEventJsUnitTest025 900 * @tc.type: Function 901 * @tc.size: MediumTest 902 */ 903 it('hiSysEventJsUnitTest025', 0, async function (done) { 904 const FIRST_BIG_INT_VAL = 1n 905 const SECOND_BIG_INT_VAL = -1n 906 const THIRD_BIG_INT_VAL = 123456789n 907 const FORTH_BIG_INT_VAL = -123456789n 908 const TEST_PID_UID = 3333333 909 writeCustomizedSysEvent({ 910 PID: TEST_PID_UID, 911 UID: TEST_PID_UID, 912 FIRST_BIG_INT_VAL, SECOND_BIG_INT_VAL, THIRD_BIG_INT_VAL, FORTH_BIG_INT_VAL, 913 }) 914 setTimeout(() => { 915 querySysEvent(queryArgWithOneCnt, 916 [{ 917 domain: "RELIABILITY", 918 names: ["STACK"], 919 condition: '{"version":"V1","condition":{"and":[{"param":"PID","op":"=","value":3333333},' + 920 '{"param":"UID","op":"=","value":3333333}]}}' 921 }], (infos) => { 922 expect(infos.length >= 0).assertTrue() 923 expect(BigInt(GetParam(infos[0], "FIRST_BIG_INT_VAL"))).assertEqual(FIRST_BIG_INT_VAL) 924 expect(BigInt(GetParam(infos[0], "SECOND_BIG_INT_VAL"))).assertEqual(SECOND_BIG_INT_VAL) 925 expect(BigInt(GetParam(infos[0], "THIRD_BIG_INT_VAL"))).assertEqual(THIRD_BIG_INT_VAL) 926 expect(BigInt(GetParam(infos[0], "FORTH_BIG_INT_VAL"))).assertEqual(FORTH_BIG_INT_VAL) 927 }, (reason, total) => { 928 expect(total == QUERY_1_ITEM).assertTrue() 929 }, (err) => { 930 expect(false).assertTrue() 931 }, done) 932 }, DELAY_1500_MS) 933 }) 934 935 /** 936 * @tc.desc: Test write with max or min big integer number 937 * @tc.level: Level 0 938 * @tc.name: hiSysEventJsUnitTest026 939 * @tc.number: hiSysEventJsUnitTest026 940 * @tc.type: Function 941 * @tc.size: MediumTest 942 */ 943 it('hiSysEventJsUnitTest026', 0, async function (done) { 944 const UINT64_MAX = 18446744073709551615n 945 const INT64_MAX = 9223372036854775807n 946 const INT64_MIN = -9223372036854775808n 947 const TEST_PID_UID = 4444444 948 writeCustomizedSysEvent({ 949 PID: TEST_PID_UID, 950 UID: TEST_PID_UID, 951 UINT64_MAX, INT64_MAX, INT64_MIN, 952 }) 953 setTimeout(() => { 954 querySysEvent(queryArgWithOneCnt, 955 [{ 956 domain: "RELIABILITY", 957 names: ["STACK"], 958 condition: '{"version":"V1","condition":{"and":[{"param":"PID","op":"=","value":4444444},' + 959 '{"param":"UID","op":"=","value":4444444}]}}' 960 }], (infos) => { 961 expect(infos.length >= 0).assertTrue() 962 expect(GetParam(infos[0], "UINT64_MAX")).assertEqual(UINT64_MAX) 963 expect(GetParam(infos[0], "INT64_MAX")).assertEqual(INT64_MAX) 964 expect(GetParam(infos[0], "INT64_MIN")).assertEqual(INT64_MIN) 965 }, (reason, total) => { 966 expect(total == QUERY_1_ITEM).assertTrue() 967 }, (err) => { 968 expect(false).assertTrue() 969 }, done) 970 }, DELAY_1500_MS) 971 }) 972 973 /** 974 * @tc.desc: Test write with big integer number array 975 * @tc.level: Level 0 976 * @tc.name: hiSysEventJsUnitTest027 977 * @tc.number: hiSysEventJsUnitTest027 978 * @tc.type: Function 979 * @tc.size: MediumTest 980 */ 981 it('hiSysEventJsUnitTest027', 0, async function (done) { 982 const FIRST_BIG_INT_ARR = [4n, 5n, 6n] 983 const SECOND_BIG_INT_ARR = [-4n, -5n, -6n] 984 const THIRD_BIG_INT_ARR = [123456789n, -2232333n, 2222223344n] 985 const FORTH_BIG_INT_ARR = [-123456789n, -2232333n, -2222223344n] 986 const TEST_PID_UID = 55555555 987 writeCustomizedSysEvent({ 988 PID: TEST_PID_UID, 989 UID: TEST_PID_UID, 990 FIRST_BIG_INT_ARR, SECOND_BIG_INT_ARR, THIRD_BIG_INT_ARR, FORTH_BIG_INT_ARR, 991 }) 992 setTimeout(() => { 993 querySysEvent(queryArgWithOneCnt, 994 [{ 995 domain: "RELIABILITY", 996 names: ["STACK"], 997 condition: '{"version":"V1","condition":{"and":[{"param":"PID","op":"=","value":55555555},' + 998 '{"param":"UID","op":"=","value":55555555}]}}' 999 }], (infos) => { 1000 expect(infos.length >= 0).assertTrue() 1001 const SEC_INDEX = 1 1002 const THIRD_INDEX = 2 1003 expect(BigInt(GetArrayIemParamByIndex( 1004 infos[0], "FIRST_BIG_INT_ARR", SEC_INDEX))).assertEqual(FIRST_BIG_INT_ARR[SEC_INDEX]) 1005 expect(BigInt(GetArrayIemParamByIndex( 1006 infos[0], "SECOND_BIG_INT_ARR", THIRD_INDEX))).assertEqual(SECOND_BIG_INT_ARR[THIRD_INDEX]) 1007 expect(BigInt(GetArrayIemParamByIndex( 1008 infos[0], "THIRD_BIG_INT_ARR", SEC_INDEX))).assertEqual(THIRD_BIG_INT_ARR[SEC_INDEX]) 1009 expect(BigInt(GetArrayIemParamByIndex( 1010 infos[0], "FORTH_BIG_INT_ARR", THIRD_INDEX))).assertEqual(FORTH_BIG_INT_ARR[THIRD_INDEX]) 1011 }, (reason, total) => { 1012 expect(total == QUERY_1_ITEM).assertTrue() 1013 }, (err) => { 1014 expect(false).assertTrue() 1015 }, done) 1016 }, DELAY_1500_MS) 1017 }) 1018 1019 /** 1020 * @tc.desc: Test write with integer number array 1021 * @tc.level: Level 0 1022 * @tc.name: hiSysEventJsUnitTest028 1023 * @tc.number: hiSysEventJsUnitTest028 1024 * @tc.type: Function 1025 * @tc.size: MediumTest 1026 */ 1027 it('hiSysEventJsUnitTest028', 0, async function (done) { 1028 const FIRST_INT_ARR = [1, 2, 3] 1029 const SECOND_INT_ARR = [-1, -2, -3] 1030 const THIRD_INT_ARR = [123456789, -2232333, 2222223344] 1031 const FORTH_INT_ARR = [-123456, 222333, -222222] 1032 const TEST_PID_UID = 66666666 1033 writeCustomizedSysEvent({ 1034 PID: TEST_PID_UID, 1035 UID: TEST_PID_UID, 1036 FIRST_INT_ARR, SECOND_INT_ARR, THIRD_INT_ARR, FORTH_INT_ARR, 1037 }) 1038 setTimeout(() => { 1039 querySysEvent(queryArgWithOneCnt, 1040 [{ 1041 domain: "RELIABILITY", 1042 names: ["STACK"], 1043 condition: '{"version":"V1","condition":{"and":[{"param":"PID","op":"=","value":66666666},' + 1044 '{"param":"UID","op":"=","value":66666666}]}}' 1045 }], (infos) => { 1046 expect(infos.length >= 0).assertTrue() 1047 const SEC_INDEX = 1 1048 const THIRD_INDEX = 2 1049 expect(GetArrayIemParamByIndex( 1050 infos[0], "FIRST_INT_ARR", SEC_INDEX)).assertEqual(FIRST_INT_ARR[SEC_INDEX]) 1051 expect(GetArrayIemParamByIndex( 1052 infos[0], "SECOND_INT_ARR", THIRD_INDEX)).assertEqual(SECOND_INT_ARR[THIRD_INDEX]) 1053 expect(GetArrayIemParamByIndex( 1054 infos[0], "THIRD_INT_ARR", SEC_INDEX)).assertEqual(THIRD_INT_ARR[SEC_INDEX]) 1055 expect(GetArrayIemParamByIndex( 1056 infos[0], "FORTH_INT_ARR", THIRD_INDEX)).assertEqual(FORTH_INT_ARR[THIRD_INDEX]) 1057 }, (reason, total) => { 1058 expect(total == QUERY_1_ITEM).assertTrue() 1059 }, (err) => { 1060 expect(false).assertTrue() 1061 }, done) 1062 }, DELAY_1500_MS) 1063 }) 1064 1065 /** 1066 * @tc.desc: Test query with undefined as beginTime 1067 * @tc.level: Level 0 1068 * @tc.name: hiSysEventJsUnitTest029 1069 * @tc.number: hiSysEventJsUnitTest029 1070 * @tc.type: Function 1071 * @tc.size: MediumTest 1072 */ 1073 it('hiSysEventJsUnitTest029', 0, async function (done) { 1074 querySysEvent({ 1075 beginTime: undefined, 1076 endTime: DEFAULT_QUERY_TIME, 1077 maxEvents: QUERY_2_ITEMS 1078 }, [{ 1079 domain: "KERNEL_VENDOR", 1080 names: ["POWER_KEY"], 1081 }], (infos) => { 1082 expect(false).assertTrue() 1083 }, (reason, total) => { 1084 expect(false).assertTrue() 1085 }, (err) => { 1086 expect(err.code == PARAM_CHECK_ERR).assertTrue() 1087 }, done) 1088 }) 1089 1090 /** 1091 * @tc.desc: Test query with null as endTime 1092 * @tc.level: Level 0 1093 * @tc.name: hiSysEventJsUnitTest030 1094 * @tc.number: hiSysEventJsUnitTest030 1095 * @tc.type: Function 1096 * @tc.size: MediumTest 1097 */ 1098 it('hiSysEventJsUnitTest030', 0, async function (done) { 1099 querySysEvent({ 1100 beginTime: DEFAULT_QUERY_TIME, 1101 endTime: null, 1102 maxEvents: QUERY_2_ITEMS 1103 }, [{ 1104 domain: "AAFWK", 1105 names: ["PROCESS_EXIT"], 1106 }], (infos) => { 1107 expect(infos.length >= 0).assertTrue() 1108 }, (reason, total) => { 1109 expect(true).assertTrue() 1110 }, (err) => { 1111 expect(err.code == PARAM_CHECK_ERR).assertTrue() 1112 }, done) 1113 }) 1114 1115 /** 1116 * @tc.desc: Test query with undefined as querier 1117 * @tc.level: Level 0 1118 * @tc.name: hiSysEventJsUnitTest031 1119 * @tc.number: hiSysEventJsUnitTest031 1120 * @tc.type: Function 1121 * @tc.size: MediumTest 1122 */ 1123 it('hiSysEventJsUnitTest031', 0, async function (done) { 1124 try { 1125 hiSysEvent.query(queryArgWithOneCnt, [{ 1126 domain: "HUKS", 1127 names: ["FAULT"], 1128 }], undefined) 1129 } catch (err) { 1130 expect(err.code == PARAM_CHECK_ERR).assertTrue() 1131 done() 1132 } 1133 }) 1134 1135 /** 1136 * @tc.desc: Test query with null as querier 1137 * @tc.level: Level 0 1138 * @tc.name: hiSysEventJsUnitTest032 1139 * @tc.number: hiSysEventJsUnitTest032 1140 * @tc.type: Function 1141 * @tc.size: MediumTest 1142 */ 1143 it('hiSysEventJsUnitTest032', 0, async function (done) { 1144 try { 1145 hiSysEvent.query(queryArgWithOneCnt, [{ 1146 domain: "SAMGR", 1147 names: ["ONDEMAND_SA_LOAD"], 1148 }], null) 1149 } catch (err) { 1150 expect(err.code == PARAM_CHECK_ERR).assertTrue() 1151 done() 1152 } 1153 }) 1154});