2009-07-09 15:33:43 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gob
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes";
|
2009-07-29 16:10:29 -06:00
|
|
|
"io";
|
2009-07-09 15:33:43 -06:00
|
|
|
"os";
|
|
|
|
"reflect";
|
|
|
|
"testing";
|
|
|
|
)
|
|
|
|
|
|
|
|
type ET2 struct {
|
|
|
|
x string;
|
|
|
|
}
|
|
|
|
|
|
|
|
type ET1 struct {
|
|
|
|
a int;
|
|
|
|
et2 *ET2;
|
|
|
|
next *ET1;
|
|
|
|
}
|
|
|
|
|
2009-07-11 16:45:54 -06:00
|
|
|
// Like ET1 but with a different name for a field
|
|
|
|
type ET3 struct {
|
|
|
|
a int;
|
|
|
|
et2 *ET2;
|
|
|
|
differentNext *ET1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like ET1 but with a different type for a field
|
|
|
|
type ET4 struct {
|
|
|
|
a int;
|
|
|
|
et2 *ET1;
|
2009-07-17 12:38:31 -06:00
|
|
|
next int;
|
2009-07-11 16:45:54 -06:00
|
|
|
}
|
|
|
|
|
2009-07-09 15:33:43 -06:00
|
|
|
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)
|
|
|
|
}
|
2009-07-10 14:44:37 -06:00
|
|
|
|
|
|
|
// Decode the result by hand to verify;
|
2009-07-28 18:20:19 -06:00
|
|
|
state := newDecodeState(b);
|
2009-07-10 14:44:37 -06:00
|
|
|
// The output should be:
|
2009-07-15 17:10:17 -06:00
|
|
|
// 0) The length, 38.
|
|
|
|
length := decodeUint(state);
|
|
|
|
if length != 38 {
|
|
|
|
t.Fatal("0. expected length 38; got", length);
|
|
|
|
}
|
2009-07-10 14:44:37 -06:00
|
|
|
// 1) -7: the type id of ET1
|
2009-07-15 17:10:17 -06:00
|
|
|
id1 := decodeInt(state);
|
2009-07-10 14:44:37 -06:00
|
|
|
if id1 >= 0 {
|
|
|
|
t.Fatal("expected ET1 negative id; got", id1);
|
|
|
|
}
|
|
|
|
// 2) The wireType for ET1
|
|
|
|
wire1 := new(wireType);
|
2009-07-16 18:55:16 -06:00
|
|
|
err := decode(b, tWireType, wire1);
|
2009-07-10 14:44:37 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("error decoding ET1 type:", err);
|
|
|
|
}
|
2009-07-29 18:24:25 -06:00
|
|
|
info := getTypeInfoNoError(reflect.Typeof(ET1{}));
|
2009-07-27 12:02:06 -06:00
|
|
|
trueWire1 := &wireType{s: info.id.gobType().(*structType)};
|
2009-07-10 14:44:37 -06:00
|
|
|
if !reflect.DeepEqual(wire1, trueWire1) {
|
|
|
|
t.Fatalf("invalid wireType for ET1: expected %+v; got %+v\n", *trueWire1, *wire1);
|
|
|
|
}
|
2009-07-15 17:10:17 -06:00
|
|
|
// 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);
|
2009-07-10 14:44:37 -06:00
|
|
|
if id2 >= 0 {
|
|
|
|
t.Fatal("expected ET2 negative id; got", id2);
|
|
|
|
}
|
2009-07-15 17:10:17 -06:00
|
|
|
// 5) The wireType for ET2
|
2009-07-10 14:44:37 -06:00
|
|
|
wire2 := new(wireType);
|
2009-07-16 18:55:16 -06:00
|
|
|
err = decode(b, tWireType, wire2);
|
2009-07-10 14:44:37 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("error decoding ET2 type:", err);
|
|
|
|
}
|
2009-07-29 18:24:25 -06:00
|
|
|
info = getTypeInfoNoError(reflect.Typeof(ET2{}));
|
2009-07-27 12:02:06 -06:00
|
|
|
trueWire2 := &wireType{s: info.id.gobType().(*structType)};
|
2009-07-10 14:44:37 -06:00
|
|
|
if !reflect.DeepEqual(wire2, trueWire2) {
|
|
|
|
t.Fatalf("invalid wireType for ET2: expected %+v; got %+v\n", *trueWire2, *wire2);
|
|
|
|
}
|
2009-07-15 17:10:17 -06:00
|
|
|
// 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);
|
2009-07-10 14:44:37 -06:00
|
|
|
if newId1 != -id1 {
|
|
|
|
t.Fatal("expected Et1 id", -id1, "got", newId1);
|
|
|
|
}
|
2009-07-15 17:10:17 -06:00
|
|
|
// 8) The value of et1
|
2009-07-10 14:44:37 -06:00
|
|
|
newEt1 := new(ET1);
|
2009-07-29 18:24:25 -06:00
|
|
|
et1Id := getTypeInfoNoError(reflect.Typeof(*newEt1)).id;
|
2009-07-16 18:55:16 -06:00
|
|
|
err = decode(b, et1Id, newEt1);
|
2009-07-10 14:44:37 -06:00
|
|
|
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);
|
|
|
|
}
|
2009-07-15 17:10:17 -06:00
|
|
|
// 9) EOF
|
2009-07-10 14:44:37 -06:00
|
|
|
if b.Len() != 0 {
|
|
|
|
t.Error("not at eof;", b.Len(), "bytes left")
|
|
|
|
}
|
2009-07-10 14:50:44 -06:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2009-07-15 17:10:17 -06:00
|
|
|
// The length.
|
|
|
|
length = decodeUint(state);
|
|
|
|
if length != 6 {
|
|
|
|
t.Fatal("6. expected length 6; got", length);
|
|
|
|
}
|
2009-07-10 14:50:44 -06:00
|
|
|
// 5a) The type id for the et1 value
|
2009-07-15 17:10:17 -06:00
|
|
|
newId1 = decodeInt(state);
|
2009-07-10 14:50:44 -06:00
|
|
|
if newId1 != -id1 {
|
|
|
|
t.Fatal("2nd round: expected Et1 id", -id1, "got", newId1);
|
|
|
|
}
|
|
|
|
// 6a) The value of et1
|
|
|
|
newEt1 = new(ET1);
|
2009-07-16 18:55:16 -06:00
|
|
|
err = decode(b, et1Id, newEt1);
|
2009-07-10 14:50:44 -06:00
|
|
|
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")
|
|
|
|
}
|
2009-07-09 15:33:43 -06:00
|
|
|
}
|
2009-07-11 16:45:54 -06:00
|
|
|
|
|
|
|
func TestEncoderDecoder(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)
|
|
|
|
}
|
|
|
|
dec := NewDecoder(b);
|
|
|
|
newEt1 := new(ET1);
|
|
|
|
dec.Decode(newEt1);
|
|
|
|
if dec.state.err != nil {
|
2009-07-16 14:05:46 -06:00
|
|
|
t.Fatal("error decoding ET1:", dec.state.err);
|
2009-07-11 16:45:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(et1, newEt1) {
|
|
|
|
t.Fatalf("invalid data for et1: expected %+v; got %+v\n", *et1, *newEt1);
|
|
|
|
}
|
|
|
|
if b.Len() != 0 {
|
|
|
|
t.Error("not at eof;", b.Len(), "bytes left")
|
|
|
|
}
|
|
|
|
|
|
|
|
enc.Encode(et1);
|
|
|
|
newEt1 = new(ET1);
|
|
|
|
dec.Decode(newEt1);
|
|
|
|
if dec.state.err != nil {
|
2009-07-16 14:05:46 -06:00
|
|
|
t.Fatal("round 2: error decoding ET1:", dec.state.err);
|
2009-07-11 16:45:54 -06:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(et1, newEt1) {
|
|
|
|
t.Fatalf("round 2: invalid data for et1: expected %+v; got %+v\n", *et1, *newEt1);
|
|
|
|
}
|
|
|
|
if b.Len() != 0 {
|
|
|
|
t.Error("round 2: not at eof;", b.Len(), "bytes left")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now test with a running encoder/decoder pair that we recognize a type mismatch.
|
|
|
|
enc.Encode(et1);
|
|
|
|
if enc.state.err != nil {
|
|
|
|
t.Error("round 3: encoder fail:", enc.state.err)
|
|
|
|
}
|
|
|
|
newEt2 := new(ET2);
|
|
|
|
dec.Decode(newEt2);
|
|
|
|
if dec.state.err == nil {
|
2009-07-16 14:05:46 -06:00
|
|
|
t.Fatal("round 3: expected `bad type' error decoding ET2");
|
2009-07-11 16:45:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run one value through the encoder/decoder, but use the wrong type.
|
2009-07-17 12:38:31 -06:00
|
|
|
// Input is always an ET1; we compare it to whatever is under 'e'.
|
|
|
|
func badTypeCheck(e interface{}, shouldFail bool, msg string, t *testing.T) {
|
2009-07-11 16:45:54 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
dec := NewDecoder(b);
|
|
|
|
dec.Decode(e);
|
2009-07-17 12:38:31 -06:00
|
|
|
if shouldFail && (dec.state.err == nil) {
|
2009-07-11 16:45:54 -06:00
|
|
|
t.Error("expected error for", msg);
|
|
|
|
}
|
2009-07-17 12:38:31 -06:00
|
|
|
if !shouldFail && (dec.state.err != nil) {
|
|
|
|
t.Error("unexpected error for", msg);
|
|
|
|
}
|
2009-07-11 16:45:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that we recognize a bad type the first time.
|
|
|
|
func TestWrongTypeDecoder(t *testing.T) {
|
2009-07-17 12:38:31 -06:00
|
|
|
badTypeCheck(new(ET2), true, "no fields in common", t);
|
|
|
|
badTypeCheck(new(ET3), false, "different name of field", t);
|
|
|
|
badTypeCheck(new(ET4), true, "different type of field", t);
|
2009-07-11 16:45:54 -06:00
|
|
|
}
|
2009-07-29 16:10:29 -06:00
|
|
|
|
|
|
|
func corruptDataCheck(s string, err os.Error, t *testing.T) {
|
2009-09-21 13:59:14 -06:00
|
|
|
b := bytes.NewBufferString(s);
|
2009-07-29 16:10:29 -06:00
|
|
|
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) {
|
2009-07-29 16:24:42 -06:00
|
|
|
corruptDataCheck("", os.EOF, t);
|
2009-07-29 16:10:29 -06:00
|
|
|
corruptDataCheck("\x7Fhi", io.ErrUnexpectedEOF, t);
|
|
|
|
corruptDataCheck("\x03now is the time for all good men", errBadType, t);
|
|
|
|
}
|
2009-08-30 20:46:35 -06:00
|
|
|
|
|
|
|
// Types not supported by the Encoder (only structs work at the top level).
|
|
|
|
// Basic types work implicitly.
|
|
|
|
var unsupportedValues = []interface{} {
|
|
|
|
[]int{ 1, 2, 3 },
|
|
|
|
[3]int{ 1, 2, 3 },
|
|
|
|
make(chan int),
|
|
|
|
func(a int) bool { return true },
|
|
|
|
make(map[string] int),
|
|
|
|
new(interface{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnsupported(t *testing.T) {
|
|
|
|
var b bytes.Buffer;
|
|
|
|
enc := NewEncoder(&b);
|
|
|
|
for _, v := range unsupportedValues {
|
|
|
|
err := enc.Encode(v);
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error for %T; got none", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|