robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1const s: testdir = expand ( "<script>:h" ) 2const s: runtimedir = simplify ( s: testdir . '/../../../runtime' ) 3const s: netrw_path = s: runtimedir . '/pack/dist/opt/netrw/autoload/netrw.vim' 4const s: netrw_test_path = s: testdir . '/samples/netrw.vim' 5const s: testScript =<< trim END 6 7" Testing functions: {{{1 8function! TestNetrwCaptureRemotePath(dirname) 9call s:RemotePathAnalysis(a:dirname) 10return {"method": s:method, "user": s:user, "machine": s:machine, "port": s:port, "path": s:path, "fname": s:fname} 11endfunction 12 13" Test directory creation via s:NetrwMakeDir() 14" Precondition: inputsave() and inputrestore() must be disabled in s:NetrwMakeDir 15 16function s:test_inputsave() 17if exists("s:inputguards_disabled") && s:inputguards_disabled 18return 19endif 20call inputsave() 21endfunction 22 23function s:test_inputrestore() 24if exists("s:inputguards_disabled") && s:inputguards_disabled 25return 26endif 27call inputrestore() 28endfunction 29 30function s:test_input(prompt, text = v:null, completion = v:null) " Nvim: use v:null instead of v:none 31 32if exists("s:inputdefaults_disabled") && s:inputdefaults_disabled || a:text == v:null 33return input(a:prompt) 34elseif a:completion == v:null 35return input(a:prompt, a:text) 36endif 37 38return input(a:prompt, a:text, a:completion) 39endfunction 40 41function Test_NetrwMakeDir(parentdir = $HOME, dirname = "NetrwMakeDir", symlink = 0) abort 42if a:symlink 43" Plainly delegate, this device is necessary because feedkeys() can't 44" access script functions directly. 45call s:NetrwMakeDir('') 46" wipe out the test buffer 47bw 48" reenable the guards 49let s:inputguards_disabled = 0 50else 51" Use feedkeys() to simulate user input (directory name) 52new 53let b:netrw_curdir = a:parentdir 54let s:inputguards_disabled = 1 55call feedkeys($"\<Cmd>call Test_NetrwMakeDir('{a:parentdir}', '{a:dirname}', 1)\<CR>{a:dirname}\<CR>", "x") 56endif 57endfunction 58 59" Test file copy operations via s:NetrwMarkFileCopy() 60function Test_NetrwMarkFileCopy(source_dir, target_dir, marked_files) abort 61" set up 62new 63let b:netrw_curdir= a:source_dir 64let s:netrwmftgt = a:target_dir 65let s:netrwmarkfilelist_{bufnr("%")} = a:marked_files 66let s:netrwmftgt_islocal = 1 67" delegate 68call s:NetrwMarkFileCopy(1) 69" wipe out the test buffer 70bw 71endfunction 72 73" Corner case: copy into the same dir triggers a user prompt 74function Test_NetrwMarkFileCopy_SameDir(dir = $HOME, symlink = 0) abort 75const filename = "filename.txt" 76const file = netrw#fs#PathJoin(a:dir, filename) 77 78const newfilename = "newfilename.txt" 79const newfile = netrw#fs#PathJoin(a:dir, newfilename) 80 81if a:symlink 82" Plainly delegate, this device is necessary because feedkeys() can't 83" access script functions directly. 84" set up 85new 86let b:netrw_curdir = a:dir 87let s:netrwmftgt = a:dir 88let s:netrwmarkfilelist_{bufnr("%")} = [filename] 89let s:netrwmftgt_islocal = 1 90 91" delegate 92call s:NetrwMarkFileCopy(1) 93 94" validate 95call assert_equalfile(file, newfile, "File copy in same dir failed") 96 97" tear down 98call delete(file) 99call delete(newfile) 100" wipe out the test buffer 101bw 102" reenable the guards 103let s:inputguards_disabled = 0 104let s:inputdefaults_disabled = 0 105else 106" Use feedkeys() to simulate user input (directory name) 107let s:inputguards_disabled = 1 108let s:inputdefaults_disabled = 1 109 110call writefile([$"NetrwMarkFileCopy test file"], file) 111 112call feedkeys($"\<Cmd>call Test_NetrwMarkFileCopy_SameDir('{a:dir}', 1)\<CR>{newfilename}\<CR>", "x") 113endif 114endfunction 115 116" Test file copy operations via s:NetrwMarkFileMove() 117function Test_NetrwMarkFileMove(source_dir, target_dir, marked_files) abort 118" set up 119new 120let b:netrw_curdir= a:source_dir 121let s:netrwmftgt = a:target_dir 122let s:netrwmarkfilelist_{bufnr("%")} = a:marked_files 123let s:netrwmftgt_islocal = 1 124" delegate 125call s:NetrwMarkFileMove(1) 126" wipe out the test buffer 127bw 128endfunction 129 130" Test how netrw fixes paths according with settings 131" (g:netrw_keepdir, g:netrw_cygwin, tree style ...) 132function Test_NetrwFile(fname) abort 133return s:NetrwFile(a:fname) 134endfunction 135 136function Test_NetrwLcd(newdir) abort 137return s:NetrwLcd(a:newdir) 138endfunction 139 140" Test hostname validation 141function Test_NetrwValidateHostname(hostname) abort 142return s:NetrwValidateHostname(a:hostname) 143endfunction 144 145" Test unmarking all files via s:NetrwUnMarkFile() 146function Test_NetrwUnMarkFile() 147" set up: init global and buffer-local mark lists 148new 149 150" initializing the global mark list, the loop handles the buffer-local 151let s:netrwmarkfilelist = ['test_file'] 152 153" making sure every buffer has a mark list and match pattern 154let ibuf = 1 155while ibuf <= bufnr('$') 156let s:netrwmarkfilelist_{ibuf} = ['test_mark'] 157let s:netrwmarkfilemtch_{ibuf} = 'test_mark' 158let ibuf = ibuf + 1 159endwhile 160 161call s:NetrwUnMarkFile(1) 162 163call assert_false(exists('s:netrwmarkfilelist'), 'Global list should be wiped') 164call assert_false(exists('s:netrwmarkfilelist_' . bufnr('$')), 'Last buffer-local list should be wiped') 165call assert_false(exists('s:netrwmarkfilemtch_' . bufnr('$')), 'Last buffer-local match str should be wiped') 166call assert_false(exists('s:netrwmarkfilelist_1'), 'First buffer-local list should be wiped') 167call assert_false(exists('s:netrwmarkfilemtch_1'), 'First buffer-local match str should be wiped') 168 169" wipe out the test buffer 170bw 171endfunction 172 173function Test_MakeBookmark(netrw_curdir, fname) 174new 175let b:netrw_curdir = a:netrw_curdir 176call s:MakeBookmark(a:fname) 177bw 178endfunction 179 180" Test the markings match pattern rebuilt by s:NetrwMarkFile() when unmarking an entry 181function Test_NetrwMarkFile_match_pattern_rebuild() 182new 183let curbufnr = bufnr("%") 184 185call s:NetrwMarkFile(1, 'fname_dir/') 186call s:NetrwMarkFile(1, 'fname_file') 187let match_pattern = s:netrwmarkfilemtch_{curbufnr} 188 189" Assert match pattern after marking and unmarking a directory entry 190call s:NetrwMarkFile(1, 'dir/') 191call s:NetrwMarkFile(1, 'dir/') 192let rebuilt_match_pattern = s:netrwmarkfilemtch_{curbufnr} 193call assert_equal(match_pattern, rebuilt_match_pattern) 194 195" Assert match pattern after marking and unmarking a file entry 196call s:NetrwMarkFile(1, 'file') 197call s:NetrwMarkFile(1, 'file') 198let rebuilt_match_pattern = s:netrwmarkfilemtch_{curbufnr} 199call assert_equal(match_pattern, rebuilt_match_pattern) 200 201bw 202endfunction 203 204" Test the "home" directory used for bookmarks and history 205function Test_NetrwHome() abort 206return s:NetrwHome() 207endfunction 208" }}} 209END 210 211"make copy of netrw script and add function to print local variables" 212func s: appendDebugToNetrw ( netrw_path , netrw_test_path ) 213 214" load the netrw script 215execute "split" a: netrw_test_path 216execute "read" a: netrw_path 217 218" replace input guards for convenient testing versions 219 %substitute @call inputsave()@call s:test_inputsave()@g 220 %substitute @call inputrestore()@call s:test_inputrestore()@g 221 %substitute @\<input(@s:test_input(@g 222 223call cursor ( 1 , 1 ) 224let pos = search ( "Settings Restoration:" ) - 1 225" insert the test functions before the end guard 226call assert_false ( append ( pos , s: testScript )) 227 228" save the modified script content 229write 230bwipe ! 231 232endfunction 233 234func SetUp () 235 236" prepare modified netrw script 237call s: appendDebugToNetrw ( s: netrw_path , s: netrw_test_path ) 238 239" source the modified script 240exe "source" s: netrw_test_path 241 242" Rig the package. The modified script guard prevents loading it again. 243let & runtimepath = s: runtimedir 244let & packpath = s: runtimedir 245packadd netrw 246 247" use proper path 248if has ( 'win32' ) 249let $ HOME = substitute ( $ HOME , '/' , '\\' , 'g' ) 250endif 251 252endfunction 253 254func TearDown () 255" cleanup 256call delete ( s: netrw_test_path ) 257endfunction 258 259func SetShell ( shell ) 260" select different shells 261if a: shell == "default" 262set shell & shellcmdflag & shellxquote & shellpipe & shellredir & 263if has ( "win32" ) 264" Nvim: default 'shell' is "sh" due to $SHELL being set in Makefile, 265" but here 'shell' should be cmd.exe. 266set shell = cmd.exeshellcmdflag = /s\ /c 267endif 268elseif a: shell == "powershell" " help dos-powershell 269" powershell desktop is windows only 270if ! has ( "win32" ) 271throw 'Skipped: powershell desktop is missing' 272endif 273set shell = powershellshellcmdflag = -NoProfile\ -Commandshellxquote = \" 274set shellpipe = 2>&1\ \|\ Out-File\ -Encoding\ defaultshellredir = 2>&1\ \|\ Out-File\ -Encoding\ default 275elseif a: shell == "pwsh" " help dos-powershell 276" powershell core works crossplatform 277if ! executable ( "pwsh" ) 278throw 'Skipped: powershell core is missing' 279endif 280set shell = pwshshellcmdflag = -NoProfile\ -cshellpipe = >%s\ 2>&1shellredir = >%s\ 2>&1 281if has ( "win32" ) 282set shellxquote = \" 283else 284set shellxquote = 285endif 286else 287call assert_report ( "Trying to select an unknown shell" ) 288endif 289" Nvim: 'shellxquote' needs to be empty for the tests for work. 290set shellxquote = 291endfunc 292 293func s: combine 294 \( usernames 295 \, methods 296 \, hosts 297 \, ports 298 \, dirs 299 \, files ) 300for username in a: usernames 301for method in a: methods 302for host in a: hosts 303for port in a: ports 304for dir in a: dirs 305for file in a: files 306" --- Build a full remote path --- 307 308let port_str = empty ( port ) ? "" : ':' . port 309let remote = printf ( '%s://%s@%s%s/%s%s' , method , username , host , port_str , dir , file ) 310 311let result = TestNetrwCaptureRemotePath ( remote ) 312 313call assert_equal ( result . method , method ) 314call assert_equal ( result . user , username ) 315call assert_equal ( result . machine , host ) 316call assert_equal ( result . port , port ) 317call assert_equal ( result . path , dir . file ) 318endfor 319endfor 320endfor 321endfor 322endfor 323endfor 324endfunction 325 326 327func Test_netrw_lcd_failure () 328CheckUnix 329CheckNotRoot 330let dirname = getcwd () .. '/Xnetrw_noaccess' 331call mkdir ( dirname ) 332call assert_true ( setfperm ( dirname , 'r--------' )) 333try 334for prevdir in [ 1 , 0 ] 335new 336try 337let cwd = getcwd () 338if prevdir 339let w: netrw_prvdir = cwd 340else 341unlet ! w: netrw_prvdir 342endif 343setlocal modifiable noreadonly number 344let msg = execute ( 'call assert_equal(-1, Test_NetrwLcd(dirname))' ) 345call assert_match ( 'unable to change directory to <' .. dirname .. '>' , msg ) 346call assert_equal ( cwd , getcwd ()) 347if prevdir 348call assert_equal ( cwd , w: netrw_prvdir ) 349call assert_true ( & l: modifiable ) 350else 351call assert_false ( & l: modifiable ) 352call assert_true ( & l: readonly ) 353call assert_false ( & l: number ) 354endif 355finally 356bwipe ! 357endtry 358endfor 359finally 360call setfperm ( dirname , 'rwx------' ) 361call delete ( dirname , 'd' ) 362endtry 363endfunc 364 365func Test_netrw_parse_remote_simple () 366let result = TestNetrwCaptureRemotePath ( 'scp://user@localhost:2222/test.txt' ) 367call assert_equal ( result . method , 'scp' ) 368call assert_equal ( result . user , 'user' ) 369call assert_equal ( result . machine , 'localhost' ) 370call assert_equal ( result . port , '2222' ) 371call assert_equal ( result . path , 'test.txt' ) 372endfunction 373 374"testing different combinations" 375func Test_netrw_parse_regular_usernames () 376 377" --- sample data for combinations ---" 378let usernames = [ "root" , "toor" , "user01" , "skillIssue" ] 379let methods = [ "scp" , "ssh" , "ftp" , "sftp" ] 380let hosts = [ "localhost" , "server.com" , "fit-workspaces.ksi.fit.cvut.cz" , "192.168.1.42" ] 381let ports = [ "" , "22" , "420" , "443" , "2222" , "1234" ] 382let dirs = [ "" , "somefolder/" , "path/to/the/bottom/of/the/world/please/send/help/" ] 383let files = [ "test.txt" , "tttt.vim" , "Makefile" ] 384 385call s: combine ( usernames , methods , hosts , ports , dirs , files ) 386 387endfunc 388 389"Host myserver 390" HostName 192.168.1.42 391" User alice 392func Test_netrw_parse_ssh_config_entries () 393let result = TestNetrwCaptureRemotePath ( 'scp://myserver//etc/nginx/nginx.conf' ) 394call assert_equal ( result . method , 'scp' ) 395call assert_equal ( result . user , '' ) 396call assert_equal ( result . machine , 'myserver' ) 397call assert_equal ( result . port , '' ) 398call assert_equal ( result . path , '/etc/nginx/nginx.conf' ) 399endfunction 400 401"username containing special-chars" 402func Test_netrw_parse_special_char_user () 403let result = TestNetrwCaptureRemotePath ( 'scp://user-01@localhost:2222/test.txt' ) 404call assert_equal ( result . method , 'scp' ) 405call assert_equal ( result . user , 'user-01' ) 406call assert_equal ( result . machine , 'localhost' ) 407call assert_equal ( result . port , '2222' ) 408call assert_equal ( result . path , 'test.txt' ) 409endfunction 410 411" Note: Test_netrw_a_empty_buffer_fastpath_wipe() should run before 412" any other tests that open a netrw buffer (e.g, :Explore). 413func Test_netrw_a_empty_buffer_fastpath_wipe () 414" SetUp() may have opened some buffers 415let previous = bufnr ( '$' ) 416let g: netrw_fastbrowse = 0 417call setline ( 1 , 'foobar' ) 418let bufnr = bufnr ( '%' ) 419tabnew 420Explore 421call search ( 'README.txt' , 'W' ) 422exe ":norm \<cr>" 423call assert_equal ( previous + 2 , bufnr ( '$' )) 424call assert_true ( bufexists ( bufnr )) 425bw 426 427unlet ! netrw_fastbrowse 428endfunction 429 430" Test UNC paths on windows 431func Test_netrw_check_UNC_paths () 432CheckMSWindows 433 434let test_paths = [ 435 \'\\Server2\Share\Test\Foo.txt' , 436 \'//Server2/Share/Test/Foo.txt' , 437 \'\\Server2\Share\Test\' , 438 \'//Server2/Share/Test/' , 439 \'\\wsl.localhost\Ubuntu\home\user\_vimrc' , 440 \'//wsl.localhost/Ubuntu/home/user/_vimrc' , 441 \'\\wsl.localhost\Ubuntu\home\user' , 442 \'//wsl.localhost/Ubuntu/home/user' ] 443 444" The paths must be interpreted as absolute ones 445for path in test_paths 446call assert_equal ( 447 \path , 448 \Test_NetrwFile ( path ), 449 \$ "UNC path: {path} misinterpreted") 450endfor 451 452endfunction 453 454" --------------------------------- 455" Testing file management functions 456" --------------------------------- 457 458" Browser directory creation 459func s: netrw_mkdir () 460 461" create a testdir in the fake $HOME 462call Test_NetrwMakeDir ( $ HOME , "NetrwMakeDir" ) 463 464" Check the test directory was created 465let test_dir = netrw#fs#PathJoin ( $ HOME , "NetrwMakeDir" ) 466call WaitForAssert ({ ->assert_true ( 467 \isdirectory ( test_dir ), 468 \"Unable to create a dir via s:NetrwMakeDir()" ) 469 \}) 470 471" remove the test directory 472call delete ( test_dir , 'd' ) 473endfunc 474 475func Test_netrw_mkdir_default () 476call SetShell ( 'default' ) 477call s: netrw_mkdir () 478endfunc 479 480func Test_netrw_mkdir_powershell () 481call SetShell ( 'powershell' ) 482call s: netrw_mkdir () 483endfunc 484 485func Test_netrw_mkdir_pwsh () 486call SetShell ( 'pwsh' ) 487call s: netrw_mkdir () 488endfunc 489 490func s: netrw_filecopy ( count = 1 ) 491" setup 492let marked_files = [] 493let home = expand ( '$HOME' ) 494let source_dir = netrw#fs#PathJoin ( home , "src" ) 495let target_dir = netrw#fs#PathJoin ( home , "target" ) 496 497call mkdir ( source_dir , "R" ) 498call mkdir ( target_dir , "R" ) 499 500for i in range ( a: count ) 501call add ( marked_files , $ "testfile{i}.txt") 502call writefile ( 503 \[ $ "NetrwMarkFileCopy test file {i}"], 504 \ netrw#fs#PathJoin(source_dir, marked_files[-1])) 505endfor 506 507" delegate 508call Test_NetrwMarkFileCopy ( source_dir , target_dir , marked_files ) 509 510" verify 511for file in marked_files 512call assert_equalfile ( 513 \netrw#fs#PathJoin ( source_dir , file ), 514 \netrw#fs#PathJoin ( target_dir , file ), 515 \"File copy failed for " . file ) 516endfor 517endfunc 518 519" Browser file copy 520func s: test_netrw_filecopy () 521 522" if shellslash is available, check both settings 523if exists ( '+shellslash' ) 524set shellslash & 525call s: netrw_filecopy ( 1 ) 526call s: netrw_filecopy ( 10 ) 527set shellslash ! 528endif 529 530call s: netrw_filecopy ( 1 ) 531call s: netrw_filecopy ( 10 ) 532 533endfunc 534 535func Test_netrw_filecopy_default () 536call SetShell ( 'default' ) 537call s: test_netrw_filecopy () 538endfunc 539 540func Test_netrw_filecopy_powershell () 541call SetShell ( 'powershell' ) 542call s: test_netrw_filecopy () 543endfunc 544 545func Test_netrw_filecopy_pwsh () 546call SetShell ( 'pwsh' ) 547call s: test_netrw_filecopy () 548endfunc 549 550" Browser recursive directory copy 551func s: netrw_dircopy ( count = 1 ) 552 553" setup 554let marked_dirname = "test_dir" 555let home = expand ( '$HOME' ) 556let marked_dir = netrw#fs#PathJoin ( home , marked_dirname ) 557let target_dir = netrw#fs#PathJoin ( home , "target" ) 558 559call mkdir ( marked_dir , "R" ) 560call mkdir ( target_dir , "R" ) 561 562let dir_content = [] 563for i in range ( a: count ) 564call add ( dir_content , $ "testfile{i}.txt") 565call writefile ( 566 \[ $ "NetrwMarkFileCopy test dir content {i}"], 567 \ netrw#fs#PathJoin(marked_dir, dir_content[-1])) 568endfor 569 570" delegate 571call Test_NetrwMarkFileCopy ( home , target_dir , [ marked_dirname ]) 572 573" verify 574for file in dir_content 575call assert_equalfile ( 576 \netrw#fs#PathJoin ( marked_dir , file ), 577 \netrw#fs#PathJoin ( target_dir , marked_dirname , file ), 578 \"File copy failed for " . file ) 579endfor 580 581endfunc 582 583func s: test_netrw_dircopy () 584 585" if shellslash is available, check both settings 586if exists ( '+shellslash' ) 587set shellslash & 588call s: netrw_dircopy ( 10 ) 589set shellslash ! 590endif 591 592call s: netrw_dircopy ( 10 ) 593 594endfunc 595 596func Test_netrw_dircopy_default () 597call SetShell ( 'default' ) 598call s: test_netrw_dircopy () 599endfunc 600 601func Test_netrw_dircopy_powershell () 602call SetShell ( 'powershell' ) 603call s: test_netrw_dircopy () 604endfunc 605 606func Test_netrw_dircopy_pwsh () 607call SetShell ( 'pwsh' ) 608call s: test_netrw_dircopy () 609endfunc 610 611" Copy file into the same directory with a different name 612func Test_netrw_dircopy_rename_default () 613call SetShell ( 'default' ) 614call Test_NetrwMarkFileCopy_SameDir () 615endfunc 616 617func Test_netrw_dircopy_rename_powershell () 618call SetShell ( 'powershell' ) 619call Test_NetrwMarkFileCopy_SameDir () 620endfunc 621 622func Test_netrw_dircopy_rename_pwsh () 623call SetShell ( 'pwsh' ) 624call Test_NetrwMarkFileCopy_SameDir () 625endfunc 626 627" Browser file move 628func s: netrw_filemove ( count = 1 ) 629" setup 630let marked_files = [] 631let source_dir = netrw#fs#PathJoin ( $ HOME , "src" ) 632let target_dir = netrw#fs#PathJoin ( $ HOME , "target" ) 633 634call mkdir ( source_dir , "R" ) 635call mkdir ( target_dir , "R" ) 636 637for i in range ( a: count ) 638call add ( marked_files , $ "testfile{i}.txt") 639call writefile ( 640 \[ $ "NetrwMarkFileMove test file {i}"], 641 \ netrw#fs#PathJoin(source_dir, marked_files[-1])) 642endfor 643 644" delegate 645call Test_NetrwMarkFileMove ( source_dir , target_dir , marked_files ) 646 647" verify 648for i in range ( a: count ) 649call assert_equal ( 650 \[ $ "NetrwMarkFileMove test file {i}"], 651 \ readfile(netrw#fs#PathJoin(target_dir, $"testfile{i}.txt")), 652 \ $"File move failed for testfile{i}.txt") 653endfor 654endfunc 655 656func s: test_netrw_filemove () 657 658" if shellslash is available, check both settings 659if exists ( '+shellslash' ) 660set shellslash & 661call s: netrw_filemove ( 10 ) 662set shellslash ! 663endif 664 665call s: netrw_filemove ( 10 ) 666 667endfunc 668 669func Test_netrw_filemove_default () 670call SetShell ( 'default' ) 671call s: test_netrw_filemove () 672endfunc 673 674func Test_netrw_filemove_powershell () 675call SetShell ( 'powershell' ) 676call s: test_netrw_filemove () 677endfunc 678 679func Test_netrw_filemove_pwsh () 680call SetShell ( 'pwsh' ) 681call s: test_netrw_filemove () 682endfunc 683 684func Test_netrw_reject_evil_hostname () 685let msg = execute ( ':e scp://x;touch RCE;x/dir/' ) 686let msg = split ( msg , "\n" )[ -1 ] 687call assert_match ( 'Rejecting invalid hostname' , msg ) 688endfunc 689 690func Test_netrw_hostname () 691let valid_hostnames = [ 692 \'localhost' , 693 \'_gateway' , 694 \'127.0.0.1' , 695 \'::1' , 696 \'0:0:0:0:0:0:0:1' , 697 \'user@localhost' , 698 \'usuario@127.0.0.1' , 699 \'utilisateur@::1' , 700 \'benutzer@0:0:0:0:0:0:0:1' , 701 \'localhost:22' , 702 \'127.0.0.1:80' , 703 \'[::1]:443' , 704 \'[0:0:0:0:0:0:0:1]:5432' , 705 \'user@localhost:22' , 706 \'usuario@127.0.0.1:80' , 707 \'utilisateur@[::1]:443' , 708 \'benutzer@[0:0:0:0:0:0:0:1]:5432' ] 709 710for hostname in valid_hostnames 711call assert_true ( Test_NetrwValidateHostname ( hostname ), $ "Valid hostname {hostname} was rejected") 712endfor 713endfunc 714 715func Test_netrw_FileUrlEdit_pipe_injection () 716CheckUnix 717CheckExecutable id 718let fname = 'Xtestfile' 719let url = 'file:///tmp/file.md%7C!id>' .. fname 720sil call netrw#FileUrlEdit ( url ) 721call assert_false ( filereadable ( fname ), 'Command injection via pipe in file URL' ) 722endfunc 723 724" The remote filename after '.' was allowed to contain shell metacharacters 725" and rode unescaped into the tempfile name passed to sftp/file_cmd, giving a 726" shell injection on :e sftp://host/foo.txt;<cmd>. 727func Test_netrw_tempfile_suffix_injection () 728CheckUnix 729CheckExecutable id 730let save_sftp = g: netrw_sftp_cmd 731let save_file = exists ( 'g:netrw_file_cmd' ) ? g: netrw_file_cmd : v: null 732let g: netrw_sftp_cmd = 'true' 733let g: netrw_file_cmd = 'true' 734let fname = 'Xrce_marker' 735try 736call delete ( fname ) 737sil ! call netrw#NetRead ( 2 , 'sftp://localhost/foo.txt;id>' .. fname ) 738call assert_false ( filereadable ( fname ), 'Command injection via sftp:// tempfile suffix' ) 739 740call delete ( fname ) 741sil ! call netrw#NetRead ( 2 , 'file://localhost/foo.txt;id>' .. fname ) 742call assert_false ( filereadable ( fname ), 'Command injection via file:// tempfile suffix' ) 743finally 744call delete ( fname ) 745let g: netrw_sftp_cmd = save_sftp 746if save_file is v: null 747unlet ! g: netrw_file_cmd 748else 749let g: netrw_file_cmd = save_file 750endif 751endtry 752endfunc 753 754func Test_netrw_RFC2396 () 755let fname = 'a%20b' 756call assert_equal ( 'a b' , netrw#RFC2396 ( fname )) 757endfunc 758 759func Test_netrw_Home_tilde () 760Explore ~ 761call assert_match ( 'Netrw Directory Listing' , getline ( 2 )) 762bw ! 763endfunc 764 765func Test_netrw_unmark_all () 766call Test_NetrwUnMarkFile () 767endfunc 768 769" Creating a bookmark from a marked file should use b:netrw_curdir as head directory 770func Test_netrw_bookmark_marked_file () 771let save_keepdir = g: netrw_keepdir 772let save_workdir = getcwd () 773let save_bookmarklist = exists ( 'g:netrw_bookmarklist' ) ? g: netrw_bookmarklist : v: null 774 775" Make sure Vim's working directory diverge from Netrw's 776let g: netrw_keepdir = 1 777let g: netrw_bookmarklist = [] 778let test_workdir = getcwd () . '/Xtest_workdir' 779let test_netrw_curdir = test_workdir . '/Xtest_netrw_curdir' 780call mkdir ( test_netrw_curdir , 'p' ) 781call writefile ([], test_netrw_curdir . '/test_file' ) 782 783execute 'cd ' . test_workdir 784call Test_MakeBookmark ( test_netrw_curdir , 'test_file' ) 785 786" Bookmark paths should be absolute and normalized to prevent duplicates on win32 787let expected_path = netrw#fs#AbsPath ( test_netrw_curdir . '/test_file' ) 788if has ( 'win32' ) 789let expected_path = substitute ( expected_path , '\\' , '/' , 'ge' ) 790endif 791let expected_path = simplify ( expected_path ) 792 793call assert_equal ( 1 , len ( g: netrw_bookmarklist )) 794call assert_equal ( expected_path , g: netrw_bookmarklist [ 0 ]) 795call assert_true ( isabsolutepath ( g: netrw_bookmarklist [ 0 ]), 'Bookmark paths should be absolute' ) 796 797" Tear down 798execute 'cd ' . save_workdir 799call delete ( test_workdir , 'rf' ) 800 801let g: netrw_keepdir = save_keepdir 802if save_bookmarklist is v: null 803unlet ! g: netrw_bookmarklist 804else 805let g: netrw_bookmarklist = save_bookmarklist 806endif 807 808bw ! 809endfunc 810 811" Typing gb or mB without a count should prompt 812" for a bookmark number through an inputlist(). 813" Expected dialog output (gb): # | Goto Bookmark: 814" 1| /foo/bar/baz 815" 816" Expected dialog output (mB): # | Delete Bookmark: 817" 1| /foo/bar/baz 818func Test_netrw_bookmark_goto_delete_prompt () 819let save_home = g: netrw_home 820let save_bookmarklist = exists ( 'g:netrw_bookmarklist' ) ? g: netrw_bookmarklist : v: null 821 822let g: netrw_home = getcwd () 823let g: netrw_bookmarklist = [ '/foo/bar/baz' ] 824Explore . 825 826" Inject 'q' to cancel the inputlist() prompt 827call feedkeys ( 'q' , 't' ) 828let dialog_out = execute ( 'normal gb' ) 829call assert_match ( 'Goto Bookmark:' , dialog_out ) 830 831call feedkeys ( 'q' , 't' ) 832let dialog_out = execute ( 'normal mB' ) 833call assert_match ( 'Delete Bookmark:' , dialog_out ) 834 835" Tear down 836call delete ( g: netrw_home . '/.netrwbook' ) 837call delete ( g: netrw_home . '/.netrwhist' ) 838let g: netrw_home = save_home 839if save_bookmarklist is v: null 840unlet ! g: netrw_bookmarklist 841else 842let g: netrw_bookmarklist = save_bookmarklist 843endif 844 845bw ! 846endfunc 847 848func Test_netrw_markings_match_pattern_rebuild () 849call Test_NetrwMarkFile_match_pattern_rebuild () 850endfunc 851 852func Test_netrw_mf_command_injection () 853CheckUnix 854CheckExecutable touch 855let path = tempname () 856let fname = 'x" . execute("silent! !touch poc") . "' 857call mkdir ( path , 'R' ) 858let _cwd = getcwd () 859exe "cd " path 860call writefile ([], fname ) 861Explore . 862call search ( '^x' ) 863: norm mf 864: norm mf 865call assert_false ( filereadable ( 'poc' ), 'Command injection via mf command' ) 866exe "cd " _cwd 867bw ! 868endfunc 869 870function Test_netrw_NetrwMaps_CR_dirname () 871CheckNotMSWindows 872 873let tmpdir = tempname () . '/evil<CR>:let g:netrw_pwn=1<CR>' 874call mkdir ( tmpdir , 'pR' ) 875call assert_true ( isdirectory ( tmpdir )) 876exe ":Explore " tmpdir 877" Fire D 878" If the commands are injected successfully, 879" this fails with 880" Vim(let):E488: Trailing characters: \ @ command line script 881call feedkeys ( "D\<C-c>\<C-c>" , "xt" ) 882call assert_false ( exists ( "g:netrw_pwn" )) 883 884unlet ! g: netrw_pwn 885bw ! 886endfunction 887 888func Test_netrw_injection () 889let g: netrw_home = getcwd () 890let savefile = g: netrw_home . '/.netrwhist' 891let g: netrw_dirhistmax = 10 892let g: netrw_dirhistcnt = 1 893let g: netrw_dirhist_1 = "x'|let g:injected = 1|let y='z" 894call delete ( savefile ) 895try 896call netrw#Call ( 'NetrwBookHistSave' ) 897call assert_true ( filereadable ( savefile ), savefile . ' must be written' ) 898unlet g: netrw_dirhist_1 899execute 'source ' . fnameescape ( savefile ) 900call assert_false ( exists ( "g:injected" ), 'injected statement must not execute' ) 901call assert_equal ( "x'|let g:injected = 1|let y='z" , g: netrw_dirhist_1 , 'dirname must round-trip' ) 902finally 903call delete ( savefile ) 904unlet ! g: netrw_home g: netrw_dirhistmax g: netrw_dirhistcnt g: netrw_dirhist_1 g: injected 905endtry 906endfunc 907 908" Deleting a file whose name contains an Ex command separator must not let the 909" name inject commands into the :execute in s:NetrwLocalRmFile(). 910func Test_netrw_local_rm_injection () 911CheckUnix 912let dir = getcwd () . '/Xnetrwrm' 913let fname = "x|let g:injected = 1" 914call mkdir ( dir , 'pR' ) 915call writefile ([], dir . '/' . fname ) 916try 917call netrw#Call ( 'NetrwLocalRmFile' , dir , fname , 1 ) 918call assert_false ( exists ( 'g:injected' ), 'filename must not inject Ex commands' ) 919" The file is removed before the sink, so its absence also confirms the 920" vulnerable code path was actually exercised (not skipped on an error). 921call assert_false ( filereadable ( dir . '/' . fname ), 'crafted file must be deleted' ) 922finally 923call delete ( dir . '/' . fname ) 924unlet ! g: injected 925endtry 926endfunc 927 928func Test_netrw_forward_slashes () 929Explore 930call assert_notmatch ( '\\' , b: netrw_curdir ) 931bw ! 932Explore . 933call assert_notmatch ( '\\' , b: netrw_curdir ) 934bw ! 935endfunc 936 937" Selecting a file whose name is a single backslash 938func Test_netrw_open_backslash_file () 939CheckUnix 940 941let dir = getcwd () . '/Xbslash' 942let fname = dir . '/\' 943call mkdir ( dir , 'pR' ) 944call writefile ([ 'backslash file content' ], fname ) 945call assert_true ( filereadable ( fname )) 946 947" list the directory and move onto the '\' entry 948exe 'Explore ' . dir 949call assert_true ( search ( '^\\$' , 'w' ) > 0 ) 950 951" open it 952exe "normal \<CR>" 953 954call assert_equal ( '\' , expand ( '%:t' )) 955call assert_equal ([ 'backslash file content' ], getline ( 1 , '$' )) 956 957bw ! 958endfunc 959 960" :Explore without a [dir] argument should open the dir of the current file 961func Test_netrw_open_no_dir_arg () 962let dir = tempname () 963call mkdir ( dir , 'pR' ) 964call writefile ([], dir . '/foo' ) 965exe 'edit ' . dir . '/foo' 966Explore 967call assert_equal ( fnamemodify ( dir , ':p' ), fnamemodify ( b: netrw_curdir , ':p' )) 968bw ! 969endfunc 970 971func Test_netrw_home_setting () 972let save_home = get ( g:, 'netrw_home' , '' ) 973let dir = fnamemodify ( 'XnetrwHome' , ':p' ) 974let vimdir = fnamemodify ( 'XvimDir' , ':p' ) 975let subdir = fnamemodify ( 'XnetrwHomeParent/XnetrwHome' , ':p' ) 976if has ( 'win32' ) 977let dir = substitute ( dir , '/' , '\\' , 'g' ) 978let vimdir = substitute ( vimdir , '/' , '\\' , 'g' ) 979let subdir = substitute ( subdir , '/' , '\\' , 'g' ) 980endif 981let save_myvimdir = getenv ( 'MYVIMDIR' ) 982let $ MYVIMDIR = vimdir 983 984unlet ! g: netrw_home 985if has ( 'nvim' ) 986let expected = netrw#fs#PathJoin ( stdpath ( 'state' ), 'netrw' ) 987if has ( 'win32' ) 988let expected = substitute ( expected , '/' , '\\' , 'g' ) 989endif 990call assert_equal ( expected , Test_NetrwHome ()) 991else 992" without the setting $MYVIMDIR is used 993call assert_equal ( vimdir , Test_NetrwHome ()) 994endif 995 996let g: netrw_home = dir 997call assert_equal ( dir , Test_NetrwHome ()) 998call assert_true ( isdirectory ( dir )) 999 1000let g: netrw_home = subdir 1001call assert_equal ( subdir , Test_NetrwHome ()) 1002call assert_true ( isdirectory ( subdir )) 1003 1004" the value is expanded 1005let $ NETRW_TEST_HOME = dir 1006let g: netrw_home = '$NETRW_TEST_HOME' 1007call assert_equal ( dir , Test_NetrwHome ()) 1008 1009call delete ( dir , 'd' ) 1010call delete ( vimdir , 'd' ) 1011call delete ( subdir , 'd' ) 1012call delete ( fnamemodify ( subdir , ':h' ), 'd' ) 1013unlet $ NETRW_TEST_HOME 1014call setenv ( 'MYVIMDIR' , save_myvimdir ) 1015if empty ( save_home ) 1016unlet ! g: netrw_home 1017else 1018let g: netrw_home = save_home 1019endif 1020endfunc 1021 1022" vim:ts=8 sts=2 sw=2 et