forked from TrueCloudLab/frostfs-api-go
69 lines
2 KiB
Go
69 lines
2 KiB
Go
package internalgengo
|
|
|
|
import (
|
|
"google.golang.org/protobuf/compiler/protogen"
|
|
)
|
|
|
|
var testingPackage = protogen.GoImportPath("testing")
|
|
|
|
func GenerateFuzzTests(gen *protogen.Plugin, file *protogen.File) {
|
|
{
|
|
filename := file.GeneratedFilenamePrefix + "_frostfs_fuzz.go"
|
|
g := gen.NewGeneratedFile(filename, file.GoImportPath)
|
|
|
|
g.P("//go:build gofuzz")
|
|
g.P("// +build gofuzz")
|
|
g.P("// Code generated by protoc-gen-go-frostfs. DO NOT EDIT.")
|
|
g.P()
|
|
g.P("package ", file.GoPackageName)
|
|
g.P()
|
|
|
|
for _, msg := range file.Messages {
|
|
emitFuzzWrappers(g, msg)
|
|
}
|
|
}
|
|
{
|
|
filename := file.GeneratedFilenamePrefix + "_frostfs_test.go"
|
|
g := gen.NewGeneratedFile(filename, file.GoImportPath)
|
|
|
|
g.P("//go:build gofuzz")
|
|
g.P("// +build gofuzz")
|
|
g.P("// Code generated by protoc-gen-go-frostfs. DO NOT EDIT.")
|
|
g.P()
|
|
g.P("package ", file.GoPackageName)
|
|
g.P()
|
|
|
|
for _, msg := range file.Messages {
|
|
emitFuzzTests(g, msg)
|
|
}
|
|
}
|
|
}
|
|
|
|
func emitFuzzWrappers(g *protogen.GeneratedFile, msg *protogen.Message) {
|
|
g.P("func DoFuzzProto", msg.GoIdent.GoName, "(data []byte) int {")
|
|
g.P("msg := new(", msg.GoIdent.GoName, ")")
|
|
g.P("if err := msg.UnmarshalProtobuf(data); err != nil { return 0 }")
|
|
g.P("_ = msg.MarshalProtobuf(nil)")
|
|
g.P("return 1")
|
|
g.P("}")
|
|
|
|
g.P("func DoFuzzJSON", msg.GoIdent.GoName, "(data []byte) int {")
|
|
g.P("msg := new(", msg.GoIdent.GoName, ")")
|
|
g.P("if err := msg.UnmarshalJSON(data); err != nil { return 0 }")
|
|
g.P("_, err := msg.MarshalJSON()")
|
|
g.P("if err != nil { panic(err) }")
|
|
g.P("return 1")
|
|
g.P("}")
|
|
}
|
|
|
|
func emitFuzzTests(g *protogen.GeneratedFile, msg *protogen.Message) {
|
|
g.P("func FuzzProto", msg.GoIdent.GoName, "(f *", testingPackage.Ident("F"), ") {")
|
|
g.P("f.Fuzz(func(t *", testingPackage.Ident("T"), ", data []byte) {")
|
|
g.P("DoFuzzProto", msg.GoIdent.GoName, "(data)")
|
|
g.P("})}")
|
|
|
|
g.P("func FuzzJSON", msg.GoIdent.GoName, "(f *", testingPackage.Ident("F"), ") {")
|
|
g.P("f.Fuzz(func(t *", testingPackage.Ident("T"), ", data []byte) {")
|
|
g.P("DoFuzzJSON", msg.GoIdent.GoName, "(data)")
|
|
g.P("})}")
|
|
}
|