go 1.18发布了服务器日,ago.It从Go 1.18开始在其标准工具链中支持模糊
但是,当我试图编写我的案例时,它不能在一个包(或一个文件?)中运行多个案例。代码:
package xxx
func FuzzReverse(f *testing.F) {
testcases := []string{"Hello, world", " ", "!12345"}
for _, tc := range testcases {
f.Add(tc) // Use f.Add to provide a seed corpus
}
f.Fuzz(func(t *testing.T, orig string) {
Reverse(orig)
})
}
func FuzzReverse2(f *testing.F) {
testcases := []string{"Hello, world", " ", "!12345"}
for _, tc := range testcases {
f.Add(tc) // Use f.Add to provide a seed corpus
}
f.Fuzz(func(t *testing.T, orig string) {
Reverse(orig)
})
}我运行cmd:
go test -fuzz .或
go test -fuzz=Fuzz但结果是:
testing: will not fuzz, -fuzz matches more than one fuzz test: [FuzzReverse FuzzReverse2]就像这样:

本教程没有提供任何提示,而是寻求帮助。(我在堆栈溢出中的第一个问题,很多!)
我尝试在一个源文件中处理多个模糊情况,然后运行cmd: go测试-fuzz。期望它可以进行模糊测试,但是得到了一个错误:\
测试:将不模糊,-fuzz匹配多个模糊测试: FuzzReverse FuzzReverse2
发布于 2022-03-24 03:05:45
好吧,我读过Go模块的源代码,它不支持每次执行的多个案例。
:\Go\src\testing\fuzz.go中的代码
if len(matched) > 1 {
fmt.Fprintf(os.Stderr, "testing: will not fuzz, -fuzz matches more than one fuzz test: %v\n", matched)
return false
}我希望将来能够支持多个案例的执行。
发布于 2022-06-18 23:58:38
下面是一个快速n脏bash脚本,它将在当前文件夹中找到所有的模糊测试,并分别运行10秒:
#!/bin/bash
set -e
fuzzTime=${1:-10}
files=$(grep -r --include='**_test.go' --files-with-matches 'func Fuzz' .)
for file in ${files}
do
funcs=$(grep -oP 'func \K(Fuzz\w*)' $file)
for func in ${funcs}
do
echo "Fuzzing $func in $file"
parentDir=$(dirname $file)
go test $parentDir -run=$func -fuzz=$func -fuzztime=${fuzzTime}s
done
done若要使用此脚本,请创建一个名为fuzzAll.sh的新文件并将此代码复制到其中,然后运行./fuzzAll.sh以运行所有模糊测试,每次运行10秒,或传递一个不同的数字以运行不同的时间(例如,./fuzzAll.sh 30运行30秒)。
对于进一步的参考,有现有的github问题,允许多个模糊目标,但没有ETA时,它将被添加。
https://stackoverflow.com/questions/71584005
复制相似问题