Test truncated frames at all positions

This commit is contained in:
Gary Burd 2016-04-25 08:42:53 -07:00
parent ae46df13e9
commit a724ba4528
1 changed files with 27 additions and 19 deletions

View File

@ -175,29 +175,37 @@ func TestCloseBeforeFinalFrame(t *testing.T) {
} }
func TestEOFWithinFrame(t *testing.T) { func TestEOFWithinFrame(t *testing.T) {
const bufSize = 512 const bufSize = 64
var b bytes.Buffer for n := 0; ; n++ {
wc := newConn(fakeNetConn{Reader: nil, Writer: &b}, false, 1024, 1024) var b bytes.Buffer
rc := newConn(fakeNetConn{Reader: &b, Writer: nil}, true, 1024, 1024) wc := newConn(fakeNetConn{Reader: nil, Writer: &b}, false, 1024, 1024)
rc := newConn(fakeNetConn{Reader: &b, Writer: nil}, true, 1024, 1024)
w, _ := wc.NextWriter(BinaryMessage) w, _ := wc.NextWriter(BinaryMessage)
w.Write(make([]byte, bufSize)) w.Write(make([]byte, bufSize))
w.Close() w.Close()
b.Truncate(bufSize / 2) if n >= b.Len() {
break
}
b.Truncate(n)
op, r, err := rc.NextReader() op, r, err := rc.NextReader()
if op != BinaryMessage || err != nil { if err == errUnexpectedEOF {
t.Fatalf("NextReader() returned %d, %v", op, err) continue
} }
_, err = io.Copy(ioutil.Discard, r) if op != BinaryMessage || err != nil {
if err != errUnexpectedEOF { t.Fatalf("%d: NextReader() returned %d, %v", n, op, err)
t.Fatalf("io.Copy() returned %v, want %v", err, errUnexpectedEOF) }
} _, err = io.Copy(ioutil.Discard, r)
_, _, err = rc.NextReader() if err != errUnexpectedEOF {
if err != errUnexpectedEOF { t.Fatalf("%d: io.Copy() returned %v, want %v", n, err, errUnexpectedEOF)
t.Fatalf("NextReader() returned %v, want %v", err, errUnexpectedEOF) }
_, _, err = rc.NextReader()
if err != errUnexpectedEOF {
t.Fatalf("%d: NextReader() returned %v, want %v", n, err, errUnexpectedEOF)
}
} }
} }