robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f3fbb3928c
master
1" Test :recover 2 3func SetUp () 4edit XSetUp 5preserve 6let sn = swapname ( '' ) 7let b = readblob ( sn ) 8bw ! 9 10" Not all fields are written in a system-independent manner. Detect whether 11" the test is running on a little or big-endian system. 12" The B0_MAGIC_LONG field may be 32-bit or 64-bit, depending on the system, 13" even though the value stored is only 32-bits. Therefore, need to check 14" both the high and low 32-bits to compute these values. 15let s: little_endian = ( b [ 1008 : 1011 ] == 0 z33323130) || (b[1012:1015] ==0z33323130) 16let s: system_64bit = s: little_endian ?( b [ 1012 : 1015 ] == 0 z00000000) : (b[1008:1011] ==0z00000000) 17endfunc 18 19func Test_recover_root_dir () 20" This used to access invalid memory. 21split Xtest 22set dir = / 23call assert_fails ( 'recover' , 'E305:' ) 24close ! 25 26if has ( 'win32' ) 27" can write in / directory on MS-Windows 28let & directory = 'F:\\' 29elseif filewritable ( '/' ) == 2 30set dir = /notexist/ 31endif 32call assert_fails ( 'split Xtest' , 'E303:' ) 33 34" No error with empty 'directory' setting. 35set directory = 36split XtestOK 37close ! 38 39set dir & 40endfunc 41 42" Make a copy of the current swap file to "Xswap". 43" Return the name of the swap file. 44func CopySwapfile () 45preserve 46" get the name of the swap file 47let swname = split ( execute ( "swapname" ))[ 0 ] 48let swname = substitute ( swname , '[[:blank:][:cntrl:]]*\(.\{-}\)[[:blank:][:cntrl:]]*$' , '\1' , '' ) 49" make a copy of the swap file in Xswap 50set binary 51exe 'sp ' . swname 52w ! Xswap 53set nobinary 54return swname 55endfunc 56 57" Inserts 10000 lines with text to fill the swap file with two levels of pointer 58" blocks. Then recovers from the swap file and checks all text is restored. 59" 60" We need about 10000 lines of 100 characters to get two levels of pointer 61" blocks. 62func Test_swap_file () 63set directory = . 64set fileformat = unixundolevels = -1 65edit ! Xtest 66let text = "\tabcdefghijklmnoparstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnoparstuvwxyz0123456789" 67let i = 1 68let linecount = 10000 69while i <= linecount 70call append ( i - 1 , i . text ) 71let i += 1 72endwhile 73$ delete 74 75let swname = CopySwapfile () 76 77new 78only ! 79bwipe ! Xtest 80call rename ( 'Xswap' , swname ) 81recover Xtest 82call delete ( swname ) 83let linedollar = line ( '$' ) 84call assert_equal ( linecount , linedollar ) 85if linedollar < linecount 86let linecount = linedollar 87endif 88let i = 1 89while i <= linecount 90call assert_equal ( i . text , getline ( i )) 91let i += 1 92endwhile 93 94set undolevels & 95enew ! |only 96endfunc 97 98func Test_nocatch_process_still_running () 99let g: skipped_reason = 'test_override() is N/A' 100return 101" sysinfo.uptime probably only works on Linux 102if ! has ( 'linux' ) 103let g: skipped_reason = 'only works on Linux' 104return 105endif 106" the GUI dialog can't be handled 107if has ( 'gui_running' ) 108let g: skipped_reason = 'only works in the terminal' 109return 110endif 111 112" don't intercept existing swap file here 113au ! SwapExists 114 115" Edit a file and grab its swapfile. 116edit Xswaptest 117call setline ( 1 , [ 'a' , 'b' , 'c' ]) 118let swname = CopySwapfile () 119 120" Forget we edited this file 121new 122only ! 123bwipe ! Xswaptest 124 125call rename ( 'Xswap' , swname ) 126call feedkeys ( 'e' , 'tL' ) 127redir => editOutput 128edit Xswaptest 129redir END 130call assert_match ( 'E325: ATTENTION' , editOutput ) 131call assert_match ( 'file name: .*Xswaptest' , editOutput ) 132call assert_match ( 'process ID: \d* (STILL RUNNING)' , editOutput ) 133 134" Forget we edited this file 135new 136only ! 137bwipe ! Xswaptest 138 139" pretend we rebooted 140call test_override ( "uptime" , 0 ) 141sleep 1 142 143call feedkeys ( 'e' , 'tL' ) 144redir => editOutput 145edit Xswaptest 146redir END 147call assert_match ( 'E325: ATTENTION' , editOutput ) 148call assert_notmatch ( '(STILL RUNNING)' , editOutput ) 149 150call test_override ( "ALL" , 0 ) 151call delete ( swname ) 152endfunc 153 154" Test for :recover with multiple swap files 155func Test_recover_multiple_swap_files () 156CheckUnix 157new Xfile1 158call setline ( 1 , [ 'a' , 'b' , 'c' ]) 159preserve 160let b = readblob ( swapname ( '' )) 161call writefile ( b , '.Xfile1.swm' , 'D' ) 162call writefile ( b , '.Xfile1.swn' , 'D' ) 163call writefile ( b , '.Xfile1.swo' , 'D' ) 164 %bw ! 165call feedkeys ( ":recover Xfile1\<CR>3\<CR>q" , 'xt' ) 166call assert_equal ([ 'a' , 'b' , 'c' ], getline ( 1 , '$' )) 167" try using out-of-range number to select a swap file 168bw ! 169call feedkeys ( ":recover Xfile1\<CR>4\<CR>q" , 'xt' ) 170call assert_equal ( 'Xfile1' , @%) 171call assert_equal ([ '' ], getline ( 1 , '$' )) 172bw ! 173call feedkeys ( ":recover Xfile1\<CR>0\<CR>q" , 'xt' ) 174call assert_equal ( 'Xfile1' , @%) 175call assert_equal ([ '' ], getline ( 1 , '$' )) 176bw ! 177endfunc 178 179" Test for :recover using an empty swap file 180func Test_recover_empty_swap_file () 181CheckUnix 182call writefile ([], '.Xfile1.swp' , 'D' ) 183set dir = . 184let msg = execute ( 'recover Xfile1' ) 185call assert_match ( 'Unable to read block 0 from .Xfile1.swp' , msg ) 186call assert_equal ( 'Xfile1' , @%) 187bw ! 188 189" make sure there are no old swap files laying around 190for f in glob ( '.sw?' , 0 , 1 ) 191call delete ( f ) 192endfor 193 194" :recover from an empty buffer 195call assert_fails ( 'recover' , 'E305:' ) 196set dir & vim 197endfunc 198 199" Test for :recover using a corrupted swap file 200" Refer to the comments in the memline.c file for the swap file headers 201" definition. 202func Test_recover_corrupted_swap_file () 203CheckUnix 204 205" recover using a partial swap file 206call writefile ( 0 z1234, '.Xfile1.swp') 207call assert_fails ( 'recover Xfile1' , 'E295:' ) 208bw ! 209 210" recover using invalid content in the swap file 211call writefile ([ repeat ( '1' , 2 * 1024 )], '.Xfile1.swp' ) 212call assert_fails ( 'recover Xfile1' , 'E307:' ) 213call delete ( '.Xfile1.swp' ) 214 215" :recover using a swap file with a corrupted header 216edit Xfile1 217preserve 218let sn = swapname ( '' ) 219let b = readblob ( sn ) 220let save_b = copy ( b ) 221bw ! 222 223" clear the B0_MAGIC_LONG field 224if s: system_64bit 225let b [ 1008 : 1015 ] = 0 z00000000.00000000 226else 227let b [ 1008 : 1011 ] = 0 z00000000 228endif 229call writefile ( b , sn ) 230let msg = execute ( 'recover Xfile1' ) 231call assert_match ( 'the file has been damaged' , msg ) 232call assert_equal ( 'Xfile1' , @%) 233call assert_equal ([ '' ], getline ( 1 , '$' )) 234bw ! 235 236" reduce the page size 237let b = copy ( save_b ) 238let b [ 12 : 15 ] = 0 z00010000 239call writefile ( b , sn ) 240let msg = execute ( 'recover Xfile1' ) 241call assert_match ( 'page size is smaller than minimum value' , msg ) 242call assert_equal ( 'Xfile1' , @%) 243call assert_equal ([ '' ], getline ( 1 , '$' )) 244bw ! 245 246" clear the pointer ID 247let b = copy ( save_b ) 248let b [ 4096 : 4097 ] = 0 z0000 249call writefile ( b , sn ) 250call assert_fails ( 'recover Xfile1' , 'E310:' ) 251call assert_equal ( 'Xfile1' , @%) 252call assert_equal ([ '' ], getline ( 1 , '$' )) 253bw ! 254 255" set the number of pointers in a pointer block to zero 256let b = copy ( save_b ) 257let b [ 4098 : 4099 ] = 0 z0000 258call writefile ( b , sn ) 259call assert_fails ( 'recover Xfile1' , 'E312:' ) 260call assert_equal ( 'Xfile1' , @%) 261call assert_equal ([ '???EMPTY BLOCK' ], getline ( 1 , '$' )) 262bw ! 263 264" set the number of pointers in a pointer block to a large value 265let b = copy ( save_b ) 266let b [ 4098 : 4099 ] = 0 zFFFF 267call writefile ( b , sn ) 268call assert_fails ( 'recover Xfile1' , 'E1364:' ) 269call assert_equal ( 'Xfile1' , @%) 270bw ! 271 272" set the block number in a pointer entry to a negative number 273let b = copy ( save_b ) 274if v: true " Nvim changed this field from a long to an int64_t 275let b [ 4104 : 4111 ] = s: little_endian ?0 z00000000.00000080 :0z80000000.00000000 276else 277let b [ 4104 : 4107 ] = s: little_endian ?0 z00000080 :0z80000000 278endif 279call writefile ( b , sn ) 280call assert_fails ( 'recover Xfile1' , 'E312:' ) 281call assert_equal ( 'Xfile1' , @%) 282call assert_equal ([ '???LINES MISSING' ], getline ( 1 , '$' )) 283bw ! 284 285" clear the data block ID 286let b = copy ( save_b ) 287let b [ 8192 : 8193 ] = 0 z0000 288call writefile ( b , sn ) 289call assert_fails ( 'recover Xfile1' , 'E312:' ) 290call assert_equal ( 'Xfile1' , @%) 291call assert_equal ([ '???BLOCK MISSING' ], getline ( 1 , '$' )) 292bw ! 293 294" set the number of lines in the data block to zero 295let b = copy ( save_b ) 296if s: system_64bit 297let b [ 8208 : 8215 ] = 0 z00000000.00000000 298else 299let b [ 8208 : 8211 ] = 0 z00000000 300endif 301call writefile ( b , sn ) 302call assert_fails ( 'recover Xfile1' , 'E312:' ) 303call assert_equal ( 'Xfile1' , @%) 304call assert_equal ([ '??? from here until ???END lines may have been inserted/deleted' , 305 \'???END' ], getline ( 1 , '$' )) 306bw ! 307 308" set the number of lines in the data block to a large value 309let b = copy ( save_b ) 310if s: system_64bit 311let b [ 8208 : 8215 ] = 0 z00FFFFFF.FFFFFF00 312else 313let b [ 8208 : 8211 ] = 0 z00FFFF00 314endif 315call writefile ( b , sn ) 316call assert_fails ( 'recover Xfile1' , 'E312:' ) 317call assert_equal ( 'Xfile1' , @%) 318call assert_equal ([ '??? from here until ???END lines may have been inserted/deleted' , 319 \'' , '???' , '??? lines may be missing' , 320 \'???END' ], getline ( 1 , '$' )) 321bw ! 322 323" use an invalid text start for the lines in a data block 324let b = copy ( save_b ) 325if s: system_64bit 326let b [ 8216 : 8219 ] = 0 z00000000 327else 328let b [ 8212 : 8215 ] = 0 z00000000 329endif 330call writefile ( b , sn ) 331call assert_fails ( 'recover Xfile1' , 'E312:' ) 332call assert_equal ( 'Xfile1' , @%) 333call assert_equal ([ '???' ], getline ( 1 , '$' )) 334bw ! 335 336" use an incorrect text end (db_txt_end) for the data block 337let b = copy ( save_b ) 338let b [ 8204 : 8207 ] = s: little_endian ?0 z80000000 :0z00000080 339call writefile ( b , sn ) 340call assert_fails ( 'recover Xfile1' , 'E312:' ) 341call assert_equal ( 'Xfile1' , @%) 342call assert_equal ([ '??? from here until ???END lines may be messed up' , '' , 343 \'???END' ], getline ( 1 , '$' )) 344bw ! 345 346" remove the data block 347let b = copy ( save_b ) 348call writefile ( b [: 8191 ], sn ) 349call assert_fails ( 'recover Xfile1' , 'E312:' ) 350call assert_equal ( 'Xfile1' , @%) 351call assert_equal ([ '???MANY LINES MISSING' ], getline ( 1 , '$' )) 352bw ! 353 354" use an out-of-bounds db_txt_start in the data block (would cause OOB 355" read past dp->db_index[] in ml_recover() without the bounds check) 356let b = copy ( save_b ) 357let b [ 8200 : 8203 ] = s: little_endian ?0 zFEFFFFFF :0zFFFFFFFE 358if s: system_64bit 359let b [ 8208 : 8215 ] = 0 z00FFFFFF.FFFFFF00 360else 361let b [ 8208 : 8211 ] = 0 z00FFFF00 362endif 363call writefile ( b , sn ) 364call assert_fails ( 'recover Xfile1' , 'E312:' ) 365call assert_equal ( 'Xfile1' , @%) 366call assert_match ( '??? block header corrupted' , getline ( 1 )) 367bw ! 368call delete ( sn ) 369endfunc 370 371" Test for :recover using an encrypted swap file 372func Test_recover_encrypted_swap_file () 373CheckFeature cryptv 374CheckUnix 375 376" Recover an encrypted file from the swap file without the original file 377new Xfile1 378call feedkeys ( ":X\<CR>vim\<CR>vim\<CR>" , 'xt' ) 379call setline ( 1 , [ 'aaa' , 'bbb' , 'ccc' ]) 380preserve 381let b = readblob ( '.Xfile1.swp' ) 382call writefile ( b , '.Xfile1.swm' ) 383bw ! 384call feedkeys ( ":recover Xfile1\<CR>vim\<CR>\<CR>" , 'xt' ) 385call assert_equal ([ 'aaa' , 'bbb' , 'ccc' ], getline ( 1 , '$' )) 386bw ! 387call delete ( '.Xfile1.swm' ) 388 389" Recover an encrypted file from the swap file with the original file 390new Xfile1 391call feedkeys ( ":X\<CR>vim\<CR>vim\<CR>" , 'xt' ) 392call setline ( 1 , [ 'aaa' , 'bbb' , 'ccc' ]) 393update 394call setline ( 1 , [ '111' , '222' , '333' ]) 395preserve 396let b = readblob ( '.Xfile1.swp' ) 397call writefile ( b , '.Xfile1.swm' ) 398bw ! 399call feedkeys ( ":recover Xfile1\<CR>vim\<CR>\<CR>" , 'xt' ) 400call assert_equal ([ '111' , '222' , '333' ], getline ( 1 , '$' )) 401call assert_true ( & modified ) 402bw ! 403call delete ( '.Xfile1.swm' ) 404call delete ( 'Xfile1' ) 405endfunc 406 407" Test for :recover using an unreadable swap file 408func Test_recover_unreadable_swap_file () 409CheckUnix 410CheckNotRoot 411new Xfile1 412let b = readblob ( '.Xfile1.swp' ) 413call writefile ( b , '.Xfile1.swm' , 'D' ) 414bw ! 415call setfperm ( '.Xfile1.swm' , '-w-------' ) 416call assert_fails ( 'recover Xfile1' , 'E306:' ) 417endfunc 418 419" Test for using :recover when the original file and the swap file have the 420" same contents. 421func Test_recover_unmodified_file () 422CheckUnix 423call writefile ([ 'aaa' , 'bbb' , 'ccc' ], 'Xfile1' ) 424edit Xfile1 425preserve 426let b = readblob ( '.Xfile1.swp' ) 427 %bw ! 428call writefile ( b , '.Xfile1.swz' , 'D' ) 429let msg = execute ( 'recover Xfile1' ) 430call assert_equal ([ 'aaa' , 'bbb' , 'ccc' ], getline ( 1 , '$' )) 431call assert_false ( & modified ) 432call assert_match ( 'Buffer contents equals file contents' , msg ) 433bw ! 434call delete ( 'Xfile1' ) 435endfunc 436 437" Test for recovering a file when editing a symbolically linked file 438func Test_recover_symbolic_link () 439CheckUnix 440call writefile ([ 'aaa' , 'bbb' , 'ccc' ], 'Xfile1' , 'D' ) 441silent !ln -sXfile1 Xfile2 442edit Xfile2 443call assert_equal ( '.Xfile1.swp' , fnamemodify ( swapname ( '' ), ':t' )) 444preserve 445let b = readblob ( '.Xfile1.swp' ) 446 %bw ! 447call writefile ([], 'Xfile1' ) 448call writefile ( b , '.Xfile1.swp' ) 449silent ! recover Xfile2 450call assert_equal ([ 'aaa' , 'bbb' , 'ccc' ], getline ( 1 , '$' )) 451call assert_true ( & modified ) 452update 453 %bw ! 454call assert_equal ([ 'aaa' , 'bbb' , 'ccc' ], readfile ( 'Xfile1' )) 455call delete ( 'Xfile2' ) 456call delete ( '.Xfile1.swp' ) 457endfunc 458 459" Test for recovering a file when an autocmd moves the cursor to an invalid 460" line. This used to result in an internal error (E315) which is fixed 461" by 8.2.2966. 462func Test_recover_invalid_cursor_pos () 463call writefile ([], 'Xfile1' , 'D' ) 464edit Xfile1 465preserve 466let b = readblob ( '.Xfile1.swp' ) 467bw ! 468augroup Test 469au ! 470au BufReadPost Xfile1 normal ! 3G 471augroup END 472call writefile ( range ( 1 , 3 ), 'Xfile1' ) 473call writefile ( b , '.Xfile1.swp' , 'D' ) 474try 475recover Xfile1 476catch /E308:/ 477" this test is for the :E315 internal error. 478" ignore the 'E308: Original file may have been changed' error 479endtry 480redraw ! 481augroup Test 482au ! 483augroup END 484augroup ! Test 485endfunc 486 487" Test for recovering a buffer without a name 488func Test_noname_buffer () 489new 490call setline ( 1 , [ 'one' , 'two' ]) 491preserve 492let sn = swapname ( '' ) 493let b = readblob ( sn ) 494bw ! 495call writefile ( b , sn , 'D' ) 496exe "recover " .. sn 497call assert_equal ([ 'one' , 'two' ], getline ( 1 , '$' )) 498endfunc 499 500" Test for recovering a corrupted swap file, those caused a crash 501func Test_recover_corrupted_swap_file1 () 502CheckUnix 503" only works correctly on 64bit Unix systems: 504if ! ( s: system_64bit && s: little_endian && has ( 'unix' )) 505throw 'Skipped: Corrupt Swap file sample requires a little-endian 64bit Unix build' 506endif 507" Test 1: Heap buffer-overflow 508new 509let sample = 'samples/recover-crash1.swp' 510let target = '.Xpoc1.swp' " Xpoc1.swp (non-hidden) doesn't work in Nvim 511" In an out-of-source-tree build the sample may be a symlink, this copies the 512" data into a real file. 513call writefile ( readblob ( sample ), target , 'D' ) 514try 515sil recover ! Xpoc1 516catch /^Vim\%((\S\+)\)\=:E1364:/ 517endtry 518let content = getline ( 1 , '$' ) ->join () 519call assert_match ( '???ILLEGAL BLOCK NUMBER' , content ) 520call delete ( target ) 521bw ! 522 523" Test 2: Segfault 524new 525let sample = 'samples/recover-crash2.swp' 526let target = '.Xpoc2.swp' " Xpoc1.swp (non-hidden) doesn't work in Nvim 527" In an out-of-source-tree build the sample may be a symlink, this copies the 528" data into a real file. 529call writefile ( readblob ( sample ), target , 'D' ) 530try 531sil recover ! Xpoc2 532catch /^Vim\%((\S\+)\)\=:E1364:/ 533endtry 534let content = getline ( 1 , '$' ) ->join () 535call assert_match ( '???ILLEGAL BLOCK NUMBER' , content ) 536call assert_match ( '???LINES MISSING' , content ) 537call delete ( target ) 538bw ! 539 540" Test 3: wrong page_count header 541new 542let sample = 'samples/recover-mismatch-pc.swp' 543let target = 'Xmismatch-pc.swp' 544call writefile ( readblob ( sample ), target , 'bD' ) 545try 546sil noa recover! Xmismatch-pc.swp 547catch 548endtry 549" Verifies no crash occurs. The OOB write is only reliably triggered 550" interactively due to memory pressure evicting blocks in the test runner. 551bw ! 552endfunc 553 554" vim: shiftwidth=2 sts=2 expandtab