1#!/usr/bin/env bash
2#
3# Copyright Red Hat
4# Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License as
8# published by the Free Software Foundation.
9#
10# This program is distributed in the hope that it would be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17#
18#
19# standard filters
20#
21
22_filter_date()
23{
24    sed -Ee 's/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/yyyy-mm-dd hh:mm:ss/'
25}
26
27_filter_vmstate_size()
28{
29    sed -E -e 's/[0-9. ]{5} [KMGT]iB/     SIZE/' \
30           -e 's/[0-9. ]{5} B/   SIZE/'
31}
32
33_filter_generated_node_ids()
34{
35    sed -Ee 's/\#block[0-9]{3,}/NODE_NAME/'
36}
37
38_filter_qom_path()
39{
40    gsed -e '/Attached to:/s/\device[[0-9]\+\]/device[N]/g'
41}
42
43# replace occurrences of the actual TEST_DIR value with TEST_DIR
44_filter_testdir()
45{
46    sed -e "s#$TEST_DIR/#TEST_DIR/#g" \
47        -e "s#$SOCK_DIR/#SOCK_DIR/#g" \
48        -e "s#SOCK_DIR/fuse-#TEST_DIR/#g"
49}
50
51# replace occurrences of the actual IMGFMT value with IMGFMT
52_filter_imgfmt()
53{
54    sed -e "s#$IMGFMT#IMGFMT#g"
55}
56
57# Replace error message when the format is not supported and delete
58# the output lines after the first one
59_filter_qemu_img_check()
60{
61    gsed -e '/allocated.*fragmented.*compressed clusters/d' \
62        -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
63        -e '/Image end offset: [0-9]\+/d'
64}
65
66# Removes \r from messages
67_filter_win32()
68{
69    gsed -e 's/\r//g'
70}
71
72# sanitize qemu-io output
73_filter_qemu_io()
74{
75    _filter_win32 | \
76    gsed -e "s/[0-9]* ops\; [0-9/:. sec]* ([0-9/.inf]* [EPTGMKiBbytes]*\/sec and [0-9/.inf]* ops\/sec)/X ops\; XX:XX:XX.X (XXX YYY\/sec and XXX ops\/sec)/" \
77        -e "s/: line [0-9][0-9]*:  *[0-9][0-9]*\( Aborted\| Killed\)/:\1/" \
78        -e "s/qemu-io> //g"
79}
80
81# replace occurrences of QEMU_PROG with "qemu"
82_filter_qemu()
83{
84    gsed -e "s#\\(^\\|(qemu) \\)$(basename $QEMU_PROG):#\1QEMU_PROG:#" \
85        -e 's#^QEMU [0-9]\+\.[0-9]\+\.[0-9]\+ monitor#QEMU X.Y.Z monitor#' \
86        -e $'s#\r##' # QEMU monitor uses \r\n line endings
87}
88
89# replace problematic QMP output like timestamps
90_filter_qmp()
91{
92    _filter_win32 | \
93    gsed -e 's#\("\(micro\)\?seconds": \)[0-9]\+#\1 TIMESTAMP#g' \
94        -e 's#^{"QMP":.*}$#QMP_VERSION#' \
95        -e '/^    "QMP": {\s*$/, /^    }\s*$/ c\' \
96        -e '    QMP_VERSION'
97}
98
99# readline makes HMP command strings so long that git complains
100_filter_hmp()
101{
102    gsed -e $'s/^\\((qemu) \\)\\?.*\e\\[D/\\1/g' \
103        -e $'s/\e\\[K//g'
104}
105
106# replace block job offset
107_filter_block_job_offset()
108{
109    gsed -e 's/, "offset": [0-9]\+,/, "offset": OFFSET,/'
110}
111
112# replace block job len
113_filter_block_job_len()
114{
115    gsed -e 's/, "len": [0-9]\+,/, "len": LEN,/g'
116}
117
118# replace actual image size (depends on the host filesystem)
119_filter_actual_image_size()
120{
121    gsed -s 's/\("actual-size":\s*\)[0-9]\+/\1SIZE/g'
122}
123
124# Filename filters for qemu-img create
125_filter_img_create_filenames()
126{
127    sed \
128        -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
129        -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
130        -e "s#$TEST_DIR#TEST_DIR#g" \
131        -e "s#$SOCK_DIR#SOCK_DIR#g" \
132        -e 's#SOCK_DIR/fuse-#TEST_DIR/#g' \
133        -e "s#$IMGFMT#IMGFMT#g" \
134        -e 's#nbd+unix:///\??socket=SOCK_DIR/nbd#TEST_DIR/t.IMGFMT#g'
135}
136
137# replace driver-specific options in the "Formatting..." line
138_do_filter_img_create()
139{
140    # Split the line into the pre-options part ($filename_part, which
141    # precedes ", fmt=") and the options part ($options, which starts
142    # with "fmt=")
143    # (And just echo everything before the first "^Formatting")
144    readarray formatting_line < <(gsed -e 's/, fmt=/\n/')
145
146    filename_part=${formatting_line[0]}
147    unset formatting_line[0]
148
149    options="fmt=${formatting_line[@]}"
150
151    # Set grep_data_file to '\|data_file' to keep it; make it empty
152    # to drop it.
153    # We want to drop it if it is part of the global $IMGOPTS, and we
154    # want to keep it otherwise (if the test specifically wants to
155    # test data files).
156    grep_data_file=(-e data_file)
157    if _get_data_file "$TEST_IMG" > /dev/null; then
158        grep_data_file=()
159    fi
160
161    filename_part=$(echo "$filename_part" | _filter_img_create_filenames)
162
163    # Break the option line before each option (preserving pre-existing
164    # line breaks by replacing them by \0 and restoring them at the end),
165    # then filter out the options we want to keep and sort them according
166    # to some order that all block drivers used at the time of writing
167    # this function.
168    options=$(
169        echo "$options" \
170        | tr '\n' '\0' \
171        | gsed -e 's/ \([a-z0-9_.-]*\)=/\n\1=/g' \
172        | grep -a -e '^fmt' -e '^size' -e '^backing' -e '^preallocation' \
173                  -e '^encryption' "${grep_data_file[@]}" \
174        | _filter_img_create_filenames \
175        | sed \
176            -e 's/^\(fmt\)/0-\1/' \
177            -e 's/^\(size\)/1-\1/' \
178            -e 's/^\(backing\)/2-\1/' \
179            -e 's/^\(data_file\)/3-\1/' \
180            -e 's/^\(encryption\)/4-\1/' \
181            -e 's/^\(preallocation\)/8-\1/' \
182        | LC_ALL=C sort \
183        | sed -e 's/^[0-9]-//' \
184        | tr '\n\0' ' \n' \
185        | sed -e 's/^ *$//' -e 's/ *$//'
186    )
187
188    if [ -n "$options" ]; then
189        echo "$filename_part, $options"
190    elif [ -n "$filename_part" ]; then
191        echo "$filename_part"
192    fi
193}
194
195# Filter qemu-img create output:
196# Pipe all ^Formatting lines through _do_filter_img_create, and all
197# other lines through _filter_img_create_filenames
198_filter_img_create()
199{
200    while read -r line; do
201        if echo "$line" | grep -q '^Formatting'; then
202            echo "$line" | _do_filter_img_create
203        else
204            echo "$line" | _filter_img_create_filenames
205        fi
206    done
207}
208
209_filter_img_create_size()
210{
211    gsed -e "s# size=[0-9]\\+# size=SIZE#g"
212}
213
214_filter_img_info()
215{
216    if [[ "$1" == "--format-specific" ]]; then
217        local format_specific=1
218        shift
219    else
220        local format_specific=0
221    fi
222
223    discard=0
224    regex_json_spec_start='^ *"format-specific": \{'
225    regex_json_child_start='^ *"children": \['
226    gsed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
227        -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
228        -e "s#$TEST_DIR#TEST_DIR#g" \
229        -e "s#$SOCK_DIR#SOCK_DIR#g" \
230        -e "s#$IMGFMT#IMGFMT#g" \
231        -e 's#nbd+unix:///\??socket=SOCK_DIR/nbd#TEST_DIR/t.IMGFMT#g' \
232        -e 's#SOCK_DIR/fuse-#TEST_DIR/#g' \
233        -e "/encrypted: yes/d" \
234        -e "/cluster_size: [0-9]\\+/d" \
235        -e "/table_size: [0-9]\\+/d" \
236        -e "/compat: '[^']*'/d" \
237        -e "/compat6: \\(on\\|off\\)/d" \
238        -e "s/cid: [0-9]\+/cid: XXXXXXXXXX/" \
239        -e "/static: \\(on\\|off\\)/d" \
240        -e "/zeroed_grain: \\(on\\|off\\)/d" \
241        -e "/subformat: '[^']*'/d" \
242        -e "/adapter_type: '[^']*'/d" \
243        -e "/hwversion: '[^']*'/d" \
244        -e "/lazy_refcounts: \\(on\\|off\\)/d" \
245        -e "/extended_l2=\\(on\\|off\\)/d" \
246        -e "/block_size: [0-9]\\+/d" \
247        -e "/block_state_zero: \\(on\\|off\\)/d" \
248        -e "/log_size: [0-9]\\+/d" \
249        -e "s/iters: [0-9]\\+/iters: 1024/" \
250        -e 's/\(compression type: \)\(zlib\|zstd\)/\1COMPRESSION_TYPE/' \
251        -e "s/uuid: [-a-f0-9]\\+/uuid: 00000000-0000-0000-0000-000000000000/" | \
252    while IFS='' read -r line; do
253        if [[ $discard == 0 ]]; then
254            if [[ $format_specific == 0 && $line == "Format specific information:" ]]; then
255                discard=1
256            elif [[ $line =~ "Child node '/" ]]; then
257                discard=1
258            elif [[ $line =~ $regex_json_spec_start ]]; then
259                discard=2
260                regex_json_end="^${line%%[^ ]*}\\},? *$"
261            elif [[ $line =~ $regex_json_child_start ]]; then
262                discard=2
263                regex_json_end="^${line%%[^ ]*}\\],? *$"
264            fi
265        fi
266        if [[ $discard == 0 ]]; then
267            echo "$line"
268        elif [[ $discard == 1 && ! $line ]]; then
269            echo
270            discard=0
271        elif [[ $discard == 2 && $line =~ $regex_json_end ]]; then
272            discard=0
273        fi
274    done
275}
276
277# filter out offsets and file names from qemu-img map; good for both
278# human and json output
279_filter_qemu_img_map()
280{
281    # Assuming the data_file value in $IMGOPTS contains a '$TEST_IMG',
282    # create a filter that replaces the data file name by $TEST_IMG.
283    # Example:
284    #   In $IMGOPTS: 'data_file=$TEST_IMG.data_file'
285    #   Then data_file_pattern == '\(.*\).data_file'
286    #   And  data_file_filter  == -e 's#\(.*\).data_file#\1#
287    data_file_filter=()
288    if data_file_pattern=$(_get_data_file '\\(.*\\)'); then
289        data_file_filter=(-e "s#$data_file_pattern#\\1#")
290    fi
291
292    sed -e 's/\([0-9a-fx]* *[0-9a-fx]* *\)[0-9a-fx]* */\1/g' \
293        -e 's/"offset": [0-9]\+/"offset": OFFSET/g' \
294        -e 's/Mapped to *//' \
295        "${data_file_filter[@]}" \
296        | _filter_testdir | _filter_imgfmt
297}
298
299_filter_nbd()
300{
301    # nbd.c error messages contain function names and line numbers that are
302    # prone to change.  Message ordering depends on timing between send and
303    # receive callbacks sometimes, making them unreliable.
304    #
305    # Filter out the TCP port number since this changes between runs.
306    sed -e '/nbd\/.*\.c:/d' \
307        -e 's#127\.0\.0\.1:[0-9]*#127.0.0.1:PORT#g' \
308        -e 's#localhost:[0-9]*#localhost:PORT#g' \
309        -e 's#host=127\.0\.0\.1,port=[0-9]*#host=127.0.0.1,port=PORT#g' \
310        -e 's#host=localhost,port=[0-9]*#host=localhost,port=PORT#g' \
311        -e "s#path=$SOCK_DIR#path=SOCK_DIR#g" \
312        -e "s#?socket=$SOCK_DIR#?socket=SOCK_DIR#g" \
313        -e 's#\(foo\|PORT/\?\|.sock\): Failed to .*$#\1#'
314}
315
316_filter_qemu_nbd_exports()
317{
318    grep '\(exports available\|export\|size\|min block\|qemu-nbd\):'
319}
320
321_filter_qmp_empty_return()
322{
323    grep -v '{"return": {}}'
324}
325
326_filter_json_filename()
327{
328    $PYTHON -c 'import sys
329result, *fnames = sys.stdin.read().split("json:{")
330depth = 0
331for fname in fnames:
332    depth += 1 # For the opening brace in the split separator
333    for chr_i, chr in enumerate(fname):
334        if chr == "{":
335            depth += 1
336        elif chr == "}":
337            depth -= 1
338            if depth == 0:
339                break
340
341    # json:{} filenames may be nested; filter out everything from
342    # inside the outermost one
343    if depth == 0:
344        chr_i += 1 # First character past the filename
345        result += "json:{ /* filtered */ }" + fname[chr_i:]
346
347sys.stdout.write(result)'
348}
349
350_filter_authz_check_tls()
351{
352    sed -e 's/TLS x509 authz check for .* is denied/TLS x509 authz check for DISTINGUISHED-NAME is denied/'
353}
354
355_filter_qcow2_compression_type_bit()
356{
357    gsed -e 's/\(incompatible_features\s\+\)\[3\(, \)\?/\1[/' \
358        -e 's/\(incompatible_features.*\), 3\]/\1]/' \
359        -e 's/\(incompatible_features.*\), 3\(,.*\)/\1\2/'
360}
361
362# make sure this script returns success
363true
364