为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 go1.18 在一个源文件中运行多个模糊测试用例?

如何使用 go1.18 在一个源文件中运行多个模糊测试用例?

Go
慕的地6264312 2022-11-23 19:51:17
go 1.18 几天前发布了。从 Go 1.18 开始,它在其标准工具链中支持模糊测试但是当我试图写我的案例时,它不能在一个包(或一个文件?)中运行多个案例。代码:package xxxfunc 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)    })}然后我运行命令:go test  -fuzz .或者go test  -fuzz=Fuzz但结果是:testing: will not fuzz, -fuzz matches more than one fuzz test: [FuzzReverse FuzzReverse2]像这样:本教程没有提示,感谢帮助。(我在 stackoverflow 中的第一个问题,非常感谢!!!!)我尝试在一个源文件中编写多个模糊案例,然后运行 cmd: go test -fuzz 。期望它可以进行模糊测试,但出现错误:\测试:不会进行模糊测试,-fuzz 匹配多个模糊测试:[FuzzReverse FuzzReverse2]
查看完整描述

2 回答

?
千万里不及你

TA贡献1784条经验 获得超9个赞

这是一个 quick-n-dirty 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。


查看完整回答
反对 回复 2022-11-23
?
哆啦的时光机

TA贡献1779条经验 获得超6个赞

好吧,我已经阅读了 Go-fuzz 模块的源代码,事实是它不支持每次执行的多个案例。


代码在:\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-11-23
  • 2 回答
  • 0 关注
  • 85 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信