| 1 |
package packfile_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/sha1" |
| 6 |
"encoding/binary" |
| 7 |
"io" |
| 8 |
"testing" |
| 9 |
|
| 10 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 11 |
"github.com/go-git/go-git/v5/plumbing/format/packfile" |
| 12 |
) |
| 13 |
|
| 14 |
func FuzzParser(f *testing.F) { |
| 15 |
if pf := fixtures.Basic().One().Packfile(); pf != nil { |
| 16 |
if data, rerr := io.ReadAll(pf); rerr == nil { |
| 17 |
f.Add(data) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
var overflow bytes.Buffer |
| 22 |
overflow.WriteString("PACK") |
| 23 |
_ = binary.Write(&overflow, binary.BigEndian, uint32(2)) |
| 24 |
_ = binary.Write(&overflow, binary.BigEndian, uint32(1)) |
| 25 |
overflow.WriteByte(0x90) |
| 26 |
overflow.Write(bytes.Repeat([]byte{0x80}, 9)) |
| 27 |
sum := sha1.Sum(overflow.Bytes()) |
| 28 |
overflow.Write(sum[:]) |
| 29 |
f.Add(overflow.Bytes()) |
| 30 |
|
| 31 |
f.Fuzz(func(_ *testing.T, data []byte) { |
| 32 |
scanner := packfile.NewScanner(bytes.NewReader(data)) |
| 33 |
parser, err := packfile.NewParser(scanner) |
| 34 |
if err != nil { |
| 35 |
return |
| 36 |
} |
| 37 |
_, _ = parser.Parse() |
| 38 |
}) |
| 39 |
} |