blob: 7d0cbabefafdeb40657723ffa1ad48b6734b88d2 [file] [log] [blame]
Bram Moolenaar29f9ed22018-04-10 19:20:31 +02001" Test for the xxd command
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002
Bram Moolenaar6995c0a2018-04-10 21:46:10 +02003if empty($XXD) && executable('..\xxd\xxd.exe')
4 let s:xxd_cmd = '..\xxd\xxd.exe'
5elseif empty($XXD) || !executable($XXD)
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02006 throw 'Skipped: xxd program missing'
Bram Moolenaar6995c0a2018-04-10 21:46:10 +02007else
8 let s:xxd_cmd = $XXD
Bram Moolenaar29f9ed22018-04-10 19:20:31 +02009endif
10
Bram Moolenaar1e115362019-01-09 23:01:02 +010011func PrepareBuffer(lines)
Bram Moolenaar164268d2018-04-10 20:06:17 +020012 new
13 call append(0, a:lines)
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020014 $d
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020015endfunc
16
Bram Moolenaar1e115362019-01-09 23:01:02 +010017func s:Mess(counter)
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020018 return printf("Failed xxd test %d:", a:counter)
19endfunc
20
Bram Moolenaar1e115362019-01-09 23:01:02 +010021func Test_xxd()
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020022 call PrepareBuffer(range(1,30))
Bram Moolenaar6995c0a2018-04-10 21:46:10 +020023 set ff=unix
Bram Moolenaar970f5d32019-01-25 21:52:17 +010024 w! XXDfile
Bram Moolenaar164268d2018-04-10 20:06:17 +020025
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020026 " Test 1: simple, filter the result through xxd
27 let s:test = 1
Bram Moolenaar6995c0a2018-04-10 21:46:10 +020028 exe '%!' . s:xxd_cmd . ' %'
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020029 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))
Bram Moolenaar164268d2018-04-10 20:06:17 +020037
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020038 " Test 2: reverse the result
39 let s:test += 1
Bram Moolenaar6995c0a2018-04-10 21:46:10 +020040 exe '%!' . s:xxd_cmd . ' -r'
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020041 call assert_equal(map(range(1,30), {v,c -> string(c)}), getline(1,'$'), s:Mess(s:test))
42
Bram Moolenaar970f5d32019-01-25 21:52:17 +010043 " Test 3: Skip the first 0x30 bytes
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020044 let s:test += 1
Bram Moolenaar970f5d32019-01-25 21:52:17 +010045 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
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020049
50 " Test 4: Skip the first 30 bytes
51 let s:test += 1
Bram Moolenaar970f5d32019-01-25 21:52:17 +010052 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
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020056
Bram Moolenaar0eb220c2019-01-27 14:41:43 +010057 " 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
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020067 " Test 5: Print 120 bytes as continuous hexdump with 20 octets per line
68 let s:test += 1
69 %d
Bram Moolenaar0eb220c2019-01-27 14:41:43 +010070 exe '0r! ' . s:xxd_cmd . ' -l 120 -ps -c20 ' . man_copy
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020071 $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))
Bram Moolenaar164268d2018-04-10 20:06:17 +020080
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020081 " Test 6: Print the date from xxd.1
82 let s:test += 1
Bram Moolenaar970f5d32019-01-25 21:52:17 +010083 for arg in ['-l 13', '-l13', '-len 13']
84 %d
Bram Moolenaar0eb220c2019-01-27 14:41:43 +010085 exe '0r! ' . s:xxd_cmd . ' -s 0x36 ' . arg . ' -cols 13 ' . man_copy
Bram Moolenaar970f5d32019-01-25 21:52:17 +010086 $d
87 call assert_equal('00000036: 3231 7374 204d 6179 2031 3939 36 21st May 1996', getline(1), s:Mess(s:test))
88 endfor
Bram Moolenaar164268d2018-04-10 20:06:17 +020089
Bram Moolenaar0eb220c2019-01-27 14:41:43 +010090 " Cleanup after tests 5 and 6
91 call delete(man_copy)
92
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020093 " Test 7: Print C include
94 let s:test += 1
95 call writefile(['TESTabcd09'], 'XXDfile')
96 %d
Bram Moolenaar6995c0a2018-04-10 21:46:10 +020097 exe '0r! ' . s:xxd_cmd . ' -i XXDfile'
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020098 $d
Bram Moolenaarc79745a2019-05-20 22:12:34 +020099 let expected =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200100 unsigned char XXDfile[] = {
101 0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a
102 };
103 unsigned int XXDfile_len = 11;
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200104 [CODE]
105
Bram Moolenaar29f9ed22018-04-10 19:20:31 +0200106 call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
Bram Moolenaar164268d2018-04-10 20:06:17 +0200107
Bram Moolenaar29f9ed22018-04-10 19:20:31 +0200108 " Test 8: Print C include capitalized
109 let s:test += 1
Bram Moolenaar970f5d32019-01-25 21:52:17 +0100110 for arg in ['-C', '-capitalize']
111 call writefile(['TESTabcd09'], 'XXDfile')
112 %d
113 exe '0r! ' . s:xxd_cmd . ' -i ' . arg . ' XXDfile'
114 $d
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200115 let expected =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200116 unsigned char XXDFILE[] = {
117 0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a
118 };
119 unsigned int XXDFILE_LEN = 11;
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200120 [CODE]
Bram Moolenaar970f5d32019-01-25 21:52:17 +0100121 call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
122 endfor
Bram Moolenaar164268d2018-04-10 20:06:17 +0200123
124 " Test 9: Create a file with containing a single 'A'
Bram Moolenaar29f9ed22018-04-10 19:20:31 +0200125 let s:test += 1
126 call delete('XXDfile')
Bram Moolenaar164268d2018-04-10 20:06:17 +0200127 bwipe! XXDfile
Bram Moolenaar6995c0a2018-04-10 21:46:10 +0200128 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
Bram Moolenaar29f9ed22018-04-10 19:20:31 +0200135 call PrepareBuffer(readfile('XXDfile')[0])
136 call assert_equal('A', getline(1), s:Mess(s:test))
137 call delete('XXDfile')
Bram Moolenaar970f5d32019-01-25 21:52:17 +0100138
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
Bram Moolenaar203651b2019-01-26 14:11:19 +0100151 " 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
Bram Moolenaar363d6142020-05-30 20:50:25 +0200169 " 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
Erik Auerswalda00e6222022-01-13 17:42:28 +0000208 " Test 16: -o -offset
209 let s:test += 1
210 let expected = [
211 \ '0000000f: 310a 320a 330a 340a 350a 360a 370a 380a 1.2.3.4.5.6.7.8.',
212 \ '0000001f: 390a 3130 0a31 310a 3132 0a31 330a 3134 9.10.11.12.13.14',
213 \ '0000002f: 0a31 350a 3136 0a31 370a 3138 0a31 390a .15.16.17.18.19.',
214 \ '0000003f: 3230 0a32 310a 3232 0a32 330a 3234 0a32 20.21.22.23.24.2',
215 \ '0000004f: 350a 3236 0a32 370a 3238 0a32 390a 3330 5.26.27.28.29.30',
216 \ '0000005f: 0a .']
217 for arg in ['-o 15', '-offset 15', '-o15']
218 exe '%!' . s:xxd_cmd . ' ' . arg . ' %'
219 call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
220 endfor
Bram Moolenaar970f5d32019-01-25 21:52:17 +0100221
Bram Moolenaar29f9ed22018-04-10 19:20:31 +0200222 %d
Bram Moolenaar253ea9f2020-06-10 14:21:20 +0200223 bwipe!
224 call delete('XXDfile')
Bram Moolenaar29f9ed22018-04-10 19:20:31 +0200225endfunc
Bram Moolenaar970f5d32019-01-25 21:52:17 +0100226
DungSaga47810462021-10-22 12:55:42 +0100227func Test_xxd_patch()
Bram Moolenaar5a5c1112021-10-22 15:11:37 +0100228 let cmd1 = 'silent !' .. s:xxd_cmd .. ' -r Xxxdin Xxxdfile'
229 let cmd2 = 'silent !' .. s:xxd_cmd .. ' -g1 Xxxdfile > Xxxdout'
DungSaga47810462021-10-22 12:55:42 +0100230 call writefile(["2: 41 41", "8: 42 42"], 'Xxxdin')
231 call writefile(['::::::::'], 'Xxxdfile')
Bram Moolenaar5a5c1112021-10-22 15:11:37 +0100232 exe cmd1
233 exe cmd2
DungSaga47810462021-10-22 12:55:42 +0100234 call assert_equal(['00000000: 3a 3a 41 41 3a 3a 3a 3a 42 42 ::AA::::BB'], readfile('Xxxdout'))
235
236 call writefile(["2: 43 43 ", "8: 44 44"], 'Xxxdin')
Bram Moolenaar5a5c1112021-10-22 15:11:37 +0100237 exe cmd1
238 exe cmd2
DungSaga47810462021-10-22 12:55:42 +0100239 call assert_equal(['00000000: 3a 3a 43 43 3a 3a 3a 3a 44 44 ::CC::::DD'], readfile('Xxxdout'))
240
241 call writefile(["2: 45 45 ", "8: 46 46"], 'Xxxdin')
Bram Moolenaar5a5c1112021-10-22 15:11:37 +0100242 exe cmd1
243 exe cmd2
DungSaga47810462021-10-22 12:55:42 +0100244 call assert_equal(['00000000: 3a 3a 45 45 3a 3a 3a 3a 46 46 ::EE::::FF'], readfile('Xxxdout'))
245
246 call writefile(["2: 41 41", "08: 42 42"], 'Xxxdin')
247 call writefile(['::::::::'], 'Xxxdfile')
Bram Moolenaar5a5c1112021-10-22 15:11:37 +0100248 exe cmd1
249 exe cmd2
DungSaga47810462021-10-22 12:55:42 +0100250 call assert_equal(['00000000: 3a 3a 41 41 3a 3a 3a 3a 42 42 ::AA::::BB'], readfile('Xxxdout'))
251
252 call writefile(["2: 43 43 ", "09: 44 44"], 'Xxxdin')
Bram Moolenaar5a5c1112021-10-22 15:11:37 +0100253 exe cmd1
254 exe cmd2
DungSaga47810462021-10-22 12:55:42 +0100255 call assert_equal(['00000000: 3a 3a 43 43 3a 3a 3a 3a 42 44 44 ::CC::::BDD'], readfile('Xxxdout'))
256
257 call writefile(["2: 45 45 ", "0a: 46 46"], 'Xxxdin')
Bram Moolenaar5a5c1112021-10-22 15:11:37 +0100258 exe cmd1
259 exe cmd2
DungSaga47810462021-10-22 12:55:42 +0100260 call assert_equal(['00000000: 3a 3a 45 45 3a 3a 3a 3a 42 44 46 46 ::EE::::BDFF'], readfile('Xxxdout'))
261
262 call delete('Xxxdin')
263 call delete('Xxxdfile')
264 call delete('Xxxdout')
265endfunc
266
Bram Moolenaar970f5d32019-01-25 21:52:17 +0100267" Various ways with wrong arguments that trigger the usage output.
268func Test_xxd_usage()
Erik Auerswalda00e6222022-01-13 17:42:28 +0000269 for arg in ['-h', '-c', '-g', '-o', '-s', '-l', '-X', 'one two three']
Bram Moolenaar970f5d32019-01-25 21:52:17 +0100270 new
271 exe 'r! ' . s:xxd_cmd . ' ' . arg
272 call assert_match("Usage:", join(getline(1, 3)))
273 bwipe!
274 endfor
275endfunc
276
DungSaga48608b42021-11-24 11:18:07 +0000277func Test_xxd_ignore_garbage()
278 new
279 exe 'r! printf "\n\r xxxx 0: 42 42" | ' . s:xxd_cmd . ' -r'
280 call assert_match('BB', join(getline(1, 3)))
281 bwipe!
282endfunc
283
284func Test_xxd_bit_dump()
285 new
286 exe 'r! printf "123456" | ' . s:xxd_cmd . ' -b1'
287 call assert_match('00000000: 00110001 00110010 00110011 00110100 00110101 00110110 123456', join(getline(1, 3)))
288 bwipe!
289endfunc
290
Bram Moolenaar970f5d32019-01-25 21:52:17 +0100291func Test_xxd_version()
292 new
293 exe 'r! ' . s:xxd_cmd . ' -v'
=?UTF-8?q?J=C3=BCrgen=20Weigert?=80b2ba32021-06-29 20:36:25 +0200294 call assert_match('xxd 20\d\d-\d\d-\d\d by Juergen Weigert et al\.', join(getline(1, 3)))
Bram Moolenaar970f5d32019-01-25 21:52:17 +0100295 bwipe!
296endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200297
Erik Auerswalda00e6222022-01-13 17:42:28 +0000298" number of columns must be non-negative
299func Test_xxd_min_cols()
300 for cols in ['-c-1', '-c -1', '-cols -1']
301 for fmt in ['', '-b', '-e', '-i', '-p', ]
302 new
303 exe 'r! printf "ignored" | ' . s:xxd_cmd . ' ' . cols . ' ' . fmt
304 call assert_match("invalid number of columns", join(getline(1, '$')))
305 bwipe!
306 endfor
307 endfor
308endfunc
309
310" some hex formats limit columns to 256 (a #define in xxd.c)
311func Test_xxd_max_cols()
312 for cols in ['-c257', '-c 257', '-cols 257']
313 for fmt in ['', '-b', '-e' ]
314 new
315 exe 'r! printf "ignored" | ' . s:xxd_cmd . ' ' . cols . ' ' . fmt
316 call assert_match("invalid number of columns", join(getline(1, '$')))
317 bwipe!
318 endfor
319 endfor
320endfunc
321
322" -c0 selects the format specific default column value, as if no -c was given
323func Test_xxd_c0_is_def_cols()
324 call writefile(["abcdefghijklmnopqrstuvwxyz0123456789"], 'Xxdin')
325 for cols in ['-c0', '-c 0', '-cols 0']
326 for fmt in ['', '-b', '-e', '-i', '-p', ]
327 exe 'r! ' . s:xxd_cmd . ' ' . fmt ' Xxdin > Xxdout1'
328 exe 'r! ' . s:xxd_cmd . ' ' . cols . ' ' . fmt ' Xxdin > Xxdout2'
329 call assert_equalfile('Xxdout1', 'Xxdout2')
330 endfor
331 endfor
332 call delete('Xxdin')
333 call delete('Xxdout1')
334 call delete('Xxdout2')
335endfunc
336
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200337" vim: shiftwidth=2 sts=2 expandtab