使用Golang编译带有覆盖率检测的模糊测试
使用Golang编译带有覆盖率检测的模糊测试 我想构建一个独立的模糊测试二进制文件,以便在 Docker 容器中使用。
然而,二进制文件会提示测试二进制文件未使用覆盖率检测构建,因此模糊测试将在没有覆盖率指导的情况下运行,可能效率低下。
我希望启用覆盖率检测。
示例测试:
package main
import (
"bytes"
"crypto/sha256"
"testing"
)
func FuzzFoobar(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
// 调用一些任意方法
data = sha256.New().Sum(bytes.ToLower(data))
if len(data) == 0 {
panic(1)
}
})
}
正常运行时没有问题:
$ go test . -fuzz Foo
fuzz: elapsed: 0s, gathering baseline coverage: 0/66 completed
fuzz: elapsed: 0s, gathering baseline coverage: 66/66 completed, now fuzzing with 8 workers
^Cfuzz: elapsed: 3s, execs: 107197 (39642/sec), new interesting: 8 (total: 74)
编译它时会产生警告:
$ go test -c .
$ ./testfuzz.test -test.fuzz=Foo -test.fuzzcachedir=/tmp
warning: the test binary was not built with coverage instrumentation, so fuzzing will run without coverage guidance and may be inefficient
warning: starting with empty corpus
fuzz: elapsed: 0s, execs: 0 (0/sec)
如果我尝试在编译时使用-cover,也没有帮助:
$ go test -cover -c .
$ ./testfuzz.test -test.fuzz=Foo -test.fuzzcachedir=/tmp
warning: the test binary was not built with coverage instrumentation, so fuzzing will run without coverage guidance and may be inefficient
warning: starting with empty corpus
fuzz: elapsed: 0s, execs: 0 (0/sec)
更多关于使用Golang编译带有覆盖率检测的模糊测试的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复


