robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
c5ea9ca2ad
master
1local n =require ( 'test.functional.testnvim' )() 2local t =require ( 'test.testutil' ) 3 4local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 5local api =n . api 6local eq =t . eq 7local exec_capture =n . exec_capture 8local exec_lua =n . exec_lua 9local feed =n . feed 10local fn =n . fn 11local poke_eventloop =n . poke_eventloop 12 13local fixtures =vim . fs . joinpath ( t . paths . test_source_path , 'test/functional/fixtures/zip' ) 14local old_samples =vim . fs . joinpath ( t . paths . test_source_path , 'test/old/testdir/samples' ) 15 16local function lines () 17return api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) 18end 19 20local function edit ( path ) 21api . nvim_cmd ( { cmd ='edit' , args ={ path } , magic ={ file =false , bar =false } } , {} ) 22end 23 24local function clear_zip () 25n . clear ( { args ={ '--clean' } } ) 26end 27 28local function line_of ( text ) 29for i , line in ipairs ( lines ()) do 30if line == text then 31return i 32end 33end 34error (( 'missing line %q in %s' ): format ( text , vim . inspect ( lines ()))) 35end 36 37describe ( 'nvim.zip' , function () 38local root 39 40--- Copy an archive into the test directory and return its path. 41local function stage ( source_dir , source , as ) 42local target =vim . fs . joinpath ( root , as or source ) 43assert ( vim . uv . fs_copyfile ( vim . fs . joinpath ( source_dir , source ), target )) 44return target 45end 46 47--- Stage the browsing fixture, restart, and open it. 48local function browse ( as ) 49local archive =stage ( fixtures , 'browser.zip' , as ) 50clear_zip () 51edit ( archive ) 52return archive 53end 54 55before_each ( function () 56t . skip ( vim . fn . executable ( 'unzip' ) == 0 , 'unzip not available' ) 57root =vim . fs . normalize ( t . tmpname ( false ) .. ' space%#' ) 58t . mkdir ( root ) 59end ) 60 61after_each ( function () 62n . rmdir ( root ) 63end ) 64 65describe ( 'activation' , function () 66it ( 'uses zip.lua by default' , function () 67browse () 68 69eq ( 'folder/' , lines ()[ 1 ]) 70eq ( true , exec_lua ( 'return vim.g.loaded_nvim_zip_plugin == true' )) 71eq ( false , exec_lua ( 'return vim.g.loaded_zipPlugin ~= nil' )) 72end ) 73 74it ( 'defers to zipPlugin.vim loaded before startup plugins' , function () 75local archive =stage ( old_samples , 'test.zip' , 'legacy.zip' ) 76n . clear ( { args ={ '--clean' , '--cmd' , 'packadd old-zip' } } ) 77 78edit ( archive ) 79 80eq ( true , lines ()[ 1 ]: find ( '" zip.vim version' , 1 , true ) ~= nil ) 81eq ( 0 , fn . exists ( '#nvim.zip' )) 82eq ( false , exec_lua ( 'return vim.g.loaded_nvim_zip_plugin ~= nil' )) 83end ) 84 85it ( 'yields to zipPlugin.vim loaded after startup' , function () 86local archive =stage ( old_samples , 'test.zip' , 'legacy.zip' ) 87clear_zip () 88exec_lua ( [[vim.cmd.packadd('old-zip')]] ) 89 90edit ( archive ) 91 92eq ( true , lines ()[ 1 ]: find ( '" zip.vim version' , 1 , true ) ~= nil ) 93eq ( 1 , fn . exists ( '#nvim.zip' )) 94eq ( true , exec_lua ( 'return vim.g.loaded_nvim_zip_plugin == true' )) 95end ) 96 97it ( 'can be disabled' , function () 98local archive =stage ( fixtures , 'browser.zip' ) 99n . clear ( { 100args ={ '--clean' , '--cmd' , 'let g:loaded_nvim_zip_plugin = 1' } , 101} ) 102 103edit ( archive ) 104 105eq ( 0 , fn . exists ( '#nvim.zip' )) 106eq ( '' , api . nvim_get_option_value ( 'filetype' , { buf =0 } )) 107end ) 108 109it ( 'can source zip.lua repeatedly' , function () 110clear_zip () 111 112-- Re-sourcing must reuse the augroup rather than stack duplicate autocmds. 113eq ( 1142 , 115exec_lua ( function () 116vim . cmd . runtime ( 'plugin/zip.lua' ) 117vim . cmd . runtime ( 'plugin/zip.lua' ) 118local ids ={} 119for _ , autocmd in ipairs ( vim . api . nvim_get_autocmds ( { group ='nvim.zip' } )) do 120ids [ autocmd . id ] =true 121end 122return vim . tbl_count ( ids ) 123end ) 124) 125end ) 126 127it ( 'reports an unavailable backend without claiming the buffer' , function () 128local archive =stage ( fixtures , 'browser.zip' ) 129clear_zip () 130exec_lua ( [[vim.env.PATH = '']] ) 131 132edit ( archive ) 133poke_eventloop () 134 135eq ( true , exec_capture ( 'messages' ): find ( 'unzip executable not found' , 1 , true ) ~= nil ) 136eq ( false , exec_lua ( 'return vim.b.nvim_dir ~= nil' )) 137end ) 138end ) 139 140describe ( 'browsing' , function () 141it ( 'opens zip-compatible file types' , function () 142browse ( 'browser.jar' ) 143 144eq ( 'folder/' , lines ()[ 1 ]) 145eq ( 'zip' , api . nvim_get_option_value ( 'filetype' , { buf =0 } )) 146end ) 147 148it ( 'browses directories and opens entries' , function () 149browse () 150 151eq ( { 152'folder/' , 153'inner.zip' , 154'../escape.txt' , 155'/absolute.txt' , 156'crlf.txt' , 157'noeol.txt' , 158'names/' , 159} , lines ()) 160eq ( 'zip' , api . nvim_get_option_value ( 'filetype' , { buf =0 } )) 161eq ( true , exec_capture ( 'syntax list zipDirectory' ): find ( 'zipDirectory' , 1 , true ) ~= nil ) 162 163feed ( '<CR>' ) 164poke_eventloop () 165eq ( { 'nested/' , 'root.txt' , 'root.java' } , lines ()) 166 167feed ( '<CR>' ) 168poke_eventloop () 169eq ( { 'file.txt' } , lines ()) 170 171feed ( '-' ) 172poke_eventloop () 173eq ( { 'nested/' , 'root.txt' , 'root.java' } , lines ()) 174eq ( 'nested/' , api . nvim_get_current_line ()) 175 176api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 177feed ( '<CR>' ) 178poke_eventloop () 179eq ( { 'root text' } , lines ()) 180eq ( 'nowrite' , api . nvim_get_option_value ( 'buftype' , { buf =0 } )) 181eq ( true , api . nvim_get_option_value ( 'readonly' , { buf =0 } )) 182eq ( false , api . nvim_get_option_value ( 'modifiable' , { buf =0 } )) 183eq ( false , api . nvim_get_option_value ( 'swapfile' , { buf =0 } )) 184end ) 185 186it ( 'opens the containing directory from the archive root' , function () 187browse () 188feed ( '-' ) 189poke_eventloop () 190 191eq ( root , vim . fs . normalize ( api . nvim_buf_get_name ( 0 ))) 192eq ( 'browser.zip' , api . nvim_get_current_line ()) 193eq ( 'directory' , api . nvim_get_option_value ( 'filetype' , { buf =0 } )) 194end ) 195 196it ( 'opens entries at quickfix locations' , function () 197local archive =stage ( fixtures , 'browser.zip' ) 198clear_zip () 199local uri =( 'zip://%s/crlf.txt' ): format ( archive ) 200fn . setqflist ( {} , 'r' , { items ={ { filename =uri , lnum =2 , col =1 } } } ) 201 202api . nvim_cmd ( { cmd ='cfirst' } , {} ) 203 204eq ( uri , api . nvim_buf_get_name ( 0 )) 205eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 206eq ( 'text' , api . nvim_get_option_value ( 'filetype' , { buf =0 } )) 207end ) 208 209it ( 'does not reinterpret an entry ending in .zip as an archive' , function () 210browse () 211api . nvim_win_set_cursor ( 0 , { line_of ( 'inner.zip' ), 0 } ) 212feed ( '<CR>' ) 213poke_eventloop () 214 215eq ( { 'nested payload' } , lines ()) 216eq ( false , exec_lua ( 'return vim.b.nvim_dir ~= nil' )) 217end ) 218 219it ( 'preserves normal file reading details for entries' , function () 220local archive =stage ( fixtures , 'browser.zip' ) 221clear_zip () 222 223edit (( 'zip://%s/crlf.txt' ): format ( archive )) 224eq ( { 'one' , 'two' } , lines ()) 225eq ( 'dos' , api . nvim_get_option_value ( 'fileformat' , { buf =0 } )) 226eq ( true , api . nvim_get_option_value ( 'endofline' , { buf =0 } )) 227 228edit (( 'zip://%s/noeol.txt' ): format ( archive )) 229eq ( { 'no final newline' } , lines ()) 230eq ( false , api . nvim_get_option_value ( 'endofline' , { buf =0 } )) 231end ) 232 233it ( 'keeps suspicious entry paths visible and readable' , function () 234browse () 235api . nvim_win_set_cursor ( 0 , { line_of ( '../escape.txt' ), 0 } ) 236feed ( '<CR>' ) 237poke_eventloop () 238 239eq ( { 'escape payload' } , lines ()) 240end ) 241 242it ( 'opens non-zip files normally' , function () 243local archive =vim . fs . joinpath ( root , 'plain.zip' ) 244t . write_file ( archive , 'plain text' , true ) 245clear_zip () 246 247edit ( archive ) 248 249eq ( { 'plain text' } , lines ()) 250eq ( false , exec_lua ( 'return vim.b.nvim_dir ~= nil' )) 251end ) 252 253it ( 'lists a valid empty archive as empty' , function () 254local archive =vim . fs . joinpath ( root , 'empty.zip' ) 255local file =assert ( io . open ( archive , 'wb' )) 256-- An end-of-central-directory record with no entries: a valid, empty archive. 257file : write ( 'PK\5\6' .. string . rep ( '\0' , 18 )) 258file : close () 259clear_zip () 260 261edit ( archive ) 262 263eq ( { '' } , lines ()) 264eq ( 'zip' , api . nvim_get_option_value ( 'filetype' , { buf =0 } )) 265eq ( true , exec_lua ( 'return vim.b.nvim_zip ~= nil' )) 266end ) 267 268it ( 'keeps the current level when reload fails' , function () 269local archive =browse () 270feed ( '<CR>' ) 271poke_eventloop () 272eq ( { 'nested/' , 'root.txt' , 'root.java' } , lines ()) 273 274assert ( os . remove ( archive )) 275feed ( 'R' ) 276poke_eventloop () 277eq ( { 'nested/' , 'root.txt' , 'root.java' } , lines ()) 278 279stage ( fixtures , 'browser.zip' ) 280feed ( 'R' ) 281poke_eventloop () 282eq ( { 'nested/' , 'root.txt' , 'root.java' } , lines ()) 283end ) 284 285it ( 'integrates Java sources with zip.lua and zipPlugin.vim' , function () 286local archive =stage ( fixtures , 'browser.zip' , 'source.jar' ) 287 288for _ , legacy in ipairs ( { false , true } ) do 289if legacy then 290n . clear ( { args ={ '--clean' , '--cmd' , 'packadd old-zip' } } ) 291else 292clear_zip () 293end 294exec_lua ( 'vim.g.ftplugin_java_source_path = ...' , archive ) 295api . nvim_buf_set_name ( 0 , vim . fs . joinpath ( root , 'Test.java' )) 296api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'folder.root' } ) 297api . nvim_cmd ( { cmd ='setfiletype' , args ={ 'java' } } , {} ) 298api . nvim_cmd ( { cmd ='runtime' , args ={ 'ftplugin/java.vim' } } , {} ) 299 300feed ( 'gf' ) 301poke_eventloop () 302 303local uri =legacy and ( 'zipfile://%s::folder/root.java' ): format ( archive ) 304or ( 'zip://%s/folder/root.java' ): format ( archive ) 305eq ( uri , api . nvim_buf_get_name ( 0 )) 306eq ( { 'class root {}' } , lines ()) 307eq ( 'java' , api . nvim_get_option_value ( 'filetype' , { buf =0 } )) 308end 309end ) 310end ) 311 312-- Info-ZIP expands globs itself and reads leading dashes as options, so archive 313-- and entry paths must always reach it as literal text. 314describe ( 'backend arguments' , function () 315it ( 'reads entry selectors literally' , function () 316t . skip ( t . is_os ( 'win' ), 'N/A: archive contains backslashes in entry paths' ) 317local archive =stage ( old_samples , 'testa.zip' , 'special.zip' ) 318clear_zip () 319 320local cases ={ 321{ 'zipglob/a[a].txt' , 'a test file with []' } , 322{ 'zipglob/a*.txt' , 'a test file with a*' } , 323{ 'zipglob/a?.txt' , 'a test file with a?' } , 324{ [[zipglob/a\.txt]] , [[a test file with a\]] } , 325{ [[zipglob/a\\.txt]] , [[a test file with a double \]] } , 326} 327for _ , case in ipairs ( cases ) do 328edit (( 'zip://%s/%s' ): format ( archive , case [ 1 ])) 329eq ( { case [ 2 ] } , lines ()) 330end 331end ) 332 333it ( 'runs the backend when the cwd is its directory' , function () 334t . skip ( t . is_os ( 'win' ), 'N/A: Windows searches the cwd before $PATH' ) 335local archive =stage ( fixtures , 'browser.zip' ) 336local bin =vim . fs . joinpath ( assert ( vim . uv . fs_realpath ( root )), 'bin' ) 337t . mkdir ( bin ) 338assert ( vim . uv . fs_symlink ( vim . fn . exepath ( 'unzip' ), vim . fs . joinpath ( bin , 'unzip' ))) 339n . clear ( { args ={ '--clean' } , env ={ PATH =bin .. ':' .. os . getenv ( 'PATH' ) } } ) 340 341api . nvim_set_current_dir ( bin ) 342edit ( archive ) 343eq ( 'folder/' , lines ()[ 1 ]) 344end ) 345 346it ( 'keeps a leading dash in a path from becoming a backend option' , function () 347local archive =stage ( old_samples , 'poc.zip' ) 348clear_zip () 349 350edit ( archive ) 351eq ( { '-d/' , 'pwned' } , lines ()) 352 353edit (( 'zip://%s/-d/tmp' ): format ( archive )) 354eq ( { '' } , lines ()) 355end ) 356 357it ( 'treats archive glob characters literally' , function () 358t . skip ( t . is_os ( 'win' ), 'N/A: Windows filenames cannot contain these characters' ) 359local archive =stage ( fixtures , 'browser.zip' , 'archive::[*?].zip' ) 360-- Would be matched instead of the archive above if the name were globbed. 361stage ( old_samples , 'test.zip' , 'archivex.zip' ) 362clear_zip () 363 364edit ( archive ) 365 366eq ( 'folder/' , lines ()[ 1 ]) 367api . nvim_win_set_cursor ( 0 , { line_of ( 'inner.zip' ), 0 } ) 368feed ( '<CR>' ) 369poke_eventloop () 370eq ( { 'nested payload' } , lines ()) 371 372local uri =( 'zip://%s/crlf.txt' ): format ( archive ) 373edit ( uri ) 374eq ( uri , api . nvim_buf_get_name ( 0 )) 375eq ( { 'one' , 'two' } , lines ()) 376end ) 377 378it ( 'lists and opens entries with difficult names' , function () 379t . skip ( t . is_os ( 'win' ), 'N/A: Windows filenames cannot contain these characters' ) 380local archive =browse () 381 382-- "star*" and "-dash" are the glob and option cases; the rest are shell 383-- metacharacters that must survive an argv-based backend untouched. 384local names ={ 385'space name.txt' , 386'two spaces.txt' , 387' leading.txt' , 388'trailing .txt' , 389[[back\slash.txt]] , 390[[single'quote.txt]] , 391'double"quote.txt' , 392'dollar$name.txt' , 393'backtick`name.txt' , 394'semi;name.txt' , 395'pipe|name.txt' , 396'amp&name.txt' , 397'paren(name).txt' , 398'bang!name.txt' , 399'hash#name.txt' , 400'percent%name.txt' , 401'star*.txt' , 402'question?.txt' , 403'[bracket].txt' , 404'-dash.txt' , 405} 406 407api . nvim_win_set_cursor ( 0 , { line_of ( 'names/' ), 0 } ) 408feed ( '<CR>' ) 409poke_eventloop () 410eq ( names , lines ()) 411 412api . nvim_win_set_cursor ( 0 , { line_of ( 'star*.txt' ), 0 } ) 413feed ( '<CR>' ) 414poke_eventloop () 415eq (( 'zip://%s/names/star*.txt' ): format ( archive ), api . nvim_buf_get_name ( 0 )) 416eq ( { 'content of star*.txt' } , lines ()) 417 418for _ , name in ipairs ( names ) do 419edit (( 'zip://%s/names/%s' ): format ( archive , name )) 420eq ( { 'content of ' .. name } , lines ()) 421end 422end ) 423end ) 424 425it ( 'prompts for the password of an encrypted archive' , function () 426local archive =stage ( fixtures , 'encrypted.zip' ) 427clear_zip () 428 429edit ( archive ) 430eq ( { 'secret.txt' } , lines ()) 431 432-- The password is queued first, so that the prompt consumes it from typeahead. 433exec_lua ( function ( uri ) 434vim . api . nvim_input ( 'hunter2<CR>' ) 435vim . api . nvim_cmd ( { cmd ='edit' , args ={ uri } , magic ={ file =false , bar =false } } , {} ) 436end , ( 'zip://%s/secret.txt' ): format ( archive )) 437poke_eventloop () 438 439eq ( { 'secret content' } , lines ()) 440end ) 441 442it ( 'reports an incorrect archive password' , function () 443local archive =stage ( fixtures , 'encrypted.zip' ) 444clear_zip () 445 446-- Info-ZIP allows three attempts before giving up. Failure msg is scheduled, so wait() for it. 447local reported =exec_lua ( function ( uri ) 448vim . api . nvim_input ( 'no1<CR>no2<CR>no3<CR>' ) 449vim . api . nvim_cmd ( { cmd ='edit' , args ={ uri } , magic ={ file =false , bar =false } } , {} ) 450local function messages () 451return vim . api . nvim_exec2 ( 'messages' , { output =true } ). output 452end 453vim . wait ( 1000 , function () 454return messages (): find ( 'zip: ' , 1 , true ) ~= nil 455end ) 456return messages () 457end , ( 'zip://%s/secret.txt' ): format ( archive )) 458 459t . matches ( 'incorrect password' , reported ) 460end ) 461 462describe ( 'extract' , function () 463--- Stage an archive, restart with the cwd inside the test directory, and open it. 464local function open_in_cwd ( source_dir , source ) 465local archive =stage ( source_dir , source ) 466clear_zip () 467api . nvim_set_current_dir ( root ) 468edit ( archive ) 469return archive 470end 471 472it ( 'extracts the entry under the cursor into the current directory' , function () 473open_in_cwd ( fixtures , 'browser.zip' ) 474feed ( '<CR>' ) 475poke_eventloop () 476api . nvim_win_set_cursor ( 0 , { line_of ( 'root.java' ), 0 } ) 477feed ( 'x' ) 478poke_eventloop () 479 480eq ( 'class root {}\n' , t . read_file ( vim . fs . joinpath ( root , 'root.java' ))) 481end ) 482 483it ( 'refuses to extract a directory' , function () 484open_in_cwd ( fixtures , 'browser.zip' ) 485api . nvim_win_set_cursor ( 0 , { line_of ( 'folder/' ), 0 } ) 486feed ( 'x' ) 487poke_eventloop () 488 489eq ( true , exec_capture ( 'messages' ): find ( 'not a directory' , 1 , true ) ~= nil ) 490eq ( nil , vim . uv . fs_stat ( vim . fs . joinpath ( root , 'folder' ))) 491end ) 492 493it ( 'refuses to overwrite an existing file when extracting' , function () 494local target =vim . fs . joinpath ( root , 'root.java' ) 495stage ( fixtures , 'browser.zip' ) 496t . write_file ( target , 'untouched' , true ) 497clear_zip () 498api . nvim_set_current_dir ( root ) 499 500edit ( vim . fs . joinpath ( root , 'browser.zip' )) 501feed ( '<CR>' ) 502poke_eventloop () 503api . nvim_win_set_cursor ( 0 , { line_of ( 'root.java' ), 0 } ) 504feed ( 'x' ) 505poke_eventloop () 506 507eq ( true , exec_capture ( 'messages' ): find ( 'already exists' , 1 , true ) ~= nil ) 508eq ( 'untouched' , t . read_file ( target )) 509end ) 510 511it ( 'extracts suspicious entries without escaping the current directory' , function () 512open_in_cwd ( old_samples , 'evil.zip' ) 513 514eq ( { 515'../../../../etc/ax-pwn' , 516'a/../../../../../../../../../../../../../../../../../../tmp/foobar' , 517'/tmp/vim_zip/a/b/payload.txt' , 518} , lines ()) 519 520for _ , entry in ipairs ( lines ()) do 521api . nvim_win_set_cursor ( 0 , { line_of ( entry ), 0 } ) 522feed ( 'x' ) 523poke_eventloop () 524end 525 526-- Each lands flat in the cwd, never at the path the entry asked for. 527for _ , name in ipairs ( { 'ax-pwn' , 'foobar' , 'payload.txt' } ) do 528eq ( true , vim . uv . fs_stat ( vim . fs . joinpath ( root , name )) ~= nil ) 529end 530eq ( nil , vim . uv . fs_stat ( vim . fs . joinpath ( root , '..' , 'ax-pwn' ))) 531end ) 532end ) 533end )