Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 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 ( |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 8 | "bytes" |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 9 | "errors" |
Rob Pike | bbe6017 | 2012-07-13 14:23:51 -0700 | [diff] [blame] | 10 | "flag" |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 11 | "math" |
David Symonds | 9440d82 | 2012-02-06 14:02:12 +1100 | [diff] [blame] | 12 | "math/rand" |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 13 | "reflect" |
| 14 | "strings" |
| 15 | "testing" |
David Symonds | 9440d82 | 2012-02-06 14:02:12 +1100 | [diff] [blame] | 16 | "time" |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 17 | ) |
| 18 | |
Rob Pike | bbe6017 | 2012-07-13 14:23:51 -0700 | [diff] [blame] | 19 | var doFuzzTests = flag.Bool("gob.fuzz", false, "run the fuzz tests, which are large and very slow") |
| 20 | |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 21 | // Guarantee encoding format by comparing some encodings to hand-written values |
| 22 | type EncodeT struct { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 23 | x uint64 |
| 24 | b []byte |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 25 | } |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 26 | |
| 27 | var encodeT = []EncodeT{ |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 28 | {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 Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Rob Pike | f593b37 | 2010-10-22 15:16:34 -0700 | [diff] [blame] | 44 | // testError is meant to be used as a deferred function to turn a panic(gobError) into a |
| 45 | // plain test.Error call. |
| 46 | func testError(t *testing.T) { |
| 47 | if e := recover(); e != nil { |
Russ Cox | c0a0fd6 | 2011-10-27 20:20:59 -0700 | [diff] [blame] | 48 | t.Error(e.(gobError).err) // Will re-panic if not one of our errors, such as a runtime error. |
Rob Pike | f593b37 | 2010-10-22 15:16:34 -0700 | [diff] [blame] | 49 | } |
Rob Pike | f593b37 | 2010-10-22 15:16:34 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Rob Pike | 63acc48 | 2014-10-20 07:33:08 -0700 | [diff] [blame] | 52 | func newDecBuffer(data []byte) *decBuffer { |
| 53 | return &decBuffer{ |
| 54 | data: data, |
| 55 | } |
| 56 | } |
| 57 | |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 58 | // Test basic encode/decode routines for unsigned integers |
| 59 | func TestUintCodec(t *testing.T) { |
Rob Pike | f593b37 | 2010-10-22 15:16:34 -0700 | [diff] [blame] | 60 | defer testError(t) |
Rob Pike | 65dde1e | 2014-10-17 20:51:15 -0700 | [diff] [blame] | 61 | b := new(encBuffer) |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 62 | encState := newEncoderState(b) |
Russ Cox | ca6a0fe | 2009-09-15 09:41:59 -0700 | [diff] [blame] | 63 | for _, tt := range encodeT { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 64 | b.Reset() |
Rob Pike | a80cdcb | 2011-01-11 17:56:45 -0800 | [diff] [blame] | 65 | encState.encodeUint(tt.x) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 66 | if !bytes.Equal(tt.b, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 67 | t.Errorf("encodeUint: %#x encode: expected % x got % x", tt.x, tt.b, b.Bytes()) |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
Robert Griesemer | baba292 | 2009-11-09 21:13:17 -0800 | [diff] [blame] | 70 | for u := uint64(0); ; u = (u + 1) * 7 { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 71 | b.Reset() |
Rob Pike | a80cdcb | 2011-01-11 17:56:45 -0800 | [diff] [blame] | 72 | encState.encodeUint(u) |
Rob Pike | 63acc48 | 2014-10-20 07:33:08 -0700 | [diff] [blame] | 73 | decState := newDecodeState(newDecBuffer(b.Bytes())) |
Rob Pike | a80cdcb | 2011-01-11 17:56:45 -0800 | [diff] [blame] | 74 | v := decState.decodeUint() |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 75 | if u != v { |
Rob Pike | 1959c3a | 2010-09-23 13:48:56 +1000 | [diff] [blame] | 76 | t.Errorf("Encode/Decode: sent %#x received %#x", u, v) |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 77 | } |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 78 | if u&(1<<63) != 0 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 79 | break |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func verifyInt(i int64, t *testing.T) { |
Rob Pike | f593b37 | 2010-10-22 15:16:34 -0700 | [diff] [blame] | 85 | defer testError(t) |
Rob Pike | 65dde1e | 2014-10-17 20:51:15 -0700 | [diff] [blame] | 86 | var b = new(encBuffer) |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 87 | encState := newEncoderState(b) |
Rob Pike | a80cdcb | 2011-01-11 17:56:45 -0800 | [diff] [blame] | 88 | encState.encodeInt(i) |
Rob Pike | 63acc48 | 2014-10-20 07:33:08 -0700 | [diff] [blame] | 89 | decState := newDecodeState(newDecBuffer(b.Bytes())) |
Rob Pike | a80cdcb | 2011-01-11 17:56:45 -0800 | [diff] [blame] | 90 | j := decState.decodeInt() |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 91 | if i != j { |
Rob Pike | 1959c3a | 2010-09-23 13:48:56 +1000 | [diff] [blame] | 92 | t.Errorf("Encode/Decode: sent %#x received %#x", uint64(i), uint64(j)) |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | // Test basic encode/decode routines for signed integers |
| 97 | func TestIntCodec(t *testing.T) { |
Robert Griesemer | baba292 | 2009-11-09 21:13:17 -0800 | [diff] [blame] | 98 | for u := uint64(0); ; u = (u + 1) * 7 { |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 99 | // Do positive and negative values |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 100 | i := int64(u) |
| 101 | verifyInt(i, t) |
| 102 | verifyInt(-i, t) |
| 103 | verifyInt(^i, t) |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 104 | if u&(1<<63) != 0 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 105 | break |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 108 | verifyInt(-1<<63, t) // a tricky case |
Rob Pike | b948c43 | 2009-06-29 15:15:07 -0700 | [diff] [blame] | 109 | } |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 110 | |
Rob Pike | b85147c | 2009-07-28 17:20:19 -0700 | [diff] [blame] | 111 | // The result of encoding a true boolean with field number 7 |
| 112 | var boolResult = []byte{0x07, 0x01} |
Robert Griesemer | 541b67d | 2011-12-16 15:43:06 -0800 | [diff] [blame] | 113 | |
Rob Pike | b85147c | 2009-07-28 17:20:19 -0700 | [diff] [blame] | 114 | // The result of encoding a number 17 with field number 7 |
Robert Griesemer | baba292 | 2009-11-09 21:13:17 -0800 | [diff] [blame] | 115 | var signedResult = []byte{0x07, 2 * 17} |
Rob Pike | b85147c | 2009-07-28 17:20:19 -0700 | [diff] [blame] | 116 | var unsignedResult = []byte{0x07, 17} |
| 117 | var floatResult = []byte{0x07, 0xFE, 0x31, 0x40} |
Robert Griesemer | 541b67d | 2011-12-16 15:43:06 -0800 | [diff] [blame] | 118 | |
Rob Pike | 37a6bc8 | 2010-06-24 15:07:28 -0700 | [diff] [blame] | 119 | // The result of encoding a number 17+19i with field number 7 |
| 120 | var complexResult = []byte{0x07, 0xFE, 0x31, 0x40, 0xFE, 0x33, 0x40} |
Robert Griesemer | 541b67d | 2011-12-16 15:43:06 -0800 | [diff] [blame] | 121 | |
Rob Pike | 37a6bc8 | 2010-06-24 15:07:28 -0700 | [diff] [blame] | 122 | // The result of encoding "hello" with field number 7 |
Rob Pike | b85147c | 2009-07-28 17:20:19 -0700 | [diff] [blame] | 123 | var bytesResult = []byte{0x07, 0x05, 'h', 'e', 'l', 'l', 'o'} |
Rob Pike | c701af8 | 2009-07-01 18:25:13 -0700 | [diff] [blame] | 124 | |
Rob Pike | 63acc48 | 2014-10-20 07:33:08 -0700 | [diff] [blame] | 125 | func newDecodeState(buf *decBuffer) *decoderState { |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 126 | d := new(decoderState) |
| 127 | d.b = buf |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 128 | return d |
| 129 | } |
| 130 | |
Rob Pike | 65dde1e | 2014-10-17 20:51:15 -0700 | [diff] [blame] | 131 | func newEncoderState(b *encBuffer) *encoderState { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 132 | b.Reset() |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 133 | state := &encoderState{enc: nil, b: b} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 134 | state.fieldnum = -1 |
| 135 | return state |
Rob Pike | c701af8 | 2009-07-01 18:25:13 -0700 | [diff] [blame] | 136 | } |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 137 | |
| 138 | // Test instruction execution for encoding. |
| 139 | // Do not run the machine yet; instead do individual instructions crafted by hand. |
| 140 | func TestScalarEncInstructions(t *testing.T) { |
Rob Pike | 65dde1e | 2014-10-17 20:51:15 -0700 | [diff] [blame] | 141 | var b = new(encBuffer) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 142 | |
| 143 | // bool |
| 144 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 145 | var data bool = true |
| 146 | instr := &encInstr{encBool, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 147 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 148 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 149 | if !bytes.Equal(boolResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 150 | t.Errorf("bool enc instructions: expected % x got % x", boolResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | // int |
| 155 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 156 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 157 | var data int = 17 |
| 158 | instr := &encInstr{encInt, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 159 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 160 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 161 | if !bytes.Equal(signedResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 162 | t.Errorf("int enc instructions: expected % x got % x", signedResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | // uint |
| 167 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 168 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 169 | var data uint = 17 |
| 170 | instr := &encInstr{encUint, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 171 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 172 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 173 | if !bytes.Equal(unsignedResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 174 | t.Errorf("uint enc instructions: expected % x got % x", unsignedResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | // int8 |
| 179 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 180 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 181 | var data int8 = 17 |
| 182 | instr := &encInstr{encInt, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 183 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 184 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 185 | if !bytes.Equal(signedResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 186 | t.Errorf("int8 enc instructions: expected % x got % x", signedResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | // uint8 |
| 191 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 192 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 193 | var data uint8 = 17 |
| 194 | instr := &encInstr{encUint, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 195 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 196 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 197 | if !bytes.Equal(unsignedResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 198 | t.Errorf("uint8 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | // int16 |
| 203 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 204 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 205 | var data int16 = 17 |
| 206 | instr := &encInstr{encInt, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 207 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 208 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 209 | if !bytes.Equal(signedResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 210 | t.Errorf("int16 enc instructions: expected % x got % x", signedResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | // uint16 |
| 215 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 216 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 217 | var data uint16 = 17 |
| 218 | instr := &encInstr{encUint, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 219 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 220 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 221 | if !bytes.Equal(unsignedResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 222 | t.Errorf("uint16 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | // int32 |
| 227 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 228 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 229 | var data int32 = 17 |
| 230 | instr := &encInstr{encInt, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 231 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 232 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 233 | if !bytes.Equal(signedResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 234 | t.Errorf("int32 enc instructions: expected % x got % x", signedResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | // uint32 |
| 239 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 240 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 241 | var data uint32 = 17 |
| 242 | instr := &encInstr{encUint, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 243 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 244 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 245 | if !bytes.Equal(unsignedResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 246 | t.Errorf("uint32 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | // int64 |
| 251 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 252 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 253 | var data int64 = 17 |
| 254 | instr := &encInstr{encInt, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 255 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 256 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 257 | if !bytes.Equal(signedResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 258 | t.Errorf("int64 enc instructions: expected % x got % x", signedResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | // uint64 |
| 263 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 264 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 265 | var data uint64 = 17 |
| 266 | instr := &encInstr{encUint, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 267 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 268 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 269 | if !bytes.Equal(unsignedResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 270 | t.Errorf("uint64 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 274 | // float32 |
| 275 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 276 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 277 | var data float32 = 17 |
| 278 | instr := &encInstr{encFloat, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 279 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 280 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 281 | if !bytes.Equal(floatResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 282 | t.Errorf("float32 enc instructions: expected % x got % x", floatResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | // float64 |
| 287 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 288 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 289 | var data float64 = 17 |
| 290 | instr := &encInstr{encFloat, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 291 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 292 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 293 | if !bytes.Equal(floatResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 294 | t.Errorf("float64 enc instructions: expected % x got % x", floatResult, b.Bytes()) |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
Rob Pike | 1ca1e1b | 2009-07-02 11:21:01 -0700 | [diff] [blame] | 297 | |
| 298 | // bytes == []uint8 |
| 299 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 300 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 301 | data := []byte("hello") |
| 302 | instr := &encInstr{encUint8Array, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 303 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 304 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 305 | if !bytes.Equal(bytesResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 306 | t.Errorf("bytes enc instructions: expected % x got % x", bytesResult, b.Bytes()) |
Rob Pike | 1ca1e1b | 2009-07-02 11:21:01 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | // string |
| 311 | { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 312 | b.Reset() |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 313 | var data string = "hello" |
| 314 | instr := &encInstr{encString, 6, nil, 0} |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 315 | state := newEncoderState(b) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 316 | instr.op(instr, state, reflect.ValueOf(data)) |
Rob Pike | d5be41f | 2009-09-16 15:15:00 -0700 | [diff] [blame] | 317 | if !bytes.Equal(bytesResult, b.Bytes()) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 318 | t.Errorf("string enc instructions: expected % x got % x", bytesResult, b.Bytes()) |
Rob Pike | 1ca1e1b | 2009-07-02 11:21:01 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
Rob Pike | f6f8251 | 2009-06-30 15:37:46 -0700 | [diff] [blame] | 321 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 322 | |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 323 | func execDec(instr *decInstr, state *decoderState, t *testing.T, value reflect.Value) { |
Rob Pike | f593b37 | 2010-10-22 15:16:34 -0700 | [diff] [blame] | 324 | defer testError(t) |
Rob Pike | a80cdcb | 2011-01-11 17:56:45 -0800 | [diff] [blame] | 325 | v := int(state.decodeUint()) |
Robert Griesemer | baba292 | 2009-11-09 21:13:17 -0800 | [diff] [blame] | 326 | if v+state.fieldnum != 6 { |
| 327 | t.Fatalf("decoding field number %d, got %d", 6, v+state.fieldnum) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 328 | } |
Russ Cox | e4bc3c4 | 2014-07-01 14:19:27 -0400 | [diff] [blame] | 329 | instr.op(instr, state, value.Elem()) |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 330 | state.fieldnum = 6 |
Rob Pike | c701af8 | 2009-07-01 18:25:13 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Rob Pike | c91daef | 2011-03-04 12:25:18 -0800 | [diff] [blame] | 333 | func newDecodeStateFromData(data []byte) *decoderState { |
Rob Pike | 63acc48 | 2014-10-20 07:33:08 -0700 | [diff] [blame] | 334 | b := newDecBuffer(data) |
Rob Pike | eb864e9 | 2011-03-15 21:30:07 -0700 | [diff] [blame] | 335 | state := newDecodeState(b) |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 336 | state.fieldnum = -1 |
| 337 | return state |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | // Test instruction execution for decoding. |
| 341 | // Do not run the machine yet; instead do individual instructions crafted by hand. |
| 342 | func TestScalarDecInstructions(t *testing.T) { |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 343 | ovfl := errors.New("overflow") |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 344 | |
| 345 | // bool |
| 346 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 347 | var data bool |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 348 | instr := &decInstr{decBool, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 349 | state := newDecodeStateFromData(boolResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 350 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 351 | if data != true { |
| 352 | t.Errorf("bool a = %v not true", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 353 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 354 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 355 | // int |
| 356 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 357 | var data int |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 358 | instr := &decInstr{decOpTable[reflect.Int], 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 359 | state := newDecodeStateFromData(signedResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 360 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 361 | if data != 17 { |
| 362 | t.Errorf("int a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 363 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | // uint |
| 367 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 368 | var data uint |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 369 | instr := &decInstr{decOpTable[reflect.Uint], 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 370 | state := newDecodeStateFromData(unsignedResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 371 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 372 | if data != 17 { |
| 373 | t.Errorf("uint a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 374 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // int8 |
| 378 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 379 | var data int8 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 380 | instr := &decInstr{decInt8, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 381 | state := newDecodeStateFromData(signedResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 382 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 383 | if data != 17 { |
| 384 | t.Errorf("int8 a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 385 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // uint8 |
| 389 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 390 | var data uint8 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 391 | instr := &decInstr{decUint8, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 392 | state := newDecodeStateFromData(unsignedResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 393 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 394 | if data != 17 { |
| 395 | t.Errorf("uint8 a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 396 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // int16 |
| 400 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 401 | var data int16 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 402 | instr := &decInstr{decInt16, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 403 | state := newDecodeStateFromData(signedResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 404 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 405 | if data != 17 { |
| 406 | t.Errorf("int16 a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 407 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // uint16 |
| 411 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 412 | var data uint16 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 413 | instr := &decInstr{decUint16, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 414 | state := newDecodeStateFromData(unsignedResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 415 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 416 | if data != 17 { |
| 417 | t.Errorf("uint16 a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 418 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // int32 |
| 422 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 423 | var data int32 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 424 | instr := &decInstr{decInt32, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 425 | state := newDecodeStateFromData(signedResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 426 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 427 | if data != 17 { |
| 428 | t.Errorf("int32 a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 429 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // uint32 |
| 433 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 434 | var data uint32 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 435 | instr := &decInstr{decUint32, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 436 | state := newDecodeStateFromData(unsignedResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 437 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 438 | if data != 17 { |
| 439 | t.Errorf("uint32 a = %v not 17", data) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | // uintptr |
| 444 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 445 | var data uintptr |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 446 | instr := &decInstr{decOpTable[reflect.Uintptr], 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 447 | state := newDecodeStateFromData(unsignedResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 448 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 449 | if data != 17 { |
| 450 | t.Errorf("uintptr a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 451 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // int64 |
| 455 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 456 | var data int64 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 457 | instr := &decInstr{decInt64, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 458 | state := newDecodeStateFromData(signedResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 459 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 460 | if data != 17 { |
| 461 | t.Errorf("int64 a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 462 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // uint64 |
| 466 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 467 | var data uint64 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 468 | instr := &decInstr{decUint64, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 469 | state := newDecodeStateFromData(unsignedResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 470 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 471 | if data != 17 { |
| 472 | t.Errorf("uint64 a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 473 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 476 | // float32 |
| 477 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 478 | var data float32 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 479 | instr := &decInstr{decFloat32, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 480 | state := newDecodeStateFromData(floatResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 481 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 482 | if data != 17 { |
| 483 | t.Errorf("float32 a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 484 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // float64 |
| 488 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 489 | var data float64 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 490 | instr := &decInstr{decFloat64, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 491 | state := newDecodeStateFromData(floatResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 492 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 493 | if data != 17 { |
| 494 | t.Errorf("float64 a = %v not 17", data) |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 495 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 496 | } |
Rob Pike | 1ca1e1b | 2009-07-02 11:21:01 -0700 | [diff] [blame] | 497 | |
Rob Pike | 37a6bc8 | 2010-06-24 15:07:28 -0700 | [diff] [blame] | 498 | // complex64 |
| 499 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 500 | var data complex64 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 501 | instr := &decInstr{decOpTable[reflect.Complex64], 6, nil, ovfl} |
Rob Pike | 37a6bc8 | 2010-06-24 15:07:28 -0700 | [diff] [blame] | 502 | state := newDecodeStateFromData(complexResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 503 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 504 | if data != 17+19i { |
| 505 | t.Errorf("complex a = %v not 17+19i", data) |
Rob Pike | 37a6bc8 | 2010-06-24 15:07:28 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | // complex128 |
| 510 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 511 | var data complex128 |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 512 | instr := &decInstr{decOpTable[reflect.Complex128], 6, nil, ovfl} |
Rob Pike | 37a6bc8 | 2010-06-24 15:07:28 -0700 | [diff] [blame] | 513 | state := newDecodeStateFromData(complexResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 514 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 515 | if data != 17+19i { |
| 516 | t.Errorf("complex a = %v not 17+19i", data) |
Rob Pike | 37a6bc8 | 2010-06-24 15:07:28 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Rob Pike | 1ca1e1b | 2009-07-02 11:21:01 -0700 | [diff] [blame] | 520 | // bytes == []uint8 |
| 521 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 522 | var data []byte |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 523 | instr := &decInstr{decUint8Slice, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 524 | state := newDecodeStateFromData(bytesResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 525 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 526 | if string(data) != "hello" { |
| 527 | t.Errorf(`bytes a = %q not "hello"`, string(data)) |
Rob Pike | 1ca1e1b | 2009-07-02 11:21:01 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | // string |
| 532 | { |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 533 | var data string |
Rob Pike | ce5bbfd | 2014-06-30 15:47:11 -0700 | [diff] [blame] | 534 | instr := &decInstr{decString, 6, nil, ovfl} |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 535 | state := newDecodeStateFromData(bytesResult) |
Daniel Martí | 516e6f6 | 2017-04-25 10:58:12 +0100 | [diff] [blame] | 536 | execDec(instr, state, t, reflect.ValueOf(&data)) |
Rob Pike | 3050a0a | 2014-06-30 11:06:47 -0700 | [diff] [blame] | 537 | if data != "hello" { |
| 538 | t.Errorf(`bytes a = %q not "hello"`, data) |
Rob Pike | 1ca1e1b | 2009-07-02 11:21:01 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
Rob Pike | b9689433 | 2009-06-30 17:59:41 -0700 | [diff] [blame] | 541 | } |
Rob Pike | c701af8 | 2009-07-01 18:25:13 -0700 | [diff] [blame] | 542 | |
Rob Pike | 265674f | 2009-07-02 18:02:42 -0700 | [diff] [blame] | 543 | func TestEndToEnd(t *testing.T) { |
Rob Pike | 77baac1 | 2009-07-02 13:43:47 -0700 | [diff] [blame] | 544 | type T2 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 545 | T string |
Rob Pike | 77baac1 | 2009-07-02 13:43:47 -0700 | [diff] [blame] | 546 | } |
Filip Gruszczyński | 0b9607d | 2017-03-15 20:11:30 -0700 | [diff] [blame] | 547 | type T3 struct { |
| 548 | X float64 |
| 549 | Z *int |
| 550 | } |
Rob Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 551 | type T1 struct { |
Rob Pike | d11c0f1 | 2011-07-21 10:27:11 +1000 | [diff] [blame] | 552 | A, B, C int |
| 553 | M map[string]*float64 |
Filip Gruszczyński | 0b9607d | 2017-03-15 20:11:30 -0700 | [diff] [blame] | 554 | M2 map[int]T3 |
Filip Gruszczyński | 11ab865 | 2017-03-30 19:09:56 -0700 | [diff] [blame] | 555 | Mstring map[string]string |
| 556 | Mintptr map[int]*int |
| 557 | Mcomp map[complex128]complex128 |
| 558 | Marr map[[2]string][2]*float64 |
Rob Pike | d11c0f1 | 2011-07-21 10:27:11 +1000 | [diff] [blame] | 559 | 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 Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 567 | } |
Rob Pike | 7861da7 | 2010-05-05 16:46:39 -0700 | [diff] [blame] | 568 | pi := 3.14159 |
| 569 | e := 2.71828 |
Filip Gruszczyński | 11ab865 | 2017-03-30 19:09:56 -0700 | [diff] [blame] | 570 | two := 2.0 |
Filip Gruszczyński | 0b9607d | 2017-03-15 20:11:30 -0700 | [diff] [blame] | 571 | meaning := 42 |
| 572 | fingers := 5 |
Filip Gruszczyński | 11ab865 | 2017-03-30 19:09:56 -0700 | [diff] [blame] | 573 | 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 Pike | 1ca1e1b | 2009-07-02 11:21:01 -0700 | [diff] [blame] | 589 | t1 := &T1{ |
Rob Pike | d11c0f1 | 2011-07-21 10:27:11 +1000 | [diff] [blame] | 590 | A: 17, |
| 591 | B: 18, |
| 592 | C: -5, |
| 593 | M: map[string]*float64{"pi": &pi, "e": &e}, |
Filip Gruszczyński | 0b9607d | 2017-03-15 20:11:30 -0700 | [diff] [blame] | 594 | M2: map[int]T3{4: T3{X: pi, Z: &meaning}, 10: T3{X: e, Z: &fingers}}, |
Filip Gruszczyński | 11ab865 | 2017-03-30 19:09:56 -0700 | [diff] [blame] | 595 | 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 Pike | d11c0f1 | 2011-07-21 10:27:11 +1000 | [diff] [blame] | 599 | 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 Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 607 | } |
| 608 | b := new(bytes.Buffer) |
| 609 | err := NewEncoder(b).Encode(t1) |
Rob Pike | 30b1b9a | 2009-11-16 23:32:30 -0800 | [diff] [blame] | 610 | if err != nil { |
| 611 | t.Error("encode:", err) |
| 612 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 613 | var _t1 T1 |
| 614 | err = NewDecoder(b).Decode(&_t1) |
Rob Pike | 30b1b9a | 2009-11-16 23:32:30 -0800 | [diff] [blame] | 615 | if err != nil { |
| 616 | t.Fatal("decode:", err) |
| 617 | } |
Rob Pike | c701af8 | 2009-07-01 18:25:13 -0700 | [diff] [blame] | 618 | if !reflect.DeepEqual(t1, &_t1) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 619 | t.Errorf("encode expected %v got %v", *t1, _t1) |
Rob Pike | c701af8 | 2009-07-01 18:25:13 -0700 | [diff] [blame] | 620 | } |
Rob Pike | d11c0f1 | 2011-07-21 10:27:11 +1000 | [diff] [blame] | 621 | // 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 Pike | c701af8 | 2009-07-01 18:25:13 -0700 | [diff] [blame] | 628 | } |
Rob Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 629 | |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 630 | func TestOverflow(t *testing.T) { |
| 631 | type inputT struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 632 | Maxi int64 |
| 633 | Mini int64 |
| 634 | Maxu uint64 |
| 635 | Maxf float64 |
| 636 | Minf float64 |
| 637 | Maxc complex128 |
| 638 | Minc complex128 |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 639 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 640 | var it inputT |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 641 | var err error |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 642 | b := new(bytes.Buffer) |
| 643 | enc := NewEncoder(b) |
| 644 | dec := NewDecoder(b) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 645 | |
| 646 | // int8 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 647 | b.Reset() |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 648 | it = inputT{ |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 649 | Maxi: math.MaxInt8 + 1, |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 650 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 651 | type outi8 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 652 | Maxi int8 |
| 653 | Mini int8 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 654 | } |
| 655 | var o1 outi8 |
| 656 | enc.Encode(it) |
| 657 | err = dec.Decode(&o1) |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 658 | if err == nil || err.Error() != `value for "Maxi" out of range` { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 659 | t.Error("wrong overflow error for int8:", err) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 660 | } |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 661 | it = inputT{ |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 662 | Mini: math.MinInt8 - 1, |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 663 | } |
| 664 | b.Reset() |
| 665 | enc.Encode(it) |
| 666 | err = dec.Decode(&o1) |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 667 | if err == nil || err.Error() != `value for "Mini" out of range` { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 668 | t.Error("wrong underflow error for int8:", err) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | // int16 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 672 | b.Reset() |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 673 | it = inputT{ |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 674 | Maxi: math.MaxInt16 + 1, |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 675 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 676 | type outi16 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 677 | Maxi int16 |
| 678 | Mini int16 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 679 | } |
| 680 | var o2 outi16 |
| 681 | enc.Encode(it) |
| 682 | err = dec.Decode(&o2) |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 683 | if err == nil || err.Error() != `value for "Maxi" out of range` { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 684 | t.Error("wrong overflow error for int16:", err) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 685 | } |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 686 | it = inputT{ |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 687 | Mini: math.MinInt16 - 1, |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 688 | } |
| 689 | b.Reset() |
| 690 | enc.Encode(it) |
| 691 | err = dec.Decode(&o2) |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 692 | if err == nil || err.Error() != `value for "Mini" out of range` { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 693 | t.Error("wrong underflow error for int16:", err) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | // int32 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 697 | b.Reset() |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 698 | it = inputT{ |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 699 | Maxi: math.MaxInt32 + 1, |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 700 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 701 | type outi32 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 702 | Maxi int32 |
| 703 | Mini int32 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 704 | } |
| 705 | var o3 outi32 |
| 706 | enc.Encode(it) |
| 707 | err = dec.Decode(&o3) |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 708 | if err == nil || err.Error() != `value for "Maxi" out of range` { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 709 | t.Error("wrong overflow error for int32:", err) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 710 | } |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 711 | it = inputT{ |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 712 | Mini: math.MinInt32 - 1, |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 713 | } |
| 714 | b.Reset() |
| 715 | enc.Encode(it) |
| 716 | err = dec.Decode(&o3) |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 717 | if err == nil || err.Error() != `value for "Mini" out of range` { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 718 | t.Error("wrong underflow error for int32:", err) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | // uint8 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 722 | b.Reset() |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 723 | it = inputT{ |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 724 | Maxu: math.MaxUint8 + 1, |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 725 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 726 | type outu8 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 727 | Maxu uint8 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 728 | } |
| 729 | var o4 outu8 |
| 730 | enc.Encode(it) |
| 731 | err = dec.Decode(&o4) |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 732 | if err == nil || err.Error() != `value for "Maxu" out of range` { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 733 | t.Error("wrong overflow error for uint8:", err) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | // uint16 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 737 | b.Reset() |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 738 | it = inputT{ |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 739 | Maxu: math.MaxUint16 + 1, |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 740 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 741 | type outu16 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 742 | Maxu uint16 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 743 | } |
| 744 | var o5 outu16 |
| 745 | enc.Encode(it) |
| 746 | err = dec.Decode(&o5) |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 747 | if err == nil || err.Error() != `value for "Maxu" out of range` { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 748 | t.Error("wrong overflow error for uint16:", err) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | // uint32 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 752 | b.Reset() |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 753 | it = inputT{ |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 754 | Maxu: math.MaxUint32 + 1, |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 755 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 756 | type outu32 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 757 | Maxu uint32 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 758 | } |
| 759 | var o6 outu32 |
| 760 | enc.Encode(it) |
| 761 | err = dec.Decode(&o6) |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 762 | if err == nil || err.Error() != `value for "Maxu" out of range` { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 763 | t.Error("wrong overflow error for uint32:", err) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // float32 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 767 | b.Reset() |
Robert Griesemer | f65e42d | 2009-11-05 14:53:42 -0800 | [diff] [blame] | 768 | it = inputT{ |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 769 | Maxf: math.MaxFloat32 * 2, |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 770 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 771 | type outf32 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 772 | Maxf float32 |
| 773 | Minf float32 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 774 | } |
| 775 | var o7 outf32 |
| 776 | enc.Encode(it) |
| 777 | err = dec.Decode(&o7) |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 778 | if err == nil || err.Error() != `value for "Maxf" out of range` { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 779 | t.Error("wrong overflow error for float32:", err) |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 780 | } |
Rob Pike | 37a6bc8 | 2010-06-24 15:07:28 -0700 | [diff] [blame] | 781 | |
| 782 | // complex64 |
| 783 | b.Reset() |
| 784 | it = inputT{ |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 785 | Maxc: complex(math.MaxFloat32*2, math.MaxFloat32*2), |
Rob Pike | 37a6bc8 | 2010-06-24 15:07:28 -0700 | [diff] [blame] | 786 | } |
| 787 | type outc64 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 788 | Maxc complex64 |
| 789 | Minc complex64 |
Rob Pike | 37a6bc8 | 2010-06-24 15:07:28 -0700 | [diff] [blame] | 790 | } |
| 791 | var o8 outc64 |
| 792 | enc.Encode(it) |
| 793 | err = dec.Decode(&o8) |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 794 | if err == nil || err.Error() != `value for "Maxc" out of range` { |
Rob Pike | 37a6bc8 | 2010-06-24 15:07:28 -0700 | [diff] [blame] | 795 | t.Error("wrong overflow error for complex64:", err) |
| 796 | } |
Rob Pike | 483e4fc | 2009-07-28 12:59:39 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Rob Pike | ec23467e | 2009-07-09 14:33:43 -0700 | [diff] [blame] | 799 | func TestNesting(t *testing.T) { |
| 800 | type RT struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 801 | A string |
| 802 | Next *RT |
Rob Pike | ec23467e | 2009-07-09 14:33:43 -0700 | [diff] [blame] | 803 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 804 | rt := new(RT) |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 805 | rt.A = "level1" |
| 806 | rt.Next = new(RT) |
| 807 | rt.Next.A = "level2" |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 808 | b := new(bytes.Buffer) |
| 809 | NewEncoder(b).Encode(rt) |
| 810 | var drt RT |
| 811 | dec := NewDecoder(b) |
| 812 | err := dec.Decode(&drt) |
Rob Pike | 30b1b9a | 2009-11-16 23:32:30 -0800 | [diff] [blame] | 813 | if err != nil { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 814 | t.Fatal("decoder error:", err) |
Rob Pike | 30b1b9a | 2009-11-16 23:32:30 -0800 | [diff] [blame] | 815 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 816 | if drt.A != rt.A { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 817 | t.Errorf("nesting: encode expected %v got %v", *rt, drt) |
Rob Pike | ec23467e | 2009-07-09 14:33:43 -0700 | [diff] [blame] | 818 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 819 | if drt.Next == nil { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 820 | t.Errorf("nesting: recursion failed") |
Rob Pike | ec23467e | 2009-07-09 14:33:43 -0700 | [diff] [blame] | 821 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 822 | if drt.Next.A != rt.Next.A { |
| 823 | t.Errorf("nesting: encode expected %v got %v", *rt.Next, *drt.Next) |
Rob Pike | ec23467e | 2009-07-09 14:33:43 -0700 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
Rob Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 827 | // These three structures have the same data with different indirections |
| 828 | type T0 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 829 | A int |
| 830 | B int |
| 831 | C int |
| 832 | D int |
Rob Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 833 | } |
| 834 | type T1 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 835 | A int |
| 836 | B *int |
| 837 | C **int |
| 838 | D ***int |
Rob Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 839 | } |
| 840 | type T2 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 841 | A ***int |
| 842 | B **int |
| 843 | C *int |
| 844 | D int |
Rob Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | func TestAutoIndirection(t *testing.T) { |
Rob Pike | 22b93df | 2009-07-02 09:22:38 -0700 | [diff] [blame] | 848 | // First transfer t1 into t0 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 849 | var t1 T1 |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 850 | 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 Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 860 | 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 Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 866 | if t0.A != 17 || t0.B != 177 || t0.C != 1777 || t0.D != 17777 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 867 | t.Errorf("t1->t0: expected {17 177 1777 17777}; got %v", t0) |
Rob Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Rob Pike | 22b93df | 2009-07-02 09:22:38 -0700 | [diff] [blame] | 870 | // Now transfer t2 into t0 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 871 | var t2 T2 |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 872 | 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 Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 882 | b.Reset() |
| 883 | enc.Encode(t2) |
| 884 | t0 = T0{} |
| 885 | dec.Decode(&t0) |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 886 | if t0.A != 17 || t0.B != 177 || t0.C != 1777 || t0.D != 17777 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 887 | t.Errorf("t2->t0 expected {17 177 1777 17777}; got %v", t0) |
Rob Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | // Now transfer t0 into t1 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 891 | t0 = T0{17, 177, 1777, 17777} |
| 892 | b.Reset() |
| 893 | enc.Encode(t0) |
| 894 | t1 = T1{} |
| 895 | dec.Decode(&t1) |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 896 | 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 Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | // Now transfer t0 into t2 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 901 | b.Reset() |
| 902 | enc.Encode(t0) |
| 903 | t2 = T2{} |
| 904 | dec.Decode(&t2) |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 905 | 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 Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | // Now do t2 again but without pre-allocated pointers. |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 910 | b.Reset() |
| 911 | enc.Encode(t0) |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 912 | ***t2.A = 0 |
| 913 | **t2.B = 0 |
| 914 | *t2.C = 0 |
| 915 | t2.D = 0 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 916 | dec.Decode(&t2) |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 917 | 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 Pike | c0271c4 | 2009-07-02 08:21:42 -0700 | [diff] [blame] | 919 | } |
| 920 | } |
Rob Pike | 1399bad | 2009-07-16 17:55:16 -0700 | [diff] [blame] | 921 | |
| 922 | type RT0 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 923 | A int |
| 924 | B string |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 925 | C float64 |
Rob Pike | 1399bad | 2009-07-16 17:55:16 -0700 | [diff] [blame] | 926 | } |
| 927 | type RT1 struct { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 928 | C float64 |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 929 | B string |
| 930 | A int |
| 931 | NotSet string |
Rob Pike | 1399bad | 2009-07-16 17:55:16 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | func TestReorderedFields(t *testing.T) { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 935 | var rt0 RT0 |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 936 | rt0.A = 17 |
| 937 | rt0.B = "hello" |
| 938 | rt0.C = 3.14159 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 939 | b := new(bytes.Buffer) |
| 940 | NewEncoder(b).Encode(rt0) |
| 941 | dec := NewDecoder(b) |
| 942 | var rt1 RT1 |
Rob Pike | 1399bad | 2009-07-16 17:55:16 -0700 | [diff] [blame] | 943 | // Wire type is RT0, local type is RT1. |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 944 | err := dec.Decode(&rt1) |
Rob Pike | 30b1b9a | 2009-11-16 23:32:30 -0800 | [diff] [blame] | 945 | if err != nil { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 946 | t.Fatal("decode error:", err) |
Rob Pike | 30b1b9a | 2009-11-16 23:32:30 -0800 | [diff] [blame] | 947 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 948 | if rt0.A != rt1.A || rt0.B != rt1.B || rt0.C != rt1.C { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 949 | t.Errorf("rt1->rt0: expected %v; got %v", rt0, rt1) |
Rob Pike | 1399bad | 2009-07-16 17:55:16 -0700 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | |
| 953 | // Like an RT0 but with fields we'll ignore on the decode side. |
| 954 | type IT0 struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 955 | A int64 |
| 956 | B string |
| 957 | Ignore_d []int |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 958 | Ignore_e [3]float64 |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 959 | Ignore_f bool |
| 960 | Ignore_g string |
| 961 | Ignore_h []byte |
| 962 | Ignore_i *RT1 |
| 963 | Ignore_m map[string]int |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 964 | C float64 |
Rob Pike | 1399bad | 2009-07-16 17:55:16 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | func TestIgnoredFields(t *testing.T) { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 968 | var it0 IT0 |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 969 | 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 Pike | 1399bad | 2009-07-16 17:55:16 -0700 | [diff] [blame] | 981 | |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 982 | b := new(bytes.Buffer) |
| 983 | NewEncoder(b).Encode(it0) |
| 984 | dec := NewDecoder(b) |
| 985 | var rt1 RT1 |
Rob Pike | 1399bad | 2009-07-16 17:55:16 -0700 | [diff] [blame] | 986 | // Wire type is IT0, local type is RT1. |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 987 | err := dec.Decode(&rt1) |
Rob Pike | cb0a02f | 2009-07-17 11:38:31 -0700 | [diff] [blame] | 988 | if err != nil { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 989 | t.Error("error: ", err) |
Rob Pike | cb0a02f | 2009-07-17 11:38:31 -0700 | [diff] [blame] | 990 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 991 | 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 Pike | 1399bad | 2009-07-16 17:55:16 -0700 | [diff] [blame] | 993 | } |
| 994 | } |
Rob Pike | 98607d0 | 2009-07-29 17:24:25 -0700 | [diff] [blame] | 995 | |
Rob Pike | c9b90c9 | 2011-02-23 09:49:35 -0800 | [diff] [blame] | 996 | func 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 VanBenschoten | b04f3b0 | 2015-12-22 02:40:47 -0500 | [diff] [blame] | 1003 | } else if !strings.Contains(err.Error(), "recursive") { |
Rob Pike | c9b90c9 | 2011-02-23 09:49:35 -0800 | [diff] [blame] | 1004 | 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 Pike | 98607d0 | 2009-07-29 17:24:25 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Rob Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1009 | type Indirect struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1010 | A ***[3]int |
| 1011 | S ***[]int |
| 1012 | M ****map[string]int |
Rob Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | type Direct struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1016 | A [3]int |
| 1017 | S []int |
| 1018 | M map[string]int |
Rob Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | func TestIndirectSliceMapArray(t *testing.T) { |
| 1022 | // Marshal indirect, unmarshal to direct. |
| 1023 | i := new(Indirect) |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1024 | 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 Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1037 | 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 Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1045 | 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 Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1047 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1048 | 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 Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1050 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1051 | 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 Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1053 | } |
| 1054 | // Marshal direct, unmarshal to indirect. |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1055 | 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 Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1058 | 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 Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1064 | t.Fatal("error: ", err) |
Rob Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1065 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1066 | 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 Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1068 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1069 | 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 Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1071 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1072 | 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 Pike | 9088f9f | 2010-05-06 10:45:18 -0700 | [diff] [blame] | 1074 | } |
| 1075 | } |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1076 | |
| 1077 | // An interface with several implementations |
| 1078 | type Squarer interface { |
| 1079 | Square() int |
| 1080 | } |
| 1081 | |
| 1082 | type Int int |
| 1083 | |
| 1084 | func (i Int) Square() int { |
| 1085 | return int(i * i) |
| 1086 | } |
| 1087 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 1088 | type Float float64 |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1089 | |
| 1090 | func (f Float) Square() int { |
| 1091 | return int(f * f) |
| 1092 | } |
| 1093 | |
| 1094 | type Vector []int |
| 1095 | |
| 1096 | func (v Vector) Square() int { |
| 1097 | sum := 0 |
| 1098 | for _, x := range v { |
| 1099 | sum += x * x |
| 1100 | } |
| 1101 | return sum |
| 1102 | } |
| 1103 | |
Rob Pike | 7f7cb16 | 2010-10-31 13:41:30 -0700 | [diff] [blame] | 1104 | type Point struct { |
Rob Pike | a0a4e85 | 2011-01-22 00:10:11 -0800 | [diff] [blame] | 1105 | X, Y int |
Rob Pike | 7f7cb16 | 2010-10-31 13:41:30 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | func (p Point) Square() int { |
Rob Pike | a0a4e85 | 2011-01-22 00:10:11 -0800 | [diff] [blame] | 1109 | return p.X*p.X + p.Y*p.Y |
Rob Pike | 7f7cb16 | 2010-10-31 13:41:30 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1112 | // A struct with interfaces in it. |
| 1113 | type InterfaceItem struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1114 | I int |
| 1115 | Sq1, Sq2, Sq3 Squarer |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 1116 | F float64 |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1117 | Sq []Squarer |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | // The same struct without interfaces |
| 1121 | type NoInterfaceItem struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1122 | I int |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 1123 | F float64 |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | func 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 Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1148 | if item2.I != item1.I { |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1149 | t.Error("normal int did not decode correctly") |
| 1150 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1151 | if item2.Sq1 == nil || item2.Sq1.Square() != iVal.Square() { |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1152 | t.Error("Int did not decode correctly") |
| 1153 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1154 | if item2.Sq2 == nil || item2.Sq2.Square() != fVal.Square() { |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1155 | t.Error("Float did not decode correctly") |
| 1156 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1157 | if item2.Sq3 == nil || item2.Sq3.Square() != vVal.Square() { |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1158 | t.Error("Vector did not decode correctly") |
| 1159 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1160 | if item2.F != item1.F { |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1161 | 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 Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1164 | if len(item1.Sq) != len(item2.Sq) { |
| 1165 | t.Fatalf("[]Squarer length wrong: got %d; expected %d", len(item2.Sq), len(item1.Sq)) |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1166 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1167 | for i, v1 := range item1.Sq { |
| 1168 | v2 := item2.Sq[i] |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1169 | if v1 == nil || v2 == nil { |
| 1170 | if v1 != nil || v2 != nil { |
| 1171 | t.Errorf("item %d inconsistent nils", i) |
| 1172 | } |
Rob Pike | f29b091 | 2013-03-22 14:22:55 -0700 | [diff] [blame] | 1173 | } else if v1.Square() != v2.Square() { |
| 1174 | t.Errorf("item %d inconsistent values: %v %v", i, v1, v2) |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1175 | } |
| 1176 | } |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Rob Pike | f437d4d | 2010-10-26 17:07:39 -0700 | [diff] [blame] | 1179 | // A struct with all basic types, stored in interfaces. |
| 1180 | type BasicInterfaceItem struct { |
| 1181 | Int, Int8, Int16, Int32, Int64 interface{} |
| 1182 | Uint, Uint8, Uint16, Uint32, Uint64 interface{} |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 1183 | Float32, Float64 interface{} |
| 1184 | Complex64, Complex128 interface{} |
Rob Pike | f437d4d | 2010-10-26 17:07:39 -0700 | [diff] [blame] | 1185 | Bool interface{} |
| 1186 | String interface{} |
| 1187 | Bytes interface{} |
| 1188 | } |
| 1189 | |
| 1190 | func 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 Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 1195 | float32(1), 1.0, |
Rob Pike | 9b82481 | 2011-01-28 10:53:42 -0800 | [diff] [blame] | 1196 | complex64(1i), complex128(1i), |
Rob Pike | f437d4d | 2010-10-26 17:07:39 -0700 | [diff] [blame] | 1197 | true, |
| 1198 | "hello", |
| 1199 | []byte("sailor"), |
| 1200 | } |
Rob Pike | f437d4d | 2010-10-26 17:07:39 -0700 | [diff] [blame] | 1201 | 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 Pike | 9f7f69d | 2010-11-05 10:36:27 -0700 | [diff] [blame] | 1223 | type String string |
| 1224 | |
| 1225 | type PtrInterfaceItem struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1226 | Str1 interface{} // basic |
| 1227 | Str2 interface{} // derived |
Rob Pike | 9f7f69d | 2010-11-05 10:36:27 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | // We'll send pointers; should receive values. |
| 1231 | // Also check that we can register T but send *T. |
| 1232 | func 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 Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1253 | if v, ok := item2.Str1.(string); !ok || v != str1 { |
Rob Pike | 9f7f69d | 2010-11-05 10:36:27 -0700 | [diff] [blame] | 1254 | t.Errorf("basic string failed: %q should be %q", v, str1) |
| 1255 | } |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1256 | if v, ok := item2.Str2.(String); !ok || v != str2 { |
Rob Pike | 9f7f69d | 2010-11-05 10:36:27 -0700 | [diff] [blame] | 1257 | t.Errorf("derived type String failed: %q should be %q", v, str2) |
| 1258 | } |
| 1259 | } |
| 1260 | |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1261 | func TestIgnoreInterface(t *testing.T) { |
| 1262 | iVal := Int(3) |
| 1263 | fVal := Float(5) |
Rob Pike | 7f7cb16 | 2010-10-31 13:41:30 -0700 | [diff] [blame] | 1264 | // Sending a Point will require that the receiver define a type in the middle of |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1265 | // receiving the value for item2. |
Rob Pike | 7f7cb16 | 2010-10-31 13:41:30 -0700 | [diff] [blame] | 1266 | pVal := Point{2, 3} |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1267 | b := new(bytes.Buffer) |
Rob Pike | 7f7cb16 | 2010-10-31 13:41:30 -0700 | [diff] [blame] | 1268 | item1 := &InterfaceItem{1, iVal, fVal, pVal, 11.5, nil} |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1269 | // Register the types. |
| 1270 | Register(Int(0)) |
| 1271 | Register(Float(0)) |
Rob Pike | 7f7cb16 | 2010-10-31 13:41:30 -0700 | [diff] [blame] | 1272 | Register(Point{}) |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1273 | 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 Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1283 | if item2.I != item1.I { |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1284 | t.Error("normal int did not decode correctly") |
| 1285 | } |
Alberto Donizetti | a5c5f6e | 2016-05-06 18:16:52 +0200 | [diff] [blame] | 1286 | if item2.F != item1.F { |
Rob Pike | 5d90646 | 2010-10-22 11:17:40 -0700 | [diff] [blame] | 1287 | t.Error("normal float did not decode correctly") |
| 1288 | } |
| 1289 | } |
Rob Pike | 96b9efe | 2010-10-29 15:07:56 -0700 | [diff] [blame] | 1290 | |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1291 | type U struct { |
| 1292 | A int |
| 1293 | B string |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 1294 | c float64 |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1295 | D uint |
| 1296 | } |
| 1297 | |
| 1298 | func 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 Donizetti | a5c5f6e | 2016-05-06 18:16:52 +0200 | [diff] [blame] | 1313 | if u0.A != u1.A || u0.B != u1.B || u0.D != u1.D { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1314 | 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 Pike | 5b5a674 | 2011-01-21 11:28:53 -0800 | [diff] [blame] | 1321 | var 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 | |
| 1331 | func 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 Pike | 96b9efe | 2010-10-29 15:07:56 -0700 | [diff] [blame] | 1346 | // A type that won't be defined in the gob until we send it in an interface value. |
| 1347 | type OnTheFly struct { |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1348 | A int |
Rob Pike | 96b9efe | 2010-10-29 15:07:56 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | type DT struct { |
| 1352 | // X OnTheFly |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1353 | A int |
| 1354 | B string |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 1355 | C float64 |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1356 | I interface{} |
| 1357 | J interface{} |
| 1358 | I_nil interface{} |
| 1359 | M map[string]int |
| 1360 | T [3]int |
| 1361 | S []string |
Rob Pike | 96b9efe | 2010-10-29 15:07:56 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Rémy Oudompheng | 7a73f32 | 2014-01-30 07:54:57 +0100 | [diff] [blame] | 1364 | func newDT() DT { |
Rob Pike | 96b9efe | 2010-10-29 15:07:56 -0700 | [diff] [blame] | 1365 | var dt DT |
Rob Pike | 3036604 | 2011-01-11 13:44:00 -0800 | [diff] [blame] | 1366 | 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 Oudompheng | 7a73f32 | 2014-01-30 07:54:57 +0100 | [diff] [blame] | 1375 | return dt |
| 1376 | } |
| 1377 | |
| 1378 | func TestDebugStruct(t *testing.T) { |
| 1379 | if debugFunc == nil { |
| 1380 | return |
| 1381 | } |
| 1382 | Register(OnTheFly{}) |
| 1383 | dt := newDT() |
Rob Pike | 96b9efe | 2010-10-29 15:07:56 -0700 | [diff] [blame] | 1384 | 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 Symonds | 9440d82 | 2012-02-06 14:02:12 +1100 | [diff] [blame] | 1397 | |
| 1398 | func 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. |
| 1421 | func TestFuzz(t *testing.T) { |
Rob Pike | bbe6017 | 2012-07-13 14:23:51 -0700 | [diff] [blame] | 1422 | if !*doFuzzTests { |
| 1423 | t.Logf("disabled; run with -gob.fuzz to enable") |
David Symonds | 9440d82 | 2012-02-06 14:02:12 +1100 | [diff] [blame] | 1424 | 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 | |
| 1441 | func TestFuzzRegressions(t *testing.T) { |
Rob Pike | bbe6017 | 2012-07-13 14:23:51 -0700 | [diff] [blame] | 1442 | if !*doFuzzTests { |
| 1443 | t.Logf("disabled; run with -gob.fuzz to enable") |
| 1444 | return |
| 1445 | } |
| 1446 | |
David Symonds | 9440d82 | 2012-02-06 14:02:12 +1100 | [diff] [blame] | 1447 | // An instance triggering a type name of length ~102 GB. |
| 1448 | testFuzz(t, 1328492090837718000, 100, new(float32)) |
David Symonds | 1f0f459 | 2012-03-01 15:57:54 +1100 | [diff] [blame] | 1449 | // An instance triggering a type name of 1.6 GB. |
Rob Pike | bbe6017 | 2012-07-13 14:23:51 -0700 | [diff] [blame] | 1450 | // Note: can take several minutes to run. |
| 1451 | testFuzz(t, 1330522872628565000, 100, new(int)) |
David Symonds | 9440d82 | 2012-02-06 14:02:12 +1100 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | func testFuzz(t *testing.T, seed int64, n int, input ...interface{}) { |
David Symonds | 9440d82 | 2012-02-06 14:02:12 +1100 | [diff] [blame] | 1455 | for _, e := range input { |
David Symonds | 1f0f459 | 2012-03-01 15:57:54 +1100 | [diff] [blame] | 1456 | t.Logf("seed=%d n=%d e=%T", seed, n, e) |
David Symonds | 9440d82 | 2012-02-06 14:02:12 +1100 | [diff] [blame] | 1457 | rng := rand.New(rand.NewSource(seed)) |
| 1458 | for i := 0; i < n; i++ { |
| 1459 | encFuzzDec(rng, e) |
| 1460 | } |
| 1461 | } |
| 1462 | } |
Rémy Oudompheng | 7a73f32 | 2014-01-30 07:54:57 +0100 | [diff] [blame] | 1463 | |
| 1464 | // TestFuzzOneByte tries to decode corrupted input sequences |
| 1465 | // and checks that no panic occurs. |
| 1466 | func 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 Haugen | 8e6cf5f | 2015-01-28 23:07:05 +0100 | [diff] [blame] | 1504 | |
| 1505 | // Don't crash, just give error with invalid type id. |
| 1506 | // Issue 9649. |
| 1507 | func 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 Pike | f62b749 | 2015-08-11 15:29:40 +1000 | [diff] [blame] | 1519 | t.Fatalf("decode: expected %s, got %s", errBadType, err) |
Håvard Haugen | 8e6cf5f | 2015-01-28 23:07:05 +0100 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | } |