Skip to content

Commit

Permalink
Remove Invalid Pack test for ioutil.Discard (#241)
Browse files Browse the repository at this point in the history
The internal definition of ioutil.Discard is not stable, so we should
not be testing packing with such a value.

Also, the `testEncodingInvalidSlices` function is only used in a
single place, so I just moved its implementation into
`TestEncodingPackTypeInvalid`.

Signed-off-by: Joe Richey <[email protected]>
  • Loading branch information
josephlr authored Apr 9, 2021
1 parent 48f82fb commit d331077
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions tpmutil/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,28 @@ type invalidPacked struct {
B uint32
}

func testEncodingInvalidSlices(t *testing.T, f func(io.Writer, interface{}) error) {
func TestEncodingPackTypeInvalid(t *testing.T) {
d := ioutil.Discard

// The packedSize function doesn't handle slices to anything other than bytes.
var invalid []int
if err := f(d, invalid); err == nil {
if err := packType(d, invalid); err == nil {
t.Fatal("The packing function incorrectly succeeds for a slice of integers")
}
if err := f(d, &invalid); err == nil {
if err := packType(d, &invalid); err == nil {
t.Fatal("The packing function incorrectly succeeds for a pointer to a slice of integers")
}

invalid2 := invalidPacked{
A: make([]int, 10),
B: 137,
}
if err := f(d, invalid2); err == nil {
if err := packType(d, invalid2); err == nil {
t.Fatal("The packing function incorrectly succeeds for a struct that contains an integer slice")
}
if err := f(d, &invalid2); err == nil {
if err := packType(d, &invalid2); err == nil {
t.Fatal("The packing function incorrectly succeeds for a pointer to a struct that contains an integer slice")
}

if err := f(d, d); err == nil {
t.Fatal("The packing function incorrectly succeeds for a non-packable value")
}
}

func TestEncodingPackTypeInvalid(t *testing.T) {
f := func(w io.Writer, i interface{}) error {
return packType(w, i)
}

testEncodingInvalidSlices(t, f)
}

type simplePacked struct {
Expand Down

0 comments on commit d331077

Please sign in to comment.