Fix Read() to return errUnexpectedEOF when EOF is received before all bytes in the frame have been read

This commit is contained in:
Tarmigan Casebolt 2016-04-24 22:30:05 -07:00
parent 703e8da19b
commit ae46df13e9
2 changed files with 30 additions and 0 deletions

View File

@ -821,6 +821,9 @@ func (r messageReader) Read(b []byte) (int, error) {
r.c.readMaskPos = maskBytes(r.c.readMaskKey, r.c.readMaskPos, b[:n]) r.c.readMaskPos = maskBytes(r.c.readMaskKey, r.c.readMaskPos, b[:n])
} }
r.c.readRemaining -= int64(n) r.c.readRemaining -= int64(n)
if r.c.readRemaining > 0 && r.c.readErr == io.EOF {
r.c.readErr = errUnexpectedEOF
}
return n, r.c.readErr return n, r.c.readErr
} }

View File

@ -174,6 +174,33 @@ func TestCloseBeforeFinalFrame(t *testing.T) {
} }
} }
func TestEOFWithinFrame(t *testing.T) {
const bufSize = 512
var b bytes.Buffer
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.Write(make([]byte, bufSize))
w.Close()
b.Truncate(bufSize / 2)
op, r, err := rc.NextReader()
if op != BinaryMessage || err != nil {
t.Fatalf("NextReader() returned %d, %v", op, err)
}
_, err = io.Copy(ioutil.Discard, r)
if err != errUnexpectedEOF {
t.Fatalf("io.Copy() returned %v, want %v", err, errUnexpectedEOF)
}
_, _, err = rc.NextReader()
if err != errUnexpectedEOF {
t.Fatalf("NextReader() returned %v, want %v", err, errUnexpectedEOF)
}
}
func TestEOFBeforeFinalFrame(t *testing.T) { func TestEOFBeforeFinalFrame(t *testing.T) {
const bufSize = 512 const bufSize = 512