v.io/v.io/x/ref/security/audit: Fix test to account for use of verror in v.io/v23/security
This is the second part of a multipart change that uses verror in v.io/v23/security
See
https://vanadium-review.googlesource.com/6961
The only change here is that principal_test.go used reflect.DeepEqual on an error,
and this always found taht two errors were distinct because they had different
stack traces. Alas, DeepEqual is prone to false alarms since it compared implementation
details.
On Asim's advice, I replacec the DeepEqual with a routine that does an error-specific
check if the arguments are arays of errors.
MultiPart: 2/2
Change-Id: I02c2b6989230adb00e71f7c81ba6517de57187d2
diff --git a/security/audit/principal_test.go b/security/audit/principal_test.go
index c4a63b5..c988fa8 100644
--- a/security/audit/principal_test.go
+++ b/security/audit/principal_test.go
@@ -5,12 +5,14 @@
"crypto/elliptic"
"crypto/rand"
"errors"
+ "fmt"
"reflect"
"strings"
"testing"
"time"
"v.io/v23/security"
+ "v.io/v23/verror"
"v.io/x/ref/security/audit"
)
@@ -89,6 +91,32 @@
}
}
+// equalResults returns nil iff the arrays got[] and want[] are equivalent.
+// Equivalent arrays have equal length, and either are equal according to
+// reflect.DeepEqual, or the elements of each are errors with identical verror
+// error codes.
+func equalResults(got, want []interface{}) error {
+ if len(got) != len(want) {
+ return fmt.Errorf("got %d results, want %d (%v vs. %v)", len(got), len(want), got, want)
+ }
+ // Special case comparisons on verror.E
+ for i := range want {
+ if werr, wiserr := want[i].(verror.E); wiserr {
+ // Compare verror ids
+ gerr, giserr := got[i].(verror.E)
+ if !giserr {
+ return fmt.Errorf("result #%d: Got %T, want %T", i, got, want)
+ }
+ if verror.ErrorID(gerr) != verror.ErrorID(werr) {
+ return fmt.Errorf("result #%d: Got error %v, want %v", i, gerr, werr)
+ }
+ } else if !reflect.DeepEqual(got, want) {
+ return fmt.Errorf("result #%d: Got %v, want %v", got, want)
+ }
+ }
+ return nil
+}
+
func TestUnauditedMethodsOnPrincipal(t *testing.T) {
var (
auditor = new(mockAuditor)
@@ -109,7 +137,7 @@
{"BlessingStore", V{}},
}
- for _, test := range tests {
+ for i, test := range tests {
want, err := call(p, test.Method, test.Args)
if err != nil {
t.Fatalf("%v: %v", test.Method, err)
@@ -118,8 +146,8 @@
if err != nil {
t.Fatalf("%v: %v", test.Method, err)
}
- if !reflect.DeepEqual(got, want) {
- t.Errorf("Got %v, want %v", got, want)
+ if err := equalResults(got, want); err != nil {
+ t.Errorf("testcase %d: %v", i, err)
}
if gotEntry := auditor.Release(); !reflect.DeepEqual(gotEntry, audit.Entry{}) {
t.Errorf("Unexpected entry in audit log: %v", gotEntry)