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