@@ -16,6 +16,7 @@ package pbutil
1616
1717import (
1818 "bytes"
19+ "encoding/binary"
1920 "errors"
2021 "io"
2122 "testing"
@@ -29,29 +30,34 @@ import (
2930
3031func TestReadDelimitedIllegalVarint (t * testing.T ) {
3132 var tests = []struct {
32- in [] byte
33- n int
34- err error
33+ name string
34+ in [] byte
35+ n int
3536 }{
3637 {
37- in : [] byte { 255 , 255 , 255 , 255 , 255 } ,
38- n : 5 ,
39- err : errInvalidVarint ,
38+ name : "all 0xFF" ,
39+ in : [] byte { 255 , 255 , 255 , 255 , 255 } ,
40+ n : 5 ,
4041 },
42+
43+ // Ensure ReadDelimited eventually stops parsing a varint instead of
44+ // looping as long as the input bytes have the continuation bit set.
4145 {
42- in : [] byte { 255 , 255 , 255 , 255 , 255 , 255 } ,
43- n : 5 ,
44- err : errInvalidVarint ,
46+ name : "infinite continuation bits" ,
47+ in : bytes . Repeat ([] byte { 255 }, 2 * binary . MaxVarintLen64 ) ,
48+ n : binary . MaxVarintLen64 ,
4549 },
4650 }
4751 for _ , test := range tests {
48- n , err := ReadDelimited (bytes .NewReader (test .in ), nil )
49- if got , want := n , test .n ; ! cmp .Equal (got , want ) {
50- t .Errorf ("ReadDelimited(%#v, nil) = %#v, ?; want = %#v, ?" , test .in , got , want )
51- }
52- if got , want := err , test .err ; ! errors .Is (got , want ) {
53- t .Errorf ("ReadDelimited(%#v, nil) = ?, %#v; want = ?, %#v" , test .in , got , want )
54- }
52+ t .Run (test .name , func (t * testing.T ) {
53+ n , err := ReadDelimited (bytes .NewReader (test .in ), nil )
54+ if got , want := n , test .n ; ! cmp .Equal (got , want ) {
55+ t .Errorf ("ReadDelimited(%#v, nil) = %#v, ?; want = %#v, ?" , test .in , got , want )
56+ }
57+ if err == nil {
58+ t .Errorf ("ReadDelimited(%#v) unexpectedly did not result in an error" , test .in )
59+ }
60+ })
5561 }
5662}
5763
@@ -61,7 +67,7 @@ func TestReadDelimitedPrematureHeader(t *testing.T) {
6167 if got , want := n , 1 ; ! cmp .Equal (got , want ) {
6268 t .Errorf ("ReadDelimited(%#v, nil) = %#v, ?; want = %#v, ?" , data [0 :1 ], got , want )
6369 }
64- if got , want := err , io .EOF ; ! errors .Is (got , want ) {
70+ if got , want := err , io .ErrUnexpectedEOF ; ! errors .Is (got , want ) {
6571 t .Errorf ("ReadDelimited(%#v, nil) = ?, %#v; want = ?, %#v" , data [0 :1 ], got , want )
6672 }
6773}
@@ -83,7 +89,7 @@ func TestReadDelimitedPrematureHeaderIncremental(t *testing.T) {
8389 if got , want := n , 1 ; ! cmp .Equal (got , want ) {
8490 t .Errorf ("ReadDelimited(%#v, nil) = %#v, ?; want = %#v, ?" , data [0 :1 ], got , want )
8591 }
86- if got , want := err , io .EOF ; ! errors .Is (got , want ) {
92+ if got , want := err , io .ErrUnexpectedEOF ; ! errors .Is (got , want ) {
8793 t .Errorf ("ReadDelimited(%#v, nil) = ?, %#v; want = ?, %#v" , data [0 :1 ], got , want )
8894 }
8995}
0 commit comments