mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
handle unsupported types safely.
R=rsc DELTA=154 (71 added, 6 deleted, 77 changed) OCL=32483 CL=32492
This commit is contained in:
parent
312bd7a1fc
commit
98607d01fc
6 changed files with 140 additions and 75 deletions
|
|
@ -564,7 +564,7 @@ func TestEndToEnd(t *testing.T) {
|
|||
b := new(bytes.Buffer);
|
||||
encode(b, t1);
|
||||
var _t1 T1;
|
||||
decode(b, getTypeInfo(reflect.Typeof(_t1)).id, &_t1);
|
||||
decode(b, getTypeInfoNoError(reflect.Typeof(_t1)).id, &_t1);
|
||||
if !reflect.DeepEqual(t1, &_t1) {
|
||||
t.Errorf("encode expected %v got %v", *t1, _t1);
|
||||
}
|
||||
|
|
@ -580,7 +580,7 @@ func TestOverflow(t *testing.T) {
|
|||
}
|
||||
var it inputT;
|
||||
var err os.Error;
|
||||
id := getTypeInfo(reflect.Typeof(it)).id;
|
||||
id := getTypeInfoNoError(reflect.Typeof(it)).id;
|
||||
b := new(bytes.Buffer);
|
||||
|
||||
// int8
|
||||
|
|
@ -733,7 +733,7 @@ func TestNesting(t *testing.T) {
|
|||
b := new(bytes.Buffer);
|
||||
encode(b, rt);
|
||||
var drt RT;
|
||||
decode(b, getTypeInfo(reflect.Typeof(drt)).id, &drt);
|
||||
decode(b, getTypeInfoNoError(reflect.Typeof(drt)).id, &drt);
|
||||
if drt.a != rt.a {
|
||||
t.Errorf("nesting: encode expected %v got %v", *rt, drt);
|
||||
}
|
||||
|
|
@ -775,7 +775,7 @@ func TestAutoIndirection(t *testing.T) {
|
|||
b := new(bytes.Buffer);
|
||||
encode(b, t1);
|
||||
var t0 T0;
|
||||
t0Id := getTypeInfo(reflect.Typeof(t0)).id;
|
||||
t0Id := getTypeInfoNoError(reflect.Typeof(t0)).id;
|
||||
decode(b, t0Id, &t0);
|
||||
if t0.a != 17 || t0.b != 177 || t0.c != 1777 || t0.d != 17777 {
|
||||
t.Errorf("t1->t0: expected {17 177 1777 17777}; got %v", t0);
|
||||
|
|
@ -800,7 +800,7 @@ func TestAutoIndirection(t *testing.T) {
|
|||
b.Reset();
|
||||
encode(b, t0);
|
||||
t1 = T1{};
|
||||
t1Id := getTypeInfo(reflect.Typeof(t1)).id;
|
||||
t1Id := getTypeInfoNoError(reflect.Typeof(t1)).id;
|
||||
decode(b, t1Id, &t1);
|
||||
if t1.a != 17 || *t1.b != 177 || **t1.c != 1777 || ***t1.d != 17777 {
|
||||
t.Errorf("t0->t1 expected {17 177 1777 17777}; got {%d %d %d %d}", t1.a, *t1.b, **t1.c, ***t1.d);
|
||||
|
|
@ -810,7 +810,7 @@ func TestAutoIndirection(t *testing.T) {
|
|||
b.Reset();
|
||||
encode(b, t0);
|
||||
t2 = T2{};
|
||||
t2Id := getTypeInfo(reflect.Typeof(t2)).id;
|
||||
t2Id := getTypeInfoNoError(reflect.Typeof(t2)).id;
|
||||
decode(b, t2Id, &t2);
|
||||
if ***t2.a != 17 || **t2.b != 177 || *t2.c != 1777 || t2.d != 17777 {
|
||||
t.Errorf("t0->t2 expected {17 177 1777 17777}; got {%d %d %d %d}", ***t2.a, **t2.b, *t2.c, t2.d);
|
||||
|
|
@ -848,7 +848,7 @@ func TestReorderedFields(t *testing.T) {
|
|||
rt0.c = 3.14159;
|
||||
b := new(bytes.Buffer);
|
||||
encode(b, rt0);
|
||||
rt0Id := getTypeInfo(reflect.Typeof(rt0)).id;
|
||||
rt0Id := getTypeInfoNoError(reflect.Typeof(rt0)).id;
|
||||
var rt1 RT1;
|
||||
// Wire type is RT0, local type is RT1.
|
||||
decode(b, rt0Id, &rt1);
|
||||
|
|
@ -886,7 +886,7 @@ func TestIgnoredFields(t *testing.T) {
|
|||
|
||||
b := new(bytes.Buffer);
|
||||
encode(b, it0);
|
||||
rt0Id := getTypeInfo(reflect.Typeof(it0)).id;
|
||||
rt0Id := getTypeInfoNoError(reflect.Typeof(it0)).id;
|
||||
var rt1 RT1;
|
||||
// Wire type is IT0, local type is RT1.
|
||||
err := decode(b, rt0Id, &rt1);
|
||||
|
|
@ -897,3 +897,20 @@ func TestIgnoredFields(t *testing.T) {
|
|||
t.Errorf("rt1->rt0: expected %v; got %v", it0, rt1);
|
||||
}
|
||||
}
|
||||
|
||||
type Bad0 struct {
|
||||
inter interface{};
|
||||
c float;
|
||||
}
|
||||
|
||||
func TestInvalidField(t *testing.T) {
|
||||
var bad0 Bad0;
|
||||
bad0.inter = 17;
|
||||
b := new(bytes.Buffer);
|
||||
err := encode(b, &bad0);
|
||||
if err == nil {
|
||||
t.Error("expected error; got none")
|
||||
} else if strings.Index(err.String(), "interface") < 0 {
|
||||
t.Error("expected type error; got", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue