@@ -2,11 +2,8 @@ import type { TestContext } from '../_Types';
22
33export function register ( it : Mocha . TestFunction , expect : Chai . ExpectStatic , context : TestContext ) : void {
44 const testName = `${ context . name } ${ context . adapterShortName } files: ` ;
5- // chmodFile
6- // readDir
7- // unlink
8- // rename
9- // mkdir
5+ const namespace = 'testObject.0' ;
6+ const testId = `${ namespace } .testFilesObj` ;
107
118 // setBinaryState
129 it ( testName + 'setForeignBinaryState' , async ( ) => {
@@ -267,6 +264,188 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
267264 expect ( error . toString ( ) ) . to . be . equal ( 'Error: Not exists' ) ;
268265 }
269266 } ) ;
267+
268+ it ( testName + 'should create and read file with callback' , done => {
269+ const objects = context . objects ;
270+ objects . setObject ( testId , { type : 'meta' , native : { } } as ioBroker . SettableMetaObject , err => {
271+ expect ( err ) . to . be . not . ok ;
272+ objects . writeFile ( testId , 'myFile/abc.txt' , 'dataInFile' , err => {
273+ err && console . error ( `Got ${ JSON . stringify ( objects . getStatus ( ) ) } : ${ err . stack } ` ) ;
274+ expect ( err ) . to . be . not . ok ;
275+
276+ objects . readFile ( testId , 'myFile/abc.txt' , null , ( err , data , mimeType ) => {
277+ expect ( err ) . to . be . not . ok ;
278+ expect ( data ) . to . be . equal ( 'dataInFile' ) ;
279+ expect ( mimeType ) . to . be . equal ( 'text/plain' ) ;
280+ objects . rm ( testId , 'myFile/*' , null , ( err , files ) => {
281+ expect ( err ) . to . be . not . ok ;
282+ const file = files ! . find ( f => f . file === 'abc.txt' ) ;
283+ expect ( file ! . file ) . to . be . equal ( 'abc.txt' ) ;
284+ expect ( file ! . path ) . to . be . equal ( 'myFile' ) ;
285+ objects . readFile ( testId , 'myFile/abc.txt' , null , ( err , _data , _mimeType ) => {
286+ expect ( err ! . message ) . to . be . equal ( 'Not exists' ) ;
287+ done ( ) ;
288+ } ) ;
289+ } ) ;
290+ } ) ;
291+ } ) ;
292+ } ) ;
293+ } ) ;
294+
295+ it ( testName + 'should create and read file async' , async ( ) => {
296+ const fileDir = 'myFile' ;
297+ const fileName = 'abc2.txt' ;
298+ const fullFileName = `${ fileDir } /${ fileName } ` ;
299+
300+ const objects = context . objects ;
301+ await objects . setObject ( testId , { type : 'meta' , native : { } } as ioBroker . SettableMetaObject ) ;
302+
303+ await objects . writeFile ( testId , fullFileName , 'dataInFile' ) ;
304+
305+ const { file, mimeType } = await objects . readFile ( testId , fullFileName , null ) ;
306+ expect ( file ) . to . be . equal ( 'dataInFile' ) ;
307+ expect ( mimeType ) . to . be . equal ( 'text/plain' ) ;
308+ const files = await objects . rmAsync ( testId , `${ fileDir } /*` , { } ) ;
309+ const deletedFile = files ! . find ( f => f . file === fileName ) ;
310+ expect ( deletedFile ! . file ) . to . be . equal ( fileName ) ;
311+ expect ( deletedFile ! . path ) . to . be . equal ( fileDir ) ;
312+ try {
313+ await objects . readFile ( testId , fullFileName , null ) ;
314+ expect ( 1 ) . to . be . equal ( 2 , 'Should have thrown, because file has been deleted' ) ;
315+ } catch ( e ) {
316+ expect ( e . message ) . to . be . equal ( 'Not exists' ) ;
317+ }
318+ } ) ;
319+
320+ it ( testName + 'should read directory' , done => {
321+ const objects = context . objects ;
322+ objects . writeFile ( testId , 'myFileA/abc1.txt' , 'dataInFile' , err => {
323+ expect ( err ) . to . be . not . ok ;
324+ objects . writeFile ( testId , 'myFileA/abc2.txt' , Buffer . from ( 'ABC' ) , err => {
325+ expect ( err ) . to . be . not . ok ;
326+ objects . readDir ( testId , 'myFileA/' , null , ( err , data ) => {
327+ expect ( err ) . to . be . not . ok ;
328+ expect ( data ! . length ) . to . be . equal ( 2 ) ;
329+ expect ( data ! [ 0 ] . file ) . to . be . equal ( 'abc1.txt' ) ;
330+ expect ( data ! [ 1 ] . file ) . to . be . equal ( 'abc2.txt' ) ;
331+ expect ( data ! [ 1 ] . stats . size ) . to . be . equal ( 3 ) ;
332+ done ( ) ;
333+ } ) ;
334+ } ) ;
335+ } ) ;
336+ } ) ;
337+
338+ it ( testName + 'should read file and prevent path traversing' , done => {
339+ const objects = context . objects ;
340+ objects . readFile ( testId , '../../myFileA/abc1.txt' , null , ( err , data , _mimeType ) => {
341+ expect ( err ) . to . be . not . ok ;
342+ expect ( data ) . to . be . equal ( 'dataInFile' ) ;
343+ objects . readFile ( testId , '/myFileA/abc1.txt' , null , ( err , data , _mimeType ) => {
344+ expect ( err ) . to . be . not . ok ;
345+ expect ( data ) . to . be . equal ( 'dataInFile' ) ;
346+ objects . readFile ( testId , '/../../myFileA/abc1.txt' , null , ( err , data , _mimeType ) => {
347+ expect ( err ) . to . be . not . ok ;
348+ expect ( data ) . to . be . equal ( 'dataInFile' ) ;
349+ objects . readFile ( testId , 'myFileA/../blubb/../myFileA/abc1.txt' , null , ( err , data , _mimeType ) => {
350+ expect ( err ) . to . be . not . ok ;
351+ expect ( data ) . to . be . equal ( 'dataInFile' ) ;
352+ objects . readFile (
353+ testId ,
354+ '/myFileA/../blubb/../myFileA/abc1.txt' ,
355+ null ,
356+ ( err , data , _mimeType ) => {
357+ expect ( err ) . to . be . not . ok ;
358+ expect ( data ) . to . be . equal ( 'dataInFile' ) ;
359+ objects . readFile (
360+ testId ,
361+ '../blubb/../myFileA/abc1.txt' ,
362+ null ,
363+ ( err , data , _mimeType ) => {
364+ expect ( err ) . to . be . not . ok ;
365+ expect ( data ) . to . be . equal ( 'dataInFile' ) ;
366+ objects . readFile (
367+ testId ,
368+ '/../blubb/../myFileA/abc1.txt' ,
369+ null ,
370+ ( err , data , _mimeType ) => {
371+ expect ( err ) . to . be . not . ok ;
372+ expect ( data ) . to . be . equal ( 'dataInFile' ) ;
373+ done ( ) ;
374+ }
375+ ) ;
376+ }
377+ ) ;
378+ }
379+ ) ;
380+ } ) ;
381+ } ) ;
382+ } ) ;
383+ } ) ;
384+ } ) ;
385+
386+ it ( testName + 'should unlink file' , done => {
387+ const objects = context . objects ;
388+ objects . unlink ( testId , 'myFileA/abc1.txt' , null , err => {
389+ expect ( err ) . to . be . not . ok ;
390+ objects . unlink ( testId , 'myFileA/abc1.txt' , null , err => {
391+ expect ( err ! . message ) . to . be . equal ( 'Not exists' ) ;
392+ done ( ) ;
393+ } ) ;
394+ } ) ;
395+ } ) ;
396+
397+ it ( testName + 'should rename file' , done => {
398+ const objects = context . objects ;
399+ objects . writeFile ( testId , 'myFile1/abcRename.txt' , Buffer . from ( 'abcd' ) , err => {
400+ expect ( err ) . to . be . not . ok ;
401+ objects . rename ( testId , 'myFile1/abcRename.txt' , 'myFileA/abc3.txt' , null , err => {
402+ expect ( err ) . to . be . not . ok ;
403+ objects . readFile ( testId , 'myFileA/abc3.txt' , null , ( err , data , _meta ) => {
404+ expect ( err ) . to . be . not . ok ;
405+ expect ( data ! . toString ( 'utf8' ) ) . to . be . equal ( 'abcd' ) ;
406+ objects . readFile ( testId , 'myFile1/abcRename.txt' , null , err => {
407+ expect ( err ! . message ) . to . be . equal ( 'Not exists' ) ;
408+ done ( ) ;
409+ } ) ;
410+ } ) ;
411+ } ) ;
412+ } ) ;
413+ } ) ;
414+
415+ it ( testName + 'should touch file' , done => {
416+ const objects = context . objects ;
417+ objects . readDir ( testId , 'myFileA' , null , ( err , files ) => {
418+ expect ( err ) . to . be . not . ok ;
419+ const file = files ! . find ( f => f . file === 'abc3.txt' ) ;
420+
421+ setTimeout ( ( ) => {
422+ objects . touch ( testId , 'myFileA/abc3.txt' , null , err => {
423+ expect ( err ) . to . be . not . ok ;
424+ objects . readDir ( testId , 'myFileA' , null , ( _err , files ) => {
425+ const file1 = files ! . find ( f => f . file === 'abc3.txt' ) ;
426+ expect ( file1 ! . modifiedAt ) . to . be . not . equal ( file ! . modifiedAt ) ;
427+ done ( ) ;
428+ } ) ;
429+ } ) ;
430+ } , 200 ) ;
431+ } ) ;
432+ } ) ;
433+
434+ it ( testName + 'should create directory' , done => {
435+ const objects = context . objects ;
436+ objects . mkdir ( testId , 'myFile' + Math . round ( Math . random ( ) * 100_000 ) , null , err => {
437+ expect ( err ) . to . be . not . ok ;
438+ done ( ) ;
439+ } ) ;
440+ } ) ;
441+
442+ it ( testName + 'should enable file cache' , done => {
443+ const objects = context . objects ;
444+ objects . enableFileCache ( true , err => {
445+ expect ( err ) . to . be . not . ok ;
446+ done ( ) ;
447+ } ) ;
448+ } ) ;
270449}
271450
272451module . exports . register = register ;
0 commit comments