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
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package btrfsitem_test
import (
"testing"
"github.com/stretchr/testify/require"
"git.lukeshu.com/btrfs-progs-ng/lib/binstruct"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum"
)
func FuzzRoundTrip(f *testing.F) {
keySize := binstruct.StaticSize(btrfsprim.Key{})
sumtypeSize := binstruct.StaticSize(btrfssum.CSumType(0))
f.Add(make([]byte, 256))
f.Fuzz(func(t *testing.T, inDat []byte) {
if len(inDat) < keySize+sumtypeSize {
t.Skip()
}
keyInDat, inDat := inDat[:keySize], inDat[keySize:]
sumtypeInDat, inDat := inDat[:sumtypeSize], inDat[sumtypeSize:]
itemInDat := inDat
// key
var key btrfsprim.Key
n, err := binstruct.Unmarshal(keyInDat, &key)
require.NoError(t, err, "binstruct.Unmarshal(dat, &key)")
require.Equal(t, keySize, n, "binstruct.Unmarshal(dat, &key)")
keyOutDat, err := binstruct.Marshal(key)
require.NoError(t, err, "binstruct.Marshal(key)")
require.Equal(t, keyInDat, keyOutDat, "binstruct.Marshal(key)")
// sumtype
var sumtype btrfssum.CSumType
n, err = binstruct.Unmarshal(sumtypeInDat, &sumtype)
require.NoError(t, err, "binstruct.Unmarshal(dat, &sumtype)")
require.Equal(t, sumtypeSize, n, "binstruct.Unmarshal(dat, &sumtype)")
sumtypeOutDat, err := binstruct.Marshal(sumtype)
require.NoError(t, err, "binstruct.Marshal(sumtype)")
require.Equal(t, sumtypeInDat, sumtypeOutDat, "binstruct.Marshal(sumtype)")
// item
t.Logf("key=%v sumtype=%v dat=%q", key, sumtype, itemInDat)
item := btrfsitem.UnmarshalItem(key, sumtype, itemInDat)
require.NotNil(t, item, "btrfsitem.UnmarshalItem")
itemOutDat, err := binstruct.Marshal(item)
require.NoError(t, err, "binstruct.Marshal(item)")
require.Equal(t, string(itemInDat), string(itemOutDat), "binstruct.Marshal(item)")
})
}
|