blob: eb9f306bcf5fcbf64a35e051e5261e6aad57ab2b [file] [log] [blame]
Rob Pikeb948c432009-06-29 15:15:07 -07001// 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
5package gob
6
7import (
Robert Griesemera3d10452009-12-15 15:35:38 -08008 "bytes"
Russ Coxc2049d22011-11-01 22:04:37 -04009 "errors"
Rob Pikebbe60172012-07-13 14:23:51 -070010 "flag"
Robert Griesemera3d10452009-12-15 15:35:38 -080011 "math"
David Symonds9440d822012-02-06 14:02:12 +110012 "math/rand"
Robert Griesemera3d10452009-12-15 15:35:38 -080013 "reflect"
14 "strings"
15 "testing"
David Symonds9440d822012-02-06 14:02:12 +110016 "time"
Rob Pikeb948c432009-06-29 15:15:07 -070017)
18
Rob Pikebbe60172012-07-13 14:23:51 -070019var doFuzzTests = flag.Bool("gob.fuzz", false, "run the fuzz tests, which are large and very slow")
20
Rob Pikeb948c432009-06-29 15:15:07 -070021// Guarantee encoding format by comparing some encodings to hand-written values
22type EncodeT struct {
Robert Griesemera3d10452009-12-15 15:35:38 -080023 x uint64
24 b []byte
Rob Pikeb948c432009-06-29 15:15:07 -070025}
Robert Griesemerf65e42d2009-11-05 14:53:42 -080026
27var encodeT = []EncodeT{
Robert Griesemer34788912010-10-22 10:06:33 -070028 {0x00, []byte{0x00}},
29 {0x0F, []byte{0x0F}},
30 {0xFF, []byte{0xFF, 0xFF}},
31 {0xFFFF, []byte{0xFE, 0xFF, 0xFF}},
32 {0xFFFFFF, []byte{0xFD, 0xFF, 0xFF, 0xFF}},
33 {0xFFFFFFFF, []byte{0xFC, 0xFF, 0xFF, 0xFF, 0xFF}},
34 {0xFFFFFFFFFF, []byte{0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
35 {0xFFFFFFFFFFFF, []byte{0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
36 {0xFFFFFFFFFFFFFF, []byte{0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
37 {0xFFFFFFFFFFFFFFFF, []byte{0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
38 {0x1111, []byte{0xFE, 0x11, 0x11}},
39 {0x1111111111111111, []byte{0xF8, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}},
40 {0x8888888888888888, []byte{0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88}},
41 {1 << 63, []byte{0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
Rob Pikeb948c432009-06-29 15:15:07 -070042}
43
Rob Pikef593b372010-10-22 15:16:34 -070044// testError is meant to be used as a deferred function to turn a panic(gobError) into a
45// plain test.Error call.
46func testError(t *testing.T) {
47 if e := recover(); e != nil {
Russ Coxc0a0fd62011-10-27 20:20:59 -070048 t.Error(e.(gobError).err) // Will re-panic if not one of our errors, such as a runtime error.
Rob Pikef593b372010-10-22 15:16:34 -070049 }
Rob Pikef593b372010-10-22 15:16:34 -070050}
51
Rob Pike63acc482014-10-20 07:33:08 -070052func newDecBuffer(data []byte) *decBuffer {
53 return &decBuffer{
54 data: data,
55 }
56}
57
Rob Pikeb948c432009-06-29 15:15:07 -070058// Test basic encode/decode routines for unsigned integers
59func TestUintCodec(t *testing.T) {
Rob Pikef593b372010-10-22 15:16:34 -070060 defer testError(t)
Rob Pike65dde1e2014-10-17 20:51:15 -070061 b := new(encBuffer)
Rob Pikeeb864e92011-03-15 21:30:07 -070062 encState := newEncoderState(b)
Russ Coxca6a0fe2009-09-15 09:41:59 -070063 for _, tt := range encodeT {
Robert Griesemera3d10452009-12-15 15:35:38 -080064 b.Reset()
Rob Pikea80cdcb2011-01-11 17:56:45 -080065 encState.encodeUint(tt.x)
Rob Piked5be41f2009-09-16 15:15:00 -070066 if !bytes.Equal(tt.b, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -080067 t.Errorf("encodeUint: %#x encode: expected % x got % x", tt.x, tt.b, b.Bytes())
Rob Pikeb948c432009-06-29 15:15:07 -070068 }
69 }
Robert Griesemerbaba2922009-11-09 21:13:17 -080070 for u := uint64(0); ; u = (u + 1) * 7 {
Robert Griesemera3d10452009-12-15 15:35:38 -080071 b.Reset()
Rob Pikea80cdcb2011-01-11 17:56:45 -080072 encState.encodeUint(u)
Rob Pike63acc482014-10-20 07:33:08 -070073 decState := newDecodeState(newDecBuffer(b.Bytes()))
Rob Pikea80cdcb2011-01-11 17:56:45 -080074 v := decState.decodeUint()
Rob Pikeb948c432009-06-29 15:15:07 -070075 if u != v {
Rob Pike1959c3a2010-09-23 13:48:56 +100076 t.Errorf("Encode/Decode: sent %#x received %#x", u, v)
Rob Pikeb948c432009-06-29 15:15:07 -070077 }
Robert Griesemerf65e42d2009-11-05 14:53:42 -080078 if u&(1<<63) != 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080079 break
Rob Pikeb948c432009-06-29 15:15:07 -070080 }
81 }
82}
83
84func verifyInt(i int64, t *testing.T) {
Rob Pikef593b372010-10-22 15:16:34 -070085 defer testError(t)
Rob Pike65dde1e2014-10-17 20:51:15 -070086 var b = new(encBuffer)
Rob Pikeeb864e92011-03-15 21:30:07 -070087 encState := newEncoderState(b)
Rob Pikea80cdcb2011-01-11 17:56:45 -080088 encState.encodeInt(i)
Rob Pike63acc482014-10-20 07:33:08 -070089 decState := newDecodeState(newDecBuffer(b.Bytes()))
Rob Pikea80cdcb2011-01-11 17:56:45 -080090 j := decState.decodeInt()
Rob Pikeb948c432009-06-29 15:15:07 -070091 if i != j {
Rob Pike1959c3a2010-09-23 13:48:56 +100092 t.Errorf("Encode/Decode: sent %#x received %#x", uint64(i), uint64(j))
Rob Pikeb948c432009-06-29 15:15:07 -070093 }
94}
95
96// Test basic encode/decode routines for signed integers
97func TestIntCodec(t *testing.T) {
Robert Griesemerbaba2922009-11-09 21:13:17 -080098 for u := uint64(0); ; u = (u + 1) * 7 {
Rob Pikeb948c432009-06-29 15:15:07 -070099 // Do positive and negative values
Robert Griesemera3d10452009-12-15 15:35:38 -0800100 i := int64(u)
101 verifyInt(i, t)
102 verifyInt(-i, t)
103 verifyInt(^i, t)
Robert Griesemerf65e42d2009-11-05 14:53:42 -0800104 if u&(1<<63) != 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800105 break
Rob Pikeb948c432009-06-29 15:15:07 -0700106 }
107 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800108 verifyInt(-1<<63, t) // a tricky case
Rob Pikeb948c432009-06-29 15:15:07 -0700109}
Rob Pikef6f82512009-06-30 15:37:46 -0700110
Rob Pikeb85147c2009-07-28 17:20:19 -0700111// The result of encoding a true boolean with field number 7
112var boolResult = []byte{0x07, 0x01}
Robert Griesemer541b67d2011-12-16 15:43:06 -0800113
Rob Pikeb85147c2009-07-28 17:20:19 -0700114// The result of encoding a number 17 with field number 7
Robert Griesemerbaba2922009-11-09 21:13:17 -0800115var signedResult = []byte{0x07, 2 * 17}
Rob Pikeb85147c2009-07-28 17:20:19 -0700116var unsignedResult = []byte{0x07, 17}
117var floatResult = []byte{0x07, 0xFE, 0x31, 0x40}
Robert Griesemer541b67d2011-12-16 15:43:06 -0800118
Rob Pike37a6bc82010-06-24 15:07:28 -0700119// The result of encoding a number 17+19i with field number 7
120var complexResult = []byte{0x07, 0xFE, 0x31, 0x40, 0xFE, 0x33, 0x40}
Robert Griesemer541b67d2011-12-16 15:43:06 -0800121
Rob Pike37a6bc82010-06-24 15:07:28 -0700122// The result of encoding "hello" with field number 7
Rob Pikeb85147c2009-07-28 17:20:19 -0700123var bytesResult = []byte{0x07, 0x05, 'h', 'e', 'l', 'l', 'o'}
Rob Pikec701af82009-07-01 18:25:13 -0700124
Rob Pike63acc482014-10-20 07:33:08 -0700125func newDecodeState(buf *decBuffer) *decoderState {
Rob Pikeeb864e92011-03-15 21:30:07 -0700126 d := new(decoderState)
127 d.b = buf
Rob Pikeeb864e92011-03-15 21:30:07 -0700128 return d
129}
130
Rob Pike65dde1e2014-10-17 20:51:15 -0700131func newEncoderState(b *encBuffer) *encoderState {
Robert Griesemera3d10452009-12-15 15:35:38 -0800132 b.Reset()
Rob Pikeeb864e92011-03-15 21:30:07 -0700133 state := &encoderState{enc: nil, b: b}
Robert Griesemera3d10452009-12-15 15:35:38 -0800134 state.fieldnum = -1
135 return state
Rob Pikec701af82009-07-01 18:25:13 -0700136}
Rob Pikef6f82512009-06-30 15:37:46 -0700137
138// Test instruction execution for encoding.
139// Do not run the machine yet; instead do individual instructions crafted by hand.
140func TestScalarEncInstructions(t *testing.T) {
Rob Pike65dde1e2014-10-17 20:51:15 -0700141 var b = new(encBuffer)
Rob Pikef6f82512009-06-30 15:37:46 -0700142
143 // bool
144 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700145 var data bool = true
146 instr := &encInstr{encBool, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700147 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700148 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700149 if !bytes.Equal(boolResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800150 t.Errorf("bool enc instructions: expected % x got % x", boolResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700151 }
152 }
153
154 // int
155 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800156 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700157 var data int = 17
158 instr := &encInstr{encInt, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700159 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700160 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700161 if !bytes.Equal(signedResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800162 t.Errorf("int enc instructions: expected % x got % x", signedResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700163 }
164 }
165
166 // uint
167 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800168 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700169 var data uint = 17
170 instr := &encInstr{encUint, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700171 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700172 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700173 if !bytes.Equal(unsignedResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800174 t.Errorf("uint enc instructions: expected % x got % x", unsignedResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700175 }
176 }
177
178 // int8
179 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800180 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700181 var data int8 = 17
182 instr := &encInstr{encInt, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700183 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700184 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700185 if !bytes.Equal(signedResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800186 t.Errorf("int8 enc instructions: expected % x got % x", signedResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700187 }
188 }
189
190 // uint8
191 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800192 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700193 var data uint8 = 17
194 instr := &encInstr{encUint, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700195 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700196 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700197 if !bytes.Equal(unsignedResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800198 t.Errorf("uint8 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700199 }
200 }
201
202 // int16
203 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800204 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700205 var data int16 = 17
206 instr := &encInstr{encInt, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700207 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700208 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700209 if !bytes.Equal(signedResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800210 t.Errorf("int16 enc instructions: expected % x got % x", signedResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700211 }
212 }
213
214 // uint16
215 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800216 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700217 var data uint16 = 17
218 instr := &encInstr{encUint, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700219 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700220 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700221 if !bytes.Equal(unsignedResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800222 t.Errorf("uint16 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700223 }
224 }
225
226 // int32
227 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800228 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700229 var data int32 = 17
230 instr := &encInstr{encInt, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700231 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700232 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700233 if !bytes.Equal(signedResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800234 t.Errorf("int32 enc instructions: expected % x got % x", signedResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700235 }
236 }
237
238 // uint32
239 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800240 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700241 var data uint32 = 17
242 instr := &encInstr{encUint, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700243 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700244 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700245 if !bytes.Equal(unsignedResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800246 t.Errorf("uint32 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700247 }
248 }
249
250 // int64
251 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800252 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700253 var data int64 = 17
254 instr := &encInstr{encInt, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700255 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700256 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700257 if !bytes.Equal(signedResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800258 t.Errorf("int64 enc instructions: expected % x got % x", signedResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700259 }
260 }
261
262 // uint64
263 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800264 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700265 var data uint64 = 17
266 instr := &encInstr{encUint, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700267 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700268 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700269 if !bytes.Equal(unsignedResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800270 t.Errorf("uint64 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700271 }
272 }
273
Rob Pikef6f82512009-06-30 15:37:46 -0700274 // float32
275 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800276 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700277 var data float32 = 17
278 instr := &encInstr{encFloat, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700279 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700280 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700281 if !bytes.Equal(floatResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800282 t.Errorf("float32 enc instructions: expected % x got % x", floatResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700283 }
284 }
285
286 // float64
287 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800288 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700289 var data float64 = 17
290 instr := &encInstr{encFloat, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700291 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700292 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700293 if !bytes.Equal(floatResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800294 t.Errorf("float64 enc instructions: expected % x got % x", floatResult, b.Bytes())
Rob Pikef6f82512009-06-30 15:37:46 -0700295 }
296 }
Rob Pike1ca1e1b2009-07-02 11:21:01 -0700297
298 // bytes == []uint8
299 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800300 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700301 data := []byte("hello")
302 instr := &encInstr{encUint8Array, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700303 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700304 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700305 if !bytes.Equal(bytesResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800306 t.Errorf("bytes enc instructions: expected % x got % x", bytesResult, b.Bytes())
Rob Pike1ca1e1b2009-07-02 11:21:01 -0700307 }
308 }
309
310 // string
311 {
Robert Griesemera3d10452009-12-15 15:35:38 -0800312 b.Reset()
Rob Pike3050a0a2014-06-30 11:06:47 -0700313 var data string = "hello"
314 instr := &encInstr{encString, 6, nil, 0}
Rob Pikeeb864e92011-03-15 21:30:07 -0700315 state := newEncoderState(b)
Rob Pike3050a0a2014-06-30 11:06:47 -0700316 instr.op(instr, state, reflect.ValueOf(data))
Rob Piked5be41f2009-09-16 15:15:00 -0700317 if !bytes.Equal(bytesResult, b.Bytes()) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800318 t.Errorf("string enc instructions: expected % x got % x", bytesResult, b.Bytes())
Rob Pike1ca1e1b2009-07-02 11:21:01 -0700319 }
320 }
Rob Pikef6f82512009-06-30 15:37:46 -0700321}
Rob Pikeb96894332009-06-30 17:59:41 -0700322
Daniel Martí516e6f62017-04-25 10:58:12 +0100323func execDec(instr *decInstr, state *decoderState, t *testing.T, value reflect.Value) {
Rob Pikef593b372010-10-22 15:16:34 -0700324 defer testError(t)
Rob Pikea80cdcb2011-01-11 17:56:45 -0800325 v := int(state.decodeUint())
Robert Griesemerbaba2922009-11-09 21:13:17 -0800326 if v+state.fieldnum != 6 {
327 t.Fatalf("decoding field number %d, got %d", 6, v+state.fieldnum)
Rob Pikeb96894332009-06-30 17:59:41 -0700328 }
Russ Coxe4bc3c42014-07-01 14:19:27 -0400329 instr.op(instr, state, value.Elem())
Robert Griesemera3d10452009-12-15 15:35:38 -0800330 state.fieldnum = 6
Rob Pikec701af82009-07-01 18:25:13 -0700331}
332
Rob Pikec91daef2011-03-04 12:25:18 -0800333func newDecodeStateFromData(data []byte) *decoderState {
Rob Pike63acc482014-10-20 07:33:08 -0700334 b := newDecBuffer(data)
Rob Pikeeb864e92011-03-15 21:30:07 -0700335 state := newDecodeState(b)
Robert Griesemera3d10452009-12-15 15:35:38 -0800336 state.fieldnum = -1
337 return state
Rob Pikeb96894332009-06-30 17:59:41 -0700338}
339
340// Test instruction execution for decoding.
341// Do not run the machine yet; instead do individual instructions crafted by hand.
342func TestScalarDecInstructions(t *testing.T) {
Russ Coxc2049d22011-11-01 22:04:37 -0400343 ovfl := errors.New("overflow")
Rob Pikeb96894332009-06-30 17:59:41 -0700344
345 // bool
346 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700347 var data bool
Rob Pikece5bbfd2014-06-30 15:47:11 -0700348 instr := &decInstr{decBool, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800349 state := newDecodeStateFromData(boolResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100350 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700351 if data != true {
352 t.Errorf("bool a = %v not true", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700353 }
Rob Pikeb96894332009-06-30 17:59:41 -0700354 }
Rob Pikeb96894332009-06-30 17:59:41 -0700355 // int
356 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700357 var data int
Rob Pikece5bbfd2014-06-30 15:47:11 -0700358 instr := &decInstr{decOpTable[reflect.Int], 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800359 state := newDecodeStateFromData(signedResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100360 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700361 if data != 17 {
362 t.Errorf("int a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700363 }
Rob Pikeb96894332009-06-30 17:59:41 -0700364 }
365
366 // uint
367 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700368 var data uint
Rob Pikece5bbfd2014-06-30 15:47:11 -0700369 instr := &decInstr{decOpTable[reflect.Uint], 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800370 state := newDecodeStateFromData(unsignedResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100371 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700372 if data != 17 {
373 t.Errorf("uint a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700374 }
Rob Pikeb96894332009-06-30 17:59:41 -0700375 }
376
377 // int8
378 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700379 var data int8
Rob Pikece5bbfd2014-06-30 15:47:11 -0700380 instr := &decInstr{decInt8, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800381 state := newDecodeStateFromData(signedResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100382 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700383 if data != 17 {
384 t.Errorf("int8 a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700385 }
Rob Pikeb96894332009-06-30 17:59:41 -0700386 }
387
388 // uint8
389 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700390 var data uint8
Rob Pikece5bbfd2014-06-30 15:47:11 -0700391 instr := &decInstr{decUint8, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800392 state := newDecodeStateFromData(unsignedResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100393 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700394 if data != 17 {
395 t.Errorf("uint8 a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700396 }
Rob Pikeb96894332009-06-30 17:59:41 -0700397 }
398
399 // int16
400 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700401 var data int16
Rob Pikece5bbfd2014-06-30 15:47:11 -0700402 instr := &decInstr{decInt16, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800403 state := newDecodeStateFromData(signedResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100404 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700405 if data != 17 {
406 t.Errorf("int16 a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700407 }
Rob Pikeb96894332009-06-30 17:59:41 -0700408 }
409
410 // uint16
411 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700412 var data uint16
Rob Pikece5bbfd2014-06-30 15:47:11 -0700413 instr := &decInstr{decUint16, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800414 state := newDecodeStateFromData(unsignedResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100415 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700416 if data != 17 {
417 t.Errorf("uint16 a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700418 }
Rob Pikeb96894332009-06-30 17:59:41 -0700419 }
420
421 // int32
422 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700423 var data int32
Rob Pikece5bbfd2014-06-30 15:47:11 -0700424 instr := &decInstr{decInt32, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800425 state := newDecodeStateFromData(signedResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100426 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700427 if data != 17 {
428 t.Errorf("int32 a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700429 }
Rob Pikeb96894332009-06-30 17:59:41 -0700430 }
431
432 // uint32
433 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700434 var data uint32
Rob Pikece5bbfd2014-06-30 15:47:11 -0700435 instr := &decInstr{decUint32, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800436 state := newDecodeStateFromData(unsignedResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100437 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700438 if data != 17 {
439 t.Errorf("uint32 a = %v not 17", data)
Rob Pike483e4fc2009-07-28 12:59:39 -0700440 }
441 }
442
443 // uintptr
444 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700445 var data uintptr
Rob Pikece5bbfd2014-06-30 15:47:11 -0700446 instr := &decInstr{decOpTable[reflect.Uintptr], 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800447 state := newDecodeStateFromData(unsignedResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100448 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700449 if data != 17 {
450 t.Errorf("uintptr a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700451 }
Rob Pikeb96894332009-06-30 17:59:41 -0700452 }
453
454 // int64
455 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700456 var data int64
Rob Pikece5bbfd2014-06-30 15:47:11 -0700457 instr := &decInstr{decInt64, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800458 state := newDecodeStateFromData(signedResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100459 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700460 if data != 17 {
461 t.Errorf("int64 a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700462 }
Rob Pikeb96894332009-06-30 17:59:41 -0700463 }
464
465 // uint64
466 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700467 var data uint64
Rob Pikece5bbfd2014-06-30 15:47:11 -0700468 instr := &decInstr{decUint64, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800469 state := newDecodeStateFromData(unsignedResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100470 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700471 if data != 17 {
472 t.Errorf("uint64 a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700473 }
Rob Pikeb96894332009-06-30 17:59:41 -0700474 }
475
Rob Pikeb96894332009-06-30 17:59:41 -0700476 // float32
477 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700478 var data float32
Rob Pikece5bbfd2014-06-30 15:47:11 -0700479 instr := &decInstr{decFloat32, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800480 state := newDecodeStateFromData(floatResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100481 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700482 if data != 17 {
483 t.Errorf("float32 a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700484 }
Rob Pikeb96894332009-06-30 17:59:41 -0700485 }
486
487 // float64
488 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700489 var data float64
Rob Pikece5bbfd2014-06-30 15:47:11 -0700490 instr := &decInstr{decFloat64, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800491 state := newDecodeStateFromData(floatResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100492 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700493 if data != 17 {
494 t.Errorf("float64 a = %v not 17", data)
Rob Pikeb96894332009-06-30 17:59:41 -0700495 }
Rob Pikeb96894332009-06-30 17:59:41 -0700496 }
Rob Pike1ca1e1b2009-07-02 11:21:01 -0700497
Rob Pike37a6bc82010-06-24 15:07:28 -0700498 // complex64
499 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700500 var data complex64
Rob Pikece5bbfd2014-06-30 15:47:11 -0700501 instr := &decInstr{decOpTable[reflect.Complex64], 6, nil, ovfl}
Rob Pike37a6bc82010-06-24 15:07:28 -0700502 state := newDecodeStateFromData(complexResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100503 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700504 if data != 17+19i {
505 t.Errorf("complex a = %v not 17+19i", data)
Rob Pike37a6bc82010-06-24 15:07:28 -0700506 }
507 }
508
509 // complex128
510 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700511 var data complex128
Rob Pikece5bbfd2014-06-30 15:47:11 -0700512 instr := &decInstr{decOpTable[reflect.Complex128], 6, nil, ovfl}
Rob Pike37a6bc82010-06-24 15:07:28 -0700513 state := newDecodeStateFromData(complexResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100514 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700515 if data != 17+19i {
516 t.Errorf("complex a = %v not 17+19i", data)
Rob Pike37a6bc82010-06-24 15:07:28 -0700517 }
518 }
519
Rob Pike1ca1e1b2009-07-02 11:21:01 -0700520 // bytes == []uint8
521 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700522 var data []byte
Rob Pikece5bbfd2014-06-30 15:47:11 -0700523 instr := &decInstr{decUint8Slice, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800524 state := newDecodeStateFromData(bytesResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100525 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700526 if string(data) != "hello" {
527 t.Errorf(`bytes a = %q not "hello"`, string(data))
Rob Pike1ca1e1b2009-07-02 11:21:01 -0700528 }
529 }
530
531 // string
532 {
Rob Pike3050a0a2014-06-30 11:06:47 -0700533 var data string
Rob Pikece5bbfd2014-06-30 15:47:11 -0700534 instr := &decInstr{decString, 6, nil, ovfl}
Robert Griesemera3d10452009-12-15 15:35:38 -0800535 state := newDecodeStateFromData(bytesResult)
Daniel Martí516e6f62017-04-25 10:58:12 +0100536 execDec(instr, state, t, reflect.ValueOf(&data))
Rob Pike3050a0a2014-06-30 11:06:47 -0700537 if data != "hello" {
538 t.Errorf(`bytes a = %q not "hello"`, data)
Rob Pike1ca1e1b2009-07-02 11:21:01 -0700539 }
540 }
Rob Pikeb96894332009-06-30 17:59:41 -0700541}
Rob Pikec701af82009-07-01 18:25:13 -0700542
Rob Pike265674f2009-07-02 18:02:42 -0700543func TestEndToEnd(t *testing.T) {
Rob Pike77baac12009-07-02 13:43:47 -0700544 type T2 struct {
Rob Pike30366042011-01-11 13:44:00 -0800545 T string
Rob Pike77baac12009-07-02 13:43:47 -0700546 }
Filip Gruszczyński0b9607d2017-03-15 20:11:30 -0700547 type T3 struct {
548 X float64
549 Z *int
550 }
Rob Pikec0271c42009-07-02 08:21:42 -0700551 type T1 struct {
Rob Piked11c0f12011-07-21 10:27:11 +1000552 A, B, C int
553 M map[string]*float64
Filip Gruszczyński0b9607d2017-03-15 20:11:30 -0700554 M2 map[int]T3
Filip Gruszczyński11ab8652017-03-30 19:09:56 -0700555 Mstring map[string]string
556 Mintptr map[int]*int
557 Mcomp map[complex128]complex128
558 Marr map[[2]string][2]*float64
Rob Piked11c0f12011-07-21 10:27:11 +1000559 EmptyMap map[string]int // to check that we receive a non-nil map.
560 N *[3]float64
561 Strs *[2]string
562 Int64s *[]int64
563 RI complex64
564 S string
565 Y []byte
566 T *T2
Rob Pikec0271c42009-07-02 08:21:42 -0700567 }
Rob Pike7861da72010-05-05 16:46:39 -0700568 pi := 3.14159
569 e := 2.71828
Filip Gruszczyński11ab8652017-03-30 19:09:56 -0700570 two := 2.0
Filip Gruszczyński0b9607d2017-03-15 20:11:30 -0700571 meaning := 42
572 fingers := 5
Filip Gruszczyński11ab8652017-03-30 19:09:56 -0700573 s1 := "string1"
574 s2 := "string2"
575 var comp1 complex128 = complex(1.0, 1.0)
576 var comp2 complex128 = complex(1.0, 1.0)
577 var arr1 [2]string
578 arr1[0] = s1
579 arr1[1] = s2
580 var arr2 [2]string
581 arr2[0] = s2
582 arr2[1] = s1
583 var floatArr1 [2]*float64
584 floatArr1[0] = &pi
585 floatArr1[1] = &e
586 var floatArr2 [2]*float64
587 floatArr2[0] = &e
588 floatArr2[1] = &two
Rob Pike1ca1e1b2009-07-02 11:21:01 -0700589 t1 := &T1{
Rob Piked11c0f12011-07-21 10:27:11 +1000590 A: 17,
591 B: 18,
592 C: -5,
593 M: map[string]*float64{"pi": &pi, "e": &e},
Filip Gruszczyński0b9607d2017-03-15 20:11:30 -0700594 M2: map[int]T3{4: T3{X: pi, Z: &meaning}, 10: T3{X: e, Z: &fingers}},
Filip Gruszczyński11ab8652017-03-30 19:09:56 -0700595 Mstring: map[string]string{"pi": "3.14", "e": "2.71"},
596 Mintptr: map[int]*int{meaning: &fingers, fingers: &meaning},
597 Mcomp: map[complex128]complex128{comp1: comp2, comp2: comp1},
598 Marr: map[[2]string][2]*float64{arr1: floatArr1, arr2: floatArr2},
Rob Piked11c0f12011-07-21 10:27:11 +1000599 EmptyMap: make(map[string]int),
600 N: &[3]float64{1.5, 2.5, 3.5},
601 Strs: &[2]string{s1, s2},
602 Int64s: &[]int64{77, 89, 123412342134},
603 RI: 17 - 23i,
604 S: "Now is the time",
605 Y: []byte("hello, sailor"),
606 T: &T2{"this is T2"},
Robert Griesemera3d10452009-12-15 15:35:38 -0800607 }
608 b := new(bytes.Buffer)
609 err := NewEncoder(b).Encode(t1)
Rob Pike30b1b9a2009-11-16 23:32:30 -0800610 if err != nil {
611 t.Error("encode:", err)
612 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800613 var _t1 T1
614 err = NewDecoder(b).Decode(&_t1)
Rob Pike30b1b9a2009-11-16 23:32:30 -0800615 if err != nil {
616 t.Fatal("decode:", err)
617 }
Rob Pikec701af82009-07-01 18:25:13 -0700618 if !reflect.DeepEqual(t1, &_t1) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800619 t.Errorf("encode expected %v got %v", *t1, _t1)
Rob Pikec701af82009-07-01 18:25:13 -0700620 }
Rob Piked11c0f12011-07-21 10:27:11 +1000621 // Be absolutely sure the received map is non-nil.
622 if t1.EmptyMap == nil {
623 t.Errorf("nil map sent")
624 }
625 if _t1.EmptyMap == nil {
626 t.Errorf("nil map received")
627 }
Rob Pikec701af82009-07-01 18:25:13 -0700628}
Rob Pikec0271c42009-07-02 08:21:42 -0700629
Rob Pike483e4fc2009-07-28 12:59:39 -0700630func TestOverflow(t *testing.T) {
631 type inputT struct {
Rob Pike30366042011-01-11 13:44:00 -0800632 Maxi int64
633 Mini int64
634 Maxu uint64
635 Maxf float64
636 Minf float64
637 Maxc complex128
638 Minc complex128
Rob Pike483e4fc2009-07-28 12:59:39 -0700639 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800640 var it inputT
Russ Coxc2049d22011-11-01 22:04:37 -0400641 var err error
Robert Griesemera3d10452009-12-15 15:35:38 -0800642 b := new(bytes.Buffer)
643 enc := NewEncoder(b)
644 dec := NewDecoder(b)
Rob Pike483e4fc2009-07-28 12:59:39 -0700645
646 // int8
Robert Griesemera3d10452009-12-15 15:35:38 -0800647 b.Reset()
Robert Griesemerf65e42d2009-11-05 14:53:42 -0800648 it = inputT{
Rob Pike30366042011-01-11 13:44:00 -0800649 Maxi: math.MaxInt8 + 1,
Rob Pike483e4fc2009-07-28 12:59:39 -0700650 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800651 type outi8 struct {
Rob Pike30366042011-01-11 13:44:00 -0800652 Maxi int8
653 Mini int8
Robert Griesemera3d10452009-12-15 15:35:38 -0800654 }
655 var o1 outi8
656 enc.Encode(it)
657 err = dec.Decode(&o1)
Russ Coxc2049d22011-11-01 22:04:37 -0400658 if err == nil || err.Error() != `value for "Maxi" out of range` {
Robert Griesemer40621d52009-11-09 12:07:39 -0800659 t.Error("wrong overflow error for int8:", err)
Rob Pike483e4fc2009-07-28 12:59:39 -0700660 }
Robert Griesemerf65e42d2009-11-05 14:53:42 -0800661 it = inputT{
Rob Pike30366042011-01-11 13:44:00 -0800662 Mini: math.MinInt8 - 1,
Robert Griesemera3d10452009-12-15 15:35:38 -0800663 }
664 b.Reset()
665 enc.Encode(it)
666 err = dec.Decode(&o1)
Russ Coxc2049d22011-11-01 22:04:37 -0400667 if err == nil || err.Error() != `value for "Mini" out of range` {
Robert Griesemer40621d52009-11-09 12:07:39 -0800668 t.Error("wrong underflow error for int8:", err)
Rob Pike483e4fc2009-07-28 12:59:39 -0700669 }
670
671 // int16
Robert Griesemera3d10452009-12-15 15:35:38 -0800672 b.Reset()
Robert Griesemerf65e42d2009-11-05 14:53:42 -0800673 it = inputT{
Rob Pike30366042011-01-11 13:44:00 -0800674 Maxi: math.MaxInt16 + 1,
Rob Pike483e4fc2009-07-28 12:59:39 -0700675 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800676 type outi16 struct {
Rob Pike30366042011-01-11 13:44:00 -0800677 Maxi int16
678 Mini int16
Robert Griesemera3d10452009-12-15 15:35:38 -0800679 }
680 var o2 outi16
681 enc.Encode(it)
682 err = dec.Decode(&o2)
Russ Coxc2049d22011-11-01 22:04:37 -0400683 if err == nil || err.Error() != `value for "Maxi" out of range` {
Robert Griesemer40621d52009-11-09 12:07:39 -0800684 t.Error("wrong overflow error for int16:", err)
Rob Pike483e4fc2009-07-28 12:59:39 -0700685 }
Robert Griesemerf65e42d2009-11-05 14:53:42 -0800686 it = inputT{
Rob Pike30366042011-01-11 13:44:00 -0800687 Mini: math.MinInt16 - 1,
Robert Griesemera3d10452009-12-15 15:35:38 -0800688 }
689 b.Reset()
690 enc.Encode(it)
691 err = dec.Decode(&o2)
Russ Coxc2049d22011-11-01 22:04:37 -0400692 if err == nil || err.Error() != `value for "Mini" out of range` {
Robert Griesemer40621d52009-11-09 12:07:39 -0800693 t.Error("wrong underflow error for int16:", err)
Rob Pike483e4fc2009-07-28 12:59:39 -0700694 }
695
696 // int32
Robert Griesemera3d10452009-12-15 15:35:38 -0800697 b.Reset()
Robert Griesemerf65e42d2009-11-05 14:53:42 -0800698 it = inputT{
Rob Pike30366042011-01-11 13:44:00 -0800699 Maxi: math.MaxInt32 + 1,
Rob Pike483e4fc2009-07-28 12:59:39 -0700700 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800701 type outi32 struct {
Rob Pike30366042011-01-11 13:44:00 -0800702 Maxi int32
703 Mini int32
Robert Griesemera3d10452009-12-15 15:35:38 -0800704 }
705 var o3 outi32
706 enc.Encode(it)
707 err = dec.Decode(&o3)
Russ Coxc2049d22011-11-01 22:04:37 -0400708 if err == nil || err.Error() != `value for "Maxi" out of range` {
Robert Griesemer40621d52009-11-09 12:07:39 -0800709 t.Error("wrong overflow error for int32:", err)
Rob Pike483e4fc2009-07-28 12:59:39 -0700710 }
Robert Griesemerf65e42d2009-11-05 14:53:42 -0800711 it = inputT{
Rob Pike30366042011-01-11 13:44:00 -0800712 Mini: math.MinInt32 - 1,
Robert Griesemera3d10452009-12-15 15:35:38 -0800713 }
714 b.Reset()
715 enc.Encode(it)
716 err = dec.Decode(&o3)
Russ Coxc2049d22011-11-01 22:04:37 -0400717 if err == nil || err.Error() != `value for "Mini" out of range` {
Robert Griesemer40621d52009-11-09 12:07:39 -0800718 t.Error("wrong underflow error for int32:", err)
Rob Pike483e4fc2009-07-28 12:59:39 -0700719 }
720
721 // uint8
Robert Griesemera3d10452009-12-15 15:35:38 -0800722 b.Reset()
Robert Griesemerf65e42d2009-11-05 14:53:42 -0800723 it = inputT{
Rob Pike30366042011-01-11 13:44:00 -0800724 Maxu: math.MaxUint8 + 1,
Rob Pike483e4fc2009-07-28 12:59:39 -0700725 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800726 type outu8 struct {
Rob Pike30366042011-01-11 13:44:00 -0800727 Maxu uint8
Robert Griesemera3d10452009-12-15 15:35:38 -0800728 }
729 var o4 outu8
730 enc.Encode(it)
731 err = dec.Decode(&o4)
Russ Coxc2049d22011-11-01 22:04:37 -0400732 if err == nil || err.Error() != `value for "Maxu" out of range` {
Robert Griesemer40621d52009-11-09 12:07:39 -0800733 t.Error("wrong overflow error for uint8:", err)
Rob Pike483e4fc2009-07-28 12:59:39 -0700734 }
735
736 // uint16
Robert Griesemera3d10452009-12-15 15:35:38 -0800737 b.Reset()
Robert Griesemerf65e42d2009-11-05 14:53:42 -0800738 it = inputT{
Rob Pike30366042011-01-11 13:44:00 -0800739 Maxu: math.MaxUint16 + 1,
Rob Pike483e4fc2009-07-28 12:59:39 -0700740 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800741 type outu16 struct {
Rob Pike30366042011-01-11 13:44:00 -0800742 Maxu uint16
Robert Griesemera3d10452009-12-15 15:35:38 -0800743 }
744 var o5 outu16
745 enc.Encode(it)
746 err = dec.Decode(&o5)
Russ Coxc2049d22011-11-01 22:04:37 -0400747 if err == nil || err.Error() != `value for "Maxu" out of range` {
Robert Griesemer40621d52009-11-09 12:07:39 -0800748 t.Error("wrong overflow error for uint16:", err)
Rob Pike483e4fc2009-07-28 12:59:39 -0700749 }
750
751 // uint32
Robert Griesemera3d10452009-12-15 15:35:38 -0800752 b.Reset()
Robert Griesemerf65e42d2009-11-05 14:53:42 -0800753 it = inputT{
Rob Pike30366042011-01-11 13:44:00 -0800754 Maxu: math.MaxUint32 + 1,
Rob Pike483e4fc2009-07-28 12:59:39 -0700755 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800756 type outu32 struct {
Rob Pike30366042011-01-11 13:44:00 -0800757 Maxu uint32
Robert Griesemera3d10452009-12-15 15:35:38 -0800758 }
759 var o6 outu32
760 enc.Encode(it)
761 err = dec.Decode(&o6)
Russ Coxc2049d22011-11-01 22:04:37 -0400762 if err == nil || err.Error() != `value for "Maxu" out of range` {
Robert Griesemer40621d52009-11-09 12:07:39 -0800763 t.Error("wrong overflow error for uint32:", err)
Rob Pike483e4fc2009-07-28 12:59:39 -0700764 }
765
766 // float32
Robert Griesemera3d10452009-12-15 15:35:38 -0800767 b.Reset()
Robert Griesemerf65e42d2009-11-05 14:53:42 -0800768 it = inputT{
Rob Pike30366042011-01-11 13:44:00 -0800769 Maxf: math.MaxFloat32 * 2,
Rob Pike483e4fc2009-07-28 12:59:39 -0700770 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800771 type outf32 struct {
Rob Pike30366042011-01-11 13:44:00 -0800772 Maxf float32
773 Minf float32
Robert Griesemera3d10452009-12-15 15:35:38 -0800774 }
775 var o7 outf32
776 enc.Encode(it)
777 err = dec.Decode(&o7)
Russ Coxc2049d22011-11-01 22:04:37 -0400778 if err == nil || err.Error() != `value for "Maxf" out of range` {
Robert Griesemer40621d52009-11-09 12:07:39 -0800779 t.Error("wrong overflow error for float32:", err)
Rob Pike483e4fc2009-07-28 12:59:39 -0700780 }
Rob Pike37a6bc82010-06-24 15:07:28 -0700781
782 // complex64
783 b.Reset()
784 it = inputT{
Russ Coxf2b5a072011-01-19 23:09:00 -0500785 Maxc: complex(math.MaxFloat32*2, math.MaxFloat32*2),
Rob Pike37a6bc82010-06-24 15:07:28 -0700786 }
787 type outc64 struct {
Rob Pike30366042011-01-11 13:44:00 -0800788 Maxc complex64
789 Minc complex64
Rob Pike37a6bc82010-06-24 15:07:28 -0700790 }
791 var o8 outc64
792 enc.Encode(it)
793 err = dec.Decode(&o8)
Russ Coxc2049d22011-11-01 22:04:37 -0400794 if err == nil || err.Error() != `value for "Maxc" out of range` {
Rob Pike37a6bc82010-06-24 15:07:28 -0700795 t.Error("wrong overflow error for complex64:", err)
796 }
Rob Pike483e4fc2009-07-28 12:59:39 -0700797}
798
Rob Pikeec23467e2009-07-09 14:33:43 -0700799func TestNesting(t *testing.T) {
800 type RT struct {
Rob Pike30366042011-01-11 13:44:00 -0800801 A string
802 Next *RT
Rob Pikeec23467e2009-07-09 14:33:43 -0700803 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800804 rt := new(RT)
Rob Pike30366042011-01-11 13:44:00 -0800805 rt.A = "level1"
806 rt.Next = new(RT)
807 rt.Next.A = "level2"
Robert Griesemera3d10452009-12-15 15:35:38 -0800808 b := new(bytes.Buffer)
809 NewEncoder(b).Encode(rt)
810 var drt RT
811 dec := NewDecoder(b)
812 err := dec.Decode(&drt)
Rob Pike30b1b9a2009-11-16 23:32:30 -0800813 if err != nil {
Rob Pike30366042011-01-11 13:44:00 -0800814 t.Fatal("decoder error:", err)
Rob Pike30b1b9a2009-11-16 23:32:30 -0800815 }
Rob Pike30366042011-01-11 13:44:00 -0800816 if drt.A != rt.A {
Robert Griesemer40621d52009-11-09 12:07:39 -0800817 t.Errorf("nesting: encode expected %v got %v", *rt, drt)
Rob Pikeec23467e2009-07-09 14:33:43 -0700818 }
Rob Pike30366042011-01-11 13:44:00 -0800819 if drt.Next == nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800820 t.Errorf("nesting: recursion failed")
Rob Pikeec23467e2009-07-09 14:33:43 -0700821 }
Rob Pike30366042011-01-11 13:44:00 -0800822 if drt.Next.A != rt.Next.A {
823 t.Errorf("nesting: encode expected %v got %v", *rt.Next, *drt.Next)
Rob Pikeec23467e2009-07-09 14:33:43 -0700824 }
825}
826
Rob Pikec0271c42009-07-02 08:21:42 -0700827// These three structures have the same data with different indirections
828type T0 struct {
Rob Pike30366042011-01-11 13:44:00 -0800829 A int
830 B int
831 C int
832 D int
Rob Pikec0271c42009-07-02 08:21:42 -0700833}
834type T1 struct {
Rob Pike30366042011-01-11 13:44:00 -0800835 A int
836 B *int
837 C **int
838 D ***int
Rob Pikec0271c42009-07-02 08:21:42 -0700839}
840type T2 struct {
Rob Pike30366042011-01-11 13:44:00 -0800841 A ***int
842 B **int
843 C *int
844 D int
Rob Pikec0271c42009-07-02 08:21:42 -0700845}
846
847func TestAutoIndirection(t *testing.T) {
Rob Pike22b93df2009-07-02 09:22:38 -0700848 // First transfer t1 into t0
Robert Griesemera3d10452009-12-15 15:35:38 -0800849 var t1 T1
Rob Pike30366042011-01-11 13:44:00 -0800850 t1.A = 17
851 t1.B = new(int)
852 *t1.B = 177
853 t1.C = new(*int)
854 *t1.C = new(int)
855 **t1.C = 1777
856 t1.D = new(**int)
857 *t1.D = new(*int)
858 **t1.D = new(int)
859 ***t1.D = 17777
Robert Griesemera3d10452009-12-15 15:35:38 -0800860 b := new(bytes.Buffer)
861 enc := NewEncoder(b)
862 enc.Encode(t1)
863 dec := NewDecoder(b)
864 var t0 T0
865 dec.Decode(&t0)
Rob Pike30366042011-01-11 13:44:00 -0800866 if t0.A != 17 || t0.B != 177 || t0.C != 1777 || t0.D != 17777 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800867 t.Errorf("t1->t0: expected {17 177 1777 17777}; got %v", t0)
Rob Pikec0271c42009-07-02 08:21:42 -0700868 }
869
Rob Pike22b93df2009-07-02 09:22:38 -0700870 // Now transfer t2 into t0
Robert Griesemera3d10452009-12-15 15:35:38 -0800871 var t2 T2
Rob Pike30366042011-01-11 13:44:00 -0800872 t2.D = 17777
873 t2.C = new(int)
874 *t2.C = 1777
875 t2.B = new(*int)
876 *t2.B = new(int)
877 **t2.B = 177
878 t2.A = new(**int)
879 *t2.A = new(*int)
880 **t2.A = new(int)
881 ***t2.A = 17
Robert Griesemera3d10452009-12-15 15:35:38 -0800882 b.Reset()
883 enc.Encode(t2)
884 t0 = T0{}
885 dec.Decode(&t0)
Rob Pike30366042011-01-11 13:44:00 -0800886 if t0.A != 17 || t0.B != 177 || t0.C != 1777 || t0.D != 17777 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800887 t.Errorf("t2->t0 expected {17 177 1777 17777}; got %v", t0)
Rob Pikec0271c42009-07-02 08:21:42 -0700888 }
889
890 // Now transfer t0 into t1
Robert Griesemera3d10452009-12-15 15:35:38 -0800891 t0 = T0{17, 177, 1777, 17777}
892 b.Reset()
893 enc.Encode(t0)
894 t1 = T1{}
895 dec.Decode(&t1)
Rob Pike30366042011-01-11 13:44:00 -0800896 if t1.A != 17 || *t1.B != 177 || **t1.C != 1777 || ***t1.D != 17777 {
897 t.Errorf("t0->t1 expected {17 177 1777 17777}; got {%d %d %d %d}", t1.A, *t1.B, **t1.C, ***t1.D)
Rob Pikec0271c42009-07-02 08:21:42 -0700898 }
899
900 // Now transfer t0 into t2
Robert Griesemera3d10452009-12-15 15:35:38 -0800901 b.Reset()
902 enc.Encode(t0)
903 t2 = T2{}
904 dec.Decode(&t2)
Rob Pike30366042011-01-11 13:44:00 -0800905 if ***t2.A != 17 || **t2.B != 177 || *t2.C != 1777 || t2.D != 17777 {
906 t.Errorf("t0->t2 expected {17 177 1777 17777}; got {%d %d %d %d}", ***t2.A, **t2.B, *t2.C, t2.D)
Rob Pikec0271c42009-07-02 08:21:42 -0700907 }
908
909 // Now do t2 again but without pre-allocated pointers.
Robert Griesemera3d10452009-12-15 15:35:38 -0800910 b.Reset()
911 enc.Encode(t0)
Rob Pike30366042011-01-11 13:44:00 -0800912 ***t2.A = 0
913 **t2.B = 0
914 *t2.C = 0
915 t2.D = 0
Robert Griesemera3d10452009-12-15 15:35:38 -0800916 dec.Decode(&t2)
Rob Pike30366042011-01-11 13:44:00 -0800917 if ***t2.A != 17 || **t2.B != 177 || *t2.C != 1777 || t2.D != 17777 {
918 t.Errorf("t0->t2 expected {17 177 1777 17777}; got {%d %d %d %d}", ***t2.A, **t2.B, *t2.C, t2.D)
Rob Pikec0271c42009-07-02 08:21:42 -0700919 }
920}
Rob Pike1399bad2009-07-16 17:55:16 -0700921
922type RT0 struct {
Rob Pike30366042011-01-11 13:44:00 -0800923 A int
924 B string
Russ Coxf2b5a072011-01-19 23:09:00 -0500925 C float64
Rob Pike1399bad2009-07-16 17:55:16 -0700926}
927type RT1 struct {
Russ Coxf2b5a072011-01-19 23:09:00 -0500928 C float64
Rob Pike30366042011-01-11 13:44:00 -0800929 B string
930 A int
931 NotSet string
Rob Pike1399bad2009-07-16 17:55:16 -0700932}
933
934func TestReorderedFields(t *testing.T) {
Robert Griesemera3d10452009-12-15 15:35:38 -0800935 var rt0 RT0
Rob Pike30366042011-01-11 13:44:00 -0800936 rt0.A = 17
937 rt0.B = "hello"
938 rt0.C = 3.14159
Robert Griesemera3d10452009-12-15 15:35:38 -0800939 b := new(bytes.Buffer)
940 NewEncoder(b).Encode(rt0)
941 dec := NewDecoder(b)
942 var rt1 RT1
Rob Pike1399bad2009-07-16 17:55:16 -0700943 // Wire type is RT0, local type is RT1.
Robert Griesemera3d10452009-12-15 15:35:38 -0800944 err := dec.Decode(&rt1)
Rob Pike30b1b9a2009-11-16 23:32:30 -0800945 if err != nil {
Rob Pike30366042011-01-11 13:44:00 -0800946 t.Fatal("decode error:", err)
Rob Pike30b1b9a2009-11-16 23:32:30 -0800947 }
Rob Pike30366042011-01-11 13:44:00 -0800948 if rt0.A != rt1.A || rt0.B != rt1.B || rt0.C != rt1.C {
Robert Griesemer40621d52009-11-09 12:07:39 -0800949 t.Errorf("rt1->rt0: expected %v; got %v", rt0, rt1)
Rob Pike1399bad2009-07-16 17:55:16 -0700950 }
951}
952
953// Like an RT0 but with fields we'll ignore on the decode side.
954type IT0 struct {
Rob Pike30366042011-01-11 13:44:00 -0800955 A int64
956 B string
957 Ignore_d []int
Russ Coxf2b5a072011-01-19 23:09:00 -0500958 Ignore_e [3]float64
Rob Pike30366042011-01-11 13:44:00 -0800959 Ignore_f bool
960 Ignore_g string
961 Ignore_h []byte
962 Ignore_i *RT1
963 Ignore_m map[string]int
Russ Coxf2b5a072011-01-19 23:09:00 -0500964 C float64
Rob Pike1399bad2009-07-16 17:55:16 -0700965}
966
967func TestIgnoredFields(t *testing.T) {
Robert Griesemera3d10452009-12-15 15:35:38 -0800968 var it0 IT0
Rob Pike30366042011-01-11 13:44:00 -0800969 it0.A = 17
970 it0.B = "hello"
971 it0.C = 3.14159
972 it0.Ignore_d = []int{1, 2, 3}
973 it0.Ignore_e[0] = 1.0
974 it0.Ignore_e[1] = 2.0
975 it0.Ignore_e[2] = 3.0
976 it0.Ignore_f = true
977 it0.Ignore_g = "pay no attention"
978 it0.Ignore_h = []byte("to the curtain")
979 it0.Ignore_i = &RT1{3.1, "hi", 7, "hello"}
980 it0.Ignore_m = map[string]int{"one": 1, "two": 2}
Rob Pike1399bad2009-07-16 17:55:16 -0700981
Robert Griesemera3d10452009-12-15 15:35:38 -0800982 b := new(bytes.Buffer)
983 NewEncoder(b).Encode(it0)
984 dec := NewDecoder(b)
985 var rt1 RT1
Rob Pike1399bad2009-07-16 17:55:16 -0700986 // Wire type is IT0, local type is RT1.
Robert Griesemera3d10452009-12-15 15:35:38 -0800987 err := dec.Decode(&rt1)
Rob Pikecb0a02f2009-07-17 11:38:31 -0700988 if err != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800989 t.Error("error: ", err)
Rob Pikecb0a02f2009-07-17 11:38:31 -0700990 }
Rob Pike30366042011-01-11 13:44:00 -0800991 if int(it0.A) != rt1.A || it0.B != rt1.B || it0.C != rt1.C {
992 t.Errorf("rt0->rt1: expected %v; got %v", it0, rt1)
Rob Pike1399bad2009-07-16 17:55:16 -0700993 }
994}
Rob Pike98607d02009-07-29 17:24:25 -0700995
Rob Pikec9b90c92011-02-23 09:49:35 -0800996func TestBadRecursiveType(t *testing.T) {
997 type Rec ***Rec
998 var rec Rec
999 b := new(bytes.Buffer)
1000 err := NewEncoder(b).Encode(&rec)
1001 if err == nil {
1002 t.Error("expected error; got none")
Nathan VanBenschotenb04f3b02015-12-22 02:40:47 -05001003 } else if !strings.Contains(err.Error(), "recursive") {
Rob Pikec9b90c92011-02-23 09:49:35 -08001004 t.Error("expected recursive type error; got", err)
1005 }
1006 // Can't test decode easily because we can't encode one, so we can't pass one to a Decoder.
Rob Pike98607d02009-07-29 17:24:25 -07001007}
1008
Rob Pike9088f9f2010-05-06 10:45:18 -07001009type Indirect struct {
Rob Pike30366042011-01-11 13:44:00 -08001010 A ***[3]int
1011 S ***[]int
1012 M ****map[string]int
Rob Pike9088f9f2010-05-06 10:45:18 -07001013}
1014
1015type Direct struct {
Rob Pike30366042011-01-11 13:44:00 -08001016 A [3]int
1017 S []int
1018 M map[string]int
Rob Pike9088f9f2010-05-06 10:45:18 -07001019}
1020
1021func TestIndirectSliceMapArray(t *testing.T) {
1022 // Marshal indirect, unmarshal to direct.
1023 i := new(Indirect)
Rob Pike30366042011-01-11 13:44:00 -08001024 i.A = new(**[3]int)
1025 *i.A = new(*[3]int)
1026 **i.A = new([3]int)
1027 ***i.A = [3]int{1, 2, 3}
1028 i.S = new(**[]int)
1029 *i.S = new(*[]int)
1030 **i.S = new([]int)
1031 ***i.S = []int{4, 5, 6}
1032 i.M = new(***map[string]int)
1033 *i.M = new(**map[string]int)
1034 **i.M = new(*map[string]int)
1035 ***i.M = new(map[string]int)
1036 ****i.M = map[string]int{"one": 1, "two": 2, "three": 3}
Rob Pike9088f9f2010-05-06 10:45:18 -07001037 b := new(bytes.Buffer)
1038 NewEncoder(b).Encode(i)
1039 dec := NewDecoder(b)
1040 var d Direct
1041 err := dec.Decode(&d)
1042 if err != nil {
1043 t.Error("error: ", err)
1044 }
Rob Pike30366042011-01-11 13:44:00 -08001045 if len(d.A) != 3 || d.A[0] != 1 || d.A[1] != 2 || d.A[2] != 3 {
1046 t.Errorf("indirect to direct: d.A is %v not %v", d.A, ***i.A)
Rob Pike9088f9f2010-05-06 10:45:18 -07001047 }
Rob Pike30366042011-01-11 13:44:00 -08001048 if len(d.S) != 3 || d.S[0] != 4 || d.S[1] != 5 || d.S[2] != 6 {
1049 t.Errorf("indirect to direct: d.S is %v not %v", d.S, ***i.S)
Rob Pike9088f9f2010-05-06 10:45:18 -07001050 }
Rob Pike30366042011-01-11 13:44:00 -08001051 if len(d.M) != 3 || d.M["one"] != 1 || d.M["two"] != 2 || d.M["three"] != 3 {
1052 t.Errorf("indirect to direct: d.M is %v not %v", d.M, ***i.M)
Rob Pike9088f9f2010-05-06 10:45:18 -07001053 }
1054 // Marshal direct, unmarshal to indirect.
Rob Pike30366042011-01-11 13:44:00 -08001055 d.A = [3]int{11, 22, 33}
1056 d.S = []int{44, 55, 66}
1057 d.M = map[string]int{"four": 4, "five": 5, "six": 6}
Rob Pike9088f9f2010-05-06 10:45:18 -07001058 i = new(Indirect)
1059 b.Reset()
1060 NewEncoder(b).Encode(d)
1061 dec = NewDecoder(b)
1062 err = dec.Decode(&i)
1063 if err != nil {
Rob Pike30366042011-01-11 13:44:00 -08001064 t.Fatal("error: ", err)
Rob Pike9088f9f2010-05-06 10:45:18 -07001065 }
Rob Pike30366042011-01-11 13:44:00 -08001066 if len(***i.A) != 3 || (***i.A)[0] != 11 || (***i.A)[1] != 22 || (***i.A)[2] != 33 {
1067 t.Errorf("direct to indirect: ***i.A is %v not %v", ***i.A, d.A)
Rob Pike9088f9f2010-05-06 10:45:18 -07001068 }
Rob Pike30366042011-01-11 13:44:00 -08001069 if len(***i.S) != 3 || (***i.S)[0] != 44 || (***i.S)[1] != 55 || (***i.S)[2] != 66 {
1070 t.Errorf("direct to indirect: ***i.S is %v not %v", ***i.S, ***i.S)
Rob Pike9088f9f2010-05-06 10:45:18 -07001071 }
Rob Pike30366042011-01-11 13:44:00 -08001072 if len(****i.M) != 3 || (****i.M)["four"] != 4 || (****i.M)["five"] != 5 || (****i.M)["six"] != 6 {
1073 t.Errorf("direct to indirect: ****i.M is %v not %v", ****i.M, d.M)
Rob Pike9088f9f2010-05-06 10:45:18 -07001074 }
1075}
Rob Pike5d906462010-10-22 11:17:40 -07001076
1077// An interface with several implementations
1078type Squarer interface {
1079 Square() int
1080}
1081
1082type Int int
1083
1084func (i Int) Square() int {
1085 return int(i * i)
1086}
1087
Russ Coxf2b5a072011-01-19 23:09:00 -05001088type Float float64
Rob Pike5d906462010-10-22 11:17:40 -07001089
1090func (f Float) Square() int {
1091 return int(f * f)
1092}
1093
1094type Vector []int
1095
1096func (v Vector) Square() int {
1097 sum := 0
1098 for _, x := range v {
1099 sum += x * x
1100 }
1101 return sum
1102}
1103
Rob Pike7f7cb162010-10-31 13:41:30 -07001104type Point struct {
Rob Pikea0a4e852011-01-22 00:10:11 -08001105 X, Y int
Rob Pike7f7cb162010-10-31 13:41:30 -07001106}
1107
1108func (p Point) Square() int {
Rob Pikea0a4e852011-01-22 00:10:11 -08001109 return p.X*p.X + p.Y*p.Y
Rob Pike7f7cb162010-10-31 13:41:30 -07001110}
1111
Rob Pike5d906462010-10-22 11:17:40 -07001112// A struct with interfaces in it.
1113type InterfaceItem struct {
Rob Pike30366042011-01-11 13:44:00 -08001114 I int
1115 Sq1, Sq2, Sq3 Squarer
Russ Coxf2b5a072011-01-19 23:09:00 -05001116 F float64
Rob Pike30366042011-01-11 13:44:00 -08001117 Sq []Squarer
Rob Pike5d906462010-10-22 11:17:40 -07001118}
1119
1120// The same struct without interfaces
1121type NoInterfaceItem struct {
Rob Pike30366042011-01-11 13:44:00 -08001122 I int
Russ Coxf2b5a072011-01-19 23:09:00 -05001123 F float64
Rob Pike5d906462010-10-22 11:17:40 -07001124}
1125
1126func TestInterface(t *testing.T) {
1127 iVal := Int(3)
1128 fVal := Float(5)
1129 // Sending a Vector will require that the receiver define a type in the middle of
1130 // receiving the value for item2.
1131 vVal := Vector{1, 2, 3}
1132 b := new(bytes.Buffer)
1133 item1 := &InterfaceItem{1, iVal, fVal, vVal, 11.5, []Squarer{iVal, fVal, nil, vVal}}
1134 // Register the types.
1135 Register(Int(0))
1136 Register(Float(0))
1137 Register(Vector{})
1138 err := NewEncoder(b).Encode(item1)
1139 if err != nil {
1140 t.Error("expected no encode error; got", err)
1141 }
1142
1143 item2 := InterfaceItem{}
1144 err = NewDecoder(b).Decode(&item2)
1145 if err != nil {
1146 t.Fatal("decode:", err)
1147 }
Rob Pike30366042011-01-11 13:44:00 -08001148 if item2.I != item1.I {
Rob Pike5d906462010-10-22 11:17:40 -07001149 t.Error("normal int did not decode correctly")
1150 }
Rob Pike30366042011-01-11 13:44:00 -08001151 if item2.Sq1 == nil || item2.Sq1.Square() != iVal.Square() {
Rob Pike5d906462010-10-22 11:17:40 -07001152 t.Error("Int did not decode correctly")
1153 }
Rob Pike30366042011-01-11 13:44:00 -08001154 if item2.Sq2 == nil || item2.Sq2.Square() != fVal.Square() {
Rob Pike5d906462010-10-22 11:17:40 -07001155 t.Error("Float did not decode correctly")
1156 }
Rob Pike30366042011-01-11 13:44:00 -08001157 if item2.Sq3 == nil || item2.Sq3.Square() != vVal.Square() {
Rob Pike5d906462010-10-22 11:17:40 -07001158 t.Error("Vector did not decode correctly")
1159 }
Rob Pike30366042011-01-11 13:44:00 -08001160 if item2.F != item1.F {
Rob Pike5d906462010-10-22 11:17:40 -07001161 t.Error("normal float did not decode correctly")
1162 }
1163 // Now check that we received a slice of Squarers correctly, including a nil element
Rob Pike30366042011-01-11 13:44:00 -08001164 if len(item1.Sq) != len(item2.Sq) {
1165 t.Fatalf("[]Squarer length wrong: got %d; expected %d", len(item2.Sq), len(item1.Sq))
Rob Pike5d906462010-10-22 11:17:40 -07001166 }
Rob Pike30366042011-01-11 13:44:00 -08001167 for i, v1 := range item1.Sq {
1168 v2 := item2.Sq[i]
Rob Pike5d906462010-10-22 11:17:40 -07001169 if v1 == nil || v2 == nil {
1170 if v1 != nil || v2 != nil {
1171 t.Errorf("item %d inconsistent nils", i)
1172 }
Rob Pikef29b0912013-03-22 14:22:55 -07001173 } else if v1.Square() != v2.Square() {
1174 t.Errorf("item %d inconsistent values: %v %v", i, v1, v2)
Rob Pike5d906462010-10-22 11:17:40 -07001175 }
1176 }
Rob Pike5d906462010-10-22 11:17:40 -07001177}
1178
Rob Pikef437d4d2010-10-26 17:07:39 -07001179// A struct with all basic types, stored in interfaces.
1180type BasicInterfaceItem struct {
1181 Int, Int8, Int16, Int32, Int64 interface{}
1182 Uint, Uint8, Uint16, Uint32, Uint64 interface{}
Russ Coxf2b5a072011-01-19 23:09:00 -05001183 Float32, Float64 interface{}
1184 Complex64, Complex128 interface{}
Rob Pikef437d4d2010-10-26 17:07:39 -07001185 Bool interface{}
1186 String interface{}
1187 Bytes interface{}
1188}
1189
1190func TestInterfaceBasic(t *testing.T) {
1191 b := new(bytes.Buffer)
1192 item1 := &BasicInterfaceItem{
1193 int(1), int8(1), int16(1), int32(1), int64(1),
1194 uint(1), uint8(1), uint16(1), uint32(1), uint64(1),
Russ Coxf2b5a072011-01-19 23:09:00 -05001195 float32(1), 1.0,
Rob Pike9b824812011-01-28 10:53:42 -08001196 complex64(1i), complex128(1i),
Rob Pikef437d4d2010-10-26 17:07:39 -07001197 true,
1198 "hello",
1199 []byte("sailor"),
1200 }
Rob Pikef437d4d2010-10-26 17:07:39 -07001201 err := NewEncoder(b).Encode(item1)
1202 if err != nil {
1203 t.Error("expected no encode error; got", err)
1204 }
1205
1206 item2 := &BasicInterfaceItem{}
1207 err = NewDecoder(b).Decode(&item2)
1208 if err != nil {
1209 t.Fatal("decode:", err)
1210 }
1211 if !reflect.DeepEqual(item1, item2) {
1212 t.Errorf("encode expected %v got %v", item1, item2)
1213 }
1214 // Hand check a couple for correct types.
1215 if v, ok := item2.Bool.(bool); !ok || !v {
1216 t.Error("boolean should be true")
1217 }
1218 if v, ok := item2.String.(string); !ok || v != item1.String.(string) {
1219 t.Errorf("string should be %v is %v", item1.String, v)
1220 }
1221}
1222
Rob Pike9f7f69d2010-11-05 10:36:27 -07001223type String string
1224
1225type PtrInterfaceItem struct {
Rob Pike30366042011-01-11 13:44:00 -08001226 Str1 interface{} // basic
1227 Str2 interface{} // derived
Rob Pike9f7f69d2010-11-05 10:36:27 -07001228}
1229
1230// We'll send pointers; should receive values.
1231// Also check that we can register T but send *T.
1232func TestInterfacePointer(t *testing.T) {
1233 b := new(bytes.Buffer)
1234 str1 := "howdy"
1235 str2 := String("kiddo")
1236 item1 := &PtrInterfaceItem{
1237 &str1,
1238 &str2,
1239 }
1240 // Register the type.
1241 Register(str2)
1242 err := NewEncoder(b).Encode(item1)
1243 if err != nil {
1244 t.Error("expected no encode error; got", err)
1245 }
1246
1247 item2 := &PtrInterfaceItem{}
1248 err = NewDecoder(b).Decode(&item2)
1249 if err != nil {
1250 t.Fatal("decode:", err)
1251 }
1252 // Hand test for correct types and values.
Rob Pike30366042011-01-11 13:44:00 -08001253 if v, ok := item2.Str1.(string); !ok || v != str1 {
Rob Pike9f7f69d2010-11-05 10:36:27 -07001254 t.Errorf("basic string failed: %q should be %q", v, str1)
1255 }
Rob Pike30366042011-01-11 13:44:00 -08001256 if v, ok := item2.Str2.(String); !ok || v != str2 {
Rob Pike9f7f69d2010-11-05 10:36:27 -07001257 t.Errorf("derived type String failed: %q should be %q", v, str2)
1258 }
1259}
1260
Rob Pike5d906462010-10-22 11:17:40 -07001261func TestIgnoreInterface(t *testing.T) {
1262 iVal := Int(3)
1263 fVal := Float(5)
Rob Pike7f7cb162010-10-31 13:41:30 -07001264 // Sending a Point will require that the receiver define a type in the middle of
Rob Pike5d906462010-10-22 11:17:40 -07001265 // receiving the value for item2.
Rob Pike7f7cb162010-10-31 13:41:30 -07001266 pVal := Point{2, 3}
Rob Pike5d906462010-10-22 11:17:40 -07001267 b := new(bytes.Buffer)
Rob Pike7f7cb162010-10-31 13:41:30 -07001268 item1 := &InterfaceItem{1, iVal, fVal, pVal, 11.5, nil}
Rob Pike5d906462010-10-22 11:17:40 -07001269 // Register the types.
1270 Register(Int(0))
1271 Register(Float(0))
Rob Pike7f7cb162010-10-31 13:41:30 -07001272 Register(Point{})
Rob Pike5d906462010-10-22 11:17:40 -07001273 err := NewEncoder(b).Encode(item1)
1274 if err != nil {
1275 t.Error("expected no encode error; got", err)
1276 }
1277
1278 item2 := NoInterfaceItem{}
1279 err = NewDecoder(b).Decode(&item2)
1280 if err != nil {
1281 t.Fatal("decode:", err)
1282 }
Rob Pike30366042011-01-11 13:44:00 -08001283 if item2.I != item1.I {
Rob Pike5d906462010-10-22 11:17:40 -07001284 t.Error("normal int did not decode correctly")
1285 }
Alberto Donizettia5c5f6e2016-05-06 18:16:52 +02001286 if item2.F != item1.F {
Rob Pike5d906462010-10-22 11:17:40 -07001287 t.Error("normal float did not decode correctly")
1288 }
1289}
Rob Pike96b9efe2010-10-29 15:07:56 -07001290
Rob Pike30366042011-01-11 13:44:00 -08001291type U struct {
1292 A int
1293 B string
Russ Coxf2b5a072011-01-19 23:09:00 -05001294 c float64
Rob Pike30366042011-01-11 13:44:00 -08001295 D uint
1296}
1297
1298func TestUnexportedFields(t *testing.T) {
1299 var u0 U
1300 u0.A = 17
1301 u0.B = "hello"
1302 u0.c = 3.14159
1303 u0.D = 23
1304 b := new(bytes.Buffer)
1305 NewEncoder(b).Encode(u0)
1306 dec := NewDecoder(b)
1307 var u1 U
1308 u1.c = 1234.
1309 err := dec.Decode(&u1)
1310 if err != nil {
1311 t.Fatal("decode error:", err)
1312 }
Alberto Donizettia5c5f6e2016-05-06 18:16:52 +02001313 if u0.A != u1.A || u0.B != u1.B || u0.D != u1.D {
Rob Pike30366042011-01-11 13:44:00 -08001314 t.Errorf("u1->u0: expected %v; got %v", u0, u1)
1315 }
1316 if u1.c != 1234. {
1317 t.Error("u1.c modified")
1318 }
1319}
1320
Rob Pike5b5a6742011-01-21 11:28:53 -08001321var singletons = []interface{}{
1322 true,
1323 7,
1324 3.2,
1325 "hello",
1326 [3]int{11, 22, 33},
1327 []float32{0.5, 0.25, 0.125},
1328 map[string]int{"one": 1, "two": 2},
1329}
1330
1331func TestDebugSingleton(t *testing.T) {
1332 if debugFunc == nil {
1333 return
1334 }
1335 b := new(bytes.Buffer)
1336 // Accumulate a number of values and print them out all at once.
1337 for _, x := range singletons {
1338 err := NewEncoder(b).Encode(x)
1339 if err != nil {
1340 t.Fatal("encode:", err)
1341 }
1342 }
1343 debugFunc(b)
1344}
1345
Rob Pike96b9efe2010-10-29 15:07:56 -07001346// A type that won't be defined in the gob until we send it in an interface value.
1347type OnTheFly struct {
Rob Pike30366042011-01-11 13:44:00 -08001348 A int
Rob Pike96b9efe2010-10-29 15:07:56 -07001349}
1350
1351type DT struct {
1352 // X OnTheFly
Rob Pike30366042011-01-11 13:44:00 -08001353 A int
1354 B string
Russ Coxf2b5a072011-01-19 23:09:00 -05001355 C float64
Rob Pike30366042011-01-11 13:44:00 -08001356 I interface{}
1357 J interface{}
1358 I_nil interface{}
1359 M map[string]int
1360 T [3]int
1361 S []string
Rob Pike96b9efe2010-10-29 15:07:56 -07001362}
1363
Rémy Oudompheng7a73f322014-01-30 07:54:57 +01001364func newDT() DT {
Rob Pike96b9efe2010-10-29 15:07:56 -07001365 var dt DT
Rob Pike30366042011-01-11 13:44:00 -08001366 dt.A = 17
1367 dt.B = "hello"
1368 dt.C = 3.14159
1369 dt.I = 271828
1370 dt.J = OnTheFly{3}
1371 dt.I_nil = nil
1372 dt.M = map[string]int{"one": 1, "two": 2}
1373 dt.T = [3]int{11, 22, 33}
1374 dt.S = []string{"hi", "joe"}
Rémy Oudompheng7a73f322014-01-30 07:54:57 +01001375 return dt
1376}
1377
1378func TestDebugStruct(t *testing.T) {
1379 if debugFunc == nil {
1380 return
1381 }
1382 Register(OnTheFly{})
1383 dt := newDT()
Rob Pike96b9efe2010-10-29 15:07:56 -07001384 b := new(bytes.Buffer)
1385 err := NewEncoder(b).Encode(dt)
1386 if err != nil {
1387 t.Fatal("encode:", err)
1388 }
1389 debugBuffer := bytes.NewBuffer(b.Bytes())
1390 dt2 := &DT{}
1391 err = NewDecoder(b).Decode(&dt2)
1392 if err != nil {
1393 t.Error("decode:", err)
1394 }
1395 debugFunc(debugBuffer)
1396}
David Symonds9440d822012-02-06 14:02:12 +11001397
1398func encFuzzDec(rng *rand.Rand, in interface{}) error {
1399 buf := new(bytes.Buffer)
1400 enc := NewEncoder(buf)
1401 if err := enc.Encode(&in); err != nil {
1402 return err
1403 }
1404
1405 b := buf.Bytes()
1406 for i, bi := range b {
1407 if rng.Intn(10) < 3 {
1408 b[i] = bi + uint8(rng.Intn(256))
1409 }
1410 }
1411
1412 dec := NewDecoder(buf)
1413 var e interface{}
1414 if err := dec.Decode(&e); err != nil {
1415 return err
1416 }
1417 return nil
1418}
1419
1420// This does some "fuzz testing" by attempting to decode a sequence of random bytes.
1421func TestFuzz(t *testing.T) {
Rob Pikebbe60172012-07-13 14:23:51 -07001422 if !*doFuzzTests {
1423 t.Logf("disabled; run with -gob.fuzz to enable")
David Symonds9440d822012-02-06 14:02:12 +11001424 return
1425 }
1426
1427 // all possible inputs
1428 input := []interface{}{
1429 new(int),
1430 new(float32),
1431 new(float64),
1432 new(complex128),
1433 &ByteStruct{255},
1434 &ArrayStruct{},
1435 &StringStruct{"hello"},
1436 &GobTest1{0, &StringStruct{"hello"}},
1437 }
1438 testFuzz(t, time.Now().UnixNano(), 100, input...)
1439}
1440
1441func TestFuzzRegressions(t *testing.T) {
Rob Pikebbe60172012-07-13 14:23:51 -07001442 if !*doFuzzTests {
1443 t.Logf("disabled; run with -gob.fuzz to enable")
1444 return
1445 }
1446
David Symonds9440d822012-02-06 14:02:12 +11001447 // An instance triggering a type name of length ~102 GB.
1448 testFuzz(t, 1328492090837718000, 100, new(float32))
David Symonds1f0f4592012-03-01 15:57:54 +11001449 // An instance triggering a type name of 1.6 GB.
Rob Pikebbe60172012-07-13 14:23:51 -07001450 // Note: can take several minutes to run.
1451 testFuzz(t, 1330522872628565000, 100, new(int))
David Symonds9440d822012-02-06 14:02:12 +11001452}
1453
1454func testFuzz(t *testing.T, seed int64, n int, input ...interface{}) {
David Symonds9440d822012-02-06 14:02:12 +11001455 for _, e := range input {
David Symonds1f0f4592012-03-01 15:57:54 +11001456 t.Logf("seed=%d n=%d e=%T", seed, n, e)
David Symonds9440d822012-02-06 14:02:12 +11001457 rng := rand.New(rand.NewSource(seed))
1458 for i := 0; i < n; i++ {
1459 encFuzzDec(rng, e)
1460 }
1461 }
1462}
Rémy Oudompheng7a73f322014-01-30 07:54:57 +01001463
1464// TestFuzzOneByte tries to decode corrupted input sequences
1465// and checks that no panic occurs.
1466func TestFuzzOneByte(t *testing.T) {
1467 buf := new(bytes.Buffer)
1468 Register(OnTheFly{})
1469 dt := newDT()
1470 if err := NewEncoder(buf).Encode(dt); err != nil {
1471 t.Fatal(err)
1472 }
1473 s := buf.String()
1474
1475 indices := make([]int, 0, len(s))
1476 for i := 0; i < len(s); i++ {
1477 switch i {
1478 case 14, 167, 231, 265: // a slice length, corruptions are not handled yet.
1479 continue
1480 }
1481 indices = append(indices, i)
1482 }
1483 if testing.Short() {
1484 indices = []int{1, 111, 178} // known fixed panics
1485 }
1486 for _, i := range indices {
1487 for j := 0; j < 256; j += 3 {
1488 b := []byte(s)
1489 b[i] ^= byte(j)
1490 var e DT
1491 func() {
1492 defer func() {
1493 if p := recover(); p != nil {
1494 t.Errorf("crash for b[%d] ^= 0x%x", i, j)
1495 panic(p)
1496 }
1497 }()
1498 err := NewDecoder(bytes.NewReader(b)).Decode(&e)
1499 _ = err
1500 }()
1501 }
1502 }
1503}
Håvard Haugen8e6cf5f2015-01-28 23:07:05 +01001504
1505// Don't crash, just give error with invalid type id.
1506// Issue 9649.
1507func TestErrorInvalidTypeId(t *testing.T) {
1508 data := []byte{0x01, 0x00, 0x01, 0x00}
1509 d := NewDecoder(bytes.NewReader(data))
1510 // When running d.Decode(&foo) the first time the decoder stops
1511 // after []byte{0x01, 0x00} and reports an errBadType. Running
1512 // d.Decode(&foo) again on exactly the same input sequence should
1513 // give another errBadType, but instead caused a panic because
1514 // decoderMap wasn't cleaned up properly after the first error.
1515 for i := 0; i < 2; i++ {
1516 var foo struct{}
1517 err := d.Decode(&foo)
1518 if err != errBadType {
Rob Pikef62b7492015-08-11 15:29:40 +10001519 t.Fatalf("decode: expected %s, got %s", errBadType, err)
Håvard Haugen8e6cf5f2015-01-28 23:07:05 +01001520 }
1521 }
1522}