frostfs-sdk-go/api/util/protogen/internalgengo/fuzz.go
Pavel Pogodaev 6ce73790ea
All checks were successful
DCO / DCO (pull_request) Successful in 38s
Tests and linters / Tests (pull_request) Successful in 1m13s
Tests and linters / Lint (pull_request) Successful in 2m36s
[#276] Merge repo with frostfs-api-go
Signed-off-by: Pavel Pogodaev <p.pogodaev@yadro.com>
2024-10-22 14:05:12 +00:00

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("})}")
}