1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gob 6 7 import ( 8 "bytes" 9 "errors" 10 "flag" 11 "math" 12 "math/rand" 13 "reflect" 14 "strings" 15 "testing" 16 "time" 17 ) 18 19 var doFuzzTests = flag.Bool("gob.fuzz", false, "run the fuzz tests, which are large and very slow") 20 21 // Guarantee encoding format by comparing some encodings to hand-written values 22 type EncodeT struct { 23 x uint64 24 b []byte 25 } 26 27 var encodeT = []EncodeT{ 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}}, 42 } 43 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 { 48 t.Error(e.(gobError).err) // Will re-panic if not one of our errors, such as a runtime error. 49 } 50 return 51 } 52 53 func newDecBuffer(data []byte) *decBuffer { 54 return &decBuffer{ 55 data: data, 56 } 57 } 58 59 // Test basic encode/decode routines for unsigned integers 60 func TestUintCodec(t *testing.T) { 61 defer testError(t) 62 b := new(encBuffer) 63 encState := newEncoderState(b) 64 for _, tt := range encodeT { 65 b.Reset() 66 encState.encodeUint(tt.x) 67 if !bytes.Equal(tt.b, b.Bytes()) { 68 t.Errorf("encodeUint: %#x encode: expected % x got % x", tt.x, tt.b, b.Bytes()) 69 } 70 } 71 for u := uint64(0); ; u = (u + 1) * 7 { 72 b.Reset() 73 encState.encodeUint(u) 74 decState := newDecodeState(newDecBuffer(b.Bytes())) 75 v := decState.decodeUint() 76 if u != v { 77 t.Errorf("Encode/Decode: sent %#x received %#x", u, v) 78 } 79 if u&(1<<63) != 0 { 80 break 81 } 82 } 83 } 84 85 func verifyInt(i int64, t *testing.T) { 86 defer testError(t) 87 var b = new(encBuffer) 88 encState := newEncoderState(b) 89 encState.encodeInt(i) 90 decState := newDecodeState(newDecBuffer(b.Bytes())) 91 j := decState.decodeInt() 92 if i != j { 93 t.Errorf("Encode/Decode: sent %#x received %#x", uint64(i), uint64(j)) 94 } 95 } 96 97 // Test basic encode/decode routines for signed integers 98 func TestIntCodec(t *testing.T) { 99 for u := uint64(0); ; u = (u + 1) * 7 { 100 // Do positive and negative values 101 i := int64(u) 102 verifyInt(i, t) 103 verifyInt(-i, t) 104 verifyInt(^i, t) 105 if u&(1<<63) != 0 { 106 break 107 } 108 } 109 verifyInt(-1<<63, t) // a tricky case 110 } 111 112 // The result of encoding a true boolean with field number 7 113 var boolResult = []byte{0x07, 0x01} 114 115 // The result of encoding a number 17 with field number 7 116 var signedResult = []byte{0x07, 2 * 17} 117 var unsignedResult = []byte{0x07, 17} 118 var floatResult = []byte{0x07, 0xFE, 0x31, 0x40} 119 120 // The result of encoding a number 17+19i with field number 7 121 var complexResult = []byte{0x07, 0xFE, 0x31, 0x40, 0xFE, 0x33, 0x40} 122 123 // The result of encoding "hello" with field number 7 124 var bytesResult = []byte{0x07, 0x05, 'h', 'e', 'l', 'l', 'o'} 125 126 func newDecodeState(buf *decBuffer) *decoderState { 127 d := new(decoderState) 128 d.b = buf 129 return d 130 } 131 132 func newEncoderState(b *encBuffer) *encoderState { 133 b.Reset() 134 state := &encoderState{enc: nil, b: b} 135 state.fieldnum = -1 136 return state 137 } 138 139 // Test instruction execution for encoding. 140 // Do not run the machine yet; instead do individual instructions crafted by hand. 141 func TestScalarEncInstructions(t *testing.T) { 142 var b = new(encBuffer) 143 144 // bool 145 { 146 var data bool = true 147 instr := &encInstr{encBool, 6, nil, 0} 148 state := newEncoderState(b) 149 instr.op(instr, state, reflect.ValueOf(data)) 150 if !bytes.Equal(boolResult, b.Bytes()) { 151 t.Errorf("bool enc instructions: expected % x got % x", boolResult, b.Bytes()) 152 } 153 } 154 155 // int 156 { 157 b.Reset() 158 var data int = 17 159 instr := &encInstr{encInt, 6, nil, 0} 160 state := newEncoderState(b) 161 instr.op(instr, state, reflect.ValueOf(data)) 162 if !bytes.Equal(signedResult, b.Bytes()) { 163 t.Errorf("int enc instructions: expected % x got % x", signedResult, b.Bytes()) 164 } 165 } 166 167 // uint 168 { 169 b.Reset() 170 var data uint = 17 171 instr := &encInstr{encUint, 6, nil, 0} 172 state := newEncoderState(b) 173 instr.op(instr, state, reflect.ValueOf(data)) 174 if !bytes.Equal(unsignedResult, b.Bytes()) { 175 t.Errorf("uint enc instructions: expected % x got % x", unsignedResult, b.Bytes()) 176 } 177 } 178 179 // int8 180 { 181 b.Reset() 182 var data int8 = 17 183 instr := &encInstr{encInt, 6, nil, 0} 184 state := newEncoderState(b) 185 instr.op(instr, state, reflect.ValueOf(data)) 186 if !bytes.Equal(signedResult, b.Bytes()) { 187 t.Errorf("int8 enc instructions: expected % x got % x", signedResult, b.Bytes()) 188 } 189 } 190 191 // uint8 192 { 193 b.Reset() 194 var data uint8 = 17 195 instr := &encInstr{encUint, 6, nil, 0} 196 state := newEncoderState(b) 197 instr.op(instr, state, reflect.ValueOf(data)) 198 if !bytes.Equal(unsignedResult, b.Bytes()) { 199 t.Errorf("uint8 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) 200 } 201 } 202 203 // int16 204 { 205 b.Reset() 206 var data int16 = 17 207 instr := &encInstr{encInt, 6, nil, 0} 208 state := newEncoderState(b) 209 instr.op(instr, state, reflect.ValueOf(data)) 210 if !bytes.Equal(signedResult, b.Bytes()) { 211 t.Errorf("int16 enc instructions: expected % x got % x", signedResult, b.Bytes()) 212 } 213 } 214 215 // uint16 216 { 217 b.Reset() 218 var data uint16 = 17 219 instr := &encInstr{encUint, 6, nil, 0} 220 state := newEncoderState(b) 221 instr.op(instr, state, reflect.ValueOf(data)) 222 if !bytes.Equal(unsignedResult, b.Bytes()) { 223 t.Errorf("uint16 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) 224 } 225 } 226 227 // int32 228 { 229 b.Reset() 230 var data int32 = 17 231 instr := &encInstr{encInt, 6, nil, 0} 232 state := newEncoderState(b) 233 instr.op(instr, state, reflect.ValueOf(data)) 234 if !bytes.Equal(signedResult, b.Bytes()) { 235 t.Errorf("int32 enc instructions: expected % x got % x", signedResult, b.Bytes()) 236 } 237 } 238 239 // uint32 240 { 241 b.Reset() 242 var data uint32 = 17 243 instr := &encInstr{encUint, 6, nil, 0} 244 state := newEncoderState(b) 245 instr.op(instr, state, reflect.ValueOf(data)) 246 if !bytes.Equal(unsignedResult, b.Bytes()) { 247 t.Errorf("uint32 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) 248 } 249 } 250 251 // int64 252 { 253 b.Reset() 254 var data int64 = 17 255 instr := &encInstr{encInt, 6, nil, 0} 256 state := newEncoderState(b) 257 instr.op(instr, state, reflect.ValueOf(data)) 258 if !bytes.Equal(signedResult, b.Bytes()) { 259 t.Errorf("int64 enc instructions: expected % x got % x", signedResult, b.Bytes()) 260 } 261 } 262 263 // uint64 264 { 265 b.Reset() 266 var data uint64 = 17 267 instr := &encInstr{encUint, 6, nil, 0} 268 state := newEncoderState(b) 269 instr.op(instr, state, reflect.ValueOf(data)) 270 if !bytes.Equal(unsignedResult, b.Bytes()) { 271 t.Errorf("uint64 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) 272 } 273 } 274 275 // float32 276 { 277 b.Reset() 278 var data float32 = 17 279 instr := &encInstr{encFloat, 6, nil, 0} 280 state := newEncoderState(b) 281 instr.op(instr, state, reflect.ValueOf(data)) 282 if !bytes.Equal(floatResult, b.Bytes()) { 283 t.Errorf("float32 enc instructions: expected % x got % x", floatResult, b.Bytes()) 284 } 285 } 286 287 // float64 288 { 289 b.Reset() 290 var data float64 = 17 291 instr := &encInstr{encFloat, 6, nil, 0} 292 state := newEncoderState(b) 293 instr.op(instr, state, reflect.ValueOf(data)) 294 if !bytes.Equal(floatResult, b.Bytes()) { 295 t.Errorf("float64 enc instructions: expected % x got % x", floatResult, b.Bytes()) 296 } 297 } 298 299 // bytes == []uint8 300 { 301 b.Reset() 302 data := []byte("hello") 303 instr := &encInstr{encUint8Array, 6, nil, 0} 304 state := newEncoderState(b) 305 instr.op(instr, state, reflect.ValueOf(data)) 306 if !bytes.Equal(bytesResult, b.Bytes()) { 307 t.Errorf("bytes enc instructions: expected % x got % x", bytesResult, b.Bytes()) 308 } 309 } 310 311 // string 312 { 313 b.Reset() 314 var data string = "hello" 315 instr := &encInstr{encString, 6, nil, 0} 316 state := newEncoderState(b) 317 instr.op(instr, state, reflect.ValueOf(data)) 318 if !bytes.Equal(bytesResult, b.Bytes()) { 319 t.Errorf("string enc instructions: expected % x got % x", bytesResult, b.Bytes()) 320 } 321 } 322 } 323 324 func execDec(typ string, instr *decInstr, state *decoderState, t *testing.T, value reflect.Value) { 325 defer testError(t) 326 v := int(state.decodeUint()) 327 if v+state.fieldnum != 6 { 328 t.Fatalf("decoding field number %d, got %d", 6, v+state.fieldnum) 329 } 330 instr.op(instr, state, value.Elem()) 331 state.fieldnum = 6 332 } 333 334 func newDecodeStateFromData(data []byte) *decoderState { 335 b := newDecBuffer(data) 336 state := newDecodeState(b) 337 state.fieldnum = -1 338 return state 339 } 340 341 // Test instruction execution for decoding. 342 // Do not run the machine yet; instead do individual instructions crafted by hand. 343 func TestScalarDecInstructions(t *testing.T) { 344 ovfl := errors.New("overflow") 345 346 // bool 347 { 348 var data bool 349 instr := &decInstr{decBool, 6, nil, ovfl} 350 state := newDecodeStateFromData(boolResult) 351 execDec("bool", instr, state, t, reflect.ValueOf(&data)) 352 if data != true { 353 t.Errorf("bool a = %v not true", data) 354 } 355 } 356 // int 357 { 358 var data int 359 instr := &decInstr{decOpTable[reflect.Int], 6, nil, ovfl} 360 state := newDecodeStateFromData(signedResult) 361 execDec("int", instr, state, t, reflect.ValueOf(&data)) 362 if data != 17 { 363 t.Errorf("int a = %v not 17", data) 364 } 365 } 366 367 // uint 368 { 369 var data uint 370 instr := &decInstr{decOpTable[reflect.Uint], 6, nil, ovfl} 371 state := newDecodeStateFromData(unsignedResult) 372 execDec("uint", instr, state, t, reflect.ValueOf(&data)) 373 if data != 17 { 374 t.Errorf("uint a = %v not 17", data) 375 } 376 } 377 378 // int8 379 { 380 var data int8 381 instr := &decInstr{decInt8, 6, nil, ovfl} 382 state := newDecodeStateFromData(signedResult) 383 execDec("int8", instr, state, t, reflect.ValueOf(&data)) 384 if data != 17 { 385 t.Errorf("int8 a = %v not 17", data) 386 } 387 } 388 389 // uint8 390 { 391 var data uint8 392 instr := &decInstr{decUint8, 6, nil, ovfl} 393 state := newDecodeStateFromData(unsignedResult) 394 execDec("uint8", instr, state, t, reflect.ValueOf(&data)) 395 if data != 17 { 396 t.Errorf("uint8 a = %v not 17", data) 397 } 398 } 399 400 // int16 401 { 402 var data int16 403 instr := &decInstr{decInt16, 6, nil, ovfl} 404 state := newDecodeStateFromData(signedResult) 405 execDec("int16", instr, state, t, reflect.ValueOf(&data)) 406 if data != 17 { 407 t.Errorf("int16 a = %v not 17", data) 408 } 409 } 410 411 // uint16 412 { 413 var data uint16 414 instr := &decInstr{decUint16, 6, nil, ovfl} 415 state := newDecodeStateFromData(unsignedResult) 416 execDec("uint16", instr, state, t, reflect.ValueOf(&data)) 417 if data != 17 { 418 t.Errorf("uint16 a = %v not 17", data) 419 } 420 } 421 422 // int32 423 { 424 var data int32 425 instr := &decInstr{decInt32, 6, nil, ovfl} 426 state := newDecodeStateFromData(signedResult) 427 execDec("int32", instr, state, t, reflect.ValueOf(&data)) 428 if data != 17 { 429 t.Errorf("int32 a = %v not 17", data) 430 } 431 } 432 433 // uint32 434 { 435 var data uint32 436 instr := &decInstr{decUint32, 6, nil, ovfl} 437 state := newDecodeStateFromData(unsignedResult) 438 execDec("uint32", instr, state, t, reflect.ValueOf(&data)) 439 if data != 17 { 440 t.Errorf("uint32 a = %v not 17", data) 441 } 442 } 443 444 // uintptr 445 { 446 var data uintptr 447 instr := &decInstr{decOpTable[reflect.Uintptr], 6, nil, ovfl} 448 state := newDecodeStateFromData(unsignedResult) 449 execDec("uintptr", instr, state, t, reflect.ValueOf(&data)) 450 if data != 17 { 451 t.Errorf("uintptr a = %v not 17", data) 452 } 453 } 454 455 // int64 456 { 457 var data int64 458 instr := &decInstr{decInt64, 6, nil, ovfl} 459 state := newDecodeStateFromData(signedResult) 460 execDec("int64", instr, state, t, reflect.ValueOf(&data)) 461 if data != 17 { 462 t.Errorf("int64 a = %v not 17", data) 463 } 464 } 465 466 // uint64 467 { 468 var data uint64 469 instr := &decInstr{decUint64, 6, nil, ovfl} 470 state := newDecodeStateFromData(unsignedResult) 471 execDec("uint64", instr, state, t, reflect.ValueOf(&data)) 472 if data != 17 { 473 t.Errorf("uint64 a = %v not 17", data) 474 } 475 } 476 477 // float32 478 { 479 var data float32 480 instr := &decInstr{decFloat32, 6, nil, ovfl} 481 state := newDecodeStateFromData(floatResult) 482 execDec("float32", instr, state, t, reflect.ValueOf(&data)) 483 if data != 17 { 484 t.Errorf("float32 a = %v not 17", data) 485 } 486 } 487 488 // float64 489 { 490 var data float64 491 instr := &decInstr{decFloat64, 6, nil, ovfl} 492 state := newDecodeStateFromData(floatResult) 493 execDec("float64", instr, state, t, reflect.ValueOf(&data)) 494 if data != 17 { 495 t.Errorf("float64 a = %v not 17", data) 496 } 497 } 498 499 // complex64 500 { 501 var data complex64 502 instr := &decInstr{decOpTable[reflect.Complex64], 6, nil, ovfl} 503 state := newDecodeStateFromData(complexResult) 504 execDec("complex", instr, state, t, reflect.ValueOf(&data)) 505 if data != 17+19i { 506 t.Errorf("complex a = %v not 17+19i", data) 507 } 508 } 509 510 // complex128 511 { 512 var data complex128 513 instr := &decInstr{decOpTable[reflect.Complex128], 6, nil, ovfl} 514 state := newDecodeStateFromData(complexResult) 515 execDec("complex", instr, state, t, reflect.ValueOf(&data)) 516 if data != 17+19i { 517 t.Errorf("complex a = %v not 17+19i", data) 518 } 519 } 520 521 // bytes == []uint8 522 { 523 var data []byte 524 instr := &decInstr{decUint8Slice, 6, nil, ovfl} 525 state := newDecodeStateFromData(bytesResult) 526 execDec("bytes", instr, state, t, reflect.ValueOf(&data)) 527 if string(data) != "hello" { 528 t.Errorf(`bytes a = %q not "hello"`, string(data)) 529 } 530 } 531 532 // string 533 { 534 var data string 535 instr := &decInstr{decString, 6, nil, ovfl} 536 state := newDecodeStateFromData(bytesResult) 537 execDec("bytes", instr, state, t, reflect.ValueOf(&data)) 538 if data != "hello" { 539 t.Errorf(`bytes a = %q not "hello"`, data) 540 } 541 } 542 } 543 544 func TestEndToEnd(t *testing.T) { 545 type T2 struct { 546 T string 547 } 548 s1 := "string1" 549 s2 := "string2" 550 type T1 struct { 551 A, B, C int 552 M map[string]*float64 553 EmptyMap map[string]int // to check that we receive a non-nil map. 554 N *[3]float64 555 Strs *[2]string 556 Int64s *[]int64 557 RI complex64 558 S string 559 Y []byte 560 T *T2 561 } 562 pi := 3.14159 563 e := 2.71828 564 t1 := &T1{ 565 A: 17, 566 B: 18, 567 C: -5, 568 M: map[string]*float64{"pi": &pi, "e": &e}, 569 EmptyMap: make(map[string]int), 570 N: &[3]float64{1.5, 2.5, 3.5}, 571 Strs: &[2]string{s1, s2}, 572 Int64s: &[]int64{77, 89, 123412342134}, 573 RI: 17 - 23i, 574 S: "Now is the time", 575 Y: []byte("hello, sailor"), 576 T: &T2{"this is T2"}, 577 } 578 b := new(bytes.Buffer) 579 err := NewEncoder(b).Encode(t1) 580 if err != nil { 581 t.Error("encode:", err) 582 } 583 var _t1 T1 584 err = NewDecoder(b).Decode(&_t1) 585 if err != nil { 586 t.Fatal("decode:", err) 587 } 588 if !reflect.DeepEqual(t1, &_t1) { 589 t.Errorf("encode expected %v got %v", *t1, _t1) 590 } 591 // Be absolutely sure the received map is non-nil. 592 if t1.EmptyMap == nil { 593 t.Errorf("nil map sent") 594 } 595 if _t1.EmptyMap == nil { 596 t.Errorf("nil map received") 597 } 598 } 599 600 func TestOverflow(t *testing.T) { 601 type inputT struct { 602 Maxi int64 603 Mini int64 604 Maxu uint64 605 Maxf float64 606 Minf float64 607 Maxc complex128 608 Minc complex128 609 } 610 var it inputT 611 var err error 612 b := new(bytes.Buffer) 613 enc := NewEncoder(b) 614 dec := NewDecoder(b) 615 616 // int8 617 b.Reset() 618 it = inputT{ 619 Maxi: math.MaxInt8 + 1, 620 } 621 type outi8 struct { 622 Maxi int8 623 Mini int8 624 } 625 var o1 outi8 626 enc.Encode(it) 627 err = dec.Decode(&o1) 628 if err == nil || err.Error() != `value for "Maxi" out of range` { 629 t.Error("wrong overflow error for int8:", err) 630 } 631 it = inputT{ 632 Mini: math.MinInt8 - 1, 633 } 634 b.Reset() 635 enc.Encode(it) 636 err = dec.Decode(&o1) 637 if err == nil || err.Error() != `value for "Mini" out of range` { 638 t.Error("wrong underflow error for int8:", err) 639 } 640 641 // int16 642 b.Reset() 643 it = inputT{ 644 Maxi: math.MaxInt16 + 1, 645 } 646 type outi16 struct { 647 Maxi int16 648 Mini int16 649 } 650 var o2 outi16 651 enc.Encode(it) 652 err = dec.Decode(&o2) 653 if err == nil || err.Error() != `value for "Maxi" out of range` { 654 t.Error("wrong overflow error for int16:", err) 655 } 656 it = inputT{ 657 Mini: math.MinInt16 - 1, 658 } 659 b.Reset() 660 enc.Encode(it) 661 err = dec.Decode(&o2) 662 if err == nil || err.Error() != `value for "Mini" out of range` { 663 t.Error("wrong underflow error for int16:", err) 664 } 665 666 // int32 667 b.Reset() 668 it = inputT{ 669 Maxi: math.MaxInt32 + 1, 670 } 671 type outi32 struct { 672 Maxi int32 673 Mini int32 674 } 675 var o3 outi32 676 enc.Encode(it) 677 err = dec.Decode(&o3) 678 if err == nil || err.Error() != `value for "Maxi" out of range` { 679 t.Error("wrong overflow error for int32:", err) 680 } 681 it = inputT{ 682 Mini: math.MinInt32 - 1, 683 } 684 b.Reset() 685 enc.Encode(it) 686 err = dec.Decode(&o3) 687 if err == nil || err.Error() != `value for "Mini" out of range` { 688 t.Error("wrong underflow error for int32:", err) 689 } 690 691 // uint8 692 b.Reset() 693 it = inputT{ 694 Maxu: math.MaxUint8 + 1, 695 } 696 type outu8 struct { 697 Maxu uint8 698 } 699 var o4 outu8 700 enc.Encode(it) 701 err = dec.Decode(&o4) 702 if err == nil || err.Error() != `value for "Maxu" out of range` { 703 t.Error("wrong overflow error for uint8:", err) 704 } 705 706 // uint16 707 b.Reset() 708 it = inputT{ 709 Maxu: math.MaxUint16 + 1, 710 } 711 type outu16 struct { 712 Maxu uint16 713 } 714 var o5 outu16 715 enc.Encode(it) 716 err = dec.Decode(&o5) 717 if err == nil || err.Error() != `value for "Maxu" out of range` { 718 t.Error("wrong overflow error for uint16:", err) 719 } 720 721 // uint32 722 b.Reset() 723 it = inputT{ 724 Maxu: math.MaxUint32 + 1, 725 } 726 type outu32 struct { 727 Maxu uint32 728 } 729 var o6 outu32 730 enc.Encode(it) 731 err = dec.Decode(&o6) 732 if err == nil || err.Error() != `value for "Maxu" out of range` { 733 t.Error("wrong overflow error for uint32:", err) 734 } 735 736 // float32 737 b.Reset() 738 it = inputT{ 739 Maxf: math.MaxFloat32 * 2, 740 } 741 type outf32 struct { 742 Maxf float32 743 Minf float32 744 } 745 var o7 outf32 746 enc.Encode(it) 747 err = dec.Decode(&o7) 748 if err == nil || err.Error() != `value for "Maxf" out of range` { 749 t.Error("wrong overflow error for float32:", err) 750 } 751 752 // complex64 753 b.Reset() 754 it = inputT{ 755 Maxc: complex(math.MaxFloat32*2, math.MaxFloat32*2), 756 } 757 type outc64 struct { 758 Maxc complex64 759 Minc complex64 760 } 761 var o8 outc64 762 enc.Encode(it) 763 err = dec.Decode(&o8) 764 if err == nil || err.Error() != `value for "Maxc" out of range` { 765 t.Error("wrong overflow error for complex64:", err) 766 } 767 } 768 769 func TestNesting(t *testing.T) { 770 type RT struct { 771 A string 772 Next *RT 773 } 774 rt := new(RT) 775 rt.A = "level1" 776 rt.Next = new(RT) 777 rt.Next.A = "level2" 778 b := new(bytes.Buffer) 779 NewEncoder(b).Encode(rt) 780 var drt RT 781 dec := NewDecoder(b) 782 err := dec.Decode(&drt) 783 if err != nil { 784 t.Fatal("decoder error:", err) 785 } 786 if drt.A != rt.A { 787 t.Errorf("nesting: encode expected %v got %v", *rt, drt) 788 } 789 if drt.Next == nil { 790 t.Errorf("nesting: recursion failed") 791 } 792 if drt.Next.A != rt.Next.A { 793 t.Errorf("nesting: encode expected %v got %v", *rt.Next, *drt.Next) 794 } 795 } 796 797 // These three structures have the same data with different indirections 798 type T0 struct { 799 A int 800 B int 801 C int 802 D int 803 } 804 type T1 struct { 805 A int 806 B *int 807 C **int 808 D ***int 809 } 810 type T2 struct { 811 A ***int 812 B **int 813 C *int 814 D int 815 } 816 817 func TestAutoIndirection(t *testing.T) { 818 // First transfer t1 into t0 819 var t1 T1 820 t1.A = 17 821 t1.B = new(int) 822 *t1.B = 177 823 t1.C = new(*int) 824 *t1.C = new(int) 825 **t1.C = 1777 826 t1.D = new(**int) 827 *t1.D = new(*int) 828 **t1.D = new(int) 829 ***t1.D = 17777 830 b := new(bytes.Buffer) 831 enc := NewEncoder(b) 832 enc.Encode(t1) 833 dec := NewDecoder(b) 834 var t0 T0 835 dec.Decode(&t0) 836 if t0.A != 17 || t0.B != 177 || t0.C != 1777 || t0.D != 17777 { 837 t.Errorf("t1->t0: expected {17 177 1777 17777}; got %v", t0) 838 } 839 840 // Now transfer t2 into t0 841 var t2 T2 842 t2.D = 17777 843 t2.C = new(int) 844 *t2.C = 1777 845 t2.B = new(*int) 846 *t2.B = new(int) 847 **t2.B = 177 848 t2.A = new(**int) 849 *t2.A = new(*int) 850 **t2.A = new(int) 851 ***t2.A = 17 852 b.Reset() 853 enc.Encode(t2) 854 t0 = T0{} 855 dec.Decode(&t0) 856 if t0.A != 17 || t0.B != 177 || t0.C != 1777 || t0.D != 17777 { 857 t.Errorf("t2->t0 expected {17 177 1777 17777}; got %v", t0) 858 } 859 860 // Now transfer t0 into t1 861 t0 = T0{17, 177, 1777, 17777} 862 b.Reset() 863 enc.Encode(t0) 864 t1 = T1{} 865 dec.Decode(&t1) 866 if t1.A != 17 || *t1.B != 177 || **t1.C != 1777 || ***t1.D != 17777 { 867 t.Errorf("t0->t1 expected {17 177 1777 17777}; got {%d %d %d %d}", t1.A, *t1.B, **t1.C, ***t1.D) 868 } 869 870 // Now transfer t0 into t2 871 b.Reset() 872 enc.Encode(t0) 873 t2 = T2{} 874 dec.Decode(&t2) 875 if ***t2.A != 17 || **t2.B != 177 || *t2.C != 1777 || t2.D != 17777 { 876 t.Errorf("t0->t2 expected {17 177 1777 17777}; got {%d %d %d %d}", ***t2.A, **t2.B, *t2.C, t2.D) 877 } 878 879 // Now do t2 again but without pre-allocated pointers. 880 b.Reset() 881 enc.Encode(t0) 882 ***t2.A = 0 883 **t2.B = 0 884 *t2.C = 0 885 t2.D = 0 886 dec.Decode(&t2) 887 if ***t2.A != 17 || **t2.B != 177 || *t2.C != 1777 || t2.D != 17777 { 888 t.Errorf("t0->t2 expected {17 177 1777 17777}; got {%d %d %d %d}", ***t2.A, **t2.B, *t2.C, t2.D) 889 } 890 } 891 892 type RT0 struct { 893 A int 894 B string 895 C float64 896 } 897 type RT1 struct { 898 C float64 899 B string 900 A int 901 NotSet string 902 } 903 904 func TestReorderedFields(t *testing.T) { 905 var rt0 RT0 906 rt0.A = 17 907 rt0.B = "hello" 908 rt0.C = 3.14159 909 b := new(bytes.Buffer) 910 NewEncoder(b).Encode(rt0) 911 dec := NewDecoder(b) 912 var rt1 RT1 913 // Wire type is RT0, local type is RT1. 914 err := dec.Decode(&rt1) 915 if err != nil { 916 t.Fatal("decode error:", err) 917 } 918 if rt0.A != rt1.A || rt0.B != rt1.B || rt0.C != rt1.C { 919 t.Errorf("rt1->rt0: expected %v; got %v", rt0, rt1) 920 } 921 } 922 923 // Like an RT0 but with fields we'll ignore on the decode side. 924 type IT0 struct { 925 A int64 926 B string 927 Ignore_d []int 928 Ignore_e [3]float64 929 Ignore_f bool 930 Ignore_g string 931 Ignore_h []byte 932 Ignore_i *RT1 933 Ignore_m map[string]int 934 C float64 935 } 936 937 func TestIgnoredFields(t *testing.T) { 938 var it0 IT0 939 it0.A = 17 940 it0.B = "hello" 941 it0.C = 3.14159 942 it0.Ignore_d = []int{1, 2, 3} 943 it0.Ignore_e[0] = 1.0 944 it0.Ignore_e[1] = 2.0 945 it0.Ignore_e[2] = 3.0 946 it0.Ignore_f = true 947 it0.Ignore_g = "pay no attention" 948 it0.Ignore_h = []byte("to the curtain") 949 it0.Ignore_i = &RT1{3.1, "hi", 7, "hello"} 950 it0.Ignore_m = map[string]int{"one": 1, "two": 2} 951 952 b := new(bytes.Buffer) 953 NewEncoder(b).Encode(it0) 954 dec := NewDecoder(b) 955 var rt1 RT1 956 // Wire type is IT0, local type is RT1. 957 err := dec.Decode(&rt1) 958 if err != nil { 959 t.Error("error: ", err) 960 } 961 if int(it0.A) != rt1.A || it0.B != rt1.B || it0.C != rt1.C { 962 t.Errorf("rt0->rt1: expected %v; got %v", it0, rt1) 963 } 964 } 965 966 func TestBadRecursiveType(t *testing.T) { 967 type Rec ***Rec 968 var rec Rec 969 b := new(bytes.Buffer) 970 err := NewEncoder(b).Encode(&rec) 971 if err == nil { 972 t.Error("expected error; got none") 973 } else if !strings.Contains(err.Error(), "recursive") { 974 t.Error("expected recursive type error; got", err) 975 } 976 // Can't test decode easily because we can't encode one, so we can't pass one to a Decoder. 977 } 978 979 type Indirect struct { 980 A ***[3]int 981 S ***[]int 982 M ****map[string]int 983 } 984 985 type Direct struct { 986 A [3]int 987 S []int 988 M map[string]int 989 } 990 991 func TestIndirectSliceMapArray(t *testing.T) { 992 // Marshal indirect, unmarshal to direct. 993 i := new(Indirect) 994 i.A = new(**[3]int) 995 *i.A = new(*[3]int) 996 **i.A = new([3]int) 997 ***i.A = [3]int{1, 2, 3} 998 i.S = new(**[]int) 999 *i.S = new(*[]int) 1000 **i.S = new([]int) 1001 ***i.S = []int{4, 5, 6} 1002 i.M = new(***map[string]int) 1003 *i.M = new(**map[string]int) 1004 **i.M = new(*map[string]int) 1005 ***i.M = new(map[string]int) 1006 ****i.M = map[string]int{"one": 1, "two": 2, "three": 3} 1007 b := new(bytes.Buffer) 1008 NewEncoder(b).Encode(i) 1009 dec := NewDecoder(b) 1010 var d Direct 1011 err := dec.Decode(&d) 1012 if err != nil { 1013 t.Error("error: ", err) 1014 } 1015 if len(d.A) != 3 || d.A[0] != 1 || d.A[1] != 2 || d.A[2] != 3 { 1016 t.Errorf("indirect to direct: d.A is %v not %v", d.A, ***i.A) 1017 } 1018 if len(d.S) != 3 || d.S[0] != 4 || d.S[1] != 5 || d.S[2] != 6 { 1019 t.Errorf("indirect to direct: d.S is %v not %v", d.S, ***i.S) 1020 } 1021 if len(d.M) != 3 || d.M["one"] != 1 || d.M["two"] != 2 || d.M["three"] != 3 { 1022 t.Errorf("indirect to direct: d.M is %v not %v", d.M, ***i.M) 1023 } 1024 // Marshal direct, unmarshal to indirect. 1025 d.A = [3]int{11, 22, 33} 1026 d.S = []int{44, 55, 66} 1027 d.M = map[string]int{"four": 4, "five": 5, "six": 6} 1028 i = new(Indirect) 1029 b.Reset() 1030 NewEncoder(b).Encode(d) 1031 dec = NewDecoder(b) 1032 err = dec.Decode(&i) 1033 if err != nil { 1034 t.Fatal("error: ", err) 1035 } 1036 if len(***i.A) != 3 || (***i.A)[0] != 11 || (***i.A)[1] != 22 || (***i.A)[2] != 33 { 1037 t.Errorf("direct to indirect: ***i.A is %v not %v", ***i.A, d.A) 1038 } 1039 if len(***i.S) != 3 || (***i.S)[0] != 44 || (***i.S)[1] != 55 || (***i.S)[2] != 66 { 1040 t.Errorf("direct to indirect: ***i.S is %v not %v", ***i.S, ***i.S) 1041 } 1042 if len(****i.M) != 3 || (****i.M)["four"] != 4 || (****i.M)["five"] != 5 || (****i.M)["six"] != 6 { 1043 t.Errorf("direct to indirect: ****i.M is %v not %v", ****i.M, d.M) 1044 } 1045 } 1046 1047 // An interface with several implementations 1048 type Squarer interface { 1049 Square() int 1050 } 1051 1052 type Int int 1053 1054 func (i Int) Square() int { 1055 return int(i * i) 1056 } 1057 1058 type Float float64 1059 1060 func (f Float) Square() int { 1061 return int(f * f) 1062 } 1063 1064 type Vector []int 1065 1066 func (v Vector) Square() int { 1067 sum := 0 1068 for _, x := range v { 1069 sum += x * x 1070 } 1071 return sum 1072 } 1073 1074 type Point struct { 1075 X, Y int 1076 } 1077 1078 func (p Point) Square() int { 1079 return p.X*p.X + p.Y*p.Y 1080 } 1081 1082 // A struct with interfaces in it. 1083 type InterfaceItem struct { 1084 I int 1085 Sq1, Sq2, Sq3 Squarer 1086 F float64 1087 Sq []Squarer 1088 } 1089 1090 // The same struct without interfaces 1091 type NoInterfaceItem struct { 1092 I int 1093 F float64 1094 } 1095 1096 func TestInterface(t *testing.T) { 1097 iVal := Int(3) 1098 fVal := Float(5) 1099 // Sending a Vector will require that the receiver define a type in the middle of 1100 // receiving the value for item2. 1101 vVal := Vector{1, 2, 3} 1102 b := new(bytes.Buffer) 1103 item1 := &InterfaceItem{1, iVal, fVal, vVal, 11.5, []Squarer{iVal, fVal, nil, vVal}} 1104 // Register the types. 1105 Register(Int(0)) 1106 Register(Float(0)) 1107 Register(Vector{}) 1108 err := NewEncoder(b).Encode(item1) 1109 if err != nil { 1110 t.Error("expected no encode error; got", err) 1111 } 1112 1113 item2 := InterfaceItem{} 1114 err = NewDecoder(b).Decode(&item2) 1115 if err != nil { 1116 t.Fatal("decode:", err) 1117 } 1118 if item2.I != item1.I { 1119 t.Error("normal int did not decode correctly") 1120 } 1121 if item2.Sq1 == nil || item2.Sq1.Square() != iVal.Square() { 1122 t.Error("Int did not decode correctly") 1123 } 1124 if item2.Sq2 == nil || item2.Sq2.Square() != fVal.Square() { 1125 t.Error("Float did not decode correctly") 1126 } 1127 if item2.Sq3 == nil || item2.Sq3.Square() != vVal.Square() { 1128 t.Error("Vector did not decode correctly") 1129 } 1130 if item2.F != item1.F { 1131 t.Error("normal float did not decode correctly") 1132 } 1133 // Now check that we received a slice of Squarers correctly, including a nil element 1134 if len(item1.Sq) != len(item2.Sq) { 1135 t.Fatalf("[]Squarer length wrong: got %d; expected %d", len(item2.Sq), len(item1.Sq)) 1136 } 1137 for i, v1 := range item1.Sq { 1138 v2 := item2.Sq[i] 1139 if v1 == nil || v2 == nil { 1140 if v1 != nil || v2 != nil { 1141 t.Errorf("item %d inconsistent nils", i) 1142 } 1143 } else if v1.Square() != v2.Square() { 1144 t.Errorf("item %d inconsistent values: %v %v", i, v1, v2) 1145 } 1146 } 1147 } 1148 1149 // A struct with all basic types, stored in interfaces. 1150 type BasicInterfaceItem struct { 1151 Int, Int8, Int16, Int32, Int64 interface{} 1152 Uint, Uint8, Uint16, Uint32, Uint64 interface{} 1153 Float32, Float64 interface{} 1154 Complex64, Complex128 interface{} 1155 Bool interface{} 1156 String interface{} 1157 Bytes interface{} 1158 } 1159 1160 func TestInterfaceBasic(t *testing.T) { 1161 b := new(bytes.Buffer) 1162 item1 := &BasicInterfaceItem{ 1163 int(1), int8(1), int16(1), int32(1), int64(1), 1164 uint(1), uint8(1), uint16(1), uint32(1), uint64(1), 1165 float32(1), 1.0, 1166 complex64(1i), complex128(1i), 1167 true, 1168 "hello", 1169 []byte("sailor"), 1170 } 1171 err := NewEncoder(b).Encode(item1) 1172 if err != nil { 1173 t.Error("expected no encode error; got", err) 1174 } 1175 1176 item2 := &BasicInterfaceItem{} 1177 err = NewDecoder(b).Decode(&item2) 1178 if err != nil { 1179 t.Fatal("decode:", err) 1180 } 1181 if !reflect.DeepEqual(item1, item2) { 1182 t.Errorf("encode expected %v got %v", item1, item2) 1183 } 1184 // Hand check a couple for correct types. 1185 if v, ok := item2.Bool.(bool); !ok || !v { 1186 t.Error("boolean should be true") 1187 } 1188 if v, ok := item2.String.(string); !ok || v != item1.String.(string) { 1189 t.Errorf("string should be %v is %v", item1.String, v) 1190 } 1191 } 1192 1193 type String string 1194 1195 type PtrInterfaceItem struct { 1196 Str1 interface{} // basic 1197 Str2 interface{} // derived 1198 } 1199 1200 // We'll send pointers; should receive values. 1201 // Also check that we can register T but send *T. 1202 func TestInterfacePointer(t *testing.T) { 1203 b := new(bytes.Buffer) 1204 str1 := "howdy" 1205 str2 := String("kiddo") 1206 item1 := &PtrInterfaceItem{ 1207 &str1, 1208 &str2, 1209 } 1210 // Register the type. 1211 Register(str2) 1212 err := NewEncoder(b).Encode(item1) 1213 if err != nil { 1214 t.Error("expected no encode error; got", err) 1215 } 1216 1217 item2 := &PtrInterfaceItem{} 1218 err = NewDecoder(b).Decode(&item2) 1219 if err != nil { 1220 t.Fatal("decode:", err) 1221 } 1222 // Hand test for correct types and values. 1223 if v, ok := item2.Str1.(string); !ok || v != str1 { 1224 t.Errorf("basic string failed: %q should be %q", v, str1) 1225 } 1226 if v, ok := item2.Str2.(String); !ok || v != str2 { 1227 t.Errorf("derived type String failed: %q should be %q", v, str2) 1228 } 1229 } 1230 1231 func TestIgnoreInterface(t *testing.T) { 1232 iVal := Int(3) 1233 fVal := Float(5) 1234 // Sending a Point will require that the receiver define a type in the middle of 1235 // receiving the value for item2. 1236 pVal := Point{2, 3} 1237 b := new(bytes.Buffer) 1238 item1 := &InterfaceItem{1, iVal, fVal, pVal, 11.5, nil} 1239 // Register the types. 1240 Register(Int(0)) 1241 Register(Float(0)) 1242 Register(Point{}) 1243 err := NewEncoder(b).Encode(item1) 1244 if err != nil { 1245 t.Error("expected no encode error; got", err) 1246 } 1247 1248 item2 := NoInterfaceItem{} 1249 err = NewDecoder(b).Decode(&item2) 1250 if err != nil { 1251 t.Fatal("decode:", err) 1252 } 1253 if item2.I != item1.I { 1254 t.Error("normal int did not decode correctly") 1255 } 1256 if item2.F != item1.F { 1257 t.Error("normal float did not decode correctly") 1258 } 1259 } 1260 1261 type U struct { 1262 A int 1263 B string 1264 c float64 1265 D uint 1266 } 1267 1268 func TestUnexportedFields(t *testing.T) { 1269 var u0 U 1270 u0.A = 17 1271 u0.B = "hello" 1272 u0.c = 3.14159 1273 u0.D = 23 1274 b := new(bytes.Buffer) 1275 NewEncoder(b).Encode(u0) 1276 dec := NewDecoder(b) 1277 var u1 U 1278 u1.c = 1234. 1279 err := dec.Decode(&u1) 1280 if err != nil { 1281 t.Fatal("decode error:", err) 1282 } 1283 if u0.A != u1.A || u0.B != u1.B || u0.D != u1.D { 1284 t.Errorf("u1->u0: expected %v; got %v", u0, u1) 1285 } 1286 if u1.c != 1234. { 1287 t.Error("u1.c modified") 1288 } 1289 } 1290 1291 var singletons = []interface{}{ 1292 true, 1293 7, 1294 3.2, 1295 "hello", 1296 [3]int{11, 22, 33}, 1297 []float32{0.5, 0.25, 0.125}, 1298 map[string]int{"one": 1, "two": 2}, 1299 } 1300 1301 func TestDebugSingleton(t *testing.T) { 1302 if debugFunc == nil { 1303 return 1304 } 1305 b := new(bytes.Buffer) 1306 // Accumulate a number of values and print them out all at once. 1307 for _, x := range singletons { 1308 err := NewEncoder(b).Encode(x) 1309 if err != nil { 1310 t.Fatal("encode:", err) 1311 } 1312 } 1313 debugFunc(b) 1314 } 1315 1316 // A type that won't be defined in the gob until we send it in an interface value. 1317 type OnTheFly struct { 1318 A int 1319 } 1320 1321 type DT struct { 1322 // X OnTheFly 1323 A int 1324 B string 1325 C float64 1326 I interface{} 1327 J interface{} 1328 I_nil interface{} 1329 M map[string]int 1330 T [3]int 1331 S []string 1332 } 1333 1334 func newDT() DT { 1335 var dt DT 1336 dt.A = 17 1337 dt.B = "hello" 1338 dt.C = 3.14159 1339 dt.I = 271828 1340 dt.J = OnTheFly{3} 1341 dt.I_nil = nil 1342 dt.M = map[string]int{"one": 1, "two": 2} 1343 dt.T = [3]int{11, 22, 33} 1344 dt.S = []string{"hi", "joe"} 1345 return dt 1346 } 1347 1348 func TestDebugStruct(t *testing.T) { 1349 if debugFunc == nil { 1350 return 1351 } 1352 Register(OnTheFly{}) 1353 dt := newDT() 1354 b := new(bytes.Buffer) 1355 err := NewEncoder(b).Encode(dt) 1356 if err != nil { 1357 t.Fatal("encode:", err) 1358 } 1359 debugBuffer := bytes.NewBuffer(b.Bytes()) 1360 dt2 := &DT{} 1361 err = NewDecoder(b).Decode(&dt2) 1362 if err != nil { 1363 t.Error("decode:", err) 1364 } 1365 debugFunc(debugBuffer) 1366 } 1367 1368 func encFuzzDec(rng *rand.Rand, in interface{}) error { 1369 buf := new(bytes.Buffer) 1370 enc := NewEncoder(buf) 1371 if err := enc.Encode(&in); err != nil { 1372 return err 1373 } 1374 1375 b := buf.Bytes() 1376 for i, bi := range b { 1377 if rng.Intn(10) < 3 { 1378 b[i] = bi + uint8(rng.Intn(256)) 1379 } 1380 } 1381 1382 dec := NewDecoder(buf) 1383 var e interface{} 1384 if err := dec.Decode(&e); err != nil { 1385 return err 1386 } 1387 return nil 1388 } 1389 1390 // This does some "fuzz testing" by attempting to decode a sequence of random bytes. 1391 func TestFuzz(t *testing.T) { 1392 if !*doFuzzTests { 1393 t.Logf("disabled; run with -gob.fuzz to enable") 1394 return 1395 } 1396 1397 // all possible inputs 1398 input := []interface{}{ 1399 new(int), 1400 new(float32), 1401 new(float64), 1402 new(complex128), 1403 &ByteStruct{255}, 1404 &ArrayStruct{}, 1405 &StringStruct{"hello"}, 1406 &GobTest1{0, &StringStruct{"hello"}}, 1407 } 1408 testFuzz(t, time.Now().UnixNano(), 100, input...) 1409 } 1410 1411 func TestFuzzRegressions(t *testing.T) { 1412 if !*doFuzzTests { 1413 t.Logf("disabled; run with -gob.fuzz to enable") 1414 return 1415 } 1416 1417 // An instance triggering a type name of length ~102 GB. 1418 testFuzz(t, 1328492090837718000, 100, new(float32)) 1419 // An instance triggering a type name of 1.6 GB. 1420 // Note: can take several minutes to run. 1421 testFuzz(t, 1330522872628565000, 100, new(int)) 1422 } 1423 1424 func testFuzz(t *testing.T, seed int64, n int, input ...interface{}) { 1425 for _, e := range input { 1426 t.Logf("seed=%d n=%d e=%T", seed, n, e) 1427 rng := rand.New(rand.NewSource(seed)) 1428 for i := 0; i < n; i++ { 1429 encFuzzDec(rng, e) 1430 } 1431 } 1432 } 1433 1434 // TestFuzzOneByte tries to decode corrupted input sequences 1435 // and checks that no panic occurs. 1436 func TestFuzzOneByte(t *testing.T) { 1437 buf := new(bytes.Buffer) 1438 Register(OnTheFly{}) 1439 dt := newDT() 1440 if err := NewEncoder(buf).Encode(dt); err != nil { 1441 t.Fatal(err) 1442 } 1443 s := buf.String() 1444 1445 indices := make([]int, 0, len(s)) 1446 for i := 0; i < len(s); i++ { 1447 switch i { 1448 case 14, 167, 231, 265: // a slice length, corruptions are not handled yet. 1449 continue 1450 } 1451 indices = append(indices, i) 1452 } 1453 if testing.Short() { 1454 indices = []int{1, 111, 178} // known fixed panics 1455 } 1456 for _, i := range indices { 1457 for j := 0; j < 256; j += 3 { 1458 b := []byte(s) 1459 b[i] ^= byte(j) 1460 var e DT 1461 func() { 1462 defer func() { 1463 if p := recover(); p != nil { 1464 t.Errorf("crash for b[%d] ^= 0x%x", i, j) 1465 panic(p) 1466 } 1467 }() 1468 err := NewDecoder(bytes.NewReader(b)).Decode(&e) 1469 _ = err 1470 }() 1471 } 1472 } 1473 } 1474 1475 // Don't crash, just give error with invalid type id. 1476 // Issue 9649. 1477 func TestErrorInvalidTypeId(t *testing.T) { 1478 data := []byte{0x01, 0x00, 0x01, 0x00} 1479 d := NewDecoder(bytes.NewReader(data)) 1480 // When running d.Decode(&foo) the first time the decoder stops 1481 // after []byte{0x01, 0x00} and reports an errBadType. Running 1482 // d.Decode(&foo) again on exactly the same input sequence should 1483 // give another errBadType, but instead caused a panic because 1484 // decoderMap wasn't cleaned up properly after the first error. 1485 for i := 0; i < 2; i++ { 1486 var foo struct{} 1487 err := d.Decode(&foo) 1488 if err != errBadType { 1489 t.Fatalf("decode: expected %s, got %s", errBadType, err) 1490 } 1491 } 1492 } 1493