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