forked from TrueCloudLab/frostfs-api-go
[#77] protogen: Initial implementation
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
a28ceb251a
commit
adb7c602d7
115 changed files with 36376 additions and 20397 deletions
69
util/protogen/internalgengo/fuzz.go
Normal file
69
util/protogen/internalgengo/fuzz.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
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("})}")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue