#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2017, Oracle and/or its affiliates.  All Rights Reserved.
#
# FS QA Test No. 058
#
# Ensure that xfs_db fuzz command works as advertised.
#
. ./common/preamble
_begin_fstest auto quick fuzzers

# Import common functions.
. ./common/filter
. ./common/fuzzy

_require_scratch_nocheck
_require_command "$XFS_DB_PROG" "xfs_db"
_require_xfs_db_command "fuzz"

rm -f "$seqres.full"

echo "Format"
_scratch_mkfs | _filter_mkfs 2>$tmp.mkfs >> "$seqres.full"
source $tmp.mkfs

do_xfs_db()
{
	local cmd=$1
	if [ $_fs_has_crcs -eq 0 ]; then
		echo "Allowing $cmd of corrupted data with good CRC"
	fi
	_scratch_xfs_db -x -c 'sb 0' -c "$*"
}

# Make sure that each fuzz verb works.  For the firstbit/middlebit/lastbit
# tests, we depend on 'ones' having set the field to all ones.
field="fdblocks"
do_xfs_db write -d ${field} 0
SCRATCH_XFS_LIST_FUZZ_VERBS= _scratch_xfs_list_fuzz_verbs | while read verb; do
	test "${verb}" = "random" && continue
	echo "Test verb ${verb}"
	do_xfs_db fuzz -d ${field} ${verb}
done

echo "Test verb random"
before="$(_scratch_xfs_get_metadata_field "${field}" 'sb 0')"
do_xfs_db fuzz -d ${field} random >> $seqres.full
after="$(_scratch_xfs_get_metadata_field "${field}" 'sb 0')"
test "${before}" != "${after}" && echo "${field} changed"

echo "Done"
# success, all done
status=0
exit
