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 */ 15import hiSysEvent from '@ohos.hiSysEvent'; 16import fs from '@ohos.file.fs'; 17import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' 18 19export default function hiSysEventBoxTest() { 20describe('hiSysEventBoxTest', function () { 21 function sleep(numberMillis) { 22 var now = new Date(); 23 var exitTime = now.getTime() + numberMillis; 24 while (true) { 25 now = new Date(); 26 if (now.getTime() > exitTime) 27 return; 28 } 29 } 30 beforeAll(function() { 31 console.info('hiSysEventBoxTest beforeAll called') 32 }) 33 34 afterAll(function() { 35 console.info('hiSysEventBoxTest afterAll called') 36 }) 37 38 beforeEach(function() { 39 console.info('hiSysEventBoxTest beforeEach called') 40 }) 41 42 afterEach(function() { 43 console.info('hiSysEventBoxTest afterEach called') 44 }) 45 46 /** 47 * @tc.number DFX_DFT_HiSysevent_SandBox_0400 48 * @tc.name testHiSysEventBox04 49 * @tc.desc 验证调用exportSysEvents接口,QueryRule11条,抛出错误码11200301 50 */ 51 it('testHiSysEventBox04', 1, async function (done) { 52 console.info('testHiSysEventBox04 start') 53 try { 54 let time = hiSysEvent.exportSysEvents({ 55 beginTime: -1, 56 endTime: -1, 57 maxEvents: 1, 58 }, [{ 59 domain: "RELIABILITY", 60 names: ["STACK"], 61 },{ 62 domain: "RELIABILITY", 63 names: ["SERVICE_BLOCK_REPORT"], 64 },{ 65 domain: "RELIABILITY", 66 names: ["SERVICE_TIMEOUT_REPORT"], 67 },{ 68 domain: "RELIABILITY", 69 names: ["CPP_CRASH_NO_LOG"], 70 },{ 71 domain: "RELIABILITY", 72 names: ["RUST_PANIC"], 73 },{ 74 domain: "RELIABILITY", 75 names: ["MEMORY_LEAK"], 76 },{ 77 domain: "RELIABILITY", 78 names: ["FD_LEAK"], 79 },{ 80 domain: "RELIABILITY", 81 names: ["THREAD_LEAK"], 82 },{ 83 domain: "RELIABILITY", 84 names: ["ADDR_SANITIZER"], 85 },{ 86 domain: "RELIABILITY", 87 names: ["CPP_CRASH"], 88 },{ 89 domain: "RELIABILITY", 90 names: ["APP_FREEZE"], 91 }]) 92 expect().assertFail() 93 done(); 94 } catch (error) { 95 console.error(`testHiSysEventBox04 error code: ${error.code}, error msg: ${error.message}`); 96 expect(error.code == 11200301).assertTrue() 97 console.info('testHiSysEventBox04 end') 98 done(); 99 } 100 }) 101 102 /** 103 * @tc.number DFX_DFT_HiSysevent_SandBox_0500 104 * @tc.name testHiSysEventBox05 105 * @tc.desc 验证调用exportSysEvents接口,QueryRule-domain无效,抛出错误码11200302 106 */ 107 it('testHiSysEventBox05', 1, async function (done) { 108 console.info('testHiSysEventBox05 start') 109 try { 110 let time = hiSysEvent.exportSysEvents({ 111 beginTime: -1, 112 endTime: -1, 113 maxEvents: 1, 114 }, [{ 115 domain: "RELIABILITY_RE", 116 names: ["SERVICE_BLOCK_REPORT"], 117 }]) 118 expect().assertFail() 119 done(); 120 } catch (error) { 121 console.error(`testHiSysEventBox05 error code: ${error.code}, error msg: ${error.message}`); 122 expect(error.code == 11200302).assertTrue() 123 console.info('testHiSysEventBox05 end') 124 done(); 125 } 126 }) 127 128 /** 129 * @tc.number DFX_DFT_HiSysevent_SandBox_0600 130 * @tc.name testHiSysEventBox06 131 * @tc.desc 验证调用exportSysEvents接口,QueryRule-evetname无效,抛出错误码11200302 132 */ 133 it('testHiSysEventBox06', 3, async function (done) { 134 console.info('testHiSysEventBox06 start') 135 try { 136 let time = hiSysEvent.exportSysEvents({ 137 beginTime: -1, 138 endTime: -1, 139 maxEvents: 1, 140 }, [{ 141 domain: "RELIABILITY", 142 names: ["SERVICE_BLOCK_REPORT_SERVICE"], 143 }]) 144 expect().assertFail() 145 done(); 146 } catch (error) { 147 console.error(`testHiSysEventBox06 error code: ${error.code}, error msg: ${error.message}`); 148 expect(error.code == 11200302).assertTrue() 149 console.info('testHiSysEventBox06 end') 150 done(); 151 } 152 }) 153 154 /** 155 * @tc.number DFX_DFT_HiSysevent_SandBox_0700 156 * @tc.name testHiSysEventBox07 157 * @tc.desc 验证调用exportSysEvents接口,QueryRule-domain为空,抛出错误码11200302 158 */ 159 it('testHiSysEventBox07', 3, async function (done) { 160 console.info('testHiSysEventBox07 start') 161 try { 162 let time = hiSysEvent.exportSysEvents({ 163 beginTime: -1, 164 endTime: -1, 165 maxEvents: 1, 166 }, [{ 167 domain: "", 168 names: ["SERVICE_BLOCK_REPORT"], 169 }]) 170 expect().assertFail() 171 done(); 172 } catch (error) { 173 console.error(`testHiSysEventBox07 error code: ${error.code}, error msg: ${error.message}`); 174 expect(error.code == 11200302).assertTrue() 175 console.info('testHiSysEventBox07 end') 176 done(); 177 } 178 }) 179 180 /** 181 * @tc.number DFX_DFT_HiSysevent_SandBox_0800 182 * @tc.name testHiSysEventBox08 183 * @tc.desc 验证调用exportSysEvents接口,QueryRule-eventname为空,抛出错误码11200302 184 */ 185 it('testHiSysEventBox08', 3, async function (done) { 186 console.info('testHiSysEventBox08 start') 187 try { 188 let time = hiSysEvent.exportSysEvents({ 189 beginTime: -1, 190 endTime: -1, 191 maxEvents: 1, 192 }, [{ 193 domain: "RELIABILITY", 194 names: [""], 195 }]) 196 expect().assertFail() 197 done(); 198 } catch (error) { 199 console.error(`testHiSysEventBox08 error code: ${error.code}, error msg: ${error.message}`); 200 expect(error.code == 11200302).assertTrue() 201 console.info('testHiSysEventBox08 end') 202 done(); 203 } 204 }) 205 206 /** 207 * @tc.number DFX_DFT_HiSysevent_SandBox_1000 208 * @tc.name testHiSysEventBox10 209 * @tc.desc 验证设备重启后exportSysEvents接口一小时内可调用一次,QueryRule10条,返回值为调用时间戳,沙箱路径存在导出的文件 210 */ 211 it('testHiSysEventBox10', 3, async function (done) { 212 console.info('testHiSysEventBox10 start') 213 try { 214 hiSysEvent.write({ 215 domain: "RELIABILITY", 216 name: "STACK", 217 eventType: hiSysEvent.EventType.FAULT, 218 params: { 219 PID: 487, 220 UID: 103, 221 PACKAGE_NAME: "com.ohos.hisysevent.test", 222 PROCESS_NAME: "syseventservice", 223 MSG: "no msg." 224 } 225 }, (err, val) => { 226 if (err) { 227 console.error(`HiSysEvent json-callback-error code=${err.code}`); 228 expect().assertFail() 229 done(); 230 } 231 }) 232 let time = hiSysEvent.exportSysEvents({ 233 beginTime: -1, 234 endTime: -1, 235 maxEvents: 1, 236 }, [{ 237 domain: "RELIABILITY", 238 names: ["STACK"], 239 },{ 240 domain: "RELIABILITY", 241 names: ["SERVICE_BLOCK_REPORT"], 242 },{ 243 domain: "RELIABILITY", 244 names: ["SERVICE_TIMEOUT_REPORT"], 245 },{ 246 domain: "RELIABILITY", 247 names: ["CPP_CRASH_NO_LOG"], 248 },{ 249 domain: "RELIABILITY", 250 names: ["RUST_PANIC"], 251 },{ 252 domain: "RELIABILITY", 253 names: ["MEMORY_LEAK"], 254 },{ 255 domain: "RELIABILITY", 256 names: ["FD_LEAK"], 257 },{ 258 domain: "RELIABILITY", 259 names: ["THREAD_LEAK"], 260 },{ 261 domain: "RELIABILITY", 262 names: ["ADDR_SANITIZER"], 263 },{ 264 domain: "RELIABILITY", 265 names: ["CPP_CRASH"], 266 }]) 267 // 延迟读取本次导出的事件 268 sleep(5000); 269 let eventDir = '/data/storage/el2/base/cache/hiview/event'; 270 let filenames = fs.listFileSync(eventDir); 271 for (let i = 0; i < filenames.length; i++) { 272 if (filenames[i].indexOf(time.toString()) != -1) { 273 let res = fs.readTextSync(eventDir + '/' + filenames[i]); 274 let events = JSON.parse('[' + res.slice(0, res.length - 1) + ']'); 275 console.log("read file end, events is :" + JSON.stringify(events)); 276 expect(JSON.stringify(events) != "").assertTrue() 277 } 278 } 279 expect(typeof(time) == "bigint").assertTrue() 280 console.info('testHiSysEventBox10 end') 281 done(); 282 } catch (error) { 283 console.error(`testHiSysEventBox10 error code: ${error.code}, error msg: ${error.message}`); 284 console.info('testHiSysEventBox10 error') 285 expect().assertFail() 286 done(); 287 } 288 }) 289 290 /** 291 * @tc.number DFX_DFT_HiSysevent_SandBox_1100 292 * @tc.name testHiSysEventBox11 293 * @tc.desc 验证一小时内重复调用exportSysEvents的接口,抛出错误码11200304 294 */ 295 it('testHiSysEventBox11', 3, async function (done) { 296 console.info('testHiSysEventBox11 start') 297 try { 298 let time = hiSysEvent.exportSysEvents({ 299 beginTime: -1, 300 endTime: -1, 301 maxEvents: 1, 302 }, [{ 303 domain: "RELIABILITY", 304 names: ["SERVICE_BLOCK_REPORT"], 305 }]) 306 expect().assertFail() 307 done(); 308 } catch (error) { 309 console.error(`testHiSysEventBox11 error code: ${error.code}, error msg: ${error.message}`); 310 expect(error.code == 11200304).assertTrue() 311 console.info('testHiSysEventBox11 end') 312 done(); 313 } 314 }) 315 316 /** 317 * @tc.number DFX_DFT_HiSysevent_SandBox_1300 318 * @tc.name testHiSysEventBox13 319 * @tc.desc 验证调用subscribe,30条订阅事件,其中一个domain16字节,eventname32字节,沙箱路径存在导出的文件,Hiview数据库存在uid信息 320 */ 321 it('testHiSysEventBox13', 3, async function (done) { 322 console.info('testHiSysEventBox13 start') 323 let msgArray = [] 324 for (let i = 0; i < 29; i++) { 325 msgArray[i] = { 326 domain: "RELIABILITY", 327 names: ["STACK"], 328 } 329 } 330 msgArray[29] = { 331 domain: "RELIABILITYRELIA", 332 names: ["CPP_CRASH_NO_LOGCPP_CRASH_NO_LOG"], 333 } 334 try { 335 hiSysEvent.subscribe(msgArray) 336 hiSysEvent.write({ 337 domain: "RELIABILITY", 338 name: "STACK", 339 eventType: hiSysEvent.EventType.FAULT, 340 params: { 341 PID: 487, 342 UID: 103, 343 PACKAGE_NAME: "com.ohos.hisysevent.test", 344 PROCESS_NAME: "syseventservice", 345 MSG: "no msg." 346 } 347 }, (err, val) => { 348 }) 349 sleep(5000); 350 let eventDir = '/data/storage/el2/base/cache/hiview/event'; 351 let filenames = fs.listFileSync(eventDir); 352 for (let i = 0; i < filenames.length; i++) { 353 let res = fs.readTextSync(eventDir + '/' + filenames[i]); 354 let events = JSON.parse('[' + res.slice(0, res.length - 1) + ']'); 355 console.log("read file end, events is :" + JSON.stringify(events)); 356 expect(JSON.stringify(events) != "").assertTrue() 357 } 358 hiSysEvent.unsubscribe(); 359 console.info('testHiSysEventBox13 end') 360 done(); 361 } catch (error) { 362 console.error(`testHiSysEventBox13 error code: ${error.code}, error msg: ${error.message}`); 363 console.info('testHiSysEventBox13 error') 364 expect().assertFail() 365 done(); 366 } 367 }) 368 369 /** 370 * @tc.number DFX_DFT_HiSysevent_SandBox_1400 371 * @tc.name testHiSysEventBox14 372 * @tc.desc 验证调用subscribe接口订阅事件后取消订阅unsubscribe,沙箱路径存在导出的文件,Hiview数据库不存在uid信息 373 */ 374 it('testHiSysEventBox14', 3, async function (done) { 375 console.info('testHiSysEventBox14 start') 376 let msgArray = [] 377 for (let i = 0; i < 29; i++) { 378 msgArray[i] = { 379 domain: "RELIABILITY", 380 names: ["STACK"], 381 } 382 } 383 msgArray[29] = { 384 domain: "RELIABILITYRELIA", 385 names: ["CPP_CRASH_NO_LOGCPP_CRASH_NO_LOG"], 386 } 387 try { 388 hiSysEvent.subscribe(msgArray) 389 hiSysEvent.write({ 390 domain: "RELIABILITY", 391 name: "STACK", 392 eventType: hiSysEvent.EventType.FAULT, 393 params: { 394 PID: 487, 395 UID: 103, 396 PACKAGE_NAME: "com.ohos.hisysevent.test", 397 PROCESS_NAME: "syseventservice", 398 MSG: "no msg." 399 } 400 }, (err, val) => { 401 }) 402 sleep(5000); 403 hiSysEvent.unsubscribe(); 404 let eventDir = '/data/storage/el2/base/cache/hiview/event'; 405 let filenames = fs.listFileSync(eventDir); 406 for (let i = 0; i < filenames.length; i++) { 407 let res = fs.readTextSync(eventDir + '/' + filenames[i]); 408 let events = JSON.parse('[' + res.slice(0, res.length - 1) + ']'); 409 console.log("read file end, events is :" + JSON.stringify(events)); 410 expect(JSON.stringify(events) != "").assertTrue() 411 } 412 console.info('testHiSysEventBox14 end') 413 done(); 414 } catch (error) { 415 console.error(`testHiSysEventBox14 error code: ${error.code}, error msg: ${error.message}`); 416 console.info('testHiSysEventBox14 error') 417 expect().assertFail() 418 done(); 419 } 420 }) 421 422 /** 423 * @tc.number DFX_DFT_HiSysevent_SandBox_1600 424 * @tc.name testHiSysEventBox16 425 * @tc.desc 验证调用subscribe,31条订阅事件,抛出错误码11200301 426 */ 427 it('testHiSysEventBox16', 3, async function (done) { 428 console.info('testHiSysEventBox16 start') 429 let msgArray = [] 430 for (let i = 0; i < 30; i++) { 431 msgArray[i] = { 432 domain: "RELIABILITY", 433 names: ["STACK"], 434 } 435 } 436 msgArray[30] = { 437 domain: "RELIABILITYRELIA", 438 names: ["CPP_CRASH_NO_LOGCPP_CRASH_NO_LOG"], 439 } 440 try { 441 hiSysEvent.subscribe(msgArray) 442 expect().assertFail() 443 done(); 444 } catch (error) { 445 console.error(`testHiSysEventBox16 error code: ${error.code}, error msg: ${error.message}`); 446 expect(error.code == 11200301).assertTrue() 447 console.info('testHiSysEventBox16 end') 448 done(); 449 } 450 }) 451 452 /** 453 * @tc.number DFX_DFT_HiSysevent_SandBox_1700 454 * @tc.name testHiSysEventBox17 455 * @tc.desc 验证调用subscribe接口,QueryRule-domain17字节,抛出错误码11200302 456 */ 457 it('testHiSysEventBox17', 3, async function (done) { 458 console.info('testHiSysEventBox17 start') 459 try { 460 hiSysEvent.subscribe([{ 461 domain: "RELIABILITYRELIAB", 462 names: ["CPP_CRASH_NO_LOGCPP_CRASH_NO_LOG"] 463 }]) 464 expect().assertFail() 465 done(); 466 } catch (error) { 467 console.error(`testHiSysEventBox17 error code: ${error.code}, error msg: ${error.message}`); 468 expect(error.code == 11200302).assertTrue() 469 console.info('testHiSysEventBox17 end') 470 done(); 471 } 472 }) 473 474 /** 475 * @tc.number DFX_DFT_HiSysevent_SandBox_1800 476 * @tc.name testHiSysEventBox18 477 * @tc.desc 验证调用subscribe接口,QueryRule-evetname33字节,抛出错误码11200302 478 */ 479 it('testHiSysEventBox18', 3, async function (done) { 480 console.info('testHiSysEventBox18 start') 481 try { 482 hiSysEvent.subscribe([{ 483 domain: "RELIABILITYRELIA", 484 names: ["CPP_CRASH_NO_LOGCPP_CRASH_NO_LOGC"] 485 }]) 486 expect().assertFail() 487 done(); 488 } catch (error) { 489 console.error(`testHiSysEventBox18 error code: ${error.code}, error msg: ${error.message}`); 490 expect(error.code == 11200302).assertTrue() 491 console.info('testHiSysEventBox18 end') 492 done(); 493 } 494 }) 495 496 /** 497 * @tc.number DFX_DFT_HiSysevent_SandBox_1900 498 * @tc.name testHiSysEventBox19 499 * @tc.desc 验证调用subscribe接口,QueryRule-domain为空,抛出错误码11200302 500 */ 501 it('testHiSysEventBox19', 3, async function (done) { 502 console.info('testHiSysEventBox19 start') 503 try { 504 hiSysEvent.subscribe([{ 505 domain: "", 506 names: ["CPP_CRASH_NO_LOG"] 507 }]) 508 expect().assertFail() 509 done(); 510 } catch (error) { 511 console.error(`testHiSysEventBox19 error code: ${error.code}, error msg: ${error.message}`); 512 expect(error.code == 11200302).assertTrue() 513 console.info('testHiSysEventBox19 end') 514 done(); 515 } 516 }) 517 518 /** 519 * @tc.number DFX_DFT_HiSysevent_SandBox_2000 520 * @tc.name testHiSysEventBox20 521 * @tc.desc 验证调用subscribe接口,QueryRule-evetname为空,抛出错误码11200302 522 */ 523 it('testHiSysEventBox20', 3, async function (done) { 524 console.info('testHiSysEventBox20 start') 525 try { 526 hiSysEvent.subscribe([{ 527 domain: "RELIABILITY", 528 names: [""] 529 }]) 530 expect().assertFail() 531 done(); 532 } catch (error) { 533 console.error(`testHiSysEventBox20 error code: ${error.code}, error msg: ${error.message}`); 534 expect(error.code == 11200302).assertTrue() 535 console.info('testHiSysEventBox20 end') 536 done(); 537 } 538 }) 539 540 /** 541 * @tc.number DFX_DFT_HiSysevent_SandBox_2100 542 * @tc.name testHiSysEventBox21 543 * @tc.desc 验证调用subscribe接口,QueryRule-domain含有特殊字节,抛出错误码11200302 544 */ 545 it('testHiSysEventBox21', 3, async function (done) { 546 console.info('testHiSysEventBox21 start') 547 try { 548 hiSysEvent.subscribe([{ 549 domain: "R?*ELIABILITY", 550 names: ["CPP_CRASH_NO_LOG"] 551 }]) 552 expect().assertFail() 553 done(); 554 } catch (error) { 555 console.error(`testHiSysEventBox21 error code: ${error.code}, error msg: ${error.message}`); 556 expect(error.code == 11200302).assertTrue() 557 console.info('testHiSysEventBox21 end') 558 done(); 559 } 560 }) 561 562 /** 563 * @tc.number DFX_DFT_HiSysevent_SandBox_2200 564 * @tc.name testHiSysEventBox22 565 * @tc.desc 验证调用subscribe接口,QueryRule-evetname含有特殊字节,抛出错误码11200302 566 */ 567 it('testHiSysEventBox22', 3, async function (done) { 568 console.info('testHiSysEventBox22 start') 569 try { 570 hiSysEvent.subscribe([{ 571 domain: "RELIABILITY", 572 names: ["C?*PP_CRASH_NO_LOG"] 573 }]) 574 expect().assertFail() 575 done(); 576 } catch (error) { 577 console.error(`testHiSysEventBox22 error code: ${error.code}, error msg: ${error.message}`); 578 expect(error.code == 11200302).assertTrue() 579 console.info('testHiSysEventBox22 end') 580 done(); 581 } 582 }) 583 584 /** 585 * @tc.number DFX_DFT_HiSysevent_SandBox_2500 586 * @tc.name testHiSysEventBox25 587 * @tc.desc 验证无订阅,直接调用unsubscribe取消订阅,抛出错误码11200305 588 */ 589 it('testHiSysEventBox25', 3, async function (done) { 590 console.info('testHiSysEventBox25 start') 591 try { 592 hiSysEvent.unsubscribe(); 593 expect().assertFail() 594 done(); 595 } catch (error) { 596 console.error(`testHiSysEventBox25 error code: ${error.code}, error msg: ${error.message}`); 597 expect(error.code == 11200305).assertTrue() 598 console.info('testHiSysEventBox25 end') 599 done(); 600 } 601 }) 602}) 603} 604