1/* 2 * Copyright (C) 2023 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 fileio from '@ohos.fileio'; 17import { 18 fileIO, FILE_CONTENT, prepareFile, sleep, nextFileName, 19 describe, it, expect, 20} from '../Common'; 21 22 const WAIT_HALF_SECOND = 500; 23 const watcherEvent = { 24 'IN_ACCESS': 0x00000001, 25 'IN_MODIFY': 0x00000002, 26 'IN_ATTRIB': 0x00000004, 27 'IN_CLOSE_WRITE': 0x00000008, 28 'IN_CLOSE_NOWRITE': 0x00000010, 29 'IN_OPEN': 0x00000020, 30 'IN_MOVED_FROM': 0x00000040, 31 'IN_MOVED_TO': 0x00000080, 32 'IN_CREATE': 0x00000100, 33 'IN_DELETE': 0x00000200, 34 'IN_DELETE_SELF': 0x00000400, 35 'IN_MOVE_SELF': 0x00000800 36 } 37 38 const callback = (data) => { 39 console.log(`The watcher is triggered :{event: ${data.event}, fileName: ${data.fileName}, cookie:${data.cookie}}`); 40 } 41 42 function startWatcher(testNum, eventCode, watchPath) { 43 let ret = { 44 flag: false, 45 watcher: null 46 } 47 try { 48 ret.watcher = fileIO.createWatcher(watchPath, eventCode, (data) => { 49 console.log(`${testNum} :{event: ${data.event}, fileName: ${data.fileName}, cookie: ${data.cookie}}`); 50 if (eventCode === data.event) { 51 ret.flag = true; 52 } 53 }); 54 } catch (e) { 55 console.log(testNum + ' startWatcher failed for ' + e.message + ', code: ' + e.code); 56 } 57 ret.watcher.start(); 58 return ret; 59 } 60 61export default function fileIOWatcher() { 62 describe('fileIO_test_watcher', function () { 63 /** 64 * @tc.number SUB_DF_FILEIO_WATCHER_0000 65 * @tc.name fileIO_test_watcher_000 66 * @tc.desc Create multiple watcher events in multi-level directories. 67 * @tc.size MEDIUM 68 * @tc.type Function 69 * @tc.level Level 3 70 * @tc.require 71 */ 72 it('fileIO_test_watcher_000', 3, async function (done) { 73 let testNum = 'fileIO_test_watcher_000'; 74 let dpath = await nextFileName(testNum); 75 let fpath = dpath + '/file_000'; 76 let ddpath = dpath + '/dir_000'; 77 let ffpath = ddpath + '/file_000_1'; 78 let dddpath = ddpath + '/dir_000_1'; 79 let fffpath = dddpath + '/file_000_2'; 80 fileIO.mkdirSync(dpath); 81 fileIO.mkdirSync(ddpath); 82 fileIO.mkdirSync(dddpath); 83 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 84 expect(prepareFile(ffpath, FILE_CONTENT)).assertTrue(); 85 86 try { 87 let resWatcher = []; 88 let file1 = fileIO.openSync(fpath, fileIO.OpenMode.WRITE_ONLY); 89 let file2 = fileIO.openSync(ffpath, fileIO.OpenMode.READ_ONLY); 90 resWatcher[0] = startWatcher(testNum, watcherEvent.IN_MODIFY, dpath); 91 resWatcher[1] = startWatcher(testNum, watcherEvent.IN_ACCESS, ddpath); 92 resWatcher[2] = startWatcher(testNum, watcherEvent.IN_CREATE, dddpath); 93 let file3 = fileIO.openSync(fffpath, fileIO.OpenMode.CREATE); 94 await sleep(WAIT_HALF_SECOND); 95 expect(resWatcher[2].flag == true).assertTrue(); 96 fileIO.readSync(file2.fd, new ArrayBuffer(20)); 97 await sleep(WAIT_HALF_SECOND); 98 expect(resWatcher[1].flag == true).assertTrue(); 99 fileIO.writeSync(file1.fd, FILE_CONTENT); 100 await sleep(WAIT_HALF_SECOND); 101 expect(resWatcher[0].flag == true).assertTrue(); 102 resWatcher.forEach(element => { element.watcher.stop(); }); 103 fileIO.closeSync(file3); 104 fileIO.closeSync(file2); 105 fileIO.closeSync(file1); 106 fileIO.rmdirSync(dpath); 107 done(); 108 } catch (e) { 109 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 110 expect(false).assertTrue(); 111 } 112 }); 113 114 /** 115 * @tc.number SUB_DF_FILEIO_WATCHER_0100 116 * @tc.name fileIO_test_watcher_001 117 * @tc.desc Test watcher event '0x00000001': 'IN_ACCESS' 118 * read a file, verifying the event is 'IN_ACCESS'. 119 * @tc.size MEDIUM 120 * @tc.type Function 121 * @tc.level Level 0 122 * @tc.require 123 */ 124 it('fileIO_test_watcher_001', 0, async function (done) { 125 let testNum = 'fileIO_test_watcher_001'; 126 let dpath = await nextFileName(testNum); 127 let fpath = dpath + '/file_001.txt'; 128 fileIO.mkdirSync(dpath); 129 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 130 131 try { 132 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); 133 let resWatcher = startWatcher(testNum, watcherEvent.IN_ACCESS, dpath); 134 fileIO.readSync(file.fd, new ArrayBuffer(4096)); 135 await sleep(WAIT_HALF_SECOND); 136 expect(resWatcher.flag == true).assertTrue(); 137 resWatcher.watcher.stop(); 138 fileIO.closeSync(file); 139 fileIO.rmdirSync(dpath); 140 done(); 141 } catch (e) { 142 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 143 expect(false).assertTrue(); 144 } 145 }); 146 147 /** 148 * @tc.number SUB_DF_FILEIO_WATCHER_0200 149 * @tc.name fileIO_test_watcher_002 150 * @tc.desc Test watcher event '0x00000002': 'IN_MODIFY' 151 * write a file, verifying the event is 'IN_MODIFY'. 152 * @tc.size MEDIUM 153 * @tc.type Function 154 * @tc.level Level 0 155 * @tc.require 156 */ 157 it('fileIO_test_watcher_002', 0, async function (done) { 158 let testNum = 'fileIO_test_watcher_002'; 159 let dpath = await nextFileName(testNum); 160 let fpath = dpath + '/file_002.txt'; 161 fileIO.mkdirSync(dpath); 162 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 163 164 try { 165 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); 166 let resWatcher = startWatcher(testNum, watcherEvent.IN_MODIFY, dpath); 167 fileIO.writeSync(file.fd, FILE_CONTENT); 168 await sleep(WAIT_HALF_SECOND); 169 expect(resWatcher.flag == true).assertTrue(); 170 resWatcher.watcher.stop(); 171 fileIO.closeSync(file); 172 fileIO.rmdirSync(dpath); 173 done(); 174 } catch (e) { 175 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 176 expect(false).assertTrue(); 177 } 178 }); 179 180 /** 181 * @tc.number SUB_DF_FILEIO_WATCHER_0300 182 * @tc.name fileIO_test_watcher_003 183 * @tc.desc Test watcher event '0x00000004': 'IN_ATTRIB' 184 * Modify time of the file, verifying the event is 'IN_ATTRIB'. 185 * @tc.size MEDIUM 186 * @tc.type Function 187 * @tc.level Level 0 188 * @tc.require 189 */ 190 it('fileIO_test_watcher_003', 0, async function (done) { 191 let testNum = 'fileIO_test_watcher_003'; 192 let dpath = await nextFileName(testNum); 193 let fpath = dpath + '/file_003.txt'; 194 fileIO.mkdirSync(dpath); 195 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 196 197 try { 198 let resWatcher = startWatcher(testNum, watcherEvent.IN_ATTRIB, dpath); 199 let time = new Date().getTime(); 200 fileIO.utimes(fpath,time); 201 await sleep(WAIT_HALF_SECOND); 202 expect(resWatcher.flag == true).assertTrue(); 203 resWatcher.watcher.stop(); 204 fileIO.rmdirSync(dpath); 205 done(); 206 } catch (e) { 207 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 208 expect(false).assertTrue(); 209 } 210 }); 211 212 /** 213 * @tc.number SUB_DF_FILEIO_WATCHER_0400 214 * @tc.name fileIO_test_watcher_004 215 * @tc.desc Test watcher event '0x00000008': 'IN_CLOSE_WRITE' 216 * open a file on read_write mode and then close the file.fd, verifying the event is 'IN_CLOSE_WRITE'. 217 * @tc.size MEDIUM 218 * @tc.type Function 219 * @tc.level Level 0 220 * @tc.require 221 */ 222 it('fileIO_test_watcher_004', 0, async function (done) { 223 let testNum = 'fileIO_test_watcher_004'; 224 let dpath = await nextFileName(testNum); 225 let fpath = dpath + '/file_004.txt'; 226 fileIO.mkdirSync(dpath); 227 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 228 229 try { 230 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); 231 let resWatcher = startWatcher(testNum, watcherEvent.IN_CLOSE_WRITE, dpath); 232 fileIO.closeSync(file.fd); 233 await sleep(WAIT_HALF_SECOND); 234 expect(resWatcher.flag == true).assertTrue(); 235 resWatcher.watcher.stop(); 236 fileIO.rmdirSync(dpath); 237 done(); 238 } catch (e) { 239 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 240 expect(false).assertTrue(); 241 } 242 }); 243 244 /** 245 * @tc.number SUB_DF_FILEIO_WATCHER_0500 246 * @tc.name fileIO_test_watcher_005 247 * @tc.desc Test watcher event '0x00000020': 'IN_OPEN' 248 * open a file on only read mode, verifying the event is 'IN_OPEN'. 249 * @tc.size MEDIUM 250 * @tc.type Function 251 * @tc.level Level 0 252 * @tc.require 253 */ 254 it('fileIO_test_watcher_005', 0, async function (done) { 255 let testNum = 'fileIO_test_watcher_005'; 256 let dpath = await nextFileName(testNum); 257 let fpath = dpath + '/file_005.txt'; 258 fileIO.mkdirSync(dpath); 259 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 260 261 try { 262 let resWatcher = startWatcher(testNum, watcherEvent.IN_OPEN, dpath); 263 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY); 264 await sleep(WAIT_HALF_SECOND); 265 expect(resWatcher.flag == true).assertTrue(); 266 resWatcher.watcher.stop(); 267 fileIO.closeSync(file); 268 fileIO.rmdirSync(dpath); 269 done(); 270 } catch (e) { 271 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 272 expect(false).assertTrue(); 273 } 274 }); 275 276 /** 277 * @tc.number SUB_DF_FILEIO_WATCHER_0600 278 * @tc.name fileIO_test_watcher_006 279 * @tc.desc Test watcher event '0x00000040': 'IN_MOVED_FROM', '0x00000080': 'IN_MOVED_TO', 280 * rename a file, verifying the event is 'IN_MOVED_FROM' and 'IN_MOVED_TO'. 281 * @tc.size MEDIUM 282 * @tc.type Function 283 * @tc.level Level 0 284 * @tc.require 285 */ 286 it('fileIO_test_watcher_006', 0, async function (done) { 287 let testNum = 'fileIO_test_watcher_006'; 288 let dpath = await nextFileName(testNum); 289 let fpath = dpath + '/file_006.txt'; 290 fileIO.mkdirSync(dpath); 291 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 292 293 try { 294 let watcherFrom = 1; 295 let watcherTo = -1; 296 let watcher = fileIO.createWatcher(dpath, watcherEvent.IN_MOVED_FROM | watcherEvent.IN_MOVED_TO, (data) => { 297 console.log(`${testNum} :{event: ${data.event}, fileName: ${data.fileName}, cookie: ${data.cookie}}`); 298 if (watcherEvent.IN_MOVED_FROM === data.event) { 299 watcherFrom = data.cookie; 300 } 301 if (watcherEvent.IN_MOVED_TO === data.event) { 302 watcherTo = data.cookie; 303 } 304 }); 305 watcher.start(); 306 fileIO.renameSync(fpath, fpath + 'newf'); 307 await sleep(WAIT_HALF_SECOND); 308 expect(watcherFrom == watcherTo).assertTrue(); 309 watcher.stop(); 310 fileIO.rmdirSync(dpath); 311 done(); 312 } catch (e) { 313 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 314 expect(false).assertTrue(); 315 } 316 }); 317 318 /** 319 * @tc.number SUB_DF_FILEIO_WATCHER_0700 320 * @tc.name fileIO_test_watcher_007 321 * @tc.desc Test watcher event '0x00000100': 'IN_CREATE' 322 * open a nonexistent file on create mode, verifying the event is 'IN_CREATE'. 323 * @tc.size MEDIUM 324 * @tc.type Function 325 * @tc.level Level 0 326 * @tc.require 327 */ 328 it('fileIO_test_watcher_007', 0, async function (done) { 329 let testNum = 'fileIO_test_watcher_007'; 330 let dpath = await nextFileName(testNum); 331 let fpath = dpath + '/file_007.txt'; 332 fileIO.mkdirSync(dpath); 333 334 try { 335 let resWatcher = startWatcher(testNum, watcherEvent.IN_CREATE, dpath); 336 let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE); 337 await sleep(WAIT_HALF_SECOND); 338 expect(resWatcher.flag == true).assertTrue(); 339 resWatcher.watcher.stop(); 340 fileIO.closeSync(file); 341 fileIO.rmdirSync(dpath); 342 done(); 343 } catch (e) { 344 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 345 expect(false).assertTrue(); 346 } 347 }); 348 349 /** 350 * @tc.number SUB_DF_FILEIO_WATCHER_0800 351 * @tc.name fileIO_test_watcher_008 352 * @tc.desc Test watcher event '0x00000100': 'IN_CREATE' 353 * open a nonexistent folder on create mode, verifying the event is 'IN_CREATE'. 354 * @tc.size MEDIUM 355 * @tc.type Function 356 * @tc.level Level 0 357 * @tc.require 358 */ 359 it('fileIO_test_watcher_008', 0, async function (done) { 360 let testNum = 'fileIO_test_watcher_008'; 361 let dpath = await nextFileName(testNum); 362 let ddpath = dpath + '/dir_008'; 363 fileIO.mkdirSync(dpath); 364 365 try { 366 let resWatcher = startWatcher(testNum, watcherEvent.IN_CREATE, dpath); 367 fileIO.mkdirSync(ddpath); 368 await sleep(WAIT_HALF_SECOND); 369 expect(resWatcher.flag == true).assertTrue(); 370 resWatcher.watcher.stop(); 371 fileIO.rmdirSync(dpath); 372 done(); 373 } catch (e) { 374 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 375 expect(false).assertTrue(); 376 } 377 }); 378 379 /** 380 * @tc.number SUB_DF_FILEIO_WATCHER_0900 381 * @tc.name fileIO_test_watcher_009 382 * @tc.desc Test watcher event '0x00000200': 'IN_DELETE' 383 * delete a file , verifying the event is 'IN_DELETE'. 384 * @tc.size MEDIUM 385 * @tc.type Function 386 * @tc.level Level 0 387 * @tc.require 388 */ 389 it('fileIO_test_watcher_009', 0, async function (done) { 390 let testNum = 'fileIO_test_watcher_009'; 391 let dpath = await nextFileName(testNum); 392 let fpath = dpath + '/file_009.txt'; 393 fileIO.mkdirSync(dpath); 394 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 395 396 try { 397 let resWatcher = startWatcher(testNum, watcherEvent.IN_DELETE, dpath); 398 fileIO.unlinkSync(fpath); 399 await sleep(WAIT_HALF_SECOND); 400 expect(resWatcher.flag == true).assertTrue(); 401 resWatcher.watcher.stop(); 402 fileIO.rmdirSync(dpath); 403 done(); 404 } catch (e) { 405 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 406 expect(false).assertTrue(); 407 } 408 }); 409 410 /** 411 * @tc.number SUB_DF_FILEIO_WATCHER_1000 412 * @tc.name fileIO_test_watcher_010 413 * @tc.desc Test watcher event '0x00000200': 'IN_DELETE' 414 * delete a directory , verifying the event is 'IN_DELETE'. 415 * @tc.size MEDIUM 416 * @tc.type Function 417 * @tc.level Level 0 418 * @tc.require 419 */ 420 it('fileIO_test_watcher_010', 0, async function (done) { 421 let testNum = 'fileIO_test_watcher_010'; 422 let dpath = await nextFileName(testNum); 423 let ddpath = dpath + '/dir_010'; 424 fileIO.mkdirSync(dpath); 425 fileIO.mkdirSync(ddpath); 426 427 try { 428 let resWatcher = startWatcher(testNum, watcherEvent.IN_DELETE, dpath); 429 fileIO.rmdirSync(ddpath); 430 await sleep(WAIT_HALF_SECOND); 431 expect(resWatcher.flag == true).assertTrue(); 432 resWatcher.watcher.stop(); 433 fileIO.rmdirSync(dpath); 434 done(); 435 } catch (e) { 436 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 437 expect(false).assertTrue(); 438 } 439 }); 440 441 /** 442 * @tc.number SUB_DF_FILEIO_WATCHER_1100 443 * @tc.name fileIO_test_watcher_011 444 * @tc.desc Test watcher event '0x00000400': 'IN_DELETE_SELF' 445 * delete watcher folder , verifying the event is 'IN_DELETE_SELF'. 446 * @tc.size MEDIUM 447 * @tc.type Function 448 * @tc.level Level 0 449 * @tc.require 450 */ 451 it('fileIO_test_watcher_011', 0, async function (done) { 452 let testNum = 'fileIO_test_watcher_011'; 453 let dpath = await nextFileName(testNum); 454 fileIO.mkdirSync(dpath); 455 456 try { 457 let resWatcher = startWatcher(testNum, watcherEvent.IN_DELETE_SELF, dpath); 458 fileIO.rmdirSync(dpath); 459 await sleep(WAIT_HALF_SECOND); 460 expect(resWatcher.flag == true).assertTrue(); 461 resWatcher.watcher.stop(); 462 done(); 463 } catch (e) { 464 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 465 expect(false).assertTrue(); 466 } 467 }); 468 469 /** 470 * @tc.number SUB_DF_FILEIO_WATCHER_1200 471 * @tc.name fileIO_test_watcher_012 472 * @tc.desc Test watcher event '0x00000800': 'IN_MOVE_SELF' 473 * rename watcher folder , verifying the event is 'IN_MOVE_SELF'. 474 * @tc.size MEDIUM 475 * @tc.type Function 476 * @tc.level Level 0 477 * @tc.require 478 */ 479 it('fileIO_test_watcher_012', 0, async function (done) { 480 let testNum = 'fileIO_test_watcher_012'; 481 let dpath = await nextFileName(testNum); 482 fileIO.mkdirSync(dpath); 483 484 try { 485 let resWatcher = startWatcher(testNum, watcherEvent.IN_MOVE_SELF, dpath); 486 fileIO.renameSync(dpath, dpath + 'newd'); 487 await sleep(WAIT_HALF_SECOND); 488 expect(resWatcher.flag == true).assertTrue(); 489 resWatcher.watcher.stop(); 490 fileIO.rmdirSync(dpath + 'newd'); 491 done(); 492 } catch (e) { 493 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 494 expect(false).assertTrue(); 495 } 496 }); 497 498 /** 499 * @tc.number SUB_DF_FILEIO_WATCHER_1300 500 * @tc.name fileIO_test_watcher_013 501 * @tc.desc Test watcher event '0x00000800': 'IN_MOVE_SELF' 502 * rename watcher file , verifying the event is 'IN_MOVE_SELF'. 503 * @tc.size MEDIUM 504 * @tc.type Function 505 * @tc.level Level 3 506 * @tc.require 507 */ 508 it('fileIO_test_watcher_013', 3, async function (done) { 509 let testNum = 'fileIO_test_watcher_013'; 510 let fpath = await nextFileName(testNum); 511 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 512 513 try { 514 let resWatcher = startWatcher(testNum, watcherEvent.IN_MOVE_SELF, fpath); 515 fileIO.renameSync(fpath, fpath + 'newd'); 516 await sleep(WAIT_HALF_SECOND); 517 expect(resWatcher.flag == true).assertTrue(); 518 resWatcher.watcher.stop(); 519 fileIO.unlinkSync(fpath + 'newd'); 520 done(); 521 } catch (e) { 522 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 523 expect(false).assertTrue(); 524 } 525 }); 526 527 528 /** 529 * @tc.number SUB_DF_FILEIO_WATCHER_1400 530 * @tc.name fileIO_test_watcher_014 531 * @tc.desc Test watcher event '0x00000010': 'IN_CLOSE_NOWRITE' 532 * open a file on read only mode and then close the file.fd, verifying the event is 'IN_CLOSE_NOWRITE'. 533 * @tc.size MEDIUM 534 * @tc.type Function 535 * @tc.level Level 0 536 * @tc.require 537 */ 538 it('fileIO_test_watcher_014', 0, async function (done) { 539 let testNum = 'fileIO_test_watcher_014'; 540 let dpath = await nextFileName(testNum); 541 let fpath = dpath + '/file_014.txt'; 542 fileIO.mkdirSync(dpath); 543 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 544 545 try { 546 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY); 547 let resWatcher = startWatcher(testNum, watcherEvent.IN_CLOSE_NOWRITE, dpath); 548 fileIO.closeSync(file.fd); 549 await sleep(WAIT_HALF_SECOND); 550 expect(resWatcher.flag == true).assertTrue(); 551 resWatcher.watcher.stop(); 552 fileIO.rmdirSync(dpath); 553 done(); 554 } catch (e) { 555 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 556 expect(false).assertTrue(); 557 } 558 }); 559 560 /** 561 * @tc.number SUB_DF_FILEIO_WATCHER_1500 562 * @tc.name fileIO_test_watcher_015 563 * @tc.desc Create multiple watchers in the same directory. 564 * @tc.size MEDIUM 565 * @tc.type Function 566 * @tc.level Level 0 567 * @tc.require 568 */ 569 it('fileIO_test_watcher_015', 0, async function (done) { 570 let testNum = 'fileIO_test_watcher_015'; 571 let dpath = await nextFileName(testNum); 572 let fpath1 = dpath + '/file_015.txt'; 573 let fpath2 = dpath + '/file_016.txt'; 574 fileIO.mkdirSync(dpath); 575 expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); 576 expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); 577 578 try { 579 let resWatcher = []; 580 resWatcher[0] = fileIO.createWatcher(dpath, watcherEvent.IN_OPEN, callback); 581 resWatcher[1] = fileIO.createWatcher(dpath, watcherEvent.IN_ACCESS, callback); 582 resWatcher[2] = fileIO.createWatcher(dpath, watcherEvent.IN_CLOSE_NOWRITE, callback); 583 resWatcher[3] = fileIO.createWatcher(dpath, watcherEvent.IN_MODIFY, callback); 584 resWatcher[4] = fileIO.createWatcher(dpath, watcherEvent.IN_CLOSE_WRITE, callback); 585 resWatcher.forEach(element => { element.start(); }); 586 let file1 = fileIO.openSync(fpath1, fileIO.OpenMode.READ_ONLY); 587 let file2 = fileIO.openSync(fpath2, fileIO.OpenMode.READ_WRITE); 588 await sleep(WAIT_HALF_SECOND); 589 fileIO.readSync(file1.fd, new ArrayBuffer(64)); 590 await sleep(WAIT_HALF_SECOND); 591 fileIO.closeSync(file1); 592 await sleep(WAIT_HALF_SECOND); 593 fileIO.writeSync(file2.fd, FILE_CONTENT); 594 await sleep(WAIT_HALF_SECOND); 595 fileIO.closeSync(file2.fd); 596 await sleep(WAIT_HALF_SECOND); 597 resWatcher.forEach(element => { element.stop(); }); 598 fileIO.rmdirSync(dpath); 599 done(); 600 } catch (e) { 601 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 602 } 603 }); 604 605 /** 606 * @tc.number SUB_DF_FILEIO_WATCHER_1600 607 * @tc.name fileIO_test_watcher_016 608 * @tc.desc Same event, different callback function. 609 * @tc.size MEDIUM 610 * @tc.type Function 611 * @tc.level Level 3 612 * @tc.require 613 */ 614 it('fileIO_test_watcher_016', 3, async function (done) { 615 let testNum = 'fileIO_test_watcher_016'; 616 let dpath = await nextFileName(testNum); 617 let fpath = dpath + '/file_005.txt'; 618 fileIO.mkdirSync(dpath); 619 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 620 621 try { 622 let resWatcher1 = fileIO.createWatcher(dpath, watcherEvent.IN_OPEN, (data) => { 623 console.log(`cb_st: ${testNum} :{event: ${data.event}, fileName: ${data.fileName}, cookie: ${data.cookie}}`); 624 }); 625 resWatcher1.start(); 626 let resWatcher2 = fileIO.createWatcher(dpath, watcherEvent.IN_OPEN, (data) => { 627 console.log(`cb_nd: ${testNum} :{event: ${data.event}, fileName: ${data.fileName}, cookie: ${data.cookie}}`); 628 }); 629 resWatcher2.start(); 630 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY); 631 await sleep(WAIT_HALF_SECOND); 632 resWatcher1.stop(); 633 resWatcher2.stop(); 634 fileIO.closeSync(file); 635 fileIO.rmdirSync(dpath); 636 done(); 637 } catch (e) { 638 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 639 expect(false).assertTrue(); 640 } 641 }); 642 643 /** 644 * @tc.number SUB_DF_FILEIO_WATCHER_1700 645 * @tc.name fileIO_test_watcher_017 646 * @tc.desc Same parameter, throw errno. 647 * @tc.size MEDIUM 648 * @tc.type Function 649 * @tc.level Level 3 650 * @tc.require 651 */ 652 it('fileIO_test_watcher_017', 3, async function (done) { 653 let testNum = 'fileIO_test_watcher_017'; 654 let fpath = await nextFileName(testNum); 655 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 656 let watcher = null; 657 658 try { 659 watcher = fileIO.createWatcher(fpath, watcherEvent.IN_ACCESS, callback); 660 watcher.start(); 661 fileIO.createWatcher(fpath, watcherEvent.IN_ACCESS, callback); 662 expect(false).assertTrue(); 663 } catch (e) { 664 watcher.stop(); 665 fileIO.unlinkSync(fpath); 666 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 667 expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); 668 done(); 669 } 670 }); 671 672 /** 673 * @tc.number SUB_DF_FILEIO_WATCHER_1800 674 * @tc.name fileIO_test_watcher_018 675 * @tc.desc Test Invalid parameter, events = string. 676 * @tc.size MEDIUM 677 * @tc.type Function 678 * @tc.level Level 3 679 * @tc.require 680 */ 681 it('fileIO_test_watcher_018', 3, async function (done) { 682 let testNum = 'fileIO_test_watcher_018'; 683 let fpath = await nextFileName(testNum); 684 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 685 const invalidEvent = 'invalid_event'; 686 687 try { 688 fileIO.createWatcher(fpath, invalidEvent, (data) => {}); 689 expect(false).assertTrue(); 690 } catch (e) { 691 fileIO.unlinkSync(fpath); 692 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 693 expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); 694 done(); 695 } 696 }); 697 698 /** 699 * @tc.number SUB_DF_FILEIO_WATCHER_1900 700 * @tc.name fileIO_test_watcher_019 701 * @tc.desc Test Invalid parameter, put a parameter in watcher.start. 702 * @tc.size MEDIUM 703 * @tc.type Function 704 * @tc.level Level 3 705 * @tc.require 706 */ 707 it('fileIO_test_watcher_019', 3, async function (done) { 708 let testNum = 'fileIO_test_watcher_019'; 709 let fpath = await nextFileName(testNum); 710 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 711 712 try { 713 let watcher = fileIO.createWatcher(fpath, watcherEvent.IN_ACCESS, (data) => {}); 714 watcher.start(-1); 715 expect(false).assertTrue(); 716 } catch (e) { 717 fileIO.unlinkSync(fpath); 718 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 719 expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); 720 done(); 721 } 722 }); 723 724 /** 725 * @tc.number SUB_DF_FILEIO_WATCHER_2000 726 * @tc.name fileIO_test_watcher_020 727 * @tc.desc Test Invalid parameter, put a parameter in watcher.stop. 728 * @tc.size MEDIUM 729 * @tc.type Function 730 * @tc.level Level 3 731 * @tc.require 732 */ 733 it('fileIO_test_watcher_020', 3, async function (done) { 734 let testNum = 'fileIO_test_watcher_020'; 735 let fpath = await nextFileName(testNum); 736 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 737 let watcher; 738 739 try { 740 watcher = fileIO.createWatcher(fpath, watcherEvent.IN_ACCESS, (data) => {}); 741 watcher.start(); 742 watcher.stop(-1); 743 expect(false).assertTrue(); 744 } catch (e) { 745 watcher.stop(); 746 fileIO.unlinkSync(fpath); 747 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 748 expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); 749 done(); 750 } 751 }); 752 753 /** 754 * @tc.number SUB_DF_FILEIO_WATCHER_2100 755 * @tc.name fileIO_test_watcher_021 756 * @tc.desc No watcher started. 757 * @tc.size MEDIUM 758 * @tc.type Function 759 * @tc.level Level 3 760 * @tc.require 761 */ 762 it('fileIO_test_watcher_021', 3, async function (done) { 763 let testNum = 'fileIO_test_watcher_021'; 764 let fpath = await nextFileName(testNum); 765 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 766 767 try { 768 let watcher = fileIO.createWatcher(fpath, watcherEvent.IN_ACCESS, (data) => {}); 769 watcher.stop(); 770 expect(false).assertTrue(); 771 } catch (e) { 772 fileIO.unlinkSync(fpath); 773 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 774 expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); 775 done(); 776 } 777 }); 778 779 /** 780 * @tc.number SUB_DF_FILEIO_WATCHER_2200 781 * @tc.name fileIO_test_watcher_022 782 * @tc.desc Test Invalid parameter, callback = object. 783 * @tc.size MEDIUM 784 * @tc.type Function 785 * @tc.level Level 3 786 * @tc.require 787 */ 788 it('fileIO_test_watcher_022', 3, async function (done) { 789 let testNum = 'fileIO_test_watcher_022'; 790 let fpath = await nextFileName(testNum); 791 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 792 const invalidListener = {}; 793 794 try { 795 fileIO.createWatcher(fpath, watcherEvent.IN_OPEN, invalidListener); 796 expect(false).assertTrue(); 797 } catch (e) { 798 fileIO.unlinkSync(fpath); 799 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 800 expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); 801 done(); 802 } 803 }); 804 805 /** 806 * @tc.number SUB_DF_FILEIO_WATCHER_2300 807 * @tc.name fileIO_test_watcher_023 808 * @tc.desc Test Invalid parameter, events = -1 809 * @tc.size MEDIUM 810 * @tc.type Function 811 * @tc.level Level 3 812 * @tc.require 813 */ 814 it('fileIO_test_watcher_023', 3, async function (done) { 815 let testNum = 'fileIO_test_watcher_023'; 816 let fpath = await nextFileName(testNum); 817 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 818 const invalidEvent = -1; 819 820 try { 821 let watcher = fileIO.createWatcher(fpath, invalidEvent, (data) => {}); 822 watcher.start(); 823 expect(false).assertTrue(); 824 } catch (e) { 825 fileIO.unlinkSync(fpath); 826 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 827 expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); 828 done(); 829 } 830 }); 831 832 /** 833 * @tc.number SUB_DF_FILEIO_WATCHER_2400 834 * @tc.name fileIO_test_watcher_024 835 * @tc.desc Missing the third parametor. 836 * @tc.size MEDIUM 837 * @tc.type Function 838 * @tc.level Level 3 839 * @tc.require 840 */ 841 it('fileIO_test_watcher_024', 3, async function (done) { 842 let testNum = 'fileIO_test_watcher_024'; 843 let fpath = await nextFileName(testNum); 844 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 845 846 try { 847 fileIO.createWatcher(fpath, watcherEvent.IN_OPEN); 848 expect(false).assertTrue(); 849 } catch (e) { 850 fileIO.unlinkSync(fpath); 851 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 852 expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); 853 done(); 854 } 855 }); 856 857 /** 858 * @tc.number SUB_DF_FILEIO_WATCHER_2500 859 * @tc.name fileIO_test_watcher_025 860 * @tc.desc The path point to nothing, no such file. 861 * @tc.size MEDIUM 862 * @tc.type Function 863 * @tc.level Level 3 864 * @tc.require 865 */ 866 it('fileIO_test_watcher_025', 3, async function (done) { 867 let testNum = 'fileIO_test_watcher_025'; 868 let fpath = await nextFileName(testNum); 869 870 try { 871 let watcher = fileIO.createWatcher(fpath, watcherEvent.IN_ACCESS, (data) => {}); 872 watcher.start(); 873 expect(false).assertTrue(); 874 } catch (e) { 875 console.log(testNum + ' has failed for ' + e.message + ', code: ' + e.code); 876 expect(e.code == 13900002 && e.message == 'No such file or directory').assertTrue(); 877 done(); 878 } 879 }); 880}); 881} 882