internal/zstd: abstract zstd binary path lookup in tests

This commit introduces a function findZstd which is responsible for locating the
zstd executable used by fuzz and unit tests. Previously, the explicit path
"/usr/bin/zstd" was hard-coded into test cases, which could lead to skipped tests
on systems where zstd is located elsewhere or not present at that specific path.

Fixes #64000
This commit is contained in:
aimuz 2023-11-08 19:50:36 +08:00
parent c9888bdfe2
commit c4dfe1139b
No known key found for this signature in database
GPG Key ID: 63C3DC9FBA22D9D7
2 changed files with 16 additions and 19 deletions

View File

@ -43,9 +43,7 @@ func FuzzReader(f *testing.F) {
// explore the space of decompressor behavior, since it can't see
// what the compressor is doing. But it's better than nothing.
func FuzzDecompressor(f *testing.F) {
if _, err := os.Stat("/usr/bin/zstd"); err != nil {
f.Skip("skipping because /usr/bin/zstd does not exist")
}
zstd := findZstd(f)
for _, test := range tests {
f.Add([]byte(test.uncompressed))
@ -61,7 +59,7 @@ func FuzzDecompressor(f *testing.F) {
f.Add(bigData(f))
f.Fuzz(func(t *testing.T, b []byte) {
cmd := exec.Command("/usr/bin/zstd", "-z")
cmd := exec.Command(zstd, "-z")
cmd.Stdin = bytes.NewReader(b)
var compressed bytes.Buffer
cmd.Stdout = &compressed
@ -84,9 +82,7 @@ func FuzzDecompressor(f *testing.F) {
// Fuzz test to check that if we can decompress some data,
// so can zstd, and that we get the same result.
func FuzzReverse(f *testing.F) {
if _, err := os.Stat("/usr/bin/zstd"); err != nil {
f.Skip("skipping because /usr/bin/zstd does not exist")
}
zstd := findZstd(f)
for _, test := range tests {
f.Add([]byte(test.compressed))
@ -100,7 +96,7 @@ func FuzzReverse(f *testing.F) {
r := NewReader(bytes.NewReader(b))
goExp, goErr := io.ReadAll(r)
cmd := exec.Command("/usr/bin/zstd", "-d")
cmd := exec.Command(zstd, "-d")
cmd.Stdin = bytes.NewReader(b)
var uncompressed bytes.Buffer
cmd.Stdout = &uncompressed

View File

@ -167,10 +167,17 @@ func bigData(t testing.TB) []byte {
return bigDataBytes
}
func findZstd(t testing.TB) string {
zstd, err := exec.LookPath("zstd")
if err != nil {
t.Skip("skipping because zstd not found")
}
return zstd
}
var (
zstdBigOnce sync.Once
zstdBigBytes []byte
zstdBigSkip bool
zstdBigErr error
)
@ -180,13 +187,10 @@ var (
func zstdBigData(t testing.TB) []byte {
input := bigData(t)
zstdBigOnce.Do(func() {
if _, err := os.Stat("/usr/bin/zstd"); err != nil {
zstdBigSkip = true
return
}
zstd := findZstd(t)
cmd := exec.Command("/usr/bin/zstd", "-z")
zstdBigOnce.Do(func() {
cmd := exec.Command(zstd, "-z")
cmd.Stdin = bytes.NewReader(input)
var compressed bytes.Buffer
cmd.Stdout = &compressed
@ -198,9 +202,6 @@ func zstdBigData(t testing.TB) []byte {
zstdBigBytes = compressed.Bytes()
})
if zstdBigSkip {
t.Skip("skipping because /usr/bin/zstd does not exist")
}
if zstdBigErr != nil {
t.Fatal(zstdBigErr)
}
@ -217,7 +218,7 @@ func TestLarge(t *testing.T) {
data := bigData(t)
compressed := zstdBigData(t)
t.Logf("/usr/bin/zstd compressed %d bytes to %d", len(data), len(compressed))
t.Logf("zstd compressed %d bytes to %d", len(data), len(compressed))
r := NewReader(bytes.NewReader(compressed))
got, err := io.ReadAll(r)