Skip to content

Commit

Permalink
Make sure that Interface() is not called on private values.
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidgecka committed Nov 30, 2016
1 parent aa5f957 commit 75a3369
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
20 changes: 16 additions & 4 deletions equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ func (t *T) isNil(obj interface{}) bool {
return v.IsNil()
}

// Returns a string representation of a Value that is sanitized based on what
// we are allowed to see. Private fields can not be exposed via a call to
// Interface() and trying will cause a panic, so we must use String()
// in that case.
func stringValue(v reflect.Value) string {
if v.CanInterface() {
return fmt.Sprintf("%#v", v.Interface())
} else {
return v.String()
}
}

// Deep comparison. This is based on golang 1.2's reflect.Equal functionality.
func (t *T) deepEqual(
desc string, have, want reflect.Value, ignores []string,
Expand Down Expand Up @@ -206,13 +218,13 @@ func (t *T) deepEqual(
checkNil := func() bool {
if want.IsNil() && !have.IsNil() {
diffs = append(diffs, fmt.Sprintf("%s: not equal.", desc))
diffs = append(diffs, fmt.Sprintf(" have: %#v", have.Interface()))
diffs = append(diffs, fmt.Sprintf(" have: %s", stringValue(have)))
diffs = append(diffs, " want: nil")
return true
} else if !want.IsNil() && have.IsNil() {
diffs = append(diffs, fmt.Sprintf("%s: not equal.", desc))
diffs = append(diffs, " have: nil")
diffs = append(diffs, fmt.Sprintf(" want: %#v", want.Interface()))
diffs = append(diffs, fmt.Sprintf(" want: %s", stringValue(want)))
return true
}
return false
Expand All @@ -224,8 +236,8 @@ func (t *T) deepEqual(
diffs = append(diffs, fmt.Sprintf(
"%s: (len(have): %d, len(want): %d)",
desc, have.Len(), want.Len()))
diffs = append(diffs, fmt.Sprintf(" have: %#v", have.Interface()))
diffs = append(diffs, fmt.Sprintf(" want: %#v", want.Interface()))
diffs = append(diffs, fmt.Sprintf(" have: %s", stringValue(have)))
diffs = append(diffs, fmt.Sprintf(" want: %s", stringValue(want)))
return true
}
return false
Expand Down
10 changes: 10 additions & 0 deletions equal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package testlib

import (
"bytes"
"fmt"
"math/rand"
"os"
Expand Down Expand Up @@ -259,6 +260,15 @@ func TestT_EqualAndNotEqual(t *testing.T) {
[]interface{}{sCust1, sCust2},
[]interface{}{dCust1})

// Structures with private, unexported fields.
sBuff1 := bytes.NewBuffer([]byte{1, 2, 3})
sBuff2 := bytes.NewBuffer([]byte{1, 2, 3})
dBuff1 := bytes.NewBuffer([]byte{1, 2, 3, 4})
dBuff2 := bytes.NewBuffer([]byte{1})
runTest(
[]interface{}{sBuff1, sBuff2},
[]interface{}{dBuff1, dBuff2})

// Structures in a slice.
sCustSlice1 := []testEqualCustomStruct{sCust1, sCust2}
sCustSlice2 := []testEqualCustomStruct{sCust1, sCust2}
Expand Down

0 comments on commit 75a3369

Please sign in to comment.