summaryrefslogtreecommitdiffstats
path: root/tests/ts/hexdump/highlighting
blob: cb94615925296b9cd79add4e04568dc0a90250fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash

# This file is part of util-linux.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
TS_TOPDIR="${0%/*}/../.."
TS_DESC="highlighting"
FILES="$TS_TOPDIR/ts/hexdump/files"
OPTS="--color=always"
ADDRFMT='-e "%07.7_Ax\n"'
#sample input consists of hexdump-ed results of the following py3script:
#for i in range(256):
#	print(chr(i), end= ' ')

. "$TS_TOPDIR"/functions.sh
ts_init "$*"

ts_check_test_command "$TS_CMD_HEXDUMP"
ts_check_test_command "$TS_HELPER_SYSINFO"

# on big endian systems some of the subtests have different expected output
BYTE_ORDER=$($TS_HELPER_SYSINFO byte-order)
BE_EXT=$(test "$BYTE_ORDER" = "BE" && echo ".BE")

#
# basic formats /w some highlighting tweaks
#

# highlight 'A' and 'a'
ts_init_subtest "1b_octal-1"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:A,red:a] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight bytes with the value of 0x41 (A) and 0x61 (a)
ts_init_subtest "1b_octal-2"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:0x41,red:0x61] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight the bytes at offsets 130 and  194 (decimal)
ts_init_subtest "1b_octal-3"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red@130,red@194] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight the byte at offset 194 if it's value is 0x61 (a)
# and the byte at offset 130 if it's value is A (0x41)
ts_init_subtest "1b_octal-4"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:A@130,red:0x61@194] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight the byte at offset 194 if it's value is 0x61 (a)
# and the byte at offset 130 if it's value is C (0x41) (false)
ts_init_subtest "1b_octal-5"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:C@130,red:0x61@194] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight a range longer than the byte count of %o
ts_init_subtest "1b_octal-6"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red@193-194] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight bytes with the value of 0101 (A) and 0141 (a)
ts_init_subtest "1b_octal-7"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:0101,red:0141] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight bytes with the value of 0101 (A), 0x61 (a), or if the character is a caret
ts_init_subtest "1b_octal-8"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:0101,blue:0x61,green:^] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# highlight bytes with the value of 0101 (A), 0x61 (a), or if the character at 196 is not 'c'
ts_init_subtest "1b_octal-9"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%03o_L[red:0101,blue:0x61,!green:c@196] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

#color the addresses red and the characters in UTIL-LINUX green
ts_init_subtest "1b_char-1"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax_L[red] " 16/1 "%3_c_L[green:-,green:I,green:L,green:N,green:T,green:U,green:X] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

#color the address 0xe0 red
ts_init_subtest "1b_char-2"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax_L[red:0xe0] " 16/1 "%3_c " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

#highlight ':' and '@'
ts_init_subtest "1b_char-3"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 16/1 "%3_c_L[red::@116,red:@@128] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color last address gray, the null byte blue, the spaces brown(-ish) and the text cyan
ts_init_subtest "canon-1"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS \
	-e '"%08.8_Ax_L[gray]\n"' \
	-e '"%08.8_ax  " 8/1 "%02x_L[blue:0x0,brown:0x20] " "  " 8/1 "%02x_L[brown:0x20] " ' \
	-e '"  |" 16/1 "%_p_L[cyan]" "|\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color last address gray - if it's 0x280 or blue if it's 0x380
# color A/a hex dumps blue and A/a in text green
ts_init_subtest "canon-2"
$TS_CMD_HEXDUMP $OPTS \
	-e '"%08.8_Ax_L[blue:0x380,red:0x280]\n"' \
	-e '"%08.8_ax  " 8/1 "%02x_L[blue:A,blue:a] " "  " 8/1 "%02x_L[blue:A,blue:a] " ' \
	-e '"  |" 16/1 "%_p_L[green:A,green:a]" "|\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if one of the bytes it prints is at offset 100 (0x64)
ts_init_subtest "2b_dec-1"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red@100] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if of the bytes it prints are at offsets 98 and 99
ts_init_subtest "2b_dec-2"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red@98-99] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if some or all of the bytes it prints are at offsets 98 and 99
ts_init_subtest "2b_dec-3"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red@97-99] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if it prints "A " at 130-131
ts_init_subtest "2b_dec-4"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red:A @130-131] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if it doesn't print "B " at 130-131
ts_init_subtest "2b_dec-5"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[!red:B @130-131] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# specify a string shorter than the range - error
ts_init_subtest "2b_dec-6"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red:A@130-131] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# specify a negative-length range
ts_init_subtest "2b_dec-7"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red:A@131-130] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if of the bytes it prints are at offsets 98-102 (multiple print units)
ts_init_subtest "2b_dec-8"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/2 "  %05u_L[red@98-102] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if some or all of the bytes it prints are at offsets 97 and 99 (true)
ts_init_subtest "4b_dec-1"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red@97-99] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# color the current print unit if some or all of the bytes it prints are at offsets 96-99
ts_init_subtest "4b_dec-2"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red@96-99] " "\n"' \
	$FILES/ascii.in &> $TS_OUTPUT
ts_finalize_subtest

# look for @@@@
ts_init_subtest "4b_dec-3"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@] " "\n"' \
	&> $TS_OUTPUT <<< "@@@@"
ts_finalize_subtest

# look for @@@@ at 0-3
ts_init_subtest "4b_dec-4"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@@0-3] " "\n"' \
	&> $TS_OUTPUT <<< "@@@@"
ts_finalize_subtest

# look for @@@ at 0-3 (wrong byte count error)
ts_init_subtest "4b_dec-5"
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@0-3] " "\n"' \
	&> $TS_OUTPUT <<< "@@@@"
ts_finalize_subtest

# look for @@@ at 1-3 - in the format boundaries - correct
ts_init_subtest "4b_dec-6"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@1-3] " "\n"' \
	&> $TS_OUTPUT <<< "@@@@"
ts_finalize_subtest

# look for @@@ at 2-4 - correct range length, however steps over 2 print units - no colors
ts_init_subtest "4b_dec-7"
TS_EXPECTED+=$BE_EXT
$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@2-4] " "\n"' \
	&> $TS_OUTPUT <<< "@@@@"
ts_finalize_subtest

ts_finalize