robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local uv =vim . uv 2 3local t =require ( 'test.unit.testutil' ) 4local describe , before_each , after_each =t . describe , t . before_each , t . after_each 5local itp =t . gen_itp ( t . it ) 6 7local eq =t . eq 8local ffi =t . ffi 9local cimport =t . cimport 10local cppimport =t . cppimport 11local mkdir =t . mkdir 12 13local m =cimport ( './src/nvim/os/os.h' , './src/nvim/os/fileio.h' ) 14cppimport ( 'fcntl.h' ) 15 16local fcontents ='' 17for i =0 , 255 do 18fcontents =fcontents .. ( i == 0 and '\0' or ( '%c' ): format ( i )) 19end 20fcontents =fcontents : rep ( 16 ) 21 22local dir ='Xtest-unit-file_spec.d' 23local file1 =dir .. '/file1.dat' 24local file2 =dir .. '/file2.dat' 25local linkf =dir .. '/file.lnk' 26local linkb =dir .. '/broken.lnk' 27local filec =dir .. '/created-file.dat' 28 29before_each ( function () 30mkdir ( dir ) 31 32local f1 =io . open ( file1 , 'w' ) 33f1 : write ( fcontents ) 34f1 : close () 35 36local f2 =io . open ( file2 , 'w' ) 37f2 : write ( fcontents ) 38f2 : close () 39 40uv . fs_symlink ( 'file1.dat' , linkf ) 41uv . fs_symlink ( 'broken.dat' , linkb ) 42end ) 43 44after_each ( function () 45os . remove ( file1 ) 46os . remove ( file2 ) 47os . remove ( linkf ) 48os . remove ( linkb ) 49os . remove ( filec ) 50uv . fs_rmdir ( dir ) 51end ) 52 53local function file_open ( fname , flags , mode ) 54local ret2 =ffi . new ( 'FileDescriptor' ) 55local ret1 =m . file_open ( ret2 , fname , flags , mode ) 56return ret1 , ret2 57end 58 59local function file_open_fd ( fd , flags ) 60local ret2 =ffi . new ( 'FileDescriptor' ) 61local ret1 =m . file_open_fd ( ret2 , fd , flags ) 62return ret1 , ret2 63end 64 65local function file_write ( fp , buf ) 66return m . file_write ( fp , buf , # buf ) 67end 68 69local function file_read ( fp , size ) 70local buf =nil 71if size == nil then 72size =0 73else 74-- For some reason if length of NUL-bytes-string is the same as `char[?]` 75-- size luajit garbage collector crashes. But it does not do so in 76-- os_read[v] tests in os/fs_spec.lua. 77buf =ffi . new ( 'char[?]' , size + 1 , ( '\0' ): rep ( size )) 78end 79local ret1 =m . file_read ( fp , buf , size ) 80local ret2 ='' 81if buf ~= nil then 82ret2 =ffi . string ( buf , size ) 83end 84return ret1 , ret2 85end 86 87local function file_flush ( fp ) 88return m . file_flush ( fp ) 89end 90 91local function file_fsync ( fp ) 92return m . file_fsync ( fp ) 93end 94 95local function file_skip ( fp , size ) 96return m . file_skip ( fp , size ) 97end 98 99describe ( 'file_open_fd' , function () 100itp ( 'can use file descriptor returned by os_open for reading' , function () 101local fd =m . os_open ( file1 , m . kO_RDONLY , 0 ) 102local err , fp =file_open_fd ( fd , m . kFileReadOnly ) 103eq ( 0 , err ) 104eq ( { # fcontents , fcontents } , { file_read ( fp , # fcontents ) } ) 105eq ( 0 , m . file_close ( fp , false )) 106end ) 107itp ( 'can use file descriptor returned by os_open for writing' , function () 108eq ( nil , uv . fs_stat ( filec )) 109local fd =m . os_open ( filec , m . kO_WRONLY + m . kO_CREAT , 384 ) 110local err , fp =file_open_fd ( fd , m . kFileWriteOnly ) 111eq ( 0 , err ) 112eq ( 4 , file_write ( fp , 'test' )) 113eq ( 0 , m . file_close ( fp , false )) 114eq ( 4 , uv . fs_stat ( filec ). size ) 115eq ( 'test' , io . open ( filec ): read ( '*a' )) 116end ) 117end ) 118 119describe ( 'file_open' , function () 120itp ( 'can create a rwx------ file with kFileCreate' , function () 121local err , fp =file_open ( filec , m . kFileCreate , 448 ) 122eq ( 0 , err ) 123local attrs =uv . fs_stat ( filec ) 124eq ( 33216 , attrs . mode ) 125eq ( 0 , m . file_close ( fp , false )) 126end ) 127 128itp ( 'can create a rw------- file with kFileCreate' , function () 129local err , fp =file_open ( filec , m . kFileCreate , 384 ) 130eq ( 0 , err ) 131local attrs =uv . fs_stat ( filec ) 132eq ( 33152 , attrs . mode ) 133eq ( 0 , m . file_close ( fp , false )) 134end ) 135 136itp ( 'can create a rwx------ file with kFileCreateOnly' , function () 137local err , fp =file_open ( filec , m . kFileCreateOnly , 448 ) 138eq ( 0 , err ) 139local attrs =uv . fs_stat ( filec ) 140eq ( 33216 , attrs . mode ) 141eq ( 0 , m . file_close ( fp , false )) 142end ) 143 144itp ( 'can create a rw------- file with kFileCreateOnly' , function () 145local err , fp =file_open ( filec , m . kFileCreateOnly , 384 ) 146eq ( 0 , err ) 147local attrs =uv . fs_stat ( filec ) 148eq ( 33152 , attrs . mode ) 149eq ( 0 , m . file_close ( fp , false )) 150end ) 151 152itp ( 'fails to open an existing file with kFileCreateOnly' , function () 153local err , _ =file_open ( file1 , m . kFileCreateOnly , 384 ) 154eq ( m . UV_EEXIST , err ) 155end ) 156 157itp ( 'fails to open an symlink with kFileNoSymlink' , function () 158local err , _ =file_open ( linkf , m . kFileNoSymlink , 384 ) 159-- err is UV_EMLINK in FreeBSD, but if I use `ok(err == m.UV_ELOOP or err == 160-- m.UV_EMLINK)`, then I loose the ability to see actual `err` value. 161if err ~= m . UV_ELOOP then 162eq ( m . UV_EMLINK , err ) 163end 164end ) 165 166itp ( 'can open an existing file write-only with kFileCreate' , function () 167local err , fp =file_open ( file1 , m . kFileCreate , 384 ) 168eq ( 0 , err ) 169eq ( true , fp . wr ) 170eq ( 0 , m . file_close ( fp , false )) 171end ) 172 173itp ( 'can open an existing file read-only with zero' , function () 174local err , fp =file_open ( file1 , 0 , 384 ) 175eq ( 0 , err ) 176eq ( false , fp . wr ) 177eq ( 0 , m . file_close ( fp , false )) 178end ) 179 180itp ( 'can open an existing file read-only with kFileReadOnly' , function () 181local err , fp =file_open ( file1 , m . kFileReadOnly , 384 ) 182eq ( 0 , err ) 183eq ( false , fp . wr ) 184eq ( 0 , m . file_close ( fp , false )) 185end ) 186 187itp ( 'can open an existing file read-only with kFileNoSymlink' , function () 188local err , fp =file_open ( file1 , m . kFileNoSymlink , 384 ) 189eq ( 0 , err ) 190eq ( false , fp . wr ) 191eq ( 0 , m . file_close ( fp , false )) 192end ) 193 194itp ( 'can truncate an existing file with kFileTruncate' , function () 195local err , fp =file_open ( file1 , m . kFileTruncate , 384 ) 196eq ( 0 , err ) 197eq ( true , fp . wr ) 198eq ( 0 , m . file_close ( fp , false )) 199local attrs =uv . fs_stat ( file1 ) 200eq ( 0 , attrs . size ) 201end ) 202 203itp ( 'can open an existing file write-only with kFileWriteOnly' , function () 204local err , fp =file_open ( file1 , m . kFileWriteOnly , 384 ) 205eq ( 0 , err ) 206eq ( true , fp . wr ) 207eq ( 0 , m . file_close ( fp , false )) 208local attrs =uv . fs_stat ( file1 ) 209eq ( 4096 , attrs . size ) 210end ) 211 212itp ( 'fails to create a file with just kFileWriteOnly' , function () 213local err , _ =file_open ( filec , m . kFileWriteOnly , 384 ) 214eq ( m . UV_ENOENT , err ) 215local attrs =uv . fs_stat ( filec ) 216eq ( nil , attrs ) 217end ) 218 219itp ( 'can truncate an existing file with kFileTruncate when opening a symlink' , function () 220local err , fp =file_open ( linkf , m . kFileTruncate , 384 ) 221eq ( 0 , err ) 222eq ( true , fp . wr ) 223eq ( 0 , m . file_close ( fp , false )) 224local attrs =uv . fs_stat ( file1 ) 225eq ( 0 , attrs . size ) 226end ) 227 228itp ( 'fails to open a directory write-only' , function () 229local err , _ =file_open ( dir , m . kFileWriteOnly , 384 ) 230eq ( m . UV_EISDIR , err ) 231end ) 232 233itp ( 'fails to open a broken symbolic link write-only' , function () 234local err , _ =file_open ( linkb , m . kFileWriteOnly , 384 ) 235eq ( m . UV_ENOENT , err ) 236end ) 237 238itp ( 'fails to open a broken symbolic link read-only' , function () 239local err , _ =file_open ( linkb , m . kFileReadOnly , 384 ) 240eq ( m . UV_ENOENT , err ) 241end ) 242end ) 243 244describe ( 'file_close' , function () 245itp ( 'can flush writes to disk also with true argument' , function () 246local err , fp =file_open ( filec , m . kFileCreateOnly , 384 ) 247eq ( 0 , err ) 248local wsize =file_write ( fp , 'test' ) 249eq ( 4 , wsize ) 250eq ( 0 , uv . fs_stat ( filec ). size ) 251eq ( 0 , m . file_close ( fp , true )) 252eq ( wsize , uv . fs_stat ( filec ). size ) 253end ) 254end ) 255 256describe ( 'file_fsync' , function () 257itp ( 'can flush writes to disk' , function () 258local err , fp =file_open ( filec , m . kFileCreateOnly , 384 ) 259eq ( 0 , file_fsync ( fp )) 260eq ( 0 , err ) 261eq ( 0 , uv . fs_stat ( filec ). size ) 262local wsize =file_write ( fp , 'test' ) 263eq ( 4 , wsize ) 264eq ( 0 , uv . fs_stat ( filec ). size ) 265eq ( 0 , file_fsync ( fp )) 266eq ( wsize , uv . fs_stat ( filec ). size ) 267eq ( 0 , m . file_close ( fp , false )) 268end ) 269end ) 270 271describe ( 'file_flush' , function () 272itp ( 'can flush writes to disk' , function () 273local err , fp =file_open ( filec , m . kFileCreateOnly , 384 ) 274eq ( 0 , file_flush ( fp )) 275eq ( 0 , err ) 276eq ( 0 , uv . fs_stat ( filec ). size ) 277local wsize =file_write ( fp , 'test' ) 278eq ( 4 , wsize ) 279eq ( 0 , uv . fs_stat ( filec ). size ) 280eq ( 0 , file_flush ( fp )) 281eq ( wsize , uv . fs_stat ( filec ). size ) 282eq ( 0 , m . file_close ( fp , false )) 283end ) 284end ) 285 286describe ( 'file_read' , function () 287itp ( 'can read small chunks of input until eof' , function () 288local err , fp =file_open ( file1 , 0 , 384 ) 289eq ( 0 , err ) 290eq ( false , fp . wr ) 291local shift =0 292while shift < # fcontents do 293local size =3 294local exp_err =size 295local exp_s =fcontents : sub ( shift + 1 , shift + size ) 296if shift + size >= # fcontents then 297exp_err =# fcontents - shift 298exp_s =( fcontents : sub ( shift + 1 , shift + size ) .. (( '\0' ): rep ( size - exp_err ))) 299end 300eq ( { exp_err , exp_s } , { file_read ( fp , size ) } ) 301shift =shift + size 302end 303eq ( 0 , m . file_close ( fp , false )) 304end ) 305 306itp ( 'can read the whole file at once' , function () 307local err , fp =file_open ( file1 , 0 , 384 ) 308eq ( 0 , err ) 309eq ( false , fp . wr ) 310eq ( { # fcontents , fcontents } , { file_read ( fp , # fcontents ) } ) 311eq ( { 0 , ( '\0' ): rep ( # fcontents ) } , { file_read ( fp , # fcontents ) } ) 312eq ( 0 , m . file_close ( fp , false )) 313end ) 314 315itp ( 'can read more then 1024 bytes after reading a small chunk' , function () 316local err , fp =file_open ( file1 , 0 , 384 ) 317eq ( 0 , err ) 318eq ( false , fp . wr ) 319eq ( { 5 , fcontents : sub ( 1 , 5 ) } , { file_read ( fp , 5 ) } ) 320eq ( { # fcontents - 5 , fcontents : sub ( 6 ) .. (( '\0' ): rep ( 5 )) } , { file_read ( fp , # fcontents ) } ) 321eq ( 0 , m . file_close ( fp , false )) 322end ) 323 324itp ( 'can read file by 768-byte-chunks' , function () 325local err , fp =file_open ( file1 , 0 , 384 ) 326eq ( 0 , err ) 327eq ( false , fp . wr ) 328local shift =0 329while shift < # fcontents do 330local size =768 331local exp_err =size 332local exp_s =fcontents : sub ( shift + 1 , shift + size ) 333if shift + size >= # fcontents then 334exp_err =# fcontents - shift 335exp_s =( fcontents : sub ( shift + 1 , shift + size ) .. (( '\0' ): rep ( size - exp_err ))) 336end 337eq ( { exp_err , exp_s } , { file_read ( fp , size ) } ) 338shift =shift + size 339end 340eq ( 0 , m . file_close ( fp , false )) 341end ) 342end ) 343 344describe ( 'file_write' , function () 345itp ( 'can write the whole file at once' , function () 346local err , fp =file_open ( filec , m . kFileCreateOnly , 384 ) 347eq ( 0 , err ) 348eq ( true , fp . wr ) 349local wr =file_write ( fp , fcontents ) 350eq ( # fcontents , wr ) 351eq ( 0 , m . file_close ( fp , false )) 352eq ( wr , uv . fs_stat ( filec ). size ) 353eq ( fcontents , io . open ( filec ): read ( '*a' )) 354end ) 355 356itp ( 'can write the whole file by small chunks' , function () 357local err , fp =file_open ( filec , m . kFileCreateOnly , 384 ) 358eq ( 0 , err ) 359eq ( true , fp . wr ) 360local shift =0 361while shift < # fcontents do 362local size =3 363local s =fcontents : sub ( shift + 1 , shift + size ) 364local wr =file_write ( fp , s ) 365eq ( wr , # s ) 366shift =shift + size 367end 368eq ( 0 , m . file_close ( fp , false )) 369eq ( # fcontents , uv . fs_stat ( filec ). size ) 370eq ( fcontents , io . open ( filec ): read ( '*a' )) 371end ) 372 373itp ( 'can write the whole file by 768-byte-chunks' , function () 374local err , fp =file_open ( filec , m . kFileCreateOnly , 384 ) 375eq ( 0 , err ) 376eq ( true , fp . wr ) 377local shift =0 378while shift < # fcontents do 379local size =768 380local s =fcontents : sub ( shift + 1 , shift + size ) 381local wr =file_write ( fp , s ) 382eq ( wr , # s ) 383shift =shift + size 384end 385eq ( 0 , m . file_close ( fp , false )) 386eq ( # fcontents , uv . fs_stat ( filec ). size ) 387eq ( fcontents , io . open ( filec ): read ( '*a' )) 388end ) 389end ) 390 391describe ( 'file_skip' , function () 392itp ( 'can skip 3 bytes' , function () 393local err , fp =file_open ( file1 , 0 , 384 ) 394eq ( 0 , err ) 395eq ( false , fp . wr ) 396eq ( 3 , file_skip ( fp , 3 )) 397local rd , s =file_read ( fp , 3 ) 398eq ( 3 , rd ) 399eq ( fcontents : sub ( 4 , 6 ), s ) 400eq ( 0 , m . file_close ( fp , false )) 401end ) 402end )