forked from lunixbochs/struc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields_test.go
81 lines (70 loc) · 1.39 KB
/
fields_test.go
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
package struc
import (
"bytes"
"reflect"
"testing"
)
var refVal = reflect.ValueOf(reference)
func TestFieldsParse(t *testing.T) {
if _, err := parseFields(refVal); err != nil {
t.Fatal(err)
}
}
func TestFieldsString(t *testing.T) {
fields, _ := parseFields(refVal)
fields.String()
}
type sizefromStruct struct {
Size1 uint `struc:"sizeof=Var1"`
Var1 []byte
Size2 int `struc:"sizeof=Var2"`
Var2 []byte
}
func TestFieldsSizefrom(t *testing.T) {
var test = sizefromStruct{
Var1: []byte{1, 2, 3},
Var2: []byte{4, 5, 6},
}
var buf bytes.Buffer
err := Pack(&buf, &test)
if err != nil {
t.Fatal(err)
}
err = Unpack(&buf, &test)
if err != nil {
t.Fatal(err)
}
}
type sizefromStructBad struct {
Size1 string `struc:"sizeof=Var1"`
Var1 []byte
}
func TestFieldsSizefromBad(t *testing.T) {
var test = &sizefromStructBad{Var1: []byte{1, 2, 3}}
var buf bytes.Buffer
defer func() {
if err := recover(); err == nil {
t.Fatal("failed to panic on bad sizeof type")
}
}()
Pack(&buf, &test)
}
type StructWithinArray struct {
a uint32
}
type StructHavingArray struct {
Props [1]StructWithinArray `struc:"[1]StructWithinArray"`
}
func TestStrucArray(t *testing.T) {
var buf bytes.Buffer
a := &StructHavingArray{[1]StructWithinArray{}}
err := Pack(&buf, a)
if err != nil {
t.Fatal(err)
}
b := &StructHavingArray{}
err = Unpack(&buf, b)
if err != nil {
t.Fatal(err)
}
}