summaryrefslogtreecommitdiff
path: root/lib/containers/fuzzutil_test.go
blob: 79459b681f441c4983304fc9bedbe01b2dfb3815 (plain)
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
// Copyright (C) 2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package containers

import (
	"bytes"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"os"
	"path/filepath"
	"testing"
)

func SaveFuzz(f *testing.F, dat []byte) {
	var buf bytes.Buffer
	fmt.Fprintf(&buf, "go test fuzz v1\n[]byte(%q)\n", dat)
	sum := sha256.Sum256(buf.Bytes())
	filename := filepath.Join(
		"testdata",
		"fuzz",
		f.Name(),
		hex.EncodeToString(sum[:]))
	if err := os.WriteFile(filename, buf.Bytes(), 0o644); err != nil {
		f.Error(err)
	}
}