test_plugin_tar.vim (3526B)
1 CheckExecutable tar 2 CheckNotMSWindows 3 4 runtime plugin/tarPlugin.vim 5 6 func s:CopyFile(source) 7 if !filecopy($"samples/{a:source}", "X.tar") 8 call assert_report($"Can't copy samples/{a:source}") 9 endif 10 endfunc 11 12 func Test_tar_basic() 13 call s:CopyFile("sample.tar") 14 defer delete("X.tar") 15 defer delete("./testtar", 'rf') 16 e X.tar 17 18 "## Check header 19 call assert_match('^" tar\.vim version v\d\+', getline(1)) 20 call assert_match('^" Browsing tarfile .*/X.tar', getline(2)) 21 call assert_match('^" Select a file with cursor and press ENTER, "x" to extract a file', getline(3)) 22 call assert_match('^$', getline(4)) 23 call assert_match('testtar/', getline(5)) 24 call assert_match('testtar/file1.txt', getline(6)) 25 26 "## Check ENTER on header 27 :1 28 exe ":normal \<cr>" 29 call assert_equal("X.tar", @%) 30 31 "## Check ENTER on file 32 :6 33 exe ":normal \<cr>" 34 call assert_equal("tarfile::testtar/file1.txt", @%) 35 36 37 "## Check editing file 38 "## Note: deleting entries not supported on BSD 39 if has("mac") 40 return 41 endif 42 if has("bsd") 43 return 44 endif 45 s/.*/some-content/ 46 call assert_equal("some-content", getline(1)) 47 w! 48 call assert_equal("tarfile::testtar/file1.txt", @%) 49 bw! 50 close 51 bw! 52 53 e X.tar 54 :6 55 exe "normal \<cr>" 56 call assert_equal("some-content", getline(1)) 57 bw! 58 close 59 60 "## Check extracting file 61 :5 62 normal x 63 call assert_true(filereadable("./testtar/file1.txt")) 64 bw! 65 endfunc 66 67 func Test_tar_evil() 68 call s:CopyFile("evil.tar") 69 defer delete("X.tar") 70 defer delete("./etc", 'rf') 71 e X.tar 72 73 "## Check header 74 call assert_match('^" tar\.vim version v\d\+', getline(1)) 75 call assert_match('^" Browsing tarfile .*/X.tar', getline(2)) 76 call assert_match('^" Select a file with cursor and press ENTER, "x" to extract a file', getline(3)) 77 call assert_match('^" Note: Path Traversal Attack detected', getline(4)) 78 call assert_match('^$', getline(5)) 79 call assert_match('/etc/ax-pwn', getline(6)) 80 81 "## Check ENTER on header 82 :1 83 exe ":normal \<cr>" 84 call assert_equal("X.tar", @%) 85 call assert_equal(1, b:leading_slash) 86 87 "## Check ENTER on file 88 :6 89 exe ":normal \<cr>" 90 call assert_equal(1, b:leading_slash) 91 call assert_equal("tarfile::/etc/ax-pwn", @%) 92 93 94 "## Check editing file 95 "## Note: deleting entries not supported on BSD 96 if has("mac") 97 return 98 endif 99 if has("bsd") 100 return 101 endif 102 s/.*/none/ 103 call assert_equal("none", getline(1)) 104 w! 105 call assert_equal(1, b:leading_slash) 106 call assert_equal("tarfile::/etc/ax-pwn", @%) 107 bw! 108 close 109 bw! 110 111 " Writing was aborted 112 e X.tar 113 call assert_match('^" Note: Path Traversal Attack detected', getline(4)) 114 :6 115 exe "normal \<cr>" 116 call assert_equal("something", getline(1)) 117 bw! 118 close 119 120 "## Check extracting file 121 :5 122 normal x 123 call assert_true(filereadable("./etc/ax-pwn")) 124 125 bw! 126 endfunc 127 128 func Test_tar_path_traversal_with_nowrapscan() 129 call s:CopyFile("evil.tar") 130 defer delete("X.tar") 131 " Make sure we still find the tar warning (or leading slashes) even when 132 " wrapscan is off 133 set nowrapscan 134 e X.tar 135 136 "## Check header 137 call assert_match('^" tar\.vim version v\d\+', getline(1)) 138 call assert_match('^" Browsing tarfile .*/X.tar', getline(2)) 139 call assert_match('^" Select a file with cursor and press ENTER, "x" to extract a file', getline(3)) 140 call assert_match('^" Note: Path Traversal Attack detected', getline(4)) 141 call assert_match('^$', getline(5)) 142 call assert_match('/etc/ax-pwn', getline(6)) 143 144 call assert_equal(1, b:leading_slash) 145 146 bw! 147 endfunc