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 5package gob 6 7import ( 8 "bytes" 9 "errors" 10 "flag" 11 "math" 12 "math/rand" 13 "reflect" 14 "strings" 15 "testing" 16 "time" 17 "unsafe" 18) 19 20var doFuzzTests = flag.Bool("gob.fuzz", false, "run the fuzz tests, which are large and very slow") 21 22// Guarantee encoding format by comparing some encodings to hand-written values 23type EncodeT struct { 24 x uint64 25 b []byte 26} 27 28var encodeT = []EncodeT{ 29 {0x00, []byte{0x00}}, 30 {0x0F, []byte{0x0F}}, 31 {0xFF, []byte{0xFF, 0xFF}}, 32 {0xFFFF, []byte{0xFE, 0xFF, 0xFF}}, 33 {0xFFFFFF, []byte{0xFD, 0xFF, 0xFF, 0xFF}}, 34 {0xFFFFFFFF, []byte{0xFC, 0xFF, 0xFF, 0xFF, 0xFF}}, 35 {0xFFFFFFFFFF, []byte{0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, 36 {0xFFFFFFFFFFFF, []byte{0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, 37 {0xFFFFFFFFFFFFFF, []byte{0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, 38 {0xFFFFFFFFFFFFFFFF, []byte{0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, 39 {0x1111, []byte{0xFE, 0x11, 0x11}}, 40 {0x1111111111111111, []byte{0xF8, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}}, 41 {0x8888888888888888, []byte{0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88}}, 42 {1 << 63, []byte{0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, 43} 44 45// testError is meant to be used as a deferred function to turn a panic(gobError) into a 46// plain test.Error call. 47func testError(t *testing.T) { 48 if e := recover(); e != nil { 49 t.Error(e.(gobError).err) // Will re-panic if not one of our errors, such as a runtime error. 50 } 51} 52 53func newDecBuffer(data []byte) *decBuffer { 54 return &decBuffer{ 55 data: data, 56 } 57} 58 59// Test basic encode/decode routines for unsigned integers 60func 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 85func 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 98func 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 113var boolResult = []byte{0x07, 0x01} 114 115// The result of encoding a number 17 with field number 7 116var signedResult = []byte{0x07, 2 * 17} 117var unsignedResult = []byte{0x07, 17} 118var floatResult = []byte{0x07, 0xFE, 0x31, 0x40} 119 120// The result of encoding a number 17+19i with field number 7 121var complexResult = []byte{0x07, 0xFE, 0x31, 0x40, 0xFE, 0x33, 0x40} 122 123// The result of encoding "hello" with field number 7 124var bytesResult = []byte{0x07, 0x05, 'h', 'e', 'l', 'l', 'o'} 125 126func newDecodeState(buf *decBuffer) *decoderState { 127 d := new(decoderState) 128 d.b = buf 129 return d 130} 131 132func 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. 141func 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 324func execDec(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 334func 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. 343func 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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(instr, state, t, reflect.ValueOf(&data)) 538 if data != "hello" { 539 t.Errorf(`bytes a = %q not "hello"`, data) 540 } 541 } 542} 543 544func TestEndToEnd(t *testing.T) { 545 type T2 struct { 546 T string 547 } 548 type T3 struct { 549 X float64 550 Z *int 551 } 552 type T1 struct { 553 A, B, C int 554 M map[string]*float64 555 M2 map[int]T3 556 Mstring map[string]string 557 Mintptr map[int]*int 558 Mcomp map[complex128]complex128 559 Marr map[[2]string][2]*float64 560 EmptyMap map[string]int // to check that we receive a non-nil map. 561 N *[3]float64 562 Strs *[2]string 563 Int64s *[]int64 564 RI complex64 565 S string 566 Y []byte 567 T *T2 568 } 569 pi := 3.14159 570 e := 2.71828 571 two := 2.0 572 meaning := 42 573 fingers := 5 574 s1 := "string1" 575 s2 := "string2" 576 var comp1 complex128 = complex(1.0, 1.0) 577 var comp2 complex128 = complex(1.0, 1.0) 578 var arr1 [2]string 579 arr1[0] = s1 580 arr1[1] = s2 581 var arr2 [2]string 582 arr2[0] = s2 583 arr2[1] = s1 584 var floatArr1 [2]*float64 585 floatArr1[0] = &pi 586 floatArr1[1] = &e 587 var floatArr2 [2]*float64 588 floatArr2[0] = &e 589 floatArr2[1] = &two 590 t1 := &T1{ 591 A: 17, 592 B: 18, 593 C: -5, 594 M: map[string]*float64{"pi": &pi, "e": &e}, 595 M2: map[int]T3{4: {X: pi, Z: &meaning}, 10: {X: e, Z: &fingers}}, 596 Mstring: map[string]string{"pi": "3.14", "e": "2.71"}, 597 Mintptr: map[int]*int{meaning: &fingers, fingers: &meaning}, 598 Mcomp: map[complex128]complex128{comp1: comp2, comp2: comp1}, 599 Marr: map[[2]string][2]*float64{arr1: floatArr1, arr2: floatArr2}, 600 EmptyMap: make(map[string]int), 601 N: &[3]float64{1.5, 2.5, 3.5}, 602 Strs: &[2]string{s1, s2}, 603 Int64s: &[]int64{77, 89, 123412342134}, 604 RI: 17 - 23i, 605 S: "Now is the time", 606 Y: []byte("hello, sailor"), 607 T: &T2{"this is T2"}, 608 } 609 b := new(bytes.Buffer) 610 err := NewEncoder(b).Encode(t1) 611 if err != nil { 612 t.Error("encode:", err) 613 } 614 var _t1 T1 615 err = NewDecoder(b).Decode(&_t1) 616 if err != nil { 617 t.Fatal("decode:", err) 618 } 619 if !reflect.DeepEqual(t1, &_t1) { 620 t.Errorf("encode expected %v got %v", *t1, _t1) 621 } 622 // Be absolutely sure the received map is non-nil. 623 if t1.EmptyMap == nil { 624 t.Errorf("nil map sent") 625 } 626 if _t1.EmptyMap == nil { 627 t.Errorf("nil map received") 628 } 629} 630 631func TestOverflow(t *testing.T) { 632 type inputT struct { 633 Maxi int64 634 Mini int64 635 Maxu uint64 636 Maxf float64 637 Minf float64 638 Maxc complex128 639 Minc complex128 640 } 641 var it inputT 642 var err error 643 b := new(bytes.Buffer) 644 enc := NewEncoder(b) 645 dec := NewDecoder(b) 646 647 // int8 648 b.Reset() 649 it = inputT{ 650 Maxi: math.MaxInt8 + 1, 651 } 652 type outi8 struct { 653 Maxi int8 654 Mini int8 655 } 656 var o1 outi8 657 enc.Encode(it) 658 err = dec.Decode(&o1) 659 if err == nil || err.Error() != `value for "Maxi" out of range` { 660 t.Error("wrong overflow error for int8:", err) 661 } 662 it = inputT{ 663 Mini: math.MinInt8 - 1, 664 } 665 b.Reset() 666 enc.Encode(it) 667 err = dec.Decode(&o1) 668 if err == nil || err.Error() != `value for "Mini" out of range` { 669 t.Error("wrong underflow error for int8:", err) 670 } 671 672 // int16 673 b.Reset() 674 it = inputT{ 675 Maxi: math.MaxInt16 + 1, 676 } 677 type outi16 struct { 678 Maxi int16 679 Mini int16 680 } 681 var o2 outi16 682 enc.Encode(it) 683 err = dec.Decode(&o2) 684 if err == nil || err.Error() != `value for "Maxi" out of range` { 685 t.Error("wrong overflow error for int16:", err) 686 } 687 it = inputT{ 688 Mini: math.MinInt16 - 1, 689 } 690 b.Reset() 691 enc.Encode(it) 692 err = dec.Decode(&o2) 693 if err == nil || err.Error() != `value for "Mini" out of range` { 694 t.Error("wrong underflow error for int16:", err) 695 } 696 697 // int32 698 b.Reset() 699 it = inputT{ 700 Maxi: math.MaxInt32 + 1, 701 } 702 type outi32 struct { 703 Maxi int32 704 Mini int32 705 } 706 var o3 outi32 707 enc.Encode(it) 708 err = dec.Decode(&o3) 709 if err == nil || err.Error() != `value for "Maxi" out of range` { 710 t.Error("wrong overflow error for int32:", err) 711 } 712 it = inputT{ 713 Mini: math.MinInt32 - 1, 714 } 715 b.Reset() 716 enc.Encode(it) 717 err = dec.Decode(&o3) 718 if err == nil || err.Error() != `value for "Mini" out of range` { 719 t.Error("wrong underflow error for int32:", err) 720 } 721 722 // uint8 723 b.Reset() 724 it = inputT{ 725 Maxu: math.MaxUint8 + 1, 726 } 727 type outu8 struct { 728 Maxu uint8 729 } 730 var o4 outu8 731 enc.Encode(it) 732 err = dec.Decode(&o4) 733 if err == nil || err.Error() != `value for "Maxu" out of range` { 734 t.Error("wrong overflow error for uint8:", err) 735 } 736 737 // uint16 738 b.Reset() 739 it = inputT{ 740 Maxu: math.MaxUint16 + 1, 741 } 742 type outu16 struct { 743 Maxu uint16 744 } 745 var o5 outu16 746 enc.Encode(it) 747 err = dec.Decode(&o5) 748 if err == nil || err.Error() != `value for "Maxu" out of range` { 749 t.Error("wrong overflow error for uint16:", err) 750 } 751 752 // uint32 753 b.Reset() 754 it = inputT{ 755 Maxu: math.MaxUint32 + 1, 756 } 757 type outu32 struct { 758 Maxu uint32 759 } 760 var o6 outu32 761 enc.Encode(it) 762 err = dec.Decode(&o6) 763 if err == nil || err.Error() != `value for "Maxu" out of range` { 764 t.Error("wrong overflow error for uint32:", err) 765 } 766 767 // float32 768 b.Reset() 769 it = inputT{ 770 Maxf: math.MaxFloat32 * 2, 771 } 772 type outf32 struct { 773 Maxf float32 774 Minf float32 775 } 776 var o7 outf32 777 enc.Encode(it) 778 err = dec.Decode(&o7) 779 if err == nil || err.Error() != `value for "Maxf" out of range` { 780 t.Error("wrong overflow error for float32:", err) 781 } 782 783 // complex64 784 b.Reset() 785 it = inputT{ 786 Maxc: complex(math.MaxFloat32*2, math.MaxFloat32*2), 787 } 788 type outc64 struct { 789 Maxc complex64 790 Minc complex64 791 } 792 var o8 outc64 793 enc.Encode(it) 794 err = dec.Decode(&o8) 795 if err == nil || err.Error() != `value for "Maxc" out of range` { 796 t.Error("wrong overflow error for complex64:", err) 797 } 798} 799 800func TestNesting(t *testing.T) { 801 type RT struct { 802 A string 803 Next *RT 804 } 805 rt := new(RT) 806 rt.A = "level1" 807 rt.Next = new(RT) 808 rt.Next.A = "level2" 809 b := new(bytes.Buffer) 810 NewEncoder(b).Encode(rt) 811 var drt RT 812 dec := NewDecoder(b) 813 err := dec.Decode(&drt) 814 if err != nil { 815 t.Fatal("decoder error:", err) 816 } 817 if drt.A != rt.A { 818 t.Errorf("nesting: encode expected %v got %v", *rt, drt) 819 } 820 if drt.Next == nil { 821 t.Errorf("nesting: recursion failed") 822 } 823 if drt.Next.A != rt.Next.A { 824 t.Errorf("nesting: encode expected %v got %v", *rt.Next, *drt.Next) 825 } 826} 827 828// These three structures have the same data with different indirections 829type T0 struct { 830 A int 831 B int 832 C int 833 D int 834} 835type T1 struct { 836 A int 837 B *int 838 C **int 839 D ***int 840} 841type T2 struct { 842 A ***int 843 B **int 844 C *int 845 D int 846} 847 848func TestAutoIndirection(t *testing.T) { 849 // First transfer t1 into t0 850 var t1 T1 851 t1.A = 17 852 t1.B = new(int) 853 *t1.B = 177 854 t1.C = new(*int) 855 *t1.C = new(int) 856 **t1.C = 1777 857 t1.D = new(**int) 858 *t1.D = new(*int) 859 **t1.D = new(int) 860 ***t1.D = 17777 861 b := new(bytes.Buffer) 862 enc := NewEncoder(b) 863 enc.Encode(t1) 864 dec := NewDecoder(b) 865 var t0 T0 866 dec.Decode(&t0) 867 if t0.A != 17 || t0.B != 177 || t0.C != 1777 || t0.D != 17777 { 868 t.Errorf("t1->t0: expected {17 177 1777 17777}; got %v", t0) 869 } 870 871 // Now transfer t2 into t0 872 var t2 T2 873 t2.D = 17777 874 t2.C = new(int) 875 *t2.C = 1777 876 t2.B = new(*int) 877 *t2.B = new(int) 878 **t2.B = 177 879 t2.A = new(**int) 880 *t2.A = new(*int) 881 **t2.A = new(int) 882 ***t2.A = 17 883 b.Reset() 884 enc.Encode(t2) 885 t0 = T0{} 886 dec.Decode(&t0) 887 if t0.A != 17 || t0.B != 177 || t0.C != 1777 || t0.D != 17777 { 888 t.Errorf("t2->t0 expected {17 177 1777 17777}; got %v", t0) 889 } 890 891 // Now transfer t0 into t1 892 t0 = T0{17, 177, 1777, 17777} 893 b.Reset() 894 enc.Encode(t0) 895 t1 = T1{} 896 dec.Decode(&t1) 897 if t1.A != 17 || *t1.B != 177 || **t1.C != 1777 || ***t1.D != 17777 { 898 t.Errorf("t0->t1 expected {17 177 1777 17777}; got {%d %d %d %d}", t1.A, *t1.B, **t1.C, ***t1.D) 899 } 900 901 // Now transfer t0 into t2 902 b.Reset() 903 enc.Encode(t0) 904 t2 = T2{} 905 dec.Decode(&t2) 906 if ***t2.A != 17 || **t2.B != 177 || *t2.C != 1777 || t2.D != 17777 { 907 t.Errorf("t0->t2 expected {17 177 1777 17777}; got {%d %d %d %d}", ***t2.A, **t2.B, *t2.C, t2.D) 908 } 909 910 // Now do t2 again but without pre-allocated pointers. 911 b.Reset() 912 enc.Encode(t0) 913 ***t2.A = 0 914 **t2.B = 0 915 *t2.C = 0 916 t2.D = 0 917 dec.Decode(&t2) 918 if ***t2.A != 17 || **t2.B != 177 || *t2.C != 1777 || t2.D != 17777 { 919 t.Errorf("t0->t2 expected {17 177 1777 17777}; got {%d %d %d %d}", ***t2.A, **t2.B, *t2.C, t2.D) 920 } 921} 922 923type RT0 struct { 924 A int 925 B string 926 C float64 927} 928type RT1 struct { 929 C float64 930 B string 931 A int 932 NotSet string 933} 934 935func TestReorderedFields(t *testing.T) { 936 var rt0 RT0 937 rt0.A = 17 938 rt0.B = "hello" 939 rt0.C = 3.14159 940 b := new(bytes.Buffer) 941 NewEncoder(b).Encode(rt0) 942 dec := NewDecoder(b) 943 var rt1 RT1 944 // Wire type is RT0, local type is RT1. 945 err := dec.Decode(&rt1) 946 if err != nil { 947 t.Fatal("decode error:", err) 948 } 949 if rt0.A != rt1.A || rt0.B != rt1.B || rt0.C != rt1.C { 950 t.Errorf("rt1->rt0: expected %v; got %v", rt0, rt1) 951 } 952} 953 954// Like an RT0 but with fields we'll ignore on the decode side. 955type IT0 struct { 956 A int64 957 B string 958 Ignore_d []int 959 Ignore_e [3]float64 960 Ignore_f bool 961 Ignore_g string 962 Ignore_h []byte 963 Ignore_i *RT1 964 Ignore_m map[string]int 965 C float64 966} 967 968func TestIgnoredFields(t *testing.T) { 969 var it0 IT0 970 it0.A = 17 971 it0.B = "hello" 972 it0.C = 3.14159 973 it0.Ignore_d = []int{1, 2, 3} 974 it0.Ignore_e[0] = 1.0 975 it0.Ignore_e[1] = 2.0 976 it0.Ignore_e[2] = 3.0 977 it0.Ignore_f = true 978 it0.Ignore_g = "pay no attention" 979 it0.Ignore_h = []byte("to the curtain") 980 it0.Ignore_i = &RT1{3.1, "hi", 7, "hello"} 981 it0.Ignore_m = map[string]int{"one": 1, "two": 2} 982 983 b := new(bytes.Buffer) 984 NewEncoder(b).Encode(it0) 985 dec := NewDecoder(b) 986 var rt1 RT1 987 // Wire type is IT0, local type is RT1. 988 err := dec.Decode(&rt1) 989 if err != nil { 990 t.Error("error: ", err) 991 } 992 if int(it0.A) != rt1.A || it0.B != rt1.B || it0.C != rt1.C { 993 t.Errorf("rt0->rt1: expected %v; got %v", it0, rt1) 994 } 995} 996 997func TestBadRecursiveType(t *testing.T) { 998 type Rec ***Rec 999 var rec Rec 1000 b := new(bytes.Buffer) 1001 err := NewEncoder(b).Encode(&rec) 1002 if err == nil { 1003 t.Error("expected error; got none") 1004 } else if !strings.Contains(err.Error(), "recursive") { 1005 t.Error("expected recursive type error; got", err) 1006 } 1007 // Can't test decode easily because we can't encode one, so we can't pass one to a Decoder. 1008} 1009 1010type Indirect struct { 1011 A ***[3]int 1012 S ***[]int 1013 M ****map[string]int 1014} 1015 1016type Direct struct { 1017 A [3]int 1018 S []int 1019 M map[string]int 1020} 1021 1022func TestIndirectSliceMapArray(t *testing.T) { 1023 // Marshal indirect, unmarshal to direct. 1024 i := new(Indirect) 1025 i.A = new(**[3]int) 1026 *i.A = new(*[3]int) 1027 **i.A = new([3]int) 1028 ***i.A = [3]int{1, 2, 3} 1029 i.S = new(**[]int) 1030 *i.S = new(*[]int) 1031 **i.S = new([]int) 1032 ***i.S = []int{4, 5, 6} 1033 i.M = new(***map[string]int) 1034 *i.M = new(**map[string]int) 1035 **i.M = new(*map[string]int) 1036 ***i.M = new(map[string]int) 1037 ****i.M = map[string]int{"one": 1, "two": 2, "three": 3} 1038 b := new(bytes.Buffer) 1039 NewEncoder(b).Encode(i) 1040 dec := NewDecoder(b) 1041 var d Direct 1042 err := dec.Decode(&d) 1043 if err != nil { 1044 t.Error("error: ", err) 1045 } 1046 if len(d.A) != 3 || d.A[0] != 1 || d.A[1] != 2 || d.A[2] != 3 { 1047 t.Errorf("indirect to direct: d.A is %v not %v", d.A, ***i.A) 1048 } 1049 if len(d.S) != 3 || d.S[0] != 4 || d.S[1] != 5 || d.S[2] != 6 { 1050 t.Errorf("indirect to direct: d.S is %v not %v", d.S, ***i.S) 1051 } 1052 if len(d.M) != 3 || d.M["one"] != 1 || d.M["two"] != 2 || d.M["three"] != 3 { 1053 t.Errorf("indirect to direct: d.M is %v not %v", d.M, ***i.M) 1054 } 1055 // Marshal direct, unmarshal to indirect. 1056 d.A = [3]int{11, 22, 33} 1057 d.S = []int{44, 55, 66} 1058 d.M = map[string]int{"four": 4, "five": 5, "six": 6} 1059 i = new(Indirect) 1060 b.Reset() 1061 NewEncoder(b).Encode(d) 1062 dec = NewDecoder(b) 1063 err = dec.Decode(&i) 1064 if err != nil { 1065 t.Fatal("error: ", err) 1066 } 1067 if len(***i.A) != 3 || (***i.A)[0] != 11 || (***i.A)[1] != 22 || (***i.A)[2] != 33 { 1068 t.Errorf("direct to indirect: ***i.A is %v not %v", ***i.A, d.A) 1069 } 1070 if len(***i.S) != 3 || (***i.S)[0] != 44 || (***i.S)[1] != 55 || (***i.S)[2] != 66 { 1071 t.Errorf("direct to indirect: ***i.S is %v not %v", ***i.S, ***i.S) 1072 } 1073 if len(****i.M) != 3 || (****i.M)["four"] != 4 || (****i.M)["five"] != 5 || (****i.M)["six"] != 6 { 1074 t.Errorf("direct to indirect: ****i.M is %v not %v", ****i.M, d.M) 1075 } 1076} 1077 1078// An interface with several implementations 1079type Squarer interface { 1080 Square() int 1081} 1082 1083type Int int 1084 1085func (i Int) Square() int { 1086 return int(i * i) 1087} 1088 1089type Float float64 1090 1091func (f Float) Square() int { 1092 return int(f * f) 1093} 1094 1095type Vector []int 1096 1097func (v Vector) Square() int { 1098 sum := 0 1099 for _, x := range v { 1100 sum += x * x 1101 } 1102 return sum 1103} 1104 1105type Point struct { 1106 X, Y int 1107} 1108 1109func (p Point) Square() int { 1110 return p.X*p.X + p.Y*p.Y 1111} 1112 1113// A struct with interfaces in it. 1114type InterfaceItem struct { 1115 I int 1116 Sq1, Sq2, Sq3 Squarer 1117 F float64 1118 Sq []Squarer 1119} 1120 1121// The same struct without interfaces 1122type NoInterfaceItem struct { 1123 I int 1124 F float64 1125} 1126 1127func TestInterface(t *testing.T) { 1128 iVal := Int(3) 1129 fVal := Float(5) 1130 // Sending a Vector will require that the receiver define a type in the middle of 1131 // receiving the value for item2. 1132 vVal := Vector{1, 2, 3} 1133 b := new(bytes.Buffer) 1134 item1 := &InterfaceItem{1, iVal, fVal, vVal, 11.5, []Squarer{iVal, fVal, nil, vVal}} 1135 // Register the types. 1136 Register(Int(0)) 1137 Register(Float(0)) 1138 Register(Vector{}) 1139 err := NewEncoder(b).Encode(item1) 1140 if err != nil { 1141 t.Error("expected no encode error; got", err) 1142 } 1143 1144 item2 := InterfaceItem{} 1145 err = NewDecoder(b).Decode(&item2) 1146 if err != nil { 1147 t.Fatal("decode:", err) 1148 } 1149 if item2.I != item1.I { 1150 t.Error("normal int did not decode correctly") 1151 } 1152 if item2.Sq1 == nil || item2.Sq1.Square() != iVal.Square() { 1153 t.Error("Int did not decode correctly") 1154 } 1155 if item2.Sq2 == nil || item2.Sq2.Square() != fVal.Square() { 1156 t.Error("Float did not decode correctly") 1157 } 1158 if item2.Sq3 == nil || item2.Sq3.Square() != vVal.Square() { 1159 t.Error("Vector did not decode correctly") 1160 } 1161 if item2.F != item1.F { 1162 t.Error("normal float did not decode correctly") 1163 } 1164 // Now check that we received a slice of Squarers correctly, including a nil element 1165 if len(item1.Sq) != len(item2.Sq) { 1166 t.Fatalf("[]Squarer length wrong: got %d; expected %d", len(item2.Sq), len(item1.Sq)) 1167 } 1168 for i, v1 := range item1.Sq { 1169 v2 := item2.Sq[i] 1170 if v1 == nil || v2 == nil { 1171 if v1 != nil || v2 != nil { 1172 t.Errorf("item %d inconsistent nils", i) 1173 } 1174 } else if v1.Square() != v2.Square() { 1175 t.Errorf("item %d inconsistent values: %v %v", i, v1, v2) 1176 } 1177 } 1178} 1179 1180// A struct with all basic types, stored in interfaces. 1181type BasicInterfaceItem struct { 1182 Int, Int8, Int16, Int32, Int64 any 1183 Uint, Uint8, Uint16, Uint32, Uint64 any 1184 Float32, Float64 any 1185 Complex64, Complex128 any 1186 Bool any 1187 String any 1188 Bytes any 1189} 1190 1191func TestInterfaceBasic(t *testing.T) { 1192 b := new(bytes.Buffer) 1193 item1 := &BasicInterfaceItem{ 1194 int(1), int8(1), int16(1), int32(1), int64(1), 1195 uint(1), uint8(1), uint16(1), uint32(1), uint64(1), 1196 float32(1), 1.0, 1197 complex64(1i), complex128(1i), 1198 true, 1199 "hello", 1200 []byte("sailor"), 1201 } 1202 err := NewEncoder(b).Encode(item1) 1203 if err != nil { 1204 t.Error("expected no encode error; got", err) 1205 } 1206 1207 item2 := &BasicInterfaceItem{} 1208 err = NewDecoder(b).Decode(&item2) 1209 if err != nil { 1210 t.Fatal("decode:", err) 1211 } 1212 if !reflect.DeepEqual(item1, item2) { 1213 t.Errorf("encode expected %v got %v", item1, item2) 1214 } 1215 // Hand check a couple for correct types. 1216 if v, ok := item2.Bool.(bool); !ok || !v { 1217 t.Error("boolean should be true") 1218 } 1219 if v, ok := item2.String.(string); !ok || v != item1.String.(string) { 1220 t.Errorf("string should be %v is %v", item1.String, v) 1221 } 1222} 1223 1224type String string 1225 1226type PtrInterfaceItem struct { 1227 Str1 any // basic 1228 Str2 any // derived 1229} 1230 1231// We'll send pointers; should receive values. 1232// Also check that we can register T but send *T. 1233func TestInterfacePointer(t *testing.T) { 1234 b := new(bytes.Buffer) 1235 str1 := "howdy" 1236 str2 := String("kiddo") 1237 item1 := &PtrInterfaceItem{ 1238 &str1, 1239 &str2, 1240 } 1241 // Register the type. 1242 Register(str2) 1243 err := NewEncoder(b).Encode(item1) 1244 if err != nil { 1245 t.Error("expected no encode error; got", err) 1246 } 1247 1248 item2 := &PtrInterfaceItem{} 1249 err = NewDecoder(b).Decode(&item2) 1250 if err != nil { 1251 t.Fatal("decode:", err) 1252 } 1253 // Hand test for correct types and values. 1254 if v, ok := item2.Str1.(string); !ok || v != str1 { 1255 t.Errorf("basic string failed: %q should be %q", v, str1) 1256 } 1257 if v, ok := item2.Str2.(String); !ok || v != str2 { 1258 t.Errorf("derived type String failed: %q should be %q", v, str2) 1259 } 1260} 1261 1262func TestIgnoreInterface(t *testing.T) { 1263 iVal := Int(3) 1264 fVal := Float(5) 1265 // Sending a Point will require that the receiver define a type in the middle of 1266 // receiving the value for item2. 1267 pVal := Point{2, 3} 1268 b := new(bytes.Buffer) 1269 item1 := &InterfaceItem{1, iVal, fVal, pVal, 11.5, nil} 1270 // Register the types. 1271 Register(Int(0)) 1272 Register(Float(0)) 1273 Register(Point{}) 1274 err := NewEncoder(b).Encode(item1) 1275 if err != nil { 1276 t.Error("expected no encode error; got", err) 1277 } 1278 1279 item2 := NoInterfaceItem{} 1280 err = NewDecoder(b).Decode(&item2) 1281 if err != nil { 1282 t.Fatal("decode:", err) 1283 } 1284 if item2.I != item1.I { 1285 t.Error("normal int did not decode correctly") 1286 } 1287 if item2.F != item1.F { 1288 t.Error("normal float did not decode correctly") 1289 } 1290} 1291 1292type U struct { 1293 A int 1294 B string 1295 c float64 1296 D uint 1297} 1298 1299func TestUnexportedFields(t *testing.T) { 1300 var u0 U 1301 u0.A = 17 1302 u0.B = "hello" 1303 u0.c = 3.14159 1304 u0.D = 23 1305 b := new(bytes.Buffer) 1306 NewEncoder(b).Encode(u0) 1307 dec := NewDecoder(b) 1308 var u1 U 1309 u1.c = 1234. 1310 err := dec.Decode(&u1) 1311 if err != nil { 1312 t.Fatal("decode error:", err) 1313 } 1314 if u0.A != u1.A || u0.B != u1.B || u0.D != u1.D { 1315 t.Errorf("u1->u0: expected %v; got %v", u0, u1) 1316 } 1317 if u1.c != 1234. { 1318 t.Error("u1.c modified") 1319 } 1320} 1321 1322var singletons = []any{ 1323 true, 1324 7, 1325 uint(10), 1326 3.2, 1327 "hello", 1328 [3]int{11, 22, 33}, 1329 []float32{0.5, 0.25, 0.125}, 1330 map[string]int{"one": 1, "two": 2}, 1331} 1332 1333func TestDebugSingleton(t *testing.T) { 1334 if debugFunc == nil { 1335 return 1336 } 1337 b := new(bytes.Buffer) 1338 // Accumulate a number of values and print them out all at once. 1339 for _, x := range singletons { 1340 err := NewEncoder(b).Encode(x) 1341 if err != nil { 1342 t.Fatal("encode:", err) 1343 } 1344 } 1345 debugFunc(b) 1346} 1347 1348// A type that won't be defined in the gob until we send it in an interface value. 1349type OnTheFly struct { 1350 A int 1351} 1352 1353type DT struct { 1354 // X OnTheFly 1355 A int 1356 B string 1357 C float64 1358 I any 1359 J any 1360 I_nil any 1361 M map[string]int 1362 T [3]int 1363 S []string 1364} 1365 1366func newDT() DT { 1367 var dt DT 1368 dt.A = 17 1369 dt.B = "hello" 1370 dt.C = 3.14159 1371 dt.I = 271828 1372 dt.J = OnTheFly{3} 1373 dt.I_nil = nil 1374 dt.M = map[string]int{"one": 1, "two": 2} 1375 dt.T = [3]int{11, 22, 33} 1376 dt.S = []string{"hi", "joe"} 1377 return dt 1378} 1379 1380func TestDebugStruct(t *testing.T) { 1381 if debugFunc == nil { 1382 return 1383 } 1384 Register(OnTheFly{}) 1385 dt := newDT() 1386 b := new(bytes.Buffer) 1387 err := NewEncoder(b).Encode(dt) 1388 if err != nil { 1389 t.Fatal("encode:", err) 1390 } 1391 debugBuffer := bytes.NewBuffer(b.Bytes()) 1392 dt2 := &DT{} 1393 err = NewDecoder(b).Decode(&dt2) 1394 if err != nil { 1395 t.Error("decode:", err) 1396 } 1397 debugFunc(debugBuffer) 1398} 1399 1400func encFuzzDec(rng *rand.Rand, in any) error { 1401 buf := new(bytes.Buffer) 1402 enc := NewEncoder(buf) 1403 if err := enc.Encode(&in); err != nil { 1404 return err 1405 } 1406 1407 b := buf.Bytes() 1408 for i, bi := range b { 1409 if rng.Intn(10) < 3 { 1410 b[i] = bi + uint8(rng.Intn(256)) 1411 } 1412 } 1413 1414 dec := NewDecoder(buf) 1415 var e any 1416 if err := dec.Decode(&e); err != nil { 1417 return err 1418 } 1419 return nil 1420} 1421 1422// This does some "fuzz testing" by attempting to decode a sequence of random bytes. 1423func TestFuzz(t *testing.T) { 1424 if !*doFuzzTests { 1425 t.Skipf("disabled; run with -gob.fuzz to enable") 1426 } 1427 1428 // all possible inputs 1429 input := []any{ 1430 new(int), 1431 new(float32), 1432 new(float64), 1433 new(complex128), 1434 &ByteStruct{255}, 1435 &ArrayStruct{}, 1436 &StringStruct{"hello"}, 1437 &GobTest1{0, &StringStruct{"hello"}}, 1438 } 1439 testFuzz(t, time.Now().UnixNano(), 100, input...) 1440} 1441 1442func TestFuzzRegressions(t *testing.T) { 1443 if !*doFuzzTests { 1444 t.Skipf("disabled; run with -gob.fuzz to enable") 1445 } 1446 1447 // An instance triggering a type name of length ~102 GB. 1448 testFuzz(t, 1328492090837718000, 100, new(float32)) 1449 // An instance triggering a type name of 1.6 GB. 1450 // Note: can take several minutes to run. 1451 testFuzz(t, 1330522872628565000, 100, new(int)) 1452} 1453 1454func testFuzz(t *testing.T, seed int64, n int, input ...any) { 1455 for _, e := range input { 1456 t.Logf("seed=%d n=%d e=%T", seed, n, e) 1457 rng := rand.New(rand.NewSource(seed)) 1458 for i := 0; i < n; i++ { 1459 encFuzzDec(rng, e) 1460 } 1461 } 1462} 1463 1464// TestFuzzOneByte tries to decode corrupted input sequences 1465// and checks that no panic occurs. 1466func TestFuzzOneByte(t *testing.T) { 1467 if !*doFuzzTests { 1468 t.Skipf("disabled; run with -gob.fuzz to enable") 1469 } 1470 1471 buf := new(strings.Builder) 1472 Register(OnTheFly{}) 1473 dt := newDT() 1474 if err := NewEncoder(buf).Encode(dt); err != nil { 1475 t.Fatal(err) 1476 } 1477 s := buf.String() 1478 1479 indices := make([]int, 0, len(s)) 1480 for i := 0; i < len(s); i++ { 1481 switch i { 1482 case 14, 167, 231, 265: // a slice length, corruptions are not handled yet. 1483 continue 1484 case 248: 1485 // Large map size, which currently causes an out of memory panic. 1486 // See golang.org/issue/24308 and golang.org/issue/20221. 1487 continue 1488 } 1489 indices = append(indices, i) 1490 } 1491 if testing.Short() { 1492 indices = []int{1, 111, 178} // known fixed panics 1493 } 1494 for _, i := range indices { 1495 for j := 0; j < 256; j += 3 { 1496 b := []byte(s) 1497 b[i] ^= byte(j) 1498 var e DT 1499 func() { 1500 defer func() { 1501 if p := recover(); p != nil { 1502 t.Errorf("crash for b[%d] ^= 0x%x", i, j) 1503 panic(p) 1504 } 1505 }() 1506 err := NewDecoder(bytes.NewReader(b)).Decode(&e) 1507 _ = err 1508 }() 1509 } 1510 } 1511} 1512 1513// Don't crash, just give error with invalid type id. 1514// Issue 9649. 1515func TestErrorInvalidTypeId(t *testing.T) { 1516 data := []byte{0x01, 0x00, 0x01, 0x00} 1517 d := NewDecoder(bytes.NewReader(data)) 1518 // When running d.Decode(&foo) the first time the decoder stops 1519 // after []byte{0x01, 0x00} and reports an errBadType. Running 1520 // d.Decode(&foo) again on exactly the same input sequence should 1521 // give another errBadType, but instead caused a panic because 1522 // decoderMap wasn't cleaned up properly after the first error. 1523 for i := 0; i < 2; i++ { 1524 var foo struct{} 1525 err := d.Decode(&foo) 1526 if err != errBadType { 1527 t.Fatalf("decode: expected %s, got %s", errBadType, err) 1528 } 1529 } 1530} 1531 1532type LargeSliceByte struct { 1533 S []byte 1534} 1535 1536type LargeSliceInt8 struct { 1537 S []int8 1538} 1539 1540type StringPair struct { 1541 A, B string 1542} 1543 1544type LargeSliceStruct struct { 1545 S []StringPair 1546} 1547 1548type LargeSliceString struct { 1549 S []string 1550} 1551 1552func testEncodeDecode(t *testing.T, in, out any) { 1553 t.Helper() 1554 var b bytes.Buffer 1555 err := NewEncoder(&b).Encode(in) 1556 if err != nil { 1557 t.Fatal("encode:", err) 1558 } 1559 err = NewDecoder(&b).Decode(out) 1560 if err != nil { 1561 t.Fatal("decode:", err) 1562 } 1563 if !reflect.DeepEqual(in, out) { 1564 t.Errorf("output mismatch") 1565 } 1566} 1567 1568func TestLargeSlice(t *testing.T) { 1569 t.Run("byte", func(t *testing.T) { 1570 if unsafe.Sizeof(uintptr(0)) > 4 { 1571 t.Parallel() // Only run in parallel in a large address space 1572 } 1573 s := make([]byte, 10<<21) 1574 for i := range s { 1575 s[i] = byte(i) 1576 } 1577 st := &LargeSliceByte{S: s} 1578 rt := &LargeSliceByte{} 1579 testEncodeDecode(t, st, rt) 1580 }) 1581 t.Run("int8", func(t *testing.T) { 1582 if unsafe.Sizeof(uintptr(0)) > 4 { 1583 t.Parallel() 1584 } 1585 s := make([]int8, 10<<21) 1586 for i := range s { 1587 s[i] = int8(i) 1588 } 1589 st := &LargeSliceInt8{S: s} 1590 rt := &LargeSliceInt8{} 1591 testEncodeDecode(t, st, rt) 1592 }) 1593 t.Run("struct", func(t *testing.T) { 1594 if unsafe.Sizeof(uintptr(0)) > 4 { 1595 t.Parallel() 1596 } 1597 s := make([]StringPair, 1<<21) 1598 for i := range s { 1599 s[i].A = string(rune(i)) 1600 s[i].B = s[i].A 1601 } 1602 st := &LargeSliceStruct{S: s} 1603 rt := &LargeSliceStruct{} 1604 testEncodeDecode(t, st, rt) 1605 }) 1606 t.Run("string", func(t *testing.T) { 1607 if unsafe.Sizeof(uintptr(0)) > 4 { 1608 t.Parallel() 1609 } 1610 s := make([]string, 1<<21) 1611 for i := range s { 1612 s[i] = string(rune(i)) 1613 } 1614 st := &LargeSliceString{S: s} 1615 rt := &LargeSliceString{} 1616 testEncodeDecode(t, st, rt) 1617 }) 1618} 1619 1620func TestLocalRemoteTypesMismatch(t *testing.T) { 1621 // Test data is from https://go.dev/issue/62117. 1622 testData := []byte{9, 127, 3, 1, 2, 255, 128, 0, 0, 0, 3, 255, 128, 0} 1623 1624 var v []*struct{} 1625 buf := bytes.NewBuffer(testData) 1626 err := NewDecoder(buf).Decode(&v) 1627 if err == nil { 1628 t.Error("Encode/Decode: expected error but got err == nil") 1629 } 1630} 1631