Rework gobs to fix bad bug related to sharing of id's between encoder and decoder side.

Fix is to move all decoder state into the decoder object.

Fixes #215.

R=rsc
CC=golang-dev
https://golang.org/cl/155077
This commit is contained in:
Rob Pike 2009-11-16 23:32:30 -08:00
parent 50c04132ac
commit 30b1b9a36a
6 changed files with 138 additions and 225 deletions

View file

@ -32,122 +32,10 @@ type ET3 struct {
// Like ET1 but with a different type for a field
type ET4 struct {
a int;
et2 *ET1;
et2 float;
next int;
}
func TestBasicEncoder(t *testing.T) {
b := new(bytes.Buffer);
enc := NewEncoder(b);
et1 := new(ET1);
et1.a = 7;
et1.et2 = new(ET2);
enc.Encode(et1);
if enc.state.err != nil {
t.Error("encoder fail:", enc.state.err)
}
// Decode the result by hand to verify;
state := newDecodeState(b);
// The output should be:
// 0) The length, 38.
length := decodeUint(state);
if length != 38 {
t.Fatal("0. expected length 38; got", length)
}
// 1) -7: the type id of ET1
id1 := decodeInt(state);
if id1 >= 0 {
t.Fatal("expected ET1 negative id; got", id1)
}
// 2) The wireType for ET1
wire1 := new(wireType);
err := decode(b, tWireType, wire1);
if err != nil {
t.Fatal("error decoding ET1 type:", err)
}
info := getTypeInfoNoError(reflect.Typeof(ET1{}));
trueWire1 := &wireType{s: info.id.gobType().(*structType)};
if !reflect.DeepEqual(wire1, trueWire1) {
t.Fatalf("invalid wireType for ET1: expected %+v; got %+v\n", *trueWire1, *wire1)
}
// 3) The length, 21.
length = decodeUint(state);
if length != 21 {
t.Fatal("3. expected length 21; got", length)
}
// 4) -8: the type id of ET2
id2 := decodeInt(state);
if id2 >= 0 {
t.Fatal("expected ET2 negative id; got", id2)
}
// 5) The wireType for ET2
wire2 := new(wireType);
err = decode(b, tWireType, wire2);
if err != nil {
t.Fatal("error decoding ET2 type:", err)
}
info = getTypeInfoNoError(reflect.Typeof(ET2{}));
trueWire2 := &wireType{s: info.id.gobType().(*structType)};
if !reflect.DeepEqual(wire2, trueWire2) {
t.Fatalf("invalid wireType for ET2: expected %+v; got %+v\n", *trueWire2, *wire2)
}
// 6) The length, 6.
length = decodeUint(state);
if length != 6 {
t.Fatal("6. expected length 6; got", length)
}
// 7) The type id for the et1 value
newId1 := decodeInt(state);
if newId1 != -id1 {
t.Fatal("expected Et1 id", -id1, "got", newId1)
}
// 8) The value of et1
newEt1 := new(ET1);
et1Id := getTypeInfoNoError(reflect.Typeof(*newEt1)).id;
err = decode(b, et1Id, newEt1);
if err != nil {
t.Fatal("error decoding ET1 value:", err)
}
if !reflect.DeepEqual(et1, newEt1) {
t.Fatalf("invalid data for et1: expected %+v; got %+v\n", *et1, *newEt1)
}
// 9) EOF
if b.Len() != 0 {
t.Error("not at eof;", b.Len(), "bytes left")
}
// Now do it again. This time we should see only the type id and value.
b.Reset();
enc.Encode(et1);
if enc.state.err != nil {
t.Error("2nd round: encoder fail:", enc.state.err)
}
// The length.
length = decodeUint(state);
if length != 6 {
t.Fatal("6. expected length 6; got", length)
}
// 5a) The type id for the et1 value
newId1 = decodeInt(state);
if newId1 != -id1 {
t.Fatal("2nd round: expected Et1 id", -id1, "got", newId1)
}
// 6a) The value of et1
newEt1 = new(ET1);
err = decode(b, et1Id, newEt1);
if err != nil {
t.Fatal("2nd round: error decoding ET1 value:", err)
}
if !reflect.DeepEqual(et1, newEt1) {
t.Fatalf("2nd round: invalid data for et1: expected %+v; got %+v\n", *et1, *newEt1)
}
// 7a) EOF
if b.Len() != 0 {
t.Error("2nd round: not at eof;", b.Len(), "bytes left")
}
}
func TestEncoderDecoder(t *testing.T) {
b := new(bytes.Buffer);
enc := NewEncoder(b);
@ -215,7 +103,7 @@ func badTypeCheck(e interface{}, shouldFail bool, msg string, t *testing.T) {
t.Error("expected error for", msg)
}
if !shouldFail && (dec.state.err != nil) {
t.Error("unexpected error for", msg)
t.Error("unexpected error for", msg, dec.state.err)
}
}