robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local uv =vim . uv 2local bit =require ( 'bit' ) 3 4local t =require ( 'test.unit.testutil' ) 5local describe , before_each , after_each , pending = 6t . describe , t . before_each , t . after_each , t . pending 7local itp =t . gen_itp ( t . it ) 8 9local cimport =t . cimport 10local cppimport =t . cppimport 11local internalize =t . internalize 12local ok =t . ok 13local eq =t . eq 14local neq =t . neq 15local ffi =t . ffi 16local cstr =t . cstr 17local to_cstr =t . to_cstr 18local OK =t . OK 19local FAIL =t . FAIL 20local NULL =t . NULL 21local mkdir =t . mkdir 22local endswith =vim . endswith 23 24local NODE_NORMAL =0 25local NODE_WRITABLE =1 26 27local fs =cimport ( './src/nvim/os/os.h' , './src/nvim/path.h' ) 28cppimport ( 'sys/stat.h' ) 29cppimport ( 'fcntl.h' ) 30cimport ( 'uv.h' ) 31 32local s ='' 33for i =0 , 255 do 34s =s .. ( i == 0 and '\0' or ( '%c' ): format ( i )) 35end 36local fcontents =s : rep ( 16 ) 37 38local directory =nil 39local absolute_executable =nil 40local executable_name =nil 41 42local function set_bit ( number , to_set ) 43return bit . bor ( number , to_set ) 44end 45 46local function unset_bit ( number , to_unset ) 47return bit . band ( number , ( bit . bnot ( to_unset ))) 48end 49 50local function assert_file_exists ( filepath ) 51neq ( nil , uv . fs_stat ( filepath )) 52end 53 54local function assert_file_does_not_exist ( filepath ) 55eq ( nil , uv . fs_stat ( filepath )) 56end 57 58local function os_setperm ( filename , perm ) 59return fs . os_setperm (( to_cstr ( filename )), perm ) 60end 61 62local function os_getperm ( filename ) 63local perm =fs . os_getperm (( to_cstr ( filename ))) 64return tonumber ( perm ) 65end 66 67describe ( 'fs.c' , function () 68local function os_isdir ( name ) 69return fs . os_isdir ( to_cstr ( name )) 70end 71 72before_each ( function () 73mkdir ( 'unit-test-directory' ) 74 75io . open ( 'unit-test-directory/test.file' , 'w' ): close () 76 77io . open ( 'unit-test-directory/test_2.file' , 'w' ): close () 78uv . fs_symlink ( 'test.file' , 'unit-test-directory/test_link.file' ) 79 80uv . fs_symlink ( 'non_existing_file.file' , 'unit-test-directory/test_broken_link.file' ) 81-- The tests are invoked with an absolute executable path in arg[0]. 82absolute_executable =vim . fs . normalize ( assert ( uv . exepath ())) 83-- Split the absolute_executable path into a directory and filename. 84directory , executable_name =string . match ( absolute_executable , '^(.*)/(.*)$' ) 85end ) 86 87after_each ( function () 88os . remove ( 'unit-test-directory/test.file' ) 89os . remove ( 'unit-test-directory/test_2.file' ) 90os . remove ( 'unit-test-directory/test_link.file' ) 91os . remove ( 'unit-test-directory/test_hlink.file' ) 92os . remove ( 'unit-test-directory/test_broken_link.file' ) 93uv . fs_rmdir ( 'unit-test-directory' ) 94end ) 95 96describe ( 'os_dirname' , function () 97itp ( 'returns OK and writes current directory to the buffer' , function () 98local length =string . len ( uv . cwd ()) + 1 99local buf =cstr ( length , '' ) 100eq ( OK , fs . os_dirname ( buf , length )) 101eq ( uv . cwd (), ffi . string ( buf )) 102end ) 103 104itp ( 'returns FAIL if the buffer is too small' , function () 105local length =string . len ( uv . cwd ()) + 1 106local buf =cstr ( length - 1 , '' ) 107eq ( FAIL , fs . os_dirname ( buf , length - 1 )) 108end ) 109end ) 110 111describe ( 'os_chdir' , function () 112itp ( 'fails with path="~"' , function () 113eq ( false , os_isdir ( '~' ), 'sanity check: no literal "~" directory' ) 114local length =4096 115local expected_cwd =cstr ( length , '' ) 116local cwd =cstr ( length , '' ) 117eq ( OK , fs . os_dirname ( expected_cwd , length )) 118 119-- os_chdir returns 0 for success, not OK (1). 120neq ( 0 , fs . os_chdir ( '~' )) -- fail 121neq ( 0 , fs . os_chdir ( '~/' )) -- fail 122 123eq ( OK , fs . os_dirname ( cwd , length )) 124-- CWD did not change. 125eq ( ffi . string ( expected_cwd ), ffi . string ( cwd )) 126end ) 127end ) 128 129describe ( 'os_isdir' , function () 130itp ( 'returns false if an empty string is given' , function () 131eq ( false , ( os_isdir ( '' ))) 132end ) 133 134itp ( 'returns false if a nonexisting directory is given' , function () 135eq ( false , ( os_isdir ( 'non-existing-directory' ))) 136end ) 137 138itp ( 'returns false if a nonexisting absolute directory is given' , function () 139eq ( false , ( os_isdir ( '/non-existing-directory' ))) 140end ) 141 142itp ( 'returns false if an existing file is given' , function () 143eq ( false , ( os_isdir ( 'unit-test-directory/test.file' ))) 144end ) 145 146itp ( 'returns true if the current directory is given' , function () 147eq ( true , ( os_isdir ( '.' ))) 148end ) 149 150itp ( 'returns true if the parent directory is given' , function () 151eq ( true , ( os_isdir ( '..' ))) 152end ) 153 154itp ( 'returns true if an arbitrary directory is given' , function () 155eq ( true , ( os_isdir ( 'unit-test-directory' ))) 156end ) 157 158itp ( 'returns true if an absolute directory is given' , function () 159eq ( true , ( os_isdir ( directory ))) 160end ) 161end ) 162 163describe ( 'os_can_exe' , function () 164local function os_can_exe ( name ) 165local buf =ffi . new ( 'char *[1]' ) 166buf [ 0 ] =NULL 167local ce_ret =fs . os_can_exe ( to_cstr ( name ), buf , true ) 168 169-- When os_can_exe returns true, it must set the path. 170-- When it returns false, the path must be NULL. 171if ce_ret then 172neq ( NULL , buf [ 0 ]) 173return internalize ( buf [ 0 ]) 174else 175eq ( NULL , buf [ 0 ]) 176return nil 177end 178end 179 180local function cant_exe ( name ) 181eq ( nil , os_can_exe ( name )) 182end 183 184local function exe ( name ) 185return os_can_exe ( name ) 186end 187 188itp ( 'returns false when given a directory' , function () 189cant_exe ( './unit-test-directory' ) 190end ) 191 192itp ( 'returns false when given a regular file without executable bit set' , function () 193cant_exe ( 'unit-test-directory/test.file' ) 194end ) 195 196itp ( 'returns false when the given file does not exists' , function () 197cant_exe ( 'does-not-exist.file' ) 198end ) 199 200itp ( 'returns the absolute path when given an executable inside $PATH' , function () 201local fullpath =exe ( 'ls' ) 202eq ( true , fs . path_is_absolute ( to_cstr ( fullpath ))) 203end ) 204 205itp ( 'returns the absolute path when given an executable relative to the current dir' , function () 206local old_dir =uv . cwd () 207 208uv . chdir ( directory ) 209 210-- Rely on currentdir to resolve symlinks, if any. Testing against 211-- the absolute path taken from arg[0] may result in failure where 212-- the path has a symlink in it. 213local canonical =uv . cwd () .. '/' .. executable_name 214local expected =exe ( canonical ) 215local relative_executable ='./' .. executable_name 216local res =exe ( relative_executable ) 217 218-- Don't test yet; we need to chdir back first. 219uv . chdir ( old_dir ) 220eq ( expected , res ) 221end ) 222end ) 223 224describe ( 'file permissions' , function () 225local function os_fchown ( filename , user_id , group_id ) 226local fd =ffi . C . open ( filename , 0 ) 227local res =fs . os_fchown ( fd , user_id , group_id ) 228ffi . C . close ( fd ) 229return res 230end 231 232local function os_file_is_readable ( filename ) 233return fs . os_file_is_readable (( to_cstr ( filename ))) 234end 235 236local function os_file_is_writable ( filename ) 237return fs . os_file_is_writable (( to_cstr ( filename ))) 238end 239 240local function bit_set ( number , check_bit ) 241return 0 ~= ( bit . band ( number , check_bit )) 242end 243 244describe ( 'os_getperm' , function () 245itp ( 'returns UV_ENOENT when the given file does not exist' , function () 246eq ( ffi . C . UV_ENOENT , ( os_getperm ( 'non-existing-file' ))) 247end ) 248 249itp ( 'returns a perm > 0 when given an existing file' , function () 250t . ok (( os_getperm ( 'unit-test-directory' )) > 0 ) 251end ) 252 253itp ( 'returns S_IRUSR when the file is readable' , function () 254local perm =os_getperm ( 'unit-test-directory' ) 255t . ok (( bit_set ( perm , ffi . C . kS_IRUSR ))) 256end ) 257end ) 258 259describe ( 'os_setperm' , function () 260itp ( 'can set and unset the executable bit of a file' , function () 261local perm =os_getperm ( 'unit-test-directory/test.file' ) 262perm =unset_bit ( perm , ffi . C . kS_IXUSR ) 263eq ( OK , ( os_setperm ( 'unit-test-directory/test.file' , perm ))) 264perm =os_getperm ( 'unit-test-directory/test.file' ) 265assert ( not ( bit_set ( perm , ffi . C . kS_IXUSR ))) 266perm =set_bit ( perm , ffi . C . kS_IXUSR ) 267eq ( OK , os_setperm ( 'unit-test-directory/test.file' , perm )) 268perm =os_getperm ( 'unit-test-directory/test.file' ) 269t . ok (( bit_set ( perm , ffi . C . kS_IXUSR ))) 270end ) 271 272itp ( 'fails if given file does not exist' , function () 273local perm =ffi . C . kS_IXUSR 274eq ( FAIL , ( os_setperm ( 'non-existing-file' , perm ))) 275end ) 276end ) 277 278describe ( 'os_fchown' , function () 279local filename ='unit-test-directory/test.file' 280itp ( 'does not change owner and group if respective IDs are equal to -1' , function () 281local uid =uv . fs_stat ( filename ). uid 282local gid =uv . fs_stat ( filename ). gid 283eq ( 0 , os_fchown ( filename , - 1ULL , - 1ULL )) 284eq ( uid , uv . fs_stat ( filename ). uid ) 285return eq ( gid , uv . fs_stat ( filename ). gid ) 286end ) 287 288-- Some systems may not have `id` utility. 289if os . execute ( 'id -G > /dev/null 2>&1' ) ~= 0 then 290pending ( 'skipped (missing `id` utility)' , function () end ) 291else 292itp ( 293'owner of a file may change the group of the file to any group of which that owner is a member' , 294function () 295local file_gid =uv . fs_stat ( filename ). gid 296 297-- Gets ID of any group of which current user is a member except the 298-- group that owns the file. 299local id_fd =io . popen ( 'id -G' ) 300local new_gid =id_fd : read ( '*n' ) 301if new_gid == file_gid then 302new_gid =id_fd : read ( '*n' ) 303end 304id_fd : close () 305 306-- User can be a member of only one group. 307-- In that case we can not perform this test. 308if new_gid then 309eq ( 0 , ( os_fchown ( filename , - 1ULL , new_gid ))) 310eq ( new_gid , uv . fs_stat ( filename ). gid ) 311end 312end 313) 314end 315 316if ffi . os == 'Windows' or ffi . C . geteuid () == 0 then 317pending ( 'skipped (uv_fs_chown is no-op on Windows)' , function () end ) 318else 319itp ( 'returns nonzero if process has not enough permissions' , function () 320-- chown to root 321neq ( 0 , os_fchown ( filename , 0 , 0 )) 322end ) 323end 324end ) 325 326describe ( 'os_file_is_readable' , function () 327itp ( 'returns false if the file is not readable' , function () 328local perm =os_getperm ( 'unit-test-directory/test.file' ) 329perm =unset_bit ( perm , ffi . C . kS_IRUSR ) 330perm =unset_bit ( perm , ffi . C . kS_IRGRP ) 331perm =unset_bit ( perm , ffi . C . kS_IROTH ) 332eq ( OK , ( os_setperm ( 'unit-test-directory/test.file' , perm ))) 333eq ( false , os_file_is_readable ( 'unit-test-directory/test.file' )) 334end ) 335 336itp ( 'returns false if the file does not exist' , function () 337eq ( false , os_file_is_readable ( 'unit-test-directory/what_are_you_smoking.gif' )) 338end ) 339 340itp ( 'returns true if the file is readable' , function () 341eq ( true , os_file_is_readable ( 'unit-test-directory/test.file' )) 342end ) 343end ) 344 345describe ( 'os_file_is_writable' , function () 346itp ( 'returns 0 if the file is readonly' , function () 347local perm =os_getperm ( 'unit-test-directory/test.file' ) 348perm =unset_bit ( perm , ffi . C . kS_IWUSR ) 349perm =unset_bit ( perm , ffi . C . kS_IWGRP ) 350perm =unset_bit ( perm , ffi . C . kS_IWOTH ) 351eq ( OK , ( os_setperm ( 'unit-test-directory/test.file' , perm ))) 352eq ( 0 , os_file_is_writable ( 'unit-test-directory/test.file' )) 353end ) 354 355itp ( 'returns 1 if the file is writable' , function () 356eq ( 1 , os_file_is_writable ( 'unit-test-directory/test.file' )) 357end ) 358 359itp ( 'returns 2 when given a folder with rights to write into' , function () 360eq ( 2 , os_file_is_writable ( 'unit-test-directory' )) 361end ) 362end ) 363end ) 364 365describe ( 'file operations' , function () 366local function os_path_exists ( filename ) 367return fs . os_path_exists (( to_cstr ( filename ))) 368end 369local function os_rename ( path , new_path ) 370return fs . os_rename (( to_cstr ( path )), ( to_cstr ( new_path ))) 371end 372local function os_remove ( path ) 373return fs . os_remove (( to_cstr ( path ))) 374end 375local function os_open ( path , flags , mode ) 376return fs . os_open (( to_cstr ( path )), flags , mode ) 377end 378local function os_close ( fd ) 379return fs . os_close ( fd ) 380end 381-- For some reason if length of NUL-bytes-string is the same as `char[?]` 382-- size luajit crashes. Though it does not do so in this test suite, better 383-- be cautious and allocate more elements then needed. I only did this to 384-- strings. 385local function os_read ( fd , size ) 386local buf =nil 387if size == nil then 388size =0 389else 390buf =ffi . new ( 'char[?]' , size + 1 , ( '\0' ): rep ( size )) 391end 392local eof =ffi . new ( 'bool[?]' , 1 , { true } ) 393local ret2 =fs . os_read ( fd , eof , buf , size , false ) 394local ret1 =eof [ 0 ] 395local ret3 ='' 396if buf ~= nil then 397ret3 =ffi . string ( buf , size ) 398end 399return ret1 , ret2 , ret3 400end 401local function os_readv ( fd , sizes ) 402local bufs ={} 403for i , size in ipairs ( sizes ) do 404bufs [ i ] ={ 405iov_base =ffi . new ( 'char[?]' , size + 1 , ( '\0' ): rep ( size )), 406iov_len =size , 407} 408end 409local iov =ffi . new ( 'struct iovec[?]' , # sizes , bufs ) 410local eof =ffi . new ( 'bool[?]' , 1 , { true } ) 411local ret2 =fs . os_readv ( fd , eof , iov , # sizes , false ) 412local ret1 =eof [ 0 ] 413local ret3 ={} 414for i =1 , # sizes do 415-- Warning: iov may not be used. 416ret3 [ i ] =ffi . string ( bufs [ i ]. iov_base , bufs [ i ]. iov_len ) 417end 418return ret1 , ret2 , ret3 419end 420local function os_write ( fd , data ) 421return fs . os_write ( fd , data , data and # data or 0 , false ) 422end 423 424describe ( 'os_path_exists' , function () 425itp ( 'returns false when given a non-existing file' , function () 426eq ( false , ( os_path_exists ( 'non-existing-file' ))) 427end ) 428 429itp ( 'returns true when given an existing file' , function () 430eq ( true , ( os_path_exists ( 'unit-test-directory/test.file' ))) 431end ) 432 433itp ( 'returns false when given a broken symlink' , function () 434eq ( false , ( os_path_exists ( 'unit-test-directory/test_broken_link.file' ))) 435end ) 436 437itp ( 'returns true when given a directory' , function () 438eq ( true , ( os_path_exists ( 'unit-test-directory' ))) 439end ) 440end ) 441 442describe ( 'os_rename' , function () 443local test ='unit-test-directory/test.file' 444local not_exist ='unit-test-directory/not_exist.file' 445 446itp ( 'can rename file if destination file does not exist' , function () 447eq ( OK , ( os_rename ( test , not_exist ))) 448eq ( false , ( os_path_exists ( test ))) 449eq ( true , ( os_path_exists ( not_exist ))) 450eq ( OK , ( os_rename ( not_exist , test ))) -- restore test file 451end ) 452 453itp ( 'fail if source file does not exist' , function () 454eq ( FAIL , ( os_rename ( not_exist , test ))) 455end ) 456 457itp ( 'can overwrite destination file if it exists' , function () 458local other ='unit-test-directory/other.file' 459local file =io . open ( other , 'w' ) 460file : write ( 'other' ) 461file : flush () 462file : close () 463 464eq ( OK , ( os_rename ( other , test ))) 465eq ( false , ( os_path_exists ( other ))) 466eq ( true , ( os_path_exists ( test ))) 467file =io . open ( test , 'r' ) 468eq ( 'other' , ( file : read ( '*all' ))) 469file : close () 470end ) 471end ) 472 473describe ( 'os_remove' , function () 474before_each ( function () 475io . open ( 'unit-test-directory/test_remove.file' , 'w' ): close () 476end ) 477 478after_each ( function () 479os . remove ( 'unit-test-directory/test_remove.file' ) 480end ) 481 482itp ( 'returns non-zero when given a non-existing file' , function () 483neq ( 0 , ( os_remove ( 'non-existing-file' ))) 484end ) 485 486itp ( 'removes the given file and returns 0' , function () 487local f ='unit-test-directory/test_remove.file' 488assert_file_exists ( f ) 489eq ( 0 , ( os_remove ( f ))) 490assert_file_does_not_exist ( f ) 491end ) 492end ) 493 494describe ( 'os_dup' , function () 495itp ( 'returns new file descriptor' , function () 496local dup0 =fs . os_dup ( 0 ) 497local dup1 =fs . os_dup ( 1 ) 498local dup2 =fs . os_dup ( 2 ) 499local tbl ={ 500[ 0 ] =true , 501[ 1 ] =true , 502[ 2 ] =true , 503[ tonumber ( dup0 )] =true , 504[ tonumber ( dup1 )] =true , 505[ tonumber ( dup2 )] =true , 506} 507local i =0 508for _ , _ in pairs ( tbl ) do 509i =i + 1 510end 511eq ( 6 , i ) -- All fds must be unique 512end ) 513end ) 514 515describe ( 'os_open' , function () 516local new_file ='test_new_file' 517local existing_file ='unit-test-directory/test_existing.file' 518 519before_each ( function () 520( io . open ( existing_file , 'w' )): close () 521end ) 522 523after_each ( function () 524os . remove ( existing_file ) 525os . remove ( new_file ) 526end ) 527 528itp ( 'returns UV_ENOENT for O_RDWR on a non-existing file' , function () 529eq ( ffi . C . UV_ENOENT , ( os_open ( 'non-existing-file' , ffi . C . kO_RDWR , 0 ))) 530end ) 531 532itp ( 533'returns non-negative for O_CREAT on a non-existing file which then can be closed' , 534function () 535assert_file_does_not_exist ( new_file ) 536local fd =os_open ( new_file , ffi . C . kO_CREAT , 0 ) 537t . ok ( 0 <= fd ) 538eq ( 0 , os_close ( fd )) 539end 540) 541 542itp ( 'returns non-negative for O_CREAT on a existing file which then can be closed' , function () 543assert_file_exists ( existing_file ) 544local fd =os_open ( existing_file , ffi . C . kO_CREAT , 0 ) 545t . ok ( 0 <= fd ) 546eq ( 0 , os_close ( fd )) 547end ) 548 549itp ( 'returns UV_EEXIST for O_CREAT|O_EXCL on a existing file' , function () 550assert_file_exists ( existing_file ) 551eq ( ffi . C . UV_EEXIST , ( os_open ( existing_file , ( bit . bor ( ffi . C . kO_CREAT , ffi . C . kO_EXCL )), 0 ))) 552end ) 553 554itp ( 'sets `rwx` permissions for O_CREAT 700 which then can be closed' , function () 555assert_file_does_not_exist ( new_file ) 556--create the file 557local fd =os_open ( new_file , ffi . C . kO_CREAT , tonumber ( '700' , 8 )) 558--verify permissions 559eq ( 33216 , uv . fs_stat ( new_file ). mode ) 560eq ( 0 , os_close ( fd )) 561end ) 562 563itp ( 'sets `rw` permissions for O_CREAT 600 which then can be closed' , function () 564assert_file_does_not_exist ( new_file ) 565--create the file 566local fd =os_open ( new_file , ffi . C . kO_CREAT , tonumber ( '600' , 8 )) 567--verify permissions 568eq ( 33152 , uv . fs_stat ( new_file ). mode ) 569eq ( 0 , os_close ( fd )) 570end ) 571 572itp ( 573'returns a non-negative file descriptor for an existing file which then can be closed' , 574function () 575local fd =os_open ( existing_file , ffi . C . kO_RDWR , 0 ) 576t . ok ( 0 <= fd ) 577eq ( 0 , os_close ( fd )) 578end 579) 580end ) 581 582describe ( 'os_close' , function () 583itp ( 'returns EBADF for negative file descriptors' , function () 584eq ( ffi . C . UV_EBADF , os_close ( - 1 )) 585eq ( ffi . C . UV_EBADF , os_close ( - 1000 )) 586end ) 587end ) 588 589describe ( 'os_read' , function () 590local file ='test-unit-os-fs_spec-os_read.dat' 591 592before_each ( function () 593local f =io . open ( file , 'w' ) 594f : write ( fcontents ) 595f : close () 596end ) 597 598after_each ( function () 599os . remove ( file ) 600end ) 601 602itp ( 'can read zero bytes from a file' , function () 603local fd =os_open ( file , ffi . C . kO_RDONLY , 0 ) 604ok ( fd >= 0 ) 605eq ( { false , 0 , '' } , { os_read ( fd , nil ) } ) 606eq ( { false , 0 , '' } , { os_read ( fd , 0 ) } ) 607eq ( 0 , os_close ( fd )) 608end ) 609 610itp ( 'can read from a file multiple times' , function () 611local fd =os_open ( file , ffi . C . kO_RDONLY , 0 ) 612ok ( fd >= 0 ) 613eq ( { false , 2 , '\000\001' } , { os_read ( fd , 2 ) } ) 614eq ( { false , 2 , '\002\003' } , { os_read ( fd , 2 ) } ) 615eq ( 0 , os_close ( fd )) 616end ) 617 618itp ( 'can read the whole file at once and then report eof' , function () 619local fd =os_open ( file , ffi . C . kO_RDONLY , 0 ) 620ok ( fd >= 0 ) 621eq ( { false , # fcontents , fcontents } , { os_read ( fd , # fcontents ) } ) 622eq ( { true , 0 , ( '\0' ): rep ( # fcontents ) } , { os_read ( fd , # fcontents ) } ) 623eq ( 0 , os_close ( fd )) 624end ) 625 626itp ( 'can read the whole file in two calls, one partially' , function () 627local fd =os_open ( file , ffi . C . kO_RDONLY , 0 ) 628ok ( fd >= 0 ) 629eq ( 630{ false , # fcontents * 3 / 4 , fcontents : sub ( 1 , # fcontents * 3 / 4 ) } , 631{ os_read ( fd , # fcontents * 3 / 4 ) } 632) 633eq ( { 634true , 635( # fcontents * 1 / 4 ), 636fcontents : sub ( # fcontents * 3 / 4 + 1 ) .. ( '\0' ): rep ( # fcontents * 2 / 4 ), 637} , { os_read ( fd , # fcontents * 3 / 4 ) } ) 638eq ( 0 , os_close ( fd )) 639end ) 640end ) 641 642describe ( 'os_readv' , function () 643-- Function may be absent 644if not pcall ( function () 645return fs . os_readv 646end ) then 647return 648end 649local file ='test-unit-os-fs_spec-os_readv.dat' 650 651before_each ( function () 652local f =io . open ( file , 'w' ) 653f : write ( fcontents ) 654f : close () 655end ) 656 657after_each ( function () 658os . remove ( file ) 659end ) 660 661itp ( 'can read zero bytes from a file' , function () 662local fd =os_open ( file , ffi . C . kO_RDONLY , 0 ) 663ok ( fd >= 0 ) 664eq ( { false , 0 , {} } , { os_readv ( fd , {} ) } ) 665eq ( { false , 0 , { '' , '' , '' } } , { os_readv ( fd , { 0 , 0 , 0 } ) } ) 666eq ( 0 , os_close ( fd )) 667end ) 668 669itp ( 'can read from a file multiple times to a differently-sized buffers' , function () 670local fd =os_open ( file , ffi . C . kO_RDONLY , 0 ) 671ok ( fd >= 0 ) 672eq ( { false , 2 , { '\000\001' } } , { os_readv ( fd , { 2 } ) } ) 673eq ( { false , 5 , { '\002\003' , '\004\005\006' } } , { os_readv ( fd , { 2 , 3 } ) } ) 674eq ( 0 , os_close ( fd )) 675end ) 676 677itp ( 'can read the whole file at once and then report eof' , function () 678local fd =os_open ( file , ffi . C . kO_RDONLY , 0 ) 679ok ( fd >= 0 ) 680eq ( { 681false , 682# fcontents , 683{ 684fcontents : sub ( 1 , # fcontents * 1 / 4 ), 685fcontents : sub ( # fcontents * 1 / 4 + 1 , # fcontents * 3 / 4 ), 686fcontents : sub ( # fcontents * 3 / 4 + 1 , # fcontents * 15 / 16 ), 687fcontents : sub ( # fcontents * 15 / 16 + 1 , # fcontents ), 688} , 689} , { 690os_readv ( 691fd , 692{ # fcontents * 1 / 4 , # fcontents * 2 / 4 , # fcontents * 3 / 16 , # fcontents * 1 / 16 } 693), 694} ) 695eq ( { true , 0 , { '\0' } } , { os_readv ( fd , { 1 } ) } ) 696eq ( 0 , os_close ( fd )) 697end ) 698 699itp ( 'can read the whole file in two calls, one partially' , function () 700local fd =os_open ( file , ffi . C . kO_RDONLY , 0 ) 701ok ( fd >= 0 ) 702eq ( 703{ false , # fcontents * 3 / 4 , { fcontents : sub ( 1 , # fcontents * 3 / 4 ) } } , 704{ os_readv ( fd , { # fcontents * 3 / 4 } ) } 705) 706eq ( { 707true , 708( # fcontents * 1 / 4 ), 709{ fcontents : sub ( # fcontents * 3 / 4 + 1 ) .. ( '\0' ): rep ( # fcontents * 2 / 4 ) } , 710} , { os_readv ( fd , { # fcontents * 3 / 4 } ) } ) 711eq ( 0 , os_close ( fd )) 712end ) 713end ) 714 715describe ( 'os_write' , function () 716-- Function may be absent 717local file ='test-unit-os-fs_spec-os_write.dat' 718 719before_each ( function () 720local f =io . open ( file , 'w' ) 721f : write ( fcontents ) 722f : close () 723end ) 724 725after_each ( function () 726os . remove ( file ) 727end ) 728 729itp ( 'can write zero bytes to a file' , function () 730local fd =os_open ( file , ffi . C . kO_WRONLY , 0 ) 731ok ( fd >= 0 ) 732eq ( 0 , os_write ( fd , '' )) 733eq ( 0 , os_write ( fd , nil )) 734eq ( fcontents , io . open ( file , 'r' ): read ( '*a' )) 735eq ( 0 , os_close ( fd )) 736end ) 737 738itp ( 'can write some data to a file' , function () 739local fd =os_open ( file , ffi . C . kO_WRONLY , 0 ) 740ok ( fd >= 0 ) 741eq ( 3 , os_write ( fd , 'abc' )) 742eq ( 4 , os_write ( fd , ' def' )) 743eq ( 'abc def' .. fcontents : sub ( 8 ), io . open ( file , 'r' ): read ( '*a' )) 744eq ( 0 , os_close ( fd )) 745end ) 746end ) 747 748describe ( 'os_nodetype' , function () 749before_each ( function () 750os . remove ( 'non-existing-file' ) 751end ) 752 753itp ( 'returns NODE_NORMAL for non-existing file' , function () 754eq ( NODE_NORMAL , fs . os_nodetype ( to_cstr ( 'non-existing-file' ))) 755end ) 756 757itp ( 'returns NODE_WRITABLE for /dev/stderr' , function () 758eq ( NODE_WRITABLE , fs . os_nodetype ( to_cstr ( '/dev/stderr' ))) 759end ) 760end ) 761end ) 762 763describe ( 'folder operations' , function () 764local function os_mkdir ( path , mode ) 765return fs . os_mkdir ( to_cstr ( path ), mode ) 766end 767 768local function os_rmdir ( path ) 769return fs . os_rmdir ( to_cstr ( path )) 770end 771 772local function os_mkdir_recurse ( path , mode ) 773local failed_str =ffi . new ( 'char *[1]' , { nil } ) 774local created_str =ffi . new ( 'char *[1]' , { nil } ) 775local ret =fs . os_mkdir_recurse ( path , mode , failed_str , created_str ) 776local failed_dir =failed_str [ 0 ] 777if failed_dir ~= nil then 778failed_dir =ffi . string ( failed_dir ) 779end 780local created_dir =created_str [ 0 ] 781if created_dir ~= nil then 782created_dir =ffi . string ( created_dir ) 783end 784return ret , failed_dir , created_dir 785end 786 787describe ( 'os_mkdir' , function () 788itp ( 'returns non-zero when given an already existing directory' , function () 789local mode =ffi . C . kS_IRUSR + ffi . C . kS_IWUSR + ffi . C . kS_IXUSR 790neq ( 0 , ( os_mkdir ( 'unit-test-directory' , mode ))) 791end ) 792 793itp ( 'creates a directory and returns 0' , function () 794local mode =ffi . C . kS_IRUSR + ffi . C . kS_IWUSR + ffi . C . kS_IXUSR 795eq ( false , ( os_isdir ( 'unit-test-directory/new-dir' ))) 796eq ( 0 , ( os_mkdir ( 'unit-test-directory/new-dir' , mode ))) 797eq ( true , ( os_isdir ( 'unit-test-directory/new-dir' ))) 798uv . fs_rmdir ( 'unit-test-directory/new-dir' ) 799end ) 800end ) 801 802describe ( 'os_mkdir_recurse' , function () 803itp ( 'returns zero when given an already existing directory' , function () 804local mode =ffi . C . kS_IRUSR + ffi . C . kS_IWUSR + ffi . C . kS_IXUSR 805local ret , failed_dir , created_dir =os_mkdir_recurse ( 'unit-test-directory' , mode ) 806eq ( 0 , ret ) 807eq ( nil , failed_dir ) 808eq ( nil , created_dir ) 809end ) 810 811itp ( 'fails to create a directory where there is a file' , function () 812local mode =ffi . C . kS_IRUSR + ffi . C . kS_IWUSR + ffi . C . kS_IXUSR 813local ret , failed_dir , created_dir =os_mkdir_recurse ( 'unit-test-directory/test.file' , mode ) 814neq ( 0 , ret ) 815eq ( 'unit-test-directory/test.file' , failed_dir ) 816eq ( nil , created_dir ) 817end ) 818 819itp ( 'fails to create a directory where there is a file in path' , function () 820local mode =ffi . C . kS_IRUSR + ffi . C . kS_IWUSR + ffi . C . kS_IXUSR 821local ret , failed_dir , created_dir = 822os_mkdir_recurse ( 'unit-test-directory/test.file/test' , mode ) 823neq ( 0 , ret ) 824eq ( 'unit-test-directory/test.file' , failed_dir ) 825eq ( nil , created_dir ) 826end ) 827 828itp ( 'succeeds to create a directory' , function () 829local mode =ffi . C . kS_IRUSR + ffi . C . kS_IWUSR + ffi . C . kS_IXUSR 830local ret , failed_dir , created_dir = 831os_mkdir_recurse ( 'unit-test-directory/new-dir-recurse' , mode ) 832eq ( 0 , ret ) 833eq ( nil , failed_dir ) 834ok ( endswith ( created_dir , 'unit-test-directory/new-dir-recurse' )) 835eq ( true , os_isdir ( 'unit-test-directory/new-dir-recurse' )) 836uv . fs_rmdir ( 'unit-test-directory/new-dir-recurse' ) 837eq ( false , os_isdir ( 'unit-test-directory/new-dir-recurse' )) 838end ) 839 840itp ( 'succeeds to create a directory ending with ///' , function () 841local mode =ffi . C . kS_IRUSR + ffi . C . kS_IWUSR + ffi . C . kS_IXUSR 842local ret , failed_dir , created_dir = 843os_mkdir_recurse ( 'unit-test-directory/new-dir-recurse///' , mode ) 844eq ( 0 , ret ) 845eq ( nil , failed_dir ) 846ok ( endswith ( created_dir , 'unit-test-directory/new-dir-recurse' )) 847eq ( true , os_isdir ( 'unit-test-directory/new-dir-recurse' )) 848uv . fs_rmdir ( 'unit-test-directory/new-dir-recurse' ) 849eq ( false , os_isdir ( 'unit-test-directory/new-dir-recurse' )) 850end ) 851 852itp ( 'succeeds to create a directory ending with /' , function () 853local mode =ffi . C . kS_IRUSR + ffi . C . kS_IWUSR + ffi . C . kS_IXUSR 854local ret , failed_dir , created_dir = 855os_mkdir_recurse ( 'unit-test-directory/new-dir-recurse/' , mode ) 856eq ( 0 , ret ) 857eq ( nil , failed_dir ) 858ok ( endswith ( created_dir , 'unit-test-directory/new-dir-recurse' )) 859eq ( true , os_isdir ( 'unit-test-directory/new-dir-recurse' )) 860uv . fs_rmdir ( 'unit-test-directory/new-dir-recurse' ) 861eq ( false , os_isdir ( 'unit-test-directory/new-dir-recurse' )) 862end ) 863 864itp ( 'succeeds to create a directory tree' , function () 865local mode =ffi . C . kS_IRUSR + ffi . C . kS_IWUSR + ffi . C . kS_IXUSR 866local ret , failed_dir , created_dir = 867os_mkdir_recurse ( 'unit-test-directory/new-dir-recurse/1/2/3' , mode ) 868eq ( 0 , ret ) 869eq ( nil , failed_dir ) 870ok ( endswith ( created_dir , 'unit-test-directory/new-dir-recurse' )) 871eq ( true , os_isdir ( 'unit-test-directory/new-dir-recurse' )) 872eq ( true , os_isdir ( 'unit-test-directory/new-dir-recurse/1' )) 873eq ( true , os_isdir ( 'unit-test-directory/new-dir-recurse/1/2' )) 874eq ( true , os_isdir ( 'unit-test-directory/new-dir-recurse/1/2/3' )) 875uv . fs_rmdir ( 'unit-test-directory/new-dir-recurse/1/2/3' ) 876uv . fs_rmdir ( 'unit-test-directory/new-dir-recurse/1/2' ) 877uv . fs_rmdir ( 'unit-test-directory/new-dir-recurse/1' ) 878uv . fs_rmdir ( 'unit-test-directory/new-dir-recurse' ) 879eq ( false , os_isdir ( 'unit-test-directory/new-dir-recurse' )) 880end ) 881end ) 882 883describe ( 'os_rmdir' , function () 884itp ( 'returns non_zero when given a non-existing directory' , function () 885neq ( 0 , ( os_rmdir ( 'non-existing-directory' ))) 886end ) 887 888itp ( 'removes the given directory and returns 0' , function () 889mkdir ( 'unit-test-directory/new-dir' ) 890eq ( 0 , os_rmdir ( 'unit-test-directory/new-dir' )) 891eq ( false , ( os_isdir ( 'unit-test-directory/new-dir' ))) 892end ) 893end ) 894end ) 895 896describe ( 'FileInfo' , function () 897local function file_info_new () 898local info =ffi . new ( 'FileInfo[1]' ) 899info [ 0 ]. stat . st_ino =0 900info [ 0 ]. stat . st_dev =0 901return info 902end 903 904-- Returns true if the FileInfo object has non-empty fields. 905local function has_fileinfo ( info ) 906return info [ 0 ]. stat . st_ino > 0 and info [ 0 ]. stat . st_dev > 0 907end 908 909local function file_id_new () 910local info =ffi . new ( 'FileID[1]' ) 911info [ 0 ]. inode =0 912info [ 0 ]. device_id =0 913return info 914end 915 916describe ( 'os_fileinfo' , function () 917itp ( 'returns false if path=NULL' , function () 918local info =file_info_new () 919assert ( not ( fs . os_fileinfo ( nil , info ))) 920end ) 921 922itp ( 'returns false if given a non-existing file' , function () 923local info =file_info_new () 924assert ( not ( fs . os_fileinfo ( '/non-existent' , info ))) 925end ) 926 927itp ( 'returns true if given an existing file and fills FileInfo' , function () 928local info =file_info_new () 929local path ='unit-test-directory/test.file' 930t . ok (( fs . os_fileinfo ( path , info ))) 931t . ok (( has_fileinfo ( info ))) 932end ) 933 934itp ( 'returns the FileInfo of the linked file, not the link' , function () 935local info =file_info_new () 936local path ='unit-test-directory/test_link.file' 937t . ok (( fs . os_fileinfo ( path , info ))) 938t . ok (( has_fileinfo ( info ))) 939local mode =tonumber ( info [ 0 ]. stat . st_mode ) 940return eq ( ffi . C . kS_IFREG , ( bit . band ( mode , ffi . C . kS_IFMT ))) 941end ) 942end ) 943 944describe ( 'os_fileinfo_link' , function () 945itp ( 'returns false for non-existing file' , function () 946local info =file_info_new () 947assert ( not ( fs . os_fileinfo_link ( '/non-existent' , info ))) 948end ) 949 950itp ( 'returns true and fills FileInfo for existing file' , function () 951local info =file_info_new () 952local path ='unit-test-directory/test.file' 953t . ok (( fs . os_fileinfo_link ( path , info ))) 954t . ok (( has_fileinfo ( info ))) 955end ) 956 957itp ( 'returns FileInfo of the link, not its target' , function () 958local info =file_info_new () 959local link ='unit-test-directory/test_link.file' 960t . ok (( fs . os_fileinfo_link ( link , info ))) 961t . ok (( has_fileinfo ( info ))) 962local mode =tonumber ( info [ 0 ]. stat . st_mode ) 963eq ( ffi . C . kS_IFLNK , ( bit . band ( mode , ffi . C . kS_IFMT ))) 964end ) 965end ) 966 967describe ( 'os_fileinfo_fd' , function () 968itp ( 'returns false if given an invalid file descriptor' , function () 969local info =file_info_new () 970assert ( not ( fs . os_fileinfo_fd ( - 1 , info ))) 971end ) 972 973itp ( 'returns true if given a file descriptor and fills FileInfo' , function () 974local info =file_info_new () 975local path ='unit-test-directory/test.file' 976local fd =ffi . C . open ( path , 0 ) 977t . ok (( fs . os_fileinfo_fd ( fd , info ))) 978t . ok (( has_fileinfo ( info ))) 979ffi . C . close ( fd ) 980end ) 981end ) 982 983describe ( 'os_fileinfo_id_equal' , function () 984itp ( 'returns false if file infos represent different files' , function () 985local file_info_1 =file_info_new () 986local file_info_2 =file_info_new () 987local path_1 ='unit-test-directory/test.file' 988local path_2 ='unit-test-directory/test_2.file' 989t . ok (( fs . os_fileinfo ( path_1 , file_info_1 ))) 990t . ok (( fs . os_fileinfo ( path_2 , file_info_2 ))) 991assert ( not ( fs . os_fileinfo_id_equal ( file_info_1 , file_info_2 ))) 992end ) 993 994itp ( 'returns true if file infos represent the same file' , function () 995local file_info_1 =file_info_new () 996local file_info_2 =file_info_new () 997local path ='unit-test-directory/test.file' 998t . ok (( fs . os_fileinfo ( path , file_info_1 ))) 999t . ok (( fs . os_fileinfo ( path , file_info_2 ))) 1000t . ok (( fs . os_fileinfo_id_equal ( file_info_1 , file_info_2 ))) 1001end ) 1002 1003itp ( 'returns true if file infos represent the same file (symlink)' , function () 1004local file_info_1 =file_info_new () 1005local file_info_2 =file_info_new () 1006local path_1 ='unit-test-directory/test.file' 1007local path_2 ='unit-test-directory/test_link.file' 1008t . ok (( fs . os_fileinfo ( path_1 , file_info_1 ))) 1009t . ok (( fs . os_fileinfo ( path_2 , file_info_2 ))) 1010t . ok (( fs . os_fileinfo_id_equal ( file_info_1 , file_info_2 ))) 1011end ) 1012end ) 1013 1014describe ( 'os_fileinfo_id' , function () 1015itp ( 'extracts ino/dev from FileInfo into file_id' , function () 1016local info =file_info_new () 1017local file_id =file_id_new () 1018local path ='unit-test-directory/test.file' 1019t . ok (( fs . os_fileinfo ( path , info ))) 1020fs . os_fileinfo_id ( info , file_id ) 1021eq ( info [ 0 ]. stat . st_ino , file_id [ 0 ]. inode ) 1022eq ( info [ 0 ]. stat . st_dev , file_id [ 0 ]. device_id ) 1023end ) 1024end ) 1025 1026describe ( 'os_fileinfo_inode' , function () 1027itp ( 'returns the inode from FileInfo' , function () 1028local info =file_info_new () 1029local path ='unit-test-directory/test.file' 1030t . ok (( fs . os_fileinfo ( path , info ))) 1031local inode =fs . os_fileinfo_inode ( info ) 1032eq ( info [ 0 ]. stat . st_ino , inode ) 1033end ) 1034end ) 1035 1036describe ( 'os_fileinfo_size' , function () 1037itp ( 'returns the correct size of a file' , function () 1038local path ='unit-test-directory/test.file' 1039local file =io . open ( path , 'w' ) 1040file : write ( 'some bytes to get filesize != 0' ) 1041file : flush () 1042file : close () 1043local size =uv . fs_stat ( path ). size 1044local info =file_info_new () 1045t . ok ( fs . os_fileinfo ( path , info )) 1046eq ( size , fs . os_fileinfo_size ( info )) 1047end ) 1048end ) 1049 1050describe ( 'os_fileinfo_hardlinks' , function () 1051itp ( 'returns the correct number of hardlinks' , function () 1052local path ='unit-test-directory/test.file' 1053local path_link ='unit-test-directory/test_hlink.file' 1054local info =file_info_new () 1055t . ok ( fs . os_fileinfo ( path , info )) 1056eq ( 1 , fs . os_fileinfo_hardlinks ( info )) 1057uv . fs_link ( path , path_link ) 1058t . ok ( fs . os_fileinfo ( path , info )) 1059eq ( 2 , fs . os_fileinfo_hardlinks ( info )) 1060end ) 1061end ) 1062 1063describe ( 'os_fileinfo_blocksize' , function () 1064itp ( 'returns the correct blocksize of a file' , function () 1065local path ='unit-test-directory/test.file' 1066local blksize =uv . fs_stat ( path ). blksize 1067local info =file_info_new () 1068t . ok ( fs . os_fileinfo ( path , info )) 1069if blksize then 1070eq ( blksize , fs . os_fileinfo_blocksize ( info )) 1071else 1072-- luafs doesn't support blksize on windows 1073-- libuv on windows returns a constant value as blocksize 1074-- checking for this constant value should be enough 1075eq ( 2048 , fs . os_fileinfo_blocksize ( info )) 1076end 1077end ) 1078end ) 1079 1080describe ( 'os_fileid' , function () 1081itp ( 'returns false if given an non-existing file' , function () 1082local file_id =file_id_new () 1083assert ( not ( fs . os_fileid ( '/non-existent' , file_id ))) 1084end ) 1085 1086itp ( 'returns true if given an existing file and fills file_id' , function () 1087local file_id =file_id_new () 1088local path ='unit-test-directory/test.file' 1089t . ok (( fs . os_fileid ( path , file_id ))) 1090t . ok ( 0 < file_id [ 0 ]. inode ) 1091t . ok ( 0 < file_id [ 0 ]. device_id ) 1092end ) 1093end ) 1094 1095describe ( 'os_fileid_equal' , function () 1096itp ( 'returns true if two FileIDs are equal' , function () 1097local file_id =file_id_new () 1098local path ='unit-test-directory/test.file' 1099t . ok (( fs . os_fileid ( path , file_id ))) 1100t . ok (( fs . os_fileid_equal ( file_id , file_id ))) 1101end ) 1102 1103itp ( 'returns false if two FileIDs are not equal' , function () 1104local file_id_1 =file_id_new () 1105local file_id_2 =file_id_new () 1106local path_1 ='unit-test-directory/test.file' 1107local path_2 ='unit-test-directory/test_2.file' 1108t . ok (( fs . os_fileid ( path_1 , file_id_1 ))) 1109t . ok (( fs . os_fileid ( path_2 , file_id_2 ))) 1110assert ( not ( fs . os_fileid_equal ( file_id_1 , file_id_2 ))) 1111end ) 1112end ) 1113end ) 1114end )