diff options
author | Ondrej Fabry <ofabry@cisco.com> | 2019-01-10 10:57:50 +0100 |
---|---|---|
committer | Ondrej Fabry <ofabry@cisco.com> | 2019-01-10 11:05:35 +0100 |
commit | 08266e35878f198e2fa59fcfc9f0fc3a4b1dfbf5 (patch) | |
tree | 1269acfc3bf6fdd47414eb64da3ecad4865e37d6 /vendor/github.com/lunixbochs/struc | |
parent | 3ef6f210edcf7dd753733d46ec3f2dd5dc795b61 (diff) |
Add support for string types
- strings are now generated as two fields for length and string itself
- aliases are now sorted by name to prevent generating different code
- dependencies are now managed by dep
- binapi files are regenerated using VPP 19.01-rc0~622-g7b01e9e8
- old stats binary api has been deprecated and removed from VPP
Change-Id: Ieb8515c73021339a45f407386f8e3d87dcf4469e
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'vendor/github.com/lunixbochs/struc')
-rw-r--r-- | vendor/github.com/lunixbochs/struc/bench_test.go | 165 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/custom_float16_test.go | 56 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/custom_test.go | 97 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/field.go | 16 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/field_test.go | 77 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/fields_test.go | 59 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/packable_test.go | 123 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/parse.go | 21 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/parse_test.go | 62 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/struc_test.go | 200 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/types_test.go | 53 |
11 files changed, 28 insertions, 901 deletions
diff --git a/vendor/github.com/lunixbochs/struc/bench_test.go b/vendor/github.com/lunixbochs/struc/bench_test.go deleted file mode 100644 index d73c110..0000000 --- a/vendor/github.com/lunixbochs/struc/bench_test.go +++ /dev/null @@ -1,165 +0,0 @@ -package struc - -import ( - "bytes" - "encoding/binary" - "testing" -) - -type BenchExample struct { - Test [5]byte - A int32 - B, C, D int16 - Test2 [4]byte - Length int32 -} - -type BenchStrucExample struct { - Test [5]byte `struc:"[5]byte"` - A int `struc:"int32"` - B, C, D int `struc:"int16"` - Test2 [4]byte `struc:"[4]byte"` - Length int `struc:"int32,sizeof=Data"` - Data []byte -} - -var benchRef = &BenchExample{ - [5]byte{1, 2, 3, 4, 5}, - 1, 2, 3, 4, - [4]byte{1, 2, 3, 4}, - 8, -} - -var eightBytes = []byte("8bytestr") - -var benchStrucRef = &BenchStrucExample{ - [5]byte{1, 2, 3, 4, 5}, - 1, 2, 3, 4, - [4]byte{1, 2, 3, 4}, - 8, eightBytes, -} - -func BenchmarkEncode(b *testing.B) { - for i := 0; i < b.N; i++ { - var buf bytes.Buffer - err := Pack(&buf, benchStrucRef) - if err != nil { - b.Fatal(err) - } - } -} - -func BenchmarkStdlibEncode(b *testing.B) { - for i := 0; i < b.N; i++ { - var buf bytes.Buffer - err := binary.Write(&buf, binary.BigEndian, benchRef) - if err != nil { - b.Fatal(err) - } - _, err = buf.Write(eightBytes) - if err != nil { - b.Fatal(err) - } - } -} - -func BenchmarkManualEncode(b *testing.B) { - order := binary.BigEndian - s := benchStrucRef - for i := 0; i < b.N; i++ { - var buf bytes.Buffer - tmp := make([]byte, 29) - copy(tmp[0:5], s.Test[:]) - order.PutUint32(tmp[5:9], uint32(s.A)) - order.PutUint16(tmp[9:11], uint16(s.B)) - order.PutUint16(tmp[11:13], uint16(s.C)) - order.PutUint16(tmp[13:15], uint16(s.D)) - copy(tmp[15:19], s.Test2[:]) - order.PutUint32(tmp[19:23], uint32(s.Length)) - copy(tmp[23:], s.Data) - _, err := buf.Write(tmp) - if err != nil { - b.Fatal(err) - } - } -} - -func BenchmarkDecode(b *testing.B) { - var out BenchStrucExample - var buf bytes.Buffer - if err := Pack(&buf, benchStrucRef); err != nil { - b.Fatal(err) - } - bufBytes := buf.Bytes() - for i := 0; i < b.N; i++ { - buf := bytes.NewReader(bufBytes) - err := Unpack(buf, &out) - if err != nil { - b.Fatal(err) - } - out.Data = nil - } -} - -func BenchmarkStdlibDecode(b *testing.B) { - var out BenchExample - var buf bytes.Buffer - binary.Write(&buf, binary.BigEndian, *benchRef) - _, err := buf.Write(eightBytes) - if err != nil { - b.Fatal(err) - } - bufBytes := buf.Bytes() - for i := 0; i < b.N; i++ { - buf := bytes.NewReader(bufBytes) - err := binary.Read(buf, binary.BigEndian, &out) - if err != nil { - b.Fatal(err) - } - tmp := make([]byte, out.Length) - _, err = buf.Read(tmp) - if err != nil { - b.Fatal(err) - } - } -} - -func BenchmarkManualDecode(b *testing.B) { - var o BenchStrucExample - var buf bytes.Buffer - if err := Pack(&buf, benchStrucRef); err != nil { - b.Fatal(err) - } - tmp := buf.Bytes() - order := binary.BigEndian - for i := 0; i < b.N; i++ { - copy(o.Test[:], tmp[0:5]) - o.A = int(order.Uint32(tmp[5:9])) - o.B = int(order.Uint16(tmp[9:11])) - o.C = int(order.Uint16(tmp[11:13])) - o.D = int(order.Uint16(tmp[13:15])) - copy(o.Test2[:], tmp[15:19]) - o.Length = int(order.Uint32(tmp[19:23])) - o.Data = make([]byte, o.Length) - copy(o.Data, tmp[23:]) - } -} - -func BenchmarkFullEncode(b *testing.B) { - for i := 0; i < b.N; i++ { - var buf bytes.Buffer - if err := Pack(&buf, reference); err != nil { - b.Fatal(err) - } - } -} - -func BenchmarkFullDecode(b *testing.B) { - var out Example - for i := 0; i < b.N; i++ { - buf := bytes.NewBuffer(referenceBytes) - if err := Unpack(buf, &out); err != nil { - b.Fatal(err) - } - } -} diff --git a/vendor/github.com/lunixbochs/struc/custom_float16_test.go b/vendor/github.com/lunixbochs/struc/custom_float16_test.go deleted file mode 100644 index 11f73cb..0000000 --- a/vendor/github.com/lunixbochs/struc/custom_float16_test.go +++ /dev/null @@ -1,56 +0,0 @@ -package struc - -import ( - "bytes" - "encoding/binary" - "fmt" - "math" - "strconv" - "strings" - "testing" -) - -func TestFloat16(t *testing.T) { - // test cases from https://en.wikipedia.org/wiki/Half-precision_floating-point_format#Half_precision_examples - tests := []struct { - B string - F float64 - }{ - //s expnt significand - {"0 01111 0000000000", 1}, - {"0 01111 0000000001", 1.0009765625}, - {"1 10000 0000000000", -2}, - {"0 11110 1111111111", 65504}, - // {"0 00001 0000000000", 0.0000610352}, - // {"0 00000 1111111111", 0.0000609756}, - // {"0 00000 0000000001", 0.0000000596046}, - {"0 00000 0000000000", 0}, - // {"1 00000 0000000000", -0}, - {"0 11111 0000000000", math.Inf(1)}, - {"1 11111 0000000000", math.Inf(-1)}, - {"0 01101 0101010101", 0.333251953125}, - } - for _, test := range tests { - var buf bytes.Buffer - f := Float16(test.F) - if err := Pack(&buf, &f); err != nil { - t.Error("pack failed:", err) - continue - } - bitval, _ := strconv.ParseUint(strings.Replace(test.B, " ", "", -1), 2, 16) - tmp := binary.BigEndian.Uint16(buf.Bytes()) - if tmp != uint16(bitval) { - t.Errorf("incorrect pack: %s != %016b (%f)", test.B, tmp, test.F) - continue - } - var f2 Float16 - if err := Unpack(&buf, &f2); err != nil { - t.Error("unpack failed:", err) - continue - } - // let sprintf deal with (im)precision for me here - if fmt.Sprintf("%f", f) != fmt.Sprintf("%f", f2) { - t.Errorf("incorrect unpack: %016b %f != %f", bitval, f, f2) - } - } -} diff --git a/vendor/github.com/lunixbochs/struc/custom_test.go b/vendor/github.com/lunixbochs/struc/custom_test.go deleted file mode 100644 index e601166..0000000 --- a/vendor/github.com/lunixbochs/struc/custom_test.go +++ /dev/null @@ -1,97 +0,0 @@ -package struc - -import ( - "bytes" - "encoding/binary" - "io" - "strconv" - "testing" -) - -type Int3 uint32 - -func (i *Int3) Pack(p []byte, opt *Options) (int, error) { - var tmp [4]byte - binary.BigEndian.PutUint32(tmp[:], uint32(*i)) - copy(p, tmp[1:]) - return 3, nil -} -func (i *Int3) Unpack(r io.Reader, length int, opt *Options) error { - var tmp [4]byte - if _, err := r.Read(tmp[1:]); err != nil { - return err - } - *i = Int3(binary.BigEndian.Uint32(tmp[:])) - return nil -} -func (i *Int3) Size(opt *Options) int { - return 3 -} -func (i *Int3) String() string { - return strconv.FormatUint(uint64(*i), 10) -} - -func TestCustom(t *testing.T) { - var buf bytes.Buffer - var i Int3 = 3 - if err := Pack(&buf, &i); err != nil { - t.Fatal(err) - } - if !bytes.Equal(buf.Bytes(), []byte{0, 0, 3}) { - t.Fatal("error packing custom int") - } - var i2 Int3 - if err := Unpack(&buf, &i2); err != nil { - t.Fatal(err) - } - if i2 != 3 { - t.Fatal("error unpacking custom int") - } -} - -type Int3Struct struct { - I Int3 -} - -func TestCustomStruct(t *testing.T) { - var buf bytes.Buffer - i := Int3Struct{3} - if err := Pack(&buf, &i); err != nil { - t.Fatal(err) - } - if !bytes.Equal(buf.Bytes(), []byte{0, 0, 3}) { - t.Fatal("error packing custom int struct") - } - var i2 Int3Struct - if err := Unpack(&buf, &i2); err != nil { - t.Fatal(err) - } - if i2.I != 3 { - t.Fatal("error unpacking custom int struct") - } -} - -// TODO: slices of custom types don't work yet -/* -type Int3SliceStruct struct { - I [2]Int3 -} - -func TestCustomSliceStruct(t *testing.T) { - var buf bytes.Buffer - i := Int3SliceStruct{[2]Int3{3, 4}} - if err := Pack(&buf, &i); err != nil { - t.Fatal(err) - } - if !bytes.Equal(buf.Bytes(), []byte{0, 0, 3}) { - t.Fatal("error packing custom int struct") - } - var i2 Int3SliceStruct - if err := Unpack(&buf, &i2); err != nil { - t.Fatal(err) - } - if i2.I[0] != 3 && i2.I[1] != 4 { - t.Fatal("error unpacking custom int struct") - } -} -*/ diff --git a/vendor/github.com/lunixbochs/struc/field.go b/vendor/github.com/lunixbochs/struc/field.go index 5ab7da6..6e59c2a 100644 --- a/vendor/github.com/lunixbochs/struc/field.go +++ b/vendor/github.com/lunixbochs/struc/field.go @@ -222,13 +222,21 @@ func (f *Field) unpackVal(buf []byte, val reflect.Value, length int, options *Op case Bool, Int8, Int16, Int32, Int64, Uint8, Uint16, Uint32, Uint64: var n uint64 switch typ { - case Bool, Int8, Uint8: + case Int8: + n = uint64(int64(int8(buf[0]))) + case Int16: + n = uint64(int64(int16(order.Uint16(buf)))) + case Int32: + n = uint64(int64(int32(order.Uint32(buf)))) + case Int64: + n = uint64(int64(order.Uint64(buf))) + case Bool, Uint8: n = uint64(buf[0]) - case Int16, Uint16: + case Uint16: n = uint64(order.Uint16(buf)) - case Int32, Uint32: + case Uint32: n = uint64(order.Uint32(buf)) - case Int64, Uint64: + case Uint64: n = uint64(order.Uint64(buf)) } switch f.kind { diff --git a/vendor/github.com/lunixbochs/struc/field_test.go b/vendor/github.com/lunixbochs/struc/field_test.go deleted file mode 100644 index 45a07b2..0000000 --- a/vendor/github.com/lunixbochs/struc/field_test.go +++ /dev/null @@ -1,77 +0,0 @@ -package struc - -import ( - "bytes" - "testing" -) - -type badFloat struct { - BadFloat int `struc:"float64"` -} - -func TestBadFloatField(t *testing.T) { - buf := bytes.NewReader([]byte("00000000")) - err := Unpack(buf, &badFloat{}) - if err == nil { - t.Fatal("failed to error on bad float unpack") - } -} - -type emptyLengthField struct { - Strlen int `struc:"sizeof=Str"` - Str []byte -} - -func TestEmptyLengthField(t *testing.T) { - var buf bytes.Buffer - s := &emptyLengthField{0, []byte("test")} - o := &emptyLengthField{} - if err := Pack(&buf, s); err != nil { - t.Fatal(err) - } - if err := Unpack(&buf, o); err != nil { - t.Fatal(err) - } - if !bytes.Equal(s.Str, o.Str) { - t.Fatal("empty length field encode failed") - } -} - -type fixedSlicePad struct { - Field []byte `struc:"[4]byte"` -} - -func TestFixedSlicePad(t *testing.T) { - var buf bytes.Buffer - ref := []byte{0, 0, 0, 0} - s := &fixedSlicePad{} - if err := Pack(&buf, s); err != nil { - t.Fatal(err) - } - if !bytes.Equal(buf.Bytes(), ref) { - t.Fatal("implicit fixed slice pack failed") - } - if err := Unpack(&buf, s); err != nil { - t.Fatal(err) - } - if !bytes.Equal(s.Field, ref) { - t.Fatal("implicit fixed slice unpack failed") - } -} - -type sliceCap struct { - Len int `struc:"sizeof=Field"` - Field []byte -} - -func TestSliceCap(t *testing.T) { - var buf bytes.Buffer - tmp := &sliceCap{0, []byte("1234")} - if err := Pack(&buf, tmp); err != nil { - t.Fatal(err) - } - tmp.Field = make([]byte, 0, 4) - if err := Unpack(&buf, tmp); err != nil { - t.Fatal(err) - } -} diff --git a/vendor/github.com/lunixbochs/struc/fields_test.go b/vendor/github.com/lunixbochs/struc/fields_test.go deleted file mode 100644 index 850e377..0000000 --- a/vendor/github.com/lunixbochs/struc/fields_test.go +++ /dev/null @@ -1,59 +0,0 @@ -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) -} diff --git a/vendor/github.com/lunixbochs/struc/packable_test.go b/vendor/github.com/lunixbochs/struc/packable_test.go deleted file mode 100644 index ec2bed9..0000000 --- a/vendor/github.com/lunixbochs/struc/packable_test.go +++ /dev/null @@ -1,123 +0,0 @@ -package struc - -import ( - "bytes" - "fmt" - "testing" -) - -var packableReference = []byte{ - 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 5, 0, 6, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 8, - 9, 10, 11, 12, 13, 14, 15, 16, - 0, 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 24, -} - -func TestPackable(t *testing.T) { - var ( - buf bytes.Buffer - - i8 int8 = 1 - i16 int16 = 2 - i32 int32 = 3 - i64 int64 = 4 - u8 uint8 = 5 - u16 uint16 = 6 - u32 uint32 = 7 - u64 uint64 = 8 - - u8a = [8]uint8{9, 10, 11, 12, 13, 14, 15, 16} - u16a = [8]uint16{17, 18, 19, 20, 21, 22, 23, 24} - ) - // pack tests - if err := Pack(&buf, i8); err != nil { - t.Fatal(err) - } - if err := Pack(&buf, i16); err != nil { - t.Fatal(err) - } - if err := Pack(&buf, i32); err != nil { - t.Fatal(err) - } - if err := Pack(&buf, i64); err != nil { - t.Fatal(err) - } - if err := Pack(&buf, u8); err != nil { - t.Fatal(err) - } - if err := Pack(&buf, u16); err != nil { - t.Fatal(err) - } - if err := Pack(&buf, u32); err != nil { - t.Fatal(err) - } - if err := Pack(&buf, u64); err != nil { - t.Fatal(err) - } - if err := Pack(&buf, u8a[:]); err != nil { - t.Fatal(err) - } - if err := Pack(&buf, u16a[:]); err != nil { - t.Fatal(err) - } - if !bytes.Equal(buf.Bytes(), packableReference) { - fmt.Println(buf.Bytes()) - fmt.Println(packableReference) - t.Fatal("Packable Pack() did not match reference.") - } - // unpack tests - i8 = 0 - i16 = 0 - i32 = 0 - i64 = 0 - u8 = 0 - u16 = 0 - u32 = 0 - u64 = 0 - if err := Unpack(&buf, &i8); err != nil { - t.Fatal(err) - } - if err := Unpack(&buf, &i16); err != nil { - t.Fatal(err) - } - if err := Unpack(&buf, &i32); err != nil { - t.Fatal(err) - } - if err := Unpack(&buf, &i64); err != nil { - t.Fatal(err) - } - if err := Unpack(&buf, &u8); err != nil { - t.Fatal(err) - } - if err := Unpack(&buf, &u16); err != nil { - t.Fatal(err) - } - if err := Unpack(&buf, &u32); err != nil { - t.Fatal(err) - } - if err := Unpack(&buf, &u64); err != nil { - t.Fatal(err) - } - if err := Unpack(&buf, u8a[:]); err != nil { - t.Fatal(err) - } - if err := Unpack(&buf, u16a[:]); err != nil { - t.Fatal(err) - } - // unpack checks - if i8 != 1 || i16 != 2 || i32 != 3 || i64 != 4 { - t.Fatal("Signed integer unpack failed.") - } - if u8 != 5 || u16 != 6 || u32 != 7 || u64 != 8 { - t.Fatal("Unsigned integer unpack failed.") - } - for i := 0; i < 8; i++ { - if u8a[i] != uint8(i+9) { - t.Fatal("uint8 array unpack failed.") - } - } - for i := 0; i < 8; i++ { - if u16a[i] != uint16(i+17) { - t.Fatal("uint16 array unpack failed.") - } - } -} diff --git a/vendor/github.com/lunixbochs/struc/parse.go b/vendor/github.com/lunixbochs/struc/parse.go index 060f43d..43c5875 100644 --- a/vendor/github.com/lunixbochs/struc/parse.go +++ b/vendor/github.com/lunixbochs/struc/parse.go @@ -11,13 +11,14 @@ import ( "sync" ) -// struc:"int32,big,sizeof=Data" +// struc:"int32,big,sizeof=Data,skip,sizefrom=Len" type strucTag struct { - Type string - Order binary.ByteOrder - Sizeof string - Skip bool + Type string + Order binary.ByteOrder + Sizeof string + Skip bool + Sizefrom string } func parseStrucTag(tag reflect.StructTag) *strucTag { @@ -35,6 +36,9 @@ func parseStrucTag(tag reflect.StructTag) *strucTag { if strings.HasPrefix(s, "sizeof=") { tmp := strings.SplitN(s, "=", 2) t.Sizeof = tmp[1] + } else if strings.HasPrefix(s, "sizefrom=") { + tmp := strings.SplitN(s, "=", 2) + t.Sizefrom = tmp[1] } else if s == "big" { t.Order = binary.BigEndian } else if s == "little" { @@ -150,6 +154,13 @@ func parseFieldsLocked(v reflect.Value) (Fields, error) { if sizefrom, ok := sizeofMap[field.Name]; ok { f.Sizefrom = sizefrom } + if tag.Sizefrom != "" { + source, ok := t.FieldByName(tag.Sizefrom) + if !ok { + return nil, fmt.Errorf("struc: `sizefrom=%s` field does not exist", tag.Sizefrom) + } + f.Sizefrom = source.Index + } if f.Len == -1 && f.Sizefrom == nil { return nil, fmt.Errorf("struc: field `%s` is a slice with no length or sizeof field", field.Name) } diff --git a/vendor/github.com/lunixbochs/struc/parse_test.go b/vendor/github.com/lunixbochs/struc/parse_test.go deleted file mode 100644 index 861fdd1..0000000 --- a/vendor/github.com/lunixbochs/struc/parse_test.go +++ /dev/null @@ -1,62 +0,0 @@ -package struc - -import ( - "bytes" - "reflect" - "testing" -) - -func parseTest(data interface{}) error { - _, err := parseFields(reflect.ValueOf(data)) - return err -} - -type empty struct{} - -func TestEmptyStruc(t *testing.T) { - if err := parseTest(&empty{}); err == nil { - t.Fatal("failed to error on empty struct") - } -} - -type chanStruct struct { - Test chan int -} - -func TestChanError(t *testing.T) { - if err := parseTest(&chanStruct{}); err == nil { - // TODO: should probably ignore channel fields - t.Fatal("failed to error on struct containing channel") - } -} - -type badSizeof struct { - Size int `struc:"sizeof=Bad"` -} - -func TestBadSizeof(t *testing.T) { - if err := parseTest(&badSizeof{}); err == nil { - t.Fatal("failed to error on missing Sizeof target") - } -} - -type missingSize struct { - Test []byte -} - -func TestMissingSize(t *testing.T) { - if err := parseTest(&missingSize{}); err == nil { - t.Fatal("failed to error on missing field size") - } -} - -type badNested struct { - Empty empty -} - -func TestNestedParseError(t *testing.T) { - var buf bytes.Buffer - if err := Pack(&buf, &badNested{}); err == nil { - t.Fatal("failed to error on bad nested struct") - } -} diff --git a/vendor/github.com/lunixbochs/struc/struc_test.go b/vendor/github.com/lunixbochs/struc/struc_test.go deleted file mode 100644 index 4b50707..0000000 --- a/vendor/github.com/lunixbochs/struc/struc_test.go +++ /dev/null @@ -1,200 +0,0 @@ -package struc - -import ( - "bytes" - "encoding/binary" - "reflect" - "testing" -) - -type Nested struct { - Test2 int `struc:"int8"` -} - -type Example struct { - Pad []byte `struc:"[5]pad"` // 00 00 00 00 00 - I8f int `struc:"int8"` // 01 - I16f int `struc:"int16"` // 00 02 - I32f int `struc:"int32"` // 00 00 00 03 - I64f int `struc:"int64"` // 00 00 00 00 00 00 00 04 - U8f int `struc:"uint8,little"` // 05 - U16f int `struc:"uint16,little"` // 06 00 - U32f int `struc:"uint32,little"` // 07 00 00 00 - U64f int `struc:"uint64,little"` // 08 00 00 00 00 00 00 00 - Boolf int `struc:"bool"` // 01 - Byte4f []byte `struc:"[4]byte"` // "abcd" - - I8 int8 // 09 - I16 int16 // 00 0a - I32 int32 // 00 00 00 0b - I64 int64 // 00 00 00 00 00 00 00 0c - U8 uint8 `struc:"little"` // 0d - U16 uint16 `struc:"little"` // 0e 00 - U32 uint32 `struc:"little"` // 0f 00 00 00 - U64 uint64 `struc:"little"` // 10 00 00 00 00 00 00 00 - BoolT bool // 01 - BoolF bool // 00 - Byte4 [4]byte // "efgh" - Float1 float32 // 41 a0 00 00 - Float2 float64 // 41 35 00 00 00 00 00 00 - - Size int `struc:"sizeof=Str,little"` // 0a 00 00 00 - Str string `struc:"[]byte"` // "ijklmnopqr" - Strb string `struc:"[4]byte"` // "stuv" - - Size2 int `struc:"uint8,sizeof=Str2"` // 04 - Str2 string // "1234" - - Size3 int `struc:"uint8,sizeof=Bstr"` // 04 - Bstr []byte // "5678" - - Nested Nested // 00 00 00 01 - NestedP *Nested // 00 00 00 02 - TestP64 *int `struc:"int64"` // 00 00 00 05 - - NestedSize int `struc:"sizeof=NestedA"` // 00 00 00 02 - NestedA []Nested // [00 00 00 03, 00 00 00 04] - - Skip int `struc:"skip"` - - CustomTypeSize Int3 `struc:"sizeof=CustomTypeSizeArr"` // 00 00 00 04 - CustomTypeSizeArr []byte // "ABCD" -} - -var five = 5 - -var reference = &Example{ - nil, - 1, 2, 3, 4, 5, 6, 7, 8, 0, []byte{'a', 'b', 'c', 'd'}, - 9, 10, 11, 12, 13, 14, 15, 16, true, false, [4]byte{'e', 'f', 'g', 'h'}, - 20, 21, - 10, "ijklmnopqr", "stuv", - 4, "1234", - 4, []byte("5678"), - Nested{1}, &Nested{2}, &five, - 6, []Nested{{3}, {4}, {5}, {6}, {7}, {8}}, - 0, - Int3(4), []byte("ABCD"), -} - -var referenceBytes = []byte{ - 0, 0, 0, 0, 0, // pad(5) - 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, // fake int8-int64(1-4) - 5, 6, 0, 7, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, // fake little-endian uint8-uint64(5-8) - 0, // fake bool(0) - 'a', 'b', 'c', 'd', // fake [4]byte - - 9, 0, 10, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 12, // real int8-int64(9-12) - 13, 14, 0, 15, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, // real little-endian uint8-uint64(13-16) - 1, 0, // real bool(1), bool(0) - 'e', 'f', 'g', 'h', // real [4]byte - 65, 160, 0, 0, // real float32(20) - 64, 53, 0, 0, 0, 0, 0, 0, // real float64(21) - - 10, 0, 0, 0, // little-endian int32(10) sizeof=Str - 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', // Str - 's', 't', 'u', 'v', // fake string([4]byte) - 04, '1', '2', '3', '4', // real string - 04, '5', '6', '7', '8', // fake []byte(string) - - 1, 2, // Nested{1}, Nested{2} - 0, 0, 0, 0, 0, 0, 0, 5, // &five - - 0, 0, 0, 6, // int32(6) - 3, 4, 5, 6, 7, 8, // [Nested{3}, ...Nested{8}] - - 0, 0, 4, 'A', 'B', 'C', 'D', // Int3(4), []byte("ABCD") -} - -func TestCodec(t *testing.T) { - var buf bytes.Buffer - if err := Pack(&buf, reference); err != nil { - t.Fatal(err) - } - out := &Example{} - if err := Unpack(&buf, out); err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(reference, out) { - t.Fatal("encode/decode failed") - } -} - -func TestEncode(t *testing.T) { - var buf bytes.Buffer - if err := Pack(&buf, reference); err != nil { - t.Fatal(err) - } - if !bytes.Equal(buf.Bytes(), referenceBytes) { - t.Fatal("encode failed") - } -} - -func TestDecode(t *testing.T) { - buf := bytes.NewReader(referenceBytes) - out := &Example{} - if err := Unpack(buf, out); err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(reference, out) { - t.Fatal("decode failed") - } -} - -func TestSizeof(t *testing.T) { - size, err := Sizeof(reference) - if err != nil { - t.Fatal(err) - } - if size != len(referenceBytes) { - t.Fatal("sizeof failed") - } -} - -type ExampleEndian struct { - T int `struc:"int16,big"` -} - -func TestEndianSwap(t *testing.T) { - var buf bytes.Buffer - big := &ExampleEndian{1} - if err := PackWithOrder(&buf, big, binary.BigEndian); err != nil { - t.Fatal(err) - } - little := &ExampleEndian{} - if err := UnpackWithOrder(&buf, little, binary.LittleEndian); err != nil { - t.Fatal(err) - } - if little.T != 256 { - t.Fatal("big -> little conversion failed") - } -} - -func TestNilValue(t *testing.T) { - var buf bytes.Buffer - if err := Pack(&buf, nil); err == nil { - t.Fatal("failed throw error for bad struct value") - } - if err := Unpack(&buf, nil); err == nil { - t.Fatal("failed throw error for bad struct value") - } - if _, err := Sizeof(nil); err == nil { - t.Fatal("failed to throw error for bad struct value") - } -} - -type sliceUnderrun struct { - Str string `struc:"[10]byte"` - Arr []uint16 `struc:"[10]uint16"` -} - -func TestSliceUnderrun(t *testing.T) { - var buf bytes.Buffer - v := sliceUnderrun{ - Str: "foo", - Arr: []uint16{1, 2, 3}, - } - if err := Pack(&buf, &v); err != nil { - t.Fatal(err) - } -} diff --git a/vendor/github.com/lunixbochs/struc/types_test.go b/vendor/github.com/lunixbochs/struc/types_test.go deleted file mode 100644 index 3b33e8f..0000000 --- a/vendor/github.com/lunixbochs/struc/types_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package struc - -import ( - "bytes" - "testing" -) - -func TestBadType(t *testing.T) { - defer func() { recover() }() - Type(-1).Size() - t.Fatal("failed to panic for invalid Type.Size()") -} - -func TestTypeString(t *testing.T) { - if Pad.String() != "pad" { - t.Fatal("type string representation failed") - } -} - -type sizeOffTest struct { - Size Size_t - Off Off_t -} - -func TestSizeOffTypes(t *testing.T) { - bits := []int{8, 16, 32, 64} - var buf bytes.Buffer - test := &sizeOffTest{1, 2} - for _, b := range bits { - if err := PackWithOptions(&buf, test, &Options{PtrSize: b}); err != nil { - t.Fatal(err) - } - } - reference := []byte{ - 1, 2, - 0, 1, 0, 2, - 0, 0, 0, 1, 0, 0, 0, 2, - 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, - } - if !bytes.Equal(reference, buf.Bytes()) { - t.Errorf("reference != bytes: %v", reference, buf.Bytes()) - } - reader := bytes.NewReader(buf.Bytes()) - for _, b := range bits { - out := &sizeOffTest{} - if err := UnpackWithOptions(reader, out, &Options{PtrSize: b}); err != nil { - t.Fatal(err) - } - if out.Size != 1 || out.Off != 2 { - t.Errorf("Size_t/Off_t mismatch: {%d, %d}\n%v", out.Size, out.Off, buf.Bytes()) - } - } -} |