Debian Codegraph @

tests/harness/go/main/harness.go

raw
1 package main
2
3 import (
4 "fmt"
5 "io/ioutil"
6 "log"
7 "os"
8
9 "github.com/envoyproxy/protoc-gen-validate/tests/harness/cases/go"
10 _ "github.com/envoyproxy/protoc-gen-validate/tests/harness/cases/go"
11 _ "github.com/envoyproxy/protoc-gen-validate/tests/harness/cases/other_package/go"
12 _ "github.com/envoyproxy/protoc-gen-validate/tests/harness/cases/yet_another_package/go"
13 "github.com/envoyproxy/protoc-gen-validate/tests/harness/go"
14 "google.golang.org/protobuf/proto"
15 )
16
17 func main() {
18 b, err := ioutil.ReadAll(os.Stdin)
19 checkErr(err)
20
21 tc := new(harness.TestCase)
22 checkErr(proto.Unmarshal(b, tc))
23
24 msg, err := tc.Message.UnmarshalNew()
25 checkErr(err)
26
27 _, isIgnored := msg.(*cases.MessageIgnored)
28
29 vMsg, hasValidate := msg.(interface {
30 Validate() error
31 })
32
33 vAllMsg, hasValidateAll := msg.(interface {
34 ValidateAll() error
35 })
36
37 var multierr error
38 if isIgnored {
39 // confirm that ignored messages don't have a validate method
40 if hasValidate {
41 checkErr(fmt.Errorf("ignored message %T has Validate() method", msg))
42 }
43 if hasValidateAll {
44 checkErr(fmt.Errorf("ignored message %T has ValidateAll() method", msg))
45 }
46 } else if !hasValidate {
47 checkErr(fmt.Errorf("non-ignored message %T is missing Validate()", msg))
48 } else if !hasValidateAll {
49 checkErr(fmt.Errorf("non-ignored message %T is missing ValidateAll()", msg))
50 } else {
51 err = vMsg.Validate()
52 multierr = vAllMsg.ValidateAll()
53 }
54 checkValid(err, multierr)
55 }
56
57 type hasAllErrors interface{ AllErrors() []error }
58 type hasCause interface{ Cause() error }
59
60 func checkValid(err, multierr error) {
61 if err == nil && multierr == nil {
62 resp(&harness.TestResult{Valid: true})
63 return
64 }
65 if (err != nil) != (multierr != nil) {
66 checkErr(fmt.Errorf("different verdict of Validate() [%v] vs. ValidateAll() [%v]", err, multierr))
67 return
68 }
69
70 // Extract the message from "lazy" Validate(), for comparison with ValidateAll()
71 rootCause := err
72 for {
73 caused, ok := rootCause.(hasCause)
74 if !ok || caused.Cause() == nil {
75 break
76 }
77 rootCause = caused.Cause()
78 }
79
80 // Retrieve the messages from "extensive" ValidateAll() and compare first one with the "lazy" message
81 m, ok := multierr.(hasAllErrors)
82 if !ok {
83 checkErr(fmt.Errorf("ValidateAll() returned error without AllErrors() method: %#v", multierr))
84 return
85 }
86 reasons := mergeReasons(nil, m)
87 if rootCause.Error() != reasons[0] {
88 checkErr(fmt.Errorf("different first message, Validate()==%q, ValidateAll()==%q", rootCause.Error(), reasons[0]))
89 return
90 }
91
92 resp(&harness.TestResult{Reasons: reasons})
93 }
94
95 func mergeReasons(reasons []string, multi hasAllErrors) []string {
96 for _, err := range multi.AllErrors() {
97 caused, ok := err.(hasCause)
98 if ok && caused.Cause() != nil {
99 err = caused.Cause()
100 }
101 multi, ok := err.(hasAllErrors)
102 if ok {
103 reasons = mergeReasons(reasons, multi)
104 } else {
105 reasons = append(reasons, err.Error())
106 }
107 }
108 return reasons
109 }
110
111 func checkErr(err error) {
112 if err == nil {
113 return
114 }
115
116 resp(&harness.TestResult{
117 Error: true,
118 Reasons: []string{err.Error()},
119 })
120 }
121
122 func resp(result *harness.TestResult) {
123 if b, err := proto.Marshal(result); err != nil {
124 log.Fatalf("could not marshal response: %v", err)
125 } else if _, err = os.Stdout.Write(b); err != nil {
126 log.Fatalf("could not write response: %v", err)
127 }
128
129 os.Exit(0)
130 }