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