This file is indexed.

/usr/share/gocode/src/github.com/tendermint/go-wire/reflect_test.go is in golang-github-tendermint-go-wire-dev 0~20161027~0git287d8ca-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
package wire

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"reflect"
	"testing"
	"time"

	. "github.com/tendermint/go-common"
)

type SimpleStruct struct {
	String string
	Bytes  []byte
	Time   time.Time
}

type Animal interface{}

// Implements Animal
type Cat struct {
	SimpleStruct
}

// Implements Animal
type Dog struct {
	SimpleStruct
}

// Implements Animal
type Snake []byte

// Implements Animal
type Viper struct {
	Bytes []byte
}

var _ = RegisterInterface(
	struct{ Animal }{},
	ConcreteType{Cat{}, 0x01},
	ConcreteType{Dog{}, 0x02},
	ConcreteType{Snake{}, 0x03},
	ConcreteType{&Viper{}, 0x04},
)

// TODO: add assertions here ...
func TestAnimalInterface(t *testing.T) {
	var foo Animal

	// Type of pointer to Animal
	rt := reflect.TypeOf(&foo)
	fmt.Printf("rt: %v\n", rt)

	// Type of Animal itself.
	// NOTE: normally this is acquired through other means
	// like introspecting on method signatures, or struct fields.
	rte := rt.Elem()
	fmt.Printf("rte: %v\n", rte)

	// Get a new pointer to the interface
	// NOTE: calling .Interface() is to get the actual value,
	// instead of reflection values.
	ptr := reflect.New(rte).Interface()
	fmt.Printf("ptr: %v", ptr)

	// Make a binary byteslice that represents a *snake.
	foo = Snake([]byte("snake"))
	snakeBytes := BinaryBytes(foo)
	snakeReader := bytes.NewReader(snakeBytes)

	// Now you can read it.
	n, err := new(int), new(error)
	it := ReadBinary(foo, snakeReader, 0, n, err).(Animal)
	fmt.Println(it, reflect.TypeOf(it))
}

//-------------------------------------

type Constructor func() interface{}
type Instantiator func() (o interface{}, ptr interface{})
type Validator func(o interface{}, t *testing.T)

type TestCase struct {
	Constructor
	Instantiator
	Validator
}

//-------------------------------------

func constructBasic() interface{} {
	cat := Cat{
		SimpleStruct{
			String: "String",
			Bytes:  []byte("Bytes"),
			Time:   time.Unix(123, 456789999),
		},
	}
	return cat
}

func instantiateBasic() (interface{}, interface{}) {
	return Cat{}, &Cat{}
}

func validateBasic(o interface{}, t *testing.T) {
	cat := o.(Cat)
	if cat.String != "String" {
		t.Errorf("Expected cat.String == 'String', got %v", cat.String)
	}
	if string(cat.Bytes) != "Bytes" {
		t.Errorf("Expected cat.Bytes == 'Bytes', got %X", cat.Bytes)
	}
	if cat.Time.UnixNano() != 123456000000 { // Only milliseconds
		t.Errorf("Expected cat.Time.UnixNano() == 123456000000, got %v", cat.Time.UnixNano())
	}
}

//-------------------------------------

type NilTestStruct struct {
	IntPtr *int
	CatPtr *Cat
	Animal Animal
}

func constructNilTestStruct() interface{} {
	return NilTestStruct{}
}

func instantiateNilTestStruct() (interface{}, interface{}) {
	return NilTestStruct{}, &NilTestStruct{}
}

func validateNilTestStruct(o interface{}, t *testing.T) {
	nts := o.(NilTestStruct)
	if nts.IntPtr != nil {
		t.Errorf("Expected nts.IntPtr to be nil, got %v", nts.IntPtr)
	}
	if nts.CatPtr != nil {
		t.Errorf("Expected nts.CatPtr to be nil, got %v", nts.CatPtr)
	}
	if nts.Animal != nil {
		t.Errorf("Expected nts.Animal to be nil, got %v", nts.Animal)
	}
}

//-------------------------------------

type ComplexStruct struct {
	Name   string
	Animal Animal
}

func constructComplex() interface{} {
	c := ComplexStruct{
		Name:   "Complex",
		Animal: constructBasic(),
	}
	return c
}

func instantiateComplex() (interface{}, interface{}) {
	return ComplexStruct{}, &ComplexStruct{}
}

func validateComplex(o interface{}, t *testing.T) {
	c2 := o.(ComplexStruct)
	if cat, ok := c2.Animal.(Cat); ok {
		validateBasic(cat, t)
	} else {
		t.Errorf("Expected c2.Animal to be of type cat, got %v", reflect.ValueOf(c2.Animal).Elem().Type())
	}
}

//-------------------------------------

type ComplexStruct2 struct {
	Cat    Cat
	Dog    *Dog
	Snake  Snake
	Snake2 *Snake
	Viper  Viper
	Viper2 *Viper
}

func constructComplex2() interface{} {
	snake_ := Snake([]byte("hiss"))
	snakePtr_ := &snake_

	c := ComplexStruct2{
		Cat: Cat{
			SimpleStruct{
				String: "String",
				Bytes:  []byte("Bytes"),
			},
		},
		Dog: &Dog{
			SimpleStruct{
				String: "Woof",
				Bytes:  []byte("Bark"),
			},
		},
		Snake:  Snake([]byte("hiss")),
		Snake2: snakePtr_,
		Viper:  Viper{Bytes: []byte("hizz")},
		Viper2: &Viper{Bytes: []byte("hizz")},
	}
	return c
}

func instantiateComplex2() (interface{}, interface{}) {
	return ComplexStruct2{}, &ComplexStruct2{}
}

func validateComplex2(o interface{}, t *testing.T) {
	c2 := o.(ComplexStruct2)
	cat := c2.Cat
	if cat.String != "String" {
		t.Errorf("Expected cat.String == 'String', got %v", cat.String)
	}
	if string(cat.Bytes) != "Bytes" {
		t.Errorf("Expected cat.Bytes == 'Bytes', got %X", cat.Bytes)
	}

	dog := c2.Dog
	if dog.String != "Woof" {
		t.Errorf("Expected dog.String == 'Woof', got %v", dog.String)
	}
	if string(dog.Bytes) != "Bark" {
		t.Errorf("Expected dog.Bytes == 'Bark', got %X", dog.Bytes)
	}

	snake := c2.Snake
	if string(snake) != "hiss" {
		t.Errorf("Expected string(snake) == 'hiss', got %v", string(snake))
	}

	snake2 := c2.Snake2
	if string(*snake2) != "hiss" {
		t.Errorf("Expected string(snake2) == 'hiss', got %v", string(*snake2))
	}

	viper := c2.Viper
	if string(viper.Bytes) != "hizz" {
		t.Errorf("Expected string(viper.Bytes) == 'hizz', got %v", string(viper.Bytes))
	}

	viper2 := c2.Viper2
	if string(viper2.Bytes) != "hizz" {
		t.Errorf("Expected string(viper2.Bytes) == 'hizz', got %v", string(viper2.Bytes))
	}
}

//-------------------------------------

type ComplexStructArray struct {
	Animals []Animal
	Bytes   [5]byte
	Ints    [5]int
	Array   SimpleArray
}

func constructComplexArray() interface{} {
	c := ComplexStructArray{
		Animals: []Animal{
			Cat{
				SimpleStruct{
					String: "String",
					Bytes:  []byte("Bytes"),
				},
			},
			Dog{
				SimpleStruct{
					String: "Woof",
					Bytes:  []byte("Bark"),
				},
			},
			Snake([]byte("hiss")),
			&Viper{
				Bytes: []byte("hizz"),
			},
		},
		Bytes: [5]byte{1, 10, 50, 100, 200},
		Ints:  [5]int{1, 2, 3, 4, 5},
		Array: SimpleArray([5]byte{1, 10, 50, 100, 200}),
	}
	return c
}

func instantiateComplexArray() (interface{}, interface{}) {
	return ComplexStructArray{}, &ComplexStructArray{}
}

func validateComplexArray(o interface{}, t *testing.T) {
	c2 := o.(ComplexStructArray)
	if cat, ok := c2.Animals[0].(Cat); ok {
		if cat.String != "String" {
			t.Errorf("Expected cat.String == 'String', got %v", cat.String)
		}
		if string(cat.Bytes) != "Bytes" {
			t.Errorf("Expected cat.Bytes == 'Bytes', got %X", cat.Bytes)
		}
	} else {
		t.Errorf("Expected c2.Animals[0] to be of type cat, got %v", reflect.ValueOf(c2.Animals[0]).Elem().Type())
	}

	if dog, ok := c2.Animals[1].(Dog); ok {
		if dog.String != "Woof" {
			t.Errorf("Expected dog.String == 'Woof', got %v", dog.String)
		}
		if string(dog.Bytes) != "Bark" {
			t.Errorf("Expected dog.Bytes == 'Bark', got %X", dog.Bytes)
		}
	} else {
		t.Errorf("Expected c2.Animals[1] to be of type dog, got %v", reflect.ValueOf(c2.Animals[1]).Elem().Type())
	}

	if snake, ok := c2.Animals[2].(Snake); ok {
		if string(snake) != "hiss" {
			t.Errorf("Expected string(snake) == 'hiss', got %v", string(snake))
		}
	} else {
		t.Errorf("Expected c2.Animals[2] to be of type Snake, got %v", reflect.ValueOf(c2.Animals[2]).Elem().Type())
	}

	if viper, ok := c2.Animals[3].(*Viper); ok {
		if string(viper.Bytes) != "hizz" {
			t.Errorf("Expected string(viper.Bytes) == 'hizz', got %v", string(viper.Bytes))
		}
	} else {
		t.Errorf("Expected c2.Animals[3] to be of type *Viper, got %v", reflect.ValueOf(c2.Animals[3]).Elem().Type())
	}
}

//-----------------------------------------------------------------------------

var testCases = []TestCase{}

func init() {
	testCases = append(testCases, TestCase{constructBasic, instantiateBasic, validateBasic})
	testCases = append(testCases, TestCase{constructComplex, instantiateComplex, validateComplex})
	testCases = append(testCases, TestCase{constructComplex2, instantiateComplex2, validateComplex2})
	testCases = append(testCases, TestCase{constructComplexArray, instantiateComplexArray, validateComplexArray})
	testCases = append(testCases, TestCase{constructNilTestStruct, instantiateNilTestStruct, validateNilTestStruct})
}

func TestBinary(t *testing.T) {

	for i, testCase := range testCases {

		t.Log(fmt.Sprintf("Running test case %v", i))

		// Construct an object
		o := testCase.Constructor()

		// Write the object
		data := BinaryBytes(o)
		t.Logf("Binary: %X", data)

		instance, instancePtr := testCase.Instantiator()

		// Read onto a struct
		n, err := new(int), new(error)
		res := ReadBinary(instance, bytes.NewReader(data), 0, n, err)
		if *err != nil {
			t.Fatalf("Failed to read into instance: %v", *err)
		}

		// Validate object
		testCase.Validator(res, t)

		// Read onto a pointer
		n, err = new(int), new(error)
		res = ReadBinaryPtr(instancePtr, bytes.NewReader(data), 0, n, err)
		if *err != nil {
			t.Fatalf("Failed to read into instance: %v", *err)
		}
		if res != instancePtr {
			t.Errorf("Expected pointer to pass through")
		}

		// Validate object
		testCase.Validator(reflect.ValueOf(res).Elem().Interface(), t)

		// Read with len(data)-1 limit should fail.
		instance, _ = testCase.Instantiator()
		n, err = new(int), new(error)
		ReadBinary(instance, bytes.NewReader(data), len(data)-1, n, err)
		if *err != ErrBinaryReadOverflow {
			t.Fatalf("Expected ErrBinaryReadOverflow")
		}

		// Read with len(data) limit should succeed.
		instance, _ = testCase.Instantiator()
		n, err = new(int), new(error)
		ReadBinary(instance, bytes.NewReader(data), len(data), n, err)
		if *err != nil {
			t.Fatalf("Failed to read instance with sufficient limit: %v", (*err).Error(), *n, len(data), reflect.TypeOf(instance))
		}
	}

}

func TestJSON(t *testing.T) {

	for i, testCase := range testCases {

		t.Log(fmt.Sprintf("Running test case %v", i))

		// Construct an object
		o := testCase.Constructor()

		// Write the object
		data := JSONBytes(o)
		t.Logf("JSON: %v", string(data))

		instance, instancePtr := testCase.Instantiator()

		// Read onto a struct
		err := new(error)
		res := ReadJSON(instance, data, err)
		if *err != nil {
			t.Fatalf("Failed to read cat: %v", *err)
		}

		// Validate object
		testCase.Validator(res, t)

		// Read onto a pointer
		res = ReadJSON(instancePtr, data, err)
		if *err != nil {
			t.Fatalf("Failed to read cat: %v", *err)
		}

		if res != instancePtr {
			t.Errorf("Expected pointer to pass through")
		}

		// Validate object
		testCase.Validator(reflect.ValueOf(res).Elem().Interface(), t)
	}

}

//------------------------------------------------------------------------------

type Foo struct {
	FieldA string `json:"fieldA"` // json field name is "fieldA"
	FieldB string // json field name is "FieldB"
	fieldC string // not exported, not serialized.
	FieldD string `json:",omitempty"`  // omit if empty
	FieldE string `json:",omitempty"`  // omit if empty (but won't be)
	FieldF string `json:"F,omitempty"` // its name is "F", omit if empty
	FieldG string `json:"G,omitempty"` // its name is "F", omit if empty (but won't be)
}

func TestJSONFieldNames(t *testing.T) {
	for i := 0; i < 20; i++ { // Try to ensure deterministic success.
		foo := Foo{
			FieldA: "a",
			FieldB: "b",
			fieldC: "c",
			FieldD: "",  // omit because empty
			FieldE: "e", // no omit, not empty
			FieldF: "",  // omit because empty
			FieldG: "g", // no omit, not empty
		}
		stringified := string(JSONBytes(foo))
		expected := `{"fieldA":"a","FieldB":"b","FieldE":"e","G":"g"}`
		if stringified != expected {
			t.Fatalf("JSONFieldNames error: expected %v, got %v",
				expected, stringified)
		}
	}
}

//------------------------------------------------------------------------------

func TestBadAlloc(t *testing.T) {
	n, err := new(int), new(error)
	instance := new([]byte)
	data := RandBytes(100 * 1024)
	b := new(bytes.Buffer)
	// this slice of data claims to be much bigger than it really is
	WriteUvarint(uint(1<<32-1), b, n, err)
	b.Write(data)
	res := ReadBinary(instance, b, 0, n, err)
	fmt.Println(res, *err)
}

//------------------------------------------------------------------------------

type SimpleArray [5]byte

func TestSimpleArray(t *testing.T) {
	var foo SimpleArray

	// Type of pointer to array
	rt := reflect.TypeOf(&foo)
	fmt.Printf("rt: %v\n", rt) // *binary.SimpleArray

	// Type of array itself.
	// NOTE: normally this is acquired through other means
	// like introspecting on method signatures, or struct fields.
	rte := rt.Elem()
	fmt.Printf("rte: %v\n", rte) // binary.SimpleArray

	// Get a new pointer to the array
	// NOTE: calling .Interface() is to get the actual value,
	// instead of reflection values.
	ptr := reflect.New(rte).Interface()
	fmt.Printf("ptr: %v\n", ptr) // &[0 0 0 0 0]

	// Make a simple int aray
	fooArray := SimpleArray([5]byte{1, 10, 50, 100, 200})
	fooBytes := BinaryBytes(fooArray)
	fooReader := bytes.NewReader(fooBytes)

	// Now you can read it.
	n, err := new(int), new(error)
	it := ReadBinary(foo, fooReader, 0, n, err).(SimpleArray)

	if !bytes.Equal(it[:], fooArray[:]) {
		t.Errorf("Expected %v but got %v", fooArray, it)
	}
}

//--------------------------------------------------------------------------------

func TestNilPointerInterface(t *testing.T) {

	type MyInterface interface{}
	type MyConcreteStruct1 struct{}
	type MyConcreteStruct2 struct{}

	RegisterInterface(
		struct{ MyInterface }{},
		ConcreteType{&MyConcreteStruct1{}, 0x01},
		ConcreteType{&MyConcreteStruct2{}, 0x02},
	)

	type MyStruct struct {
		MyInterface
	}

	myStruct := MyStruct{(*MyConcreteStruct1)(nil)}
	buf, n, err := new(bytes.Buffer), int(0), error(nil)
	WriteBinary(myStruct, buf, &n, &err)
	if err == nil {
		t.Error("Expected error in writing nil pointer interface")
	}

	myStruct = MyStruct{&MyConcreteStruct1{}}
	buf, n, err = new(bytes.Buffer), int(0), error(nil)
	WriteBinary(myStruct, buf, &n, &err)
	if err != nil {
		t.Error("Unexpected error", err)
	}

}

//--------------------------------------------------------------------------------

func TestMultipleInterfaces(t *testing.T) {

	type MyInterface1 interface{}
	type MyInterface2 interface{}
	type Struct1 struct{}
	type Struct2 struct{}

	RegisterInterface(
		struct{ MyInterface1 }{},
		ConcreteType{&Struct1{}, 0x01},
		ConcreteType{&Struct2{}, 0x02},
		ConcreteType{Struct1{}, 0x03},
		ConcreteType{Struct2{}, 0x04},
	)
	RegisterInterface(
		struct{ MyInterface2 }{},
		ConcreteType{&Struct1{}, 0x11},
		ConcreteType{&Struct2{}, 0x12},
		ConcreteType{Struct1{}, 0x13},
		ConcreteType{Struct2{}, 0x14},
	)

	type MyStruct struct {
		F1 []MyInterface1
		F2 []MyInterface2
	}

	myStruct := MyStruct{
		F1: []MyInterface1{
			nil,
			&Struct1{},
			&Struct2{},
			Struct1{},
			Struct2{},
		},
		F2: []MyInterface2{
			nil,
			&Struct1{},
			&Struct2{},
			Struct1{},
			Struct2{},
		},
	}
	buf, n, err := new(bytes.Buffer), int(0), error(nil)
	WriteBinary(myStruct, buf, &n, &err)
	if err != nil {
		t.Error("Unexpected error", err)
	}
	if hexStr := hex.EncodeToString(buf.Bytes()); hexStr !=
		"0105"+"0001020304"+"0105"+"0011121314" {
		t.Error("Unexpected binary bytes", hexStr)
	}

	// Now, read

	myStruct2 := MyStruct{}
	ReadBinaryPtr(&myStruct2, buf, 0, &n, &err)
	if err != nil {
		t.Error("Unexpected error", err)
	}

	if len(myStruct2.F1) != 5 {
		t.Error("Expected F1 to have 5 items")
	}
	if myStruct2.F1[0] != nil {
		t.Error("Expected F1[0] to be nil")
	}
	if _, ok := (myStruct2.F1[1]).(*Struct1); !ok {
		t.Error("Expected F1[1] to be of type *Struct1")
	}
	if s, _ := (myStruct2.F1[1]).(*Struct1); s == nil {
		t.Error("Expected F1[1] to be of type *Struct1 but not nil")
	}
	if _, ok := (myStruct2.F1[2]).(*Struct2); !ok {
		t.Error("Expected F1[2] to be of type *Struct2")
	}
	if s, _ := (myStruct2.F1[2]).(*Struct2); s == nil {
		t.Error("Expected F1[2] to be of type *Struct2 but not nil")
	}
	if _, ok := (myStruct2.F1[3]).(Struct1); !ok {
		t.Error("Expected F1[3] to be of type Struct1")
	}
	if _, ok := (myStruct2.F1[4]).(Struct2); !ok {
		t.Error("Expected F1[4] to be of type Struct2")
	}
	if myStruct2.F2[0] != nil {
		t.Error("Expected F2[0] to be nil")
	}
	if _, ok := (myStruct2.F2[1]).(*Struct1); !ok {
		t.Error("Expected F2[1] to be of type *Struct1")
	}
	if s, _ := (myStruct2.F2[1]).(*Struct1); s == nil {
		t.Error("Expected F2[1] to be of type *Struct1 but not nil")
	}
	if _, ok := (myStruct2.F2[2]).(*Struct2); !ok {
		t.Error("Expected F2[2] to be of type *Struct2")
	}
	if s, _ := (myStruct2.F2[2]).(*Struct2); s == nil {
		t.Error("Expected F2[2] to be of type *Struct2 but not nil")
	}
	if _, ok := (myStruct2.F2[3]).(Struct1); !ok {
		t.Error("Expected F2[3] to be of type Struct1")
	}
	if _, ok := (myStruct2.F2[4]).(Struct2); !ok {
		t.Error("Expected F2[4] to be of type Struct2")
	}

}

//--------------------------------------------------------------------------------

func TestPointers(t *testing.T) {

	type Struct1 struct {
		Foo int
	}

	type MyStruct struct {
		F1 *Struct1
		F2 *Struct1
	}

	myStruct := MyStruct{
		F1: nil,
		F2: &Struct1{8},
	}
	buf, n, err := new(bytes.Buffer), int(0), error(nil)
	WriteBinary(myStruct, buf, &n, &err)
	if err != nil {
		t.Error("Unexpected error", err)
	}
	if hexStr := hex.EncodeToString(buf.Bytes()); hexStr !=
		"00"+"010108" {
		t.Error("Unexpected binary bytes", hexStr)
	}

	// Now, read

	myStruct2 := MyStruct{}
	ReadBinaryPtr(&myStruct2, buf, 0, &n, &err)
	if err != nil {
		t.Error("Unexpected error", err)
	}

	if myStruct2.F1 != nil {
		t.Error("Expected F1 to be nil")
	}
	if myStruct2.F2.Foo != 8 {
		t.Error("Expected F2.Foo to be 8")
	}

}

//--------------------------------------------------------------------------------

func TestUnsafe(t *testing.T) {

	type Struct1 struct {
		Foo float64
	}

	type Struct2 struct {
		Foo float64 `wire:"unsafe"`
	}

	myStruct := Struct1{5.32}
	myStruct2 := Struct2{5.32}
	buf, n, err := new(bytes.Buffer), int(0), error(nil)
	WriteBinary(myStruct, buf, &n, &err)
	if err == nil {
		t.Error("Expected error due to float without `unsafe`")
	}

	buf, n, err = new(bytes.Buffer), int(0), error(nil)
	WriteBinary(myStruct2, buf, &n, &err)
	if err != nil {
		t.Error("Unexpected error", err)
	}

	var s Struct2
	n, err = int(0), error(nil)
	ReadBinaryPtr(&s, buf, 0, &n, &err)
	if err != nil {
		t.Error("Unexpected error", err)
	}

	if s.Foo != myStruct2.Foo {
		t.Error("Expected float values to be the same. Got", s.Foo, "expected", myStruct2.Foo)
	}

}

//--------------------------------------------------------------------------------

func TestUnwrap(t *testing.T) {

	type Result interface{}
	type ConcreteResult struct{ A int }
	RegisterInterface(
		struct{ Result }{},
		ConcreteType{&ConcreteResult{}, 0x01},
	)

	type Struct1 struct {
		Result Result `json:"unwrap"`
		other  string // this should be ignored, it is unexported
		Other  string `json:"-"` // this should also be ignored
	}

	myStruct := Struct1{Result: &ConcreteResult{5}}
	buf, n, err := new(bytes.Buffer), int(0), error(nil)
	WriteJSON(myStruct, buf, &n, &err)
	if err != nil {
		t.Error("Unexpected error", err)
	}
	jsonBytes := buf.Bytes()
	if string(jsonBytes) != `[1,{"A":5}]` {
		t.Error("Unexpected jsonBytes", string(jsonBytes))
	}

	var s Struct1
	err = error(nil)
	ReadJSON(&s, jsonBytes, &err)
	if err != nil {
		t.Error("Unexpected error", err)
	}

	sConcrete, ok := s.Result.(*ConcreteResult)
	if !ok {
		t.Error("Expected struct result to be of type ConcreteResult. Got", reflect.TypeOf(s.Result))
	}

	got := sConcrete.A
	expected := myStruct.Result.(*ConcreteResult).A
	if got != expected {
		t.Error("Expected values to match. Got", got, "expected", expected)
	}

}