【问题标题】:how to run multi fuzz test cases wirtten in one source file with go1.18?如何使用 go1.18 运行在一个源文件中编写的多个模糊测试用例?
【发布时间】:2022-06-19 07:58:38
【问题描述】:

go 1.18 几天前发布。从 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]

像这样:

教程没有提示,谢谢帮助。(我在stackoverflow的第一个问题,谢谢!!!!)

我尝试在一个源文件中写入多个模糊案例,然后运行 ​​cmd: go test -fuzz 。 期望它可以进行模糊测试,但出现错误:\

测试:不会模糊,-fuzz 匹配多个模糊测试:[FuzzReverse FuzzReverse2]

【问题讨论】:

    标签: go fuzzing


    【解决方案1】:

    好的,我已经阅读了 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
    }
    

    我希望将来可以支持多案例执行。

    【讨论】:

      【解决方案2】:

      这是一个 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 秒)

      为了进一步参考,an existing github issue 允许多个模糊目标,但没有关于何时添加它的 ETA。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多