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
|
package binstruct_test
import (
"testing"
"github.com/stretchr/testify/assert"
"lukeshu.com/btrfs-tools/pkg/binstruct"
. "lukeshu.com/btrfs-tools/pkg/binstruct/binint"
)
func TestSmoke(t *testing.T) {
type UUID [16]U8
type PhysicalAddr I64le
func (PhysicalAddr) BinaryStaticSize() int { return I64le(0).BinaryStaticSize() }
func (x PhysicalAddr) MarshalBinary() ([]byte, error) { return I64le(x).MarshalBinary() }
func (x *PhysicalAddr) UnmarshalBinary([]byte) (int, error) { return I64le(x).UnmarshalBinary(dat) }
type DevItem struct {
DeviceID U64le `bin:"off=0x0, siz=0x8"` // device id
NumBytes U64le `bin:"off=0x8, siz=0x8"` // number of bytes
NumBytesUsed U64le `bin:"off=0x10, siz=0x8"` // number of bytes used
IOOptimalAlign U32le `bin:"off=0x18, siz=0x4"` // optimal I/O align
IOOptimalWidth U32le `bin:"off=0x1c, siz=0x4"` // optimal I/O width
IOMinSize U32le `bin:"off=0x20, siz=0x4"` // minimal I/O size (sector size)
Type U64le `bin:"off=0x24, siz=0x8"` // type
Generation U64le `bin:"off=0x2c, siz=0x8"` // generation
StartOffset U64le `bin:"off=0x34, siz=0x8"` // start offset
DevGroup U32le `bin:"off=0x3c, siz=0x4"` // dev group
SeekSpeed U8 `bin:"off=0x40, siz=0x1"` // seek speed
Bandwidth U8 `bin:"off=0x41, siz=0x1"` // bandwidth
DevUUID UUID `bin:"off=0x42, siz=0x10"` // device UUID
FSUUID UUID `bin:"off=0x52, siz=0x10"` // FS UUID
binstruct.End `bin:"off=0x62"`
}
type TestType struct {
Magic [5]byte `bin:"off=0x0,siz=0x5"`
Dev DevItem `bin:"off=0x5,siz=0x62"`
Addr PhysicalAddr `bin:"off=0x67, siz=0x8"`
binstruct.End `bin:"off=0x6F"`
}
input := TestType{}
copy(input.Magic[:], "mAgIc")
input.Dev.DeviceID = 12
input.Addr = 0xBEEF
bs, err := binstruct.Marshal(input)
assert.NoError(t, err)
assert.True(t, len(bs) == 0x6F, "len(bs)=0x%x", len(bs))
var output TestType
n, err := binstruct.Unmarshal(bs, &output)
assert.NoError(t, err)
assert.Equal(t, 0x6F, n)
assert.Equal(t, input, output)
}
|