Skip to content

Commit 43a7035

Browse files
committed
Conver base85 roundtrip to fuzz test
1 parent 005df36 commit 43a7035

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

gitdiff/base85_test.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package gitdiff
22

33
import (
44
"bytes"
5-
"fmt"
6-
"math/rand"
75
"testing"
86
)
97

@@ -98,23 +96,22 @@ func TestBase85Encode(t *testing.T) {
9896
}
9997
}
10098

101-
func TestBase85Roundtrip(t *testing.T) {
102-
r := rand.New(rand.NewSource(72)) // chosen by fair dice roll
99+
func FuzzBase85Roundtrip(f *testing.F) {
100+
f.Add([]byte{0x2b, 0x0d})
101+
f.Add([]byte{0xbc, 0xb4, 0x3f})
102+
f.Add([]byte{0xfa, 0x62, 0x05, 0x83, 0x24, 0x39, 0xd5, 0x25})
103+
f.Add([]byte{0x31, 0x59, 0x02, 0xa0, 0x61, 0x12, 0xd9, 0x43, 0xb8, 0x23, 0x1a, 0xb4, 0x02, 0xae, 0xfa, 0xcc, 0x22, 0xad, 0x41, 0xb9, 0xb8})
103104

104-
for _, size := range []int{64, 85, 1025} {
105-
t.Run(fmt.Sprintf("size%d", size), func(t *testing.T) {
106-
in := make([]byte, size)
107-
r.Read(in)
105+
f.Fuzz(func(t *testing.T, in []byte) {
106+
n := len(in)
107+
dst := make([]byte, base85Len(n))
108+
out := make([]byte, n)
108109

109-
dst := make([]byte, base85Len(size))
110-
out := make([]byte, size)
110+
base85Encode(dst, in)
111+
base85Decode(out, dst)
111112

112-
base85Encode(dst, in)
113-
base85Decode(out, dst)
114-
115-
if !bytes.Equal(in, out) {
116-
t.Errorf("decoded data differed from input data:\n input: %x\n output: %x\nencoding: %s\n", in, out, string(dst))
117-
}
118-
})
119-
}
113+
if !bytes.Equal(in, out) {
114+
t.Errorf("decoded data differed from input data:\n input: %x\n output: %x\nencoding: %s\n", in, out, string(dst))
115+
}
116+
})
120117
}

0 commit comments

Comments
 (0)