handle some error conditions involving bad data.

R=rsc
DELTA=32  (24 added, 1 deleted, 7 changed)
OCL=32461
CL=32463
This commit is contained in:
Rob Pike 2009-07-29 15:10:29 -07:00
parent 2946069e3f
commit 1f6e18fdce
3 changed files with 31 additions and 8 deletions

View file

@ -7,6 +7,7 @@ package gob
import (
"bytes";
"gob";
"io";
"os";
"reflect";
"strings";
@ -227,3 +228,19 @@ func TestWrongTypeDecoder(t *testing.T) {
badTypeCheck(new(ET3), false, "different name of field", t);
badTypeCheck(new(ET4), true, "different type of field", t);
}
func corruptDataCheck(s string, err os.Error, t *testing.T) {
b := bytes.NewBuffer(strings.Bytes(s));
dec := NewDecoder(b);
dec.Decode(new(ET2));
if dec.state.err != err {
t.Error("expected error", err, "got", dec.state.err);
}
}
// Check that we survive bad data.
func TestBadData(t *testing.T) {
corruptDataCheck("\x01\x01\x01", os.EOF, t);
corruptDataCheck("\x7Fhi", io.ErrUnexpectedEOF, t);
corruptDataCheck("\x03now is the time for all good men", errBadType, t);
}