1" Test for the xxd command 2if empty($XXD) && executable('..\xxd\xxd.exe') 3 let s:xxd_cmd = '..\xxd\xxd.exe' 4elseif empty($XXD) || !executable($XXD) 5 throw 'Skipped: xxd program missing' 6else 7 let s:xxd_cmd = $XXD 8endif 9 10func PrepareBuffer(lines) 11 new 12 call append(0, a:lines) 13 $d 14endfunc 15 16func s:Mess(counter) 17 return printf("Failed xxd test %d:", a:counter) 18endfunc 19 20func Test_xxd() 21 call PrepareBuffer(range(1,30)) 22 set ff=unix 23 w! XXDfile 24 25 " Test 1: simple, filter the result through xxd 26 let s:test = 1 27 exe '%!' . s:xxd_cmd . ' %' 28 let expected = [ 29 \ '00000000: 310a 320a 330a 340a 350a 360a 370a 380a 1.2.3.4.5.6.7.8.', 30 \ '00000010: 390a 3130 0a31 310a 3132 0a31 330a 3134 9.10.11.12.13.14', 31 \ '00000020: 0a31 350a 3136 0a31 370a 3138 0a31 390a .15.16.17.18.19.', 32 \ '00000030: 3230 0a32 310a 3232 0a32 330a 3234 0a32 20.21.22.23.24.2', 33 \ '00000040: 350a 3236 0a32 370a 3238 0a32 390a 3330 5.26.27.28.29.30', 34 \ '00000050: 0a .'] 35 call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) 36 37 " Test 2: reverse the result 38 let s:test += 1 39 exe '%!' . s:xxd_cmd . ' -r' 40 call assert_equal(map(range(1,30), {v,c -> string(c)}), getline(1,'$'), s:Mess(s:test)) 41 42 " Test 3: Skip the first 0x30 bytes 43 let s:test += 1 44 for arg in ['-s 0x30', '-s0x30', '-s+0x30', '-skip 0x030', '-seek 0x30', '-seek +0x30 --'] 45 exe '%!' . s:xxd_cmd . ' ' . arg . ' %' 46 call assert_equal(expected[3:], getline(1,'$'), s:Mess(s:test)) 47 endfor 48 49 " Test 4: Skip the first 30 bytes 50 let s:test += 1 51 for arg in ['-s -0x31', '-s-0x31'] 52 exe '%!' . s:xxd_cmd . ' ' . arg . ' %' 53 call assert_equal(expected[2:], getline(1,'$'), s:Mess(s:test)) 54 endfor 55 56 " The following tests use the xxd man page. 57 " For these tests to pass, the fileformat must be "unix". 58 let man_copy = 'Xxd.1' 59 let man_page = '../../runtime/doc/xxd.1' 60 if has('win32') && !filereadable(man_page) 61 let man_page = '../../doc/xxd.1' 62 endif 63 %d 64 exe '0r ' man_page '| set ff=unix | $d | w' man_copy '| bwipe!' man_copy 65 66 " Test 5: Print 120 bytes as continuous hexdump with 20 octets per line 67 let s:test += 1 68 %d 69 exe '0r! ' . s:xxd_cmd . ' -l 120 -ps -c20 ' . man_copy 70 $d 71 let expected = [ 72 \ '2e54482058584420312022417567757374203139', 73 \ '39362220224d616e75616c207061676520666f72', 74 \ '20787864220a2e5c220a2e5c222032317374204d', 75 \ '617920313939360a2e5c22204d616e2070616765', 76 \ '20617574686f723a0a2e5c2220202020546f6e79', 77 \ '204e7567656e74203c746f6e79407363746e7567'] 78 call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) 79 80 " Test 6: Print the date from xxd.1 81 let s:test += 1 82 for arg in ['-l 13', '-l13', '-len 13'] 83 %d 84 exe '0r! ' . s:xxd_cmd . ' -s 0x36 ' . arg . ' -cols 13 ' . man_copy 85 $d 86 call assert_equal('00000036: 3231 7374 204d 6179 2031 3939 36 21st May 1996', getline(1), s:Mess(s:test)) 87 endfor 88 89 " Cleanup after tests 5 and 6 90 call delete(man_copy) 91 92 " Test 7: Print C include 93 let s:test += 1 94 call writefile(['TESTabcd09'], 'XXDfile') 95 %d 96 exe '0r! ' . s:xxd_cmd . ' -i XXDfile' 97 $d 98 let expected =<< trim [CODE] 99 unsigned char XXDfile[] = { 100 0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a 101 }; 102 unsigned int XXDfile_len = 11; 103 [CODE] 104 105 call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) 106 107 " Test 8: Print C include capitalized 108 let s:test += 1 109 for arg in ['-C', '-capitalize'] 110 call writefile(['TESTabcd09'], 'XXDfile') 111 %d 112 exe '0r! ' . s:xxd_cmd . ' -i ' . arg . ' XXDfile' 113 $d 114 let expected =<< trim [CODE] 115 unsigned char XXDFILE[] = { 116 0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a 117 }; 118 unsigned int XXDFILE_LEN = 11; 119 [CODE] 120 call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) 121 endfor 122 123 " Test 9: Create a file with containing a single 'A' 124 let s:test += 1 125 call delete('XXDfile') 126 bwipe! XXDfile 127 if has('unix') 128 call system('echo "010000: 41"|' . s:xxd_cmd . ' -r -s -0x10000 > XXDfile') 129 else 130 call writefile(['010000: 41'], 'Xinput') 131 silent exe '!' . s:xxd_cmd . ' -r -s -0x10000 < Xinput > XXDfile' 132 call delete('Xinput') 133 endif 134 call PrepareBuffer(readfile('XXDfile')[0]) 135 call assert_equal('A', getline(1), s:Mess(s:test)) 136 call delete('XXDfile') 137 138 " Test 10: group with 4 octets 139 let s:test += 1 140 for arg in ['-g 4', '-group 4', '-g4'] 141 call writefile(['TESTabcd09'], 'XXDfile') 142 %d 143 exe '0r! ' . s:xxd_cmd . ' ' . arg . ' XXDfile' 144 $d 145 let expected = ['00000000: 54455354 61626364 30390a TESTabcd09.'] 146 call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) 147 call delete('XXDfile') 148 endfor 149 150 " Test 11: reverse with CR, hex upper, Postscript style with a TAB 151 let s:test += 1 152 call writefile([" 54455354\t610B6364 30390A TESTa\0x0bcd09.\r"], 'Xinput') 153 silent exe '!' . s:xxd_cmd . ' -r -p < Xinput > XXDfile' 154 let blob = readfile('XXDfile', 'B') 155 call assert_equal(0z54455354.610B6364.30390A, blob) 156 call delete('Xinput') 157 call delete('XXDfile') 158 159 " Test 12: reverse with seek 160 let s:test += 1 161 call writefile(["00000000: 54455354\t610B6364 30390A TESTa\0x0bcd09.\r"], 'Xinput') 162 silent exe '!' . s:xxd_cmd . ' -r -seek 5 < Xinput > XXDfile' 163 let blob = readfile('XXDfile', 'B') 164 call assert_equal(0z0000000000.54455354.610B6364.30390A, blob) 165 call delete('Xinput') 166 call delete('XXDfile') 167 168 " Test 13: simple, decimal offset 169 call PrepareBuffer(range(1,30)) 170 set ff=unix 171 w! XXDfile 172 let s:test += 1 173 exe '%!' . s:xxd_cmd . ' -d %' 174 let expected = [ 175 \ '00000000: 310a 320a 330a 340a 350a 360a 370a 380a 1.2.3.4.5.6.7.8.', 176 \ '00000016: 390a 3130 0a31 310a 3132 0a31 330a 3134 9.10.11.12.13.14', 177 \ '00000032: 0a31 350a 3136 0a31 370a 3138 0a31 390a .15.16.17.18.19.', 178 \ '00000048: 3230 0a32 310a 3232 0a32 330a 3234 0a32 20.21.22.23.24.2', 179 \ '00000064: 350a 3236 0a32 370a 3238 0a32 390a 3330 5.26.27.28.29.30', 180 \ '00000080: 0a .'] 181 call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) 182 183 " Test 14: grouping with -d 184 let s:test += 1 185 let expected = [ 186 \ '00000000: 310a320a 330a340a 350a360a 370a380a 1.2.3.4.5.6.7.8.', 187 \ '00000016: 390a3130 0a31310a 31320a31 330a3134 9.10.11.12.13.14', 188 \ '00000032: 0a31350a 31360a31 370a3138 0a31390a .15.16.17.18.19.', 189 \ '00000048: 32300a32 310a3232 0a32330a 32340a32 20.21.22.23.24.2', 190 \ '00000064: 350a3236 0a32370a 32380a32 390a3330 5.26.27.28.29.30', 191 \ '00000080: 0a .'] 192 for arg in ['-g 4', '-group 4', '-g4'] 193 exe '%!' . s:xxd_cmd . ' ' . arg . ' -d %' 194 call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) 195 endfor 196 197 " Test 15: cols with decimal offset: -c 21 -d 198 let s:test += 1 199 let expected = [ 200 \ '00000000: 310a 320a 330a 340a 350a 360a 370a 380a 390a 3130 0a 1.2.3.4.5.6.7.8.9.10.', 201 \ '00000021: 3131 0a31 320a 3133 0a31 340a 3135 0a31 360a 3137 0a 11.12.13.14.15.16.17.', 202 \ '00000042: 3138 0a31 390a 3230 0a32 310a 3232 0a32 330a 3234 0a 18.19.20.21.22.23.24.', 203 \ '00000063: 3235 0a32 360a 3237 0a32 380a 3239 0a33 300a 25.26.27.28.29.30.'] 204 exe '%!' . s:xxd_cmd . ' -c 21 -d %' 205 call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) 206 207 " TODO: 208 " -o -offset 209 210 %d 211 bwipe! 212 call delete('XXDfile') 213endfunc 214 215" Various ways with wrong arguments that trigger the usage output. 216func Test_xxd_usage() 217 for arg in ['-c', '-g', '-o', '-s', '-l', '-X', 'one two three'] 218 new 219 exe 'r! ' . s:xxd_cmd . ' ' . arg 220 call assert_match("Usage:", join(getline(1, 3))) 221 bwipe! 222 endfor 223endfunc 224 225func Test_xxd_version() 226 new 227 exe 'r! ' . s:xxd_cmd . ' -v' 228 call assert_match("xxd V1.10 .* by Juergen Weigert", join(getline(1, 3))) 229 bwipe! 230endfunc 231