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