OSDN Git Service

5bc957bb37011eb144eaf6248da2e776f1d84f8c
[pf3gnuchains/gcc-fork.git] / libgo / go / encoding / gob / encoder_test.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package gob
6
7 import (
8         "bytes"
9         "fmt"
10         "io"
11         "reflect"
12         "strings"
13         "testing"
14 )
15
16 type ET2 struct {
17         X string
18 }
19
20 type ET1 struct {
21         A    int
22         Et2  *ET2
23         Next *ET1
24 }
25
26 // Like ET1 but with a different name for a field
27 type ET3 struct {
28         A             int
29         Et2           *ET2
30         DifferentNext *ET1
31 }
32
33 // Like ET1 but with a different type for a field
34 type ET4 struct {
35         A    int
36         Et2  float64
37         Next int
38 }
39
40 func TestEncoderDecoder(t *testing.T) {
41         b := new(bytes.Buffer)
42         enc := NewEncoder(b)
43         et1 := new(ET1)
44         et1.A = 7
45         et1.Et2 = new(ET2)
46         err := enc.Encode(et1)
47         if err != nil {
48                 t.Error("encoder fail:", err)
49         }
50         dec := NewDecoder(b)
51         newEt1 := new(ET1)
52         err = dec.Decode(newEt1)
53         if err != nil {
54                 t.Fatal("error decoding ET1:", err)
55         }
56
57         if !reflect.DeepEqual(et1, newEt1) {
58                 t.Fatalf("invalid data for et1: expected %+v; got %+v", *et1, *newEt1)
59         }
60         if b.Len() != 0 {
61                 t.Error("not at eof;", b.Len(), "bytes left")
62         }
63
64         enc.Encode(et1)
65         newEt1 = new(ET1)
66         err = dec.Decode(newEt1)
67         if err != nil {
68                 t.Fatal("round 2: error decoding ET1:", err)
69         }
70         if !reflect.DeepEqual(et1, newEt1) {
71                 t.Fatalf("round 2: invalid data for et1: expected %+v; got %+v", *et1, *newEt1)
72         }
73         if b.Len() != 0 {
74                 t.Error("round 2: not at eof;", b.Len(), "bytes left")
75         }
76
77         // Now test with a running encoder/decoder pair that we recognize a type mismatch.
78         err = enc.Encode(et1)
79         if err != nil {
80                 t.Error("round 3: encoder fail:", err)
81         }
82         newEt2 := new(ET2)
83         err = dec.Decode(newEt2)
84         if err == nil {
85                 t.Fatal("round 3: expected `bad type' error decoding ET2")
86         }
87 }
88
89 // Run one value through the encoder/decoder, but use the wrong type.
90 // Input is always an ET1; we compare it to whatever is under 'e'.
91 func badTypeCheck(e interface{}, shouldFail bool, msg string, t *testing.T) {
92         b := new(bytes.Buffer)
93         enc := NewEncoder(b)
94         et1 := new(ET1)
95         et1.A = 7
96         et1.Et2 = new(ET2)
97         err := enc.Encode(et1)
98         if err != nil {
99                 t.Error("encoder fail:", err)
100         }
101         dec := NewDecoder(b)
102         err = dec.Decode(e)
103         if shouldFail && err == nil {
104                 t.Error("expected error for", msg)
105         }
106         if !shouldFail && err != nil {
107                 t.Error("unexpected error for", msg, err)
108         }
109 }
110
111 // Test that we recognize a bad type the first time.
112 func TestWrongTypeDecoder(t *testing.T) {
113         badTypeCheck(new(ET2), true, "no fields in common", t)
114         badTypeCheck(new(ET3), false, "different name of field", t)
115         badTypeCheck(new(ET4), true, "different type of field", t)
116 }
117
118 func corruptDataCheck(s string, err error, t *testing.T) {
119         b := bytes.NewBufferString(s)
120         dec := NewDecoder(b)
121         err1 := dec.Decode(new(ET2))
122         if err1 != err {
123                 t.Errorf("from %q expected error %s; got %s", s, err, err1)
124         }
125 }
126
127 // Check that we survive bad data.
128 func TestBadData(t *testing.T) {
129         corruptDataCheck("", io.EOF, t)
130         corruptDataCheck("\x7Fhi", io.ErrUnexpectedEOF, t)
131         corruptDataCheck("\x03now is the time for all good men", errBadType, t)
132 }
133
134 // Types not supported by the Encoder.
135 var unsupportedValues = []interface{}{
136         make(chan int),
137         func(a int) bool { return true },
138 }
139
140 func TestUnsupported(t *testing.T) {
141         var b bytes.Buffer
142         enc := NewEncoder(&b)
143         for _, v := range unsupportedValues {
144                 err := enc.Encode(v)
145                 if err == nil {
146                         t.Errorf("expected error for %T; got none", v)
147                 }
148         }
149 }
150
151 func encAndDec(in, out interface{}) error {
152         b := new(bytes.Buffer)
153         enc := NewEncoder(b)
154         err := enc.Encode(in)
155         if err != nil {
156                 return err
157         }
158         dec := NewDecoder(b)
159         err = dec.Decode(out)
160         if err != nil {
161                 return err
162         }
163         return nil
164 }
165
166 func TestTypeToPtrType(t *testing.T) {
167         // Encode a T, decode a *T
168         type Type0 struct {
169                 A int
170         }
171         t0 := Type0{7}
172         t0p := new(Type0)
173         if err := encAndDec(t0, t0p); err != nil {
174                 t.Error(err)
175         }
176 }
177
178 func TestPtrTypeToType(t *testing.T) {
179         // Encode a *T, decode a T
180         type Type1 struct {
181                 A uint
182         }
183         t1p := &Type1{17}
184         var t1 Type1
185         if err := encAndDec(t1, t1p); err != nil {
186                 t.Error(err)
187         }
188 }
189
190 func TestTypeToPtrPtrPtrPtrType(t *testing.T) {
191         type Type2 struct {
192                 A ****float64
193         }
194         t2 := Type2{}
195         t2.A = new(***float64)
196         *t2.A = new(**float64)
197         **t2.A = new(*float64)
198         ***t2.A = new(float64)
199         ****t2.A = 27.4
200         t2pppp := new(***Type2)
201         if err := encAndDec(t2, t2pppp); err != nil {
202                 t.Fatal(err)
203         }
204         if ****(****t2pppp).A != ****t2.A {
205                 t.Errorf("wrong value after decode: %g not %g", ****(****t2pppp).A, ****t2.A)
206         }
207 }
208
209 func TestSlice(t *testing.T) {
210         type Type3 struct {
211                 A []string
212         }
213         t3p := &Type3{[]string{"hello", "world"}}
214         var t3 Type3
215         if err := encAndDec(t3, t3p); err != nil {
216                 t.Error(err)
217         }
218 }
219
220 func TestValueError(t *testing.T) {
221         // Encode a *T, decode a T
222         type Type4 struct {
223                 A int
224         }
225         t4p := &Type4{3}
226         var t4 Type4 // note: not a pointer.
227         if err := encAndDec(t4p, t4); err == nil || strings.Index(err.Error(), "pointer") < 0 {
228                 t.Error("expected error about pointer; got", err)
229         }
230 }
231
232 func TestArray(t *testing.T) {
233         type Type5 struct {
234                 A [3]string
235                 B [3]byte
236         }
237         type Type6 struct {
238                 A [2]string // can't hold t5.a
239         }
240         t5 := Type5{[3]string{"hello", ",", "world"}, [3]byte{1, 2, 3}}
241         var t5p Type5
242         if err := encAndDec(t5, &t5p); err != nil {
243                 t.Error(err)
244         }
245         var t6 Type6
246         if err := encAndDec(t5, &t6); err == nil {
247                 t.Error("should fail with mismatched array sizes")
248         }
249 }
250
251 func TestRecursiveMapType(t *testing.T) {
252         type recursiveMap map[string]recursiveMap
253         r1 := recursiveMap{"A": recursiveMap{"B": nil, "C": nil}, "D": nil}
254         r2 := make(recursiveMap)
255         if err := encAndDec(r1, &r2); err != nil {
256                 t.Error(err)
257         }
258 }
259
260 func TestRecursiveSliceType(t *testing.T) {
261         type recursiveSlice []recursiveSlice
262         r1 := recursiveSlice{0: recursiveSlice{0: nil}, 1: nil}
263         r2 := make(recursiveSlice, 0)
264         if err := encAndDec(r1, &r2); err != nil {
265                 t.Error(err)
266         }
267 }
268
269 // Regression test for bug: must send zero values inside arrays
270 func TestDefaultsInArray(t *testing.T) {
271         type Type7 struct {
272                 B []bool
273                 I []int
274                 S []string
275                 F []float64
276         }
277         t7 := Type7{
278                 []bool{false, false, true},
279                 []int{0, 0, 1},
280                 []string{"hi", "", "there"},
281                 []float64{0, 0, 1},
282         }
283         var t7p Type7
284         if err := encAndDec(t7, &t7p); err != nil {
285                 t.Error(err)
286         }
287 }
288
289 var testInt int
290 var testFloat32 float32
291 var testString string
292 var testSlice []string
293 var testMap map[string]int
294 var testArray [7]int
295
296 type SingleTest struct {
297         in  interface{}
298         out interface{}
299         err string
300 }
301
302 var singleTests = []SingleTest{
303         {17, &testInt, ""},
304         {float32(17.5), &testFloat32, ""},
305         {"bike shed", &testString, ""},
306         {[]string{"bike", "shed", "paint", "color"}, &testSlice, ""},
307         {map[string]int{"seven": 7, "twelve": 12}, &testMap, ""},
308         {[7]int{4, 55, 0, 0, 0, 0, 0}, &testArray, ""}, // case that once triggered a bug
309         {[7]int{4, 55, 1, 44, 22, 66, 1234}, &testArray, ""},
310
311         // Decode errors
312         {172, &testFloat32, "wrong type"},
313 }
314
315 func TestSingletons(t *testing.T) {
316         b := new(bytes.Buffer)
317         enc := NewEncoder(b)
318         dec := NewDecoder(b)
319         for _, test := range singleTests {
320                 b.Reset()
321                 err := enc.Encode(test.in)
322                 if err != nil {
323                         t.Errorf("error encoding %v: %s", test.in, err)
324                         continue
325                 }
326                 err = dec.Decode(test.out)
327                 switch {
328                 case err != nil && test.err == "":
329                         t.Errorf("error decoding %v: %s", test.in, err)
330                         continue
331                 case err == nil && test.err != "":
332                         t.Errorf("expected error decoding %v: %s", test.in, test.err)
333                         continue
334                 case err != nil && test.err != "":
335                         if strings.Index(err.Error(), test.err) < 0 {
336                                 t.Errorf("wrong error decoding %v: wanted %s, got %v", test.in, test.err, err)
337                         }
338                         continue
339                 }
340                 // Get rid of the pointer in the rhs
341                 val := reflect.ValueOf(test.out).Elem().Interface()
342                 if !reflect.DeepEqual(test.in, val) {
343                         t.Errorf("decoding singleton: expected %v got %v", test.in, val)
344                 }
345         }
346 }
347
348 func TestStructNonStruct(t *testing.T) {
349         type Struct struct {
350                 A string
351         }
352         type NonStruct string
353         s := Struct{"hello"}
354         var sp Struct
355         if err := encAndDec(s, &sp); err != nil {
356                 t.Error(err)
357         }
358         var ns NonStruct
359         if err := encAndDec(s, &ns); err == nil {
360                 t.Error("should get error for struct/non-struct")
361         } else if strings.Index(err.Error(), "type") < 0 {
362                 t.Error("for struct/non-struct expected type error; got", err)
363         }
364         // Now try the other way
365         var nsp NonStruct
366         if err := encAndDec(ns, &nsp); err != nil {
367                 t.Error(err)
368         }
369         if err := encAndDec(ns, &s); err == nil {
370                 t.Error("should get error for non-struct/struct")
371         } else if strings.Index(err.Error(), "type") < 0 {
372                 t.Error("for non-struct/struct expected type error; got", err)
373         }
374 }
375
376 type interfaceIndirectTestI interface {
377         F() bool
378 }
379
380 type interfaceIndirectTestT struct{}
381
382 func (this *interfaceIndirectTestT) F() bool {
383         return true
384 }
385
386 // A version of a bug reported on golang-nuts.  Also tests top-level
387 // slice of interfaces.  The issue was registering *T caused T to be
388 // stored as the concrete type.
389 func TestInterfaceIndirect(t *testing.T) {
390         Register(&interfaceIndirectTestT{})
391         b := new(bytes.Buffer)
392         w := []interfaceIndirectTestI{&interfaceIndirectTestT{}}
393         err := NewEncoder(b).Encode(w)
394         if err != nil {
395                 t.Fatal("encode error:", err)
396         }
397
398         var r []interfaceIndirectTestI
399         err = NewDecoder(b).Decode(&r)
400         if err != nil {
401                 t.Fatal("decode error:", err)
402         }
403 }
404
405 // Now follow various tests that decode into things that can't represent the
406 // encoded value, all of which should be legal.
407
408 // Also, when the ignored object contains an interface value, it may define
409 // types. Make sure that skipping the value still defines the types by using
410 // the encoder/decoder pair to send a value afterwards.  If an interface
411 // is sent, its type in the test is always NewType0, so this checks that the
412 // encoder and decoder don't skew with respect to type definitions.
413
414 type Struct0 struct {
415         I interface{}
416 }
417
418 type NewType0 struct {
419         S string
420 }
421
422 type ignoreTest struct {
423         in, out interface{}
424 }
425
426 var ignoreTests = []ignoreTest{
427         // Decode normal struct into an empty struct
428         {&struct{ A int }{23}, &struct{}{}},
429         // Decode normal struct into a nil.
430         {&struct{ A int }{23}, nil},
431         // Decode singleton string into a nil.
432         {"hello, world", nil},
433         // Decode singleton slice into a nil.
434         {[]int{1, 2, 3, 4}, nil},
435         // Decode struct containing an interface into a nil.
436         {&Struct0{&NewType0{"value0"}}, nil},
437         // Decode singleton slice of interfaces into a nil.
438         {[]interface{}{"hi", &NewType0{"value1"}, 23}, nil},
439 }
440
441 func TestDecodeIntoNothing(t *testing.T) {
442         Register(new(NewType0))
443         for i, test := range ignoreTests {
444                 b := new(bytes.Buffer)
445                 enc := NewEncoder(b)
446                 err := enc.Encode(test.in)
447                 if err != nil {
448                         t.Errorf("%d: encode error %s:", i, err)
449                         continue
450                 }
451                 dec := NewDecoder(b)
452                 err = dec.Decode(test.out)
453                 if err != nil {
454                         t.Errorf("%d: decode error: %s", i, err)
455                         continue
456                 }
457                 // Now see if the encoder and decoder are in a consistent state.
458                 str := fmt.Sprintf("Value %d", i)
459                 err = enc.Encode(&NewType0{str})
460                 if err != nil {
461                         t.Fatalf("%d: NewType0 encode error: %s", i, err)
462                 }
463                 ns := new(NewType0)
464                 err = dec.Decode(ns)
465                 if err != nil {
466                         t.Fatalf("%d: NewType0 decode error: %s", i, err)
467                 }
468                 if ns.S != str {
469                         t.Fatalf("%d: expected %q got %q", i, str, ns.S)
470                 }
471         }
472 }
473
474 // Another bug from golang-nuts, involving nested interfaces.
475 type Bug0Outer struct {
476         Bug0Field interface{}
477 }
478
479 type Bug0Inner struct {
480         A int
481 }
482
483 func TestNestedInterfaces(t *testing.T) {
484         var buf bytes.Buffer
485         e := NewEncoder(&buf)
486         d := NewDecoder(&buf)
487         Register(new(Bug0Outer))
488         Register(new(Bug0Inner))
489         f := &Bug0Outer{&Bug0Outer{&Bug0Inner{7}}}
490         var v interface{} = f
491         err := e.Encode(&v)
492         if err != nil {
493                 t.Fatal("Encode:", err)
494         }
495         err = d.Decode(&v)
496         if err != nil {
497                 t.Fatal("Decode:", err)
498         }
499         // Make sure it decoded correctly.
500         outer1, ok := v.(*Bug0Outer)
501         if !ok {
502                 t.Fatalf("v not Bug0Outer: %T", v)
503         }
504         outer2, ok := outer1.Bug0Field.(*Bug0Outer)
505         if !ok {
506                 t.Fatalf("v.Bug0Field not Bug0Outer: %T", outer1.Bug0Field)
507         }
508         inner, ok := outer2.Bug0Field.(*Bug0Inner)
509         if !ok {
510                 t.Fatalf("v.Bug0Field.Bug0Field not Bug0Inner: %T", outer2.Bug0Field)
511         }
512         if inner.A != 7 {
513                 t.Fatalf("final value %d; expected %d", inner.A, 7)
514         }
515 }
516
517 // The bugs keep coming. We forgot to send map subtypes before the map.
518
519 type Bug1Elem struct {
520         Name string
521         Id   int
522 }
523
524 type Bug1StructMap map[string]Bug1Elem
525
526 func bug1EncDec(in Bug1StructMap, out *Bug1StructMap) error {
527         return nil
528 }
529
530 func TestMapBug1(t *testing.T) {
531         in := make(Bug1StructMap)
532         in["val1"] = Bug1Elem{"elem1", 1}
533         in["val2"] = Bug1Elem{"elem2", 2}
534
535         b := new(bytes.Buffer)
536         enc := NewEncoder(b)
537         err := enc.Encode(in)
538         if err != nil {
539                 t.Fatal("encode:", err)
540         }
541         dec := NewDecoder(b)
542         out := make(Bug1StructMap)
543         err = dec.Decode(&out)
544         if err != nil {
545                 t.Fatal("decode:", err)
546         }
547         if !reflect.DeepEqual(in, out) {
548                 t.Errorf("mismatch: %v %v", in, out)
549         }
550 }
551
552 func TestGobMapInterfaceEncode(t *testing.T) {
553         m := map[string]interface{}{
554                 "up": uintptr(0),
555                 "i0": []int{-1},
556                 "i1": []int8{-1},
557                 "i2": []int16{-1},
558                 "i3": []int32{-1},
559                 "i4": []int64{-1},
560                 "u0": []uint{1},
561                 "u1": []uint8{1},
562                 "u2": []uint16{1},
563                 "u3": []uint32{1},
564                 "u4": []uint64{1},
565                 "f0": []float32{1},
566                 "f1": []float64{1},
567                 "c0": []complex64{complex(2, -2)},
568                 "c1": []complex128{complex(2, float64(-2))},
569                 "us": []uintptr{0},
570                 "bo": []bool{false},
571                 "st": []string{"s"},
572         }
573         buf := bytes.NewBuffer(nil)
574         enc := NewEncoder(buf)
575         err := enc.Encode(m)
576         if err != nil {
577                 t.Errorf("encode map: %s", err)
578         }
579 }
580
581 func TestSliceReusesMemory(t *testing.T) {
582         buf := bytes.NewBuffer(nil)
583         // Bytes
584         {
585                 x := []byte("abcd")
586                 enc := NewEncoder(buf)
587                 err := enc.Encode(x)
588                 if err != nil {
589                         t.Errorf("bytes: encode: %s", err)
590                 }
591                 // Decode into y, which is big enough.
592                 y := []byte("ABCDE")
593                 addr := &y[0]
594                 dec := NewDecoder(buf)
595                 err = dec.Decode(&y)
596                 if err != nil {
597                         t.Fatal("bytes: decode:", err)
598                 }
599                 if !bytes.Equal(x, y) {
600                         t.Errorf("bytes: expected %q got %q\n", x, y)
601                 }
602                 if addr != &y[0] {
603                         t.Errorf("bytes: unnecessary reallocation")
604                 }
605         }
606         // general slice
607         {
608                 x := []rune("abcd")
609                 enc := NewEncoder(buf)
610                 err := enc.Encode(x)
611                 if err != nil {
612                         t.Errorf("ints: encode: %s", err)
613                 }
614                 // Decode into y, which is big enough.
615                 y := []rune("ABCDE")
616                 addr := &y[0]
617                 dec := NewDecoder(buf)
618                 err = dec.Decode(&y)
619                 if err != nil {
620                         t.Fatal("ints: decode:", err)
621                 }
622                 if !reflect.DeepEqual(x, y) {
623                         t.Errorf("ints: expected %q got %q\n", x, y)
624                 }
625                 if addr != &y[0] {
626                         t.Errorf("ints: unnecessary reallocation")
627                 }
628         }
629 }
630
631 // Used to crash: negative count in recvMessage.
632 func TestBadCount(t *testing.T) {
633         b := []byte{0xfb, 0xa5, 0x82, 0x2f, 0xca, 0x1}
634         if err := NewDecoder(bytes.NewBuffer(b)).Decode(nil); err == nil {
635                 t.Error("expected error from bad count")
636         } else if err.Error() != errBadCount.Error() {
637                 t.Error("expected bad count error; got", err)
638         }
639 }
640
641 // Verify that sequential Decoders built on a single input will
642 // succeed if the input implements ReadByte and there is no
643 // type information in the stream.
644 func TestSequentialDecoder(t *testing.T) {
645         b := new(bytes.Buffer)
646         enc := NewEncoder(b)
647         const count = 10
648         for i := 0; i < count; i++ {
649                 s := fmt.Sprintf("%d", i)
650                 if err := enc.Encode(s); err != nil {
651                         t.Error("encoder fail:", err)
652                 }
653         }
654         for i := 0; i < count; i++ {
655                 dec := NewDecoder(b)
656                 var s string
657                 if err := dec.Decode(&s); err != nil {
658                         t.Fatal("decoder fail:", err)
659                 }
660                 if s != fmt.Sprintf("%d", i) {
661                         t.Fatalf("decode expected %d got %s", i, s)
662                 }
663         }
664 }
665
666 // Should be able to have unrepresentable fields (chan, func) as long as they
667 // are unexported.
668 type Bug2 struct {
669         A int
670         b chan int
671 }
672
673 func TestUnexportedChan(t *testing.T) {
674         b := Bug2{23, make(chan int)}
675         var stream bytes.Buffer
676         enc := NewEncoder(&stream)
677         if err := enc.Encode(b); err != nil {
678                 t.Fatalf("error encoding unexported channel: %s", err)
679         }
680 }