github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/osutil/tar_test.go (about) 1 // Copyright 2025 syzkaller project authors. All rights reserved. 2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4 package osutil 5 6 import ( 7 "archive/tar" 8 "bytes" 9 "io" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestTarDirectory(t *testing.T) { 17 dir := t.TempDir() 18 items := map[string]string{ 19 "file1.txt": "first file content", 20 "dir/file2.txt": "second file content", 21 "empty.txt": "", 22 } 23 require.NoError(t, FillDirectory(dir, items)) 24 25 var buf bytes.Buffer 26 err := tarDirectory(dir, &buf) 27 assert.NoError(t, err) 28 29 tr := tar.NewReader(&buf) 30 found := make(map[string]string) 31 for { 32 hdr, err := tr.Next() 33 if err == io.EOF { 34 break 35 } 36 if err != nil { 37 t.Fatal(err) 38 } 39 if hdr.Typeflag == tar.TypeReg { 40 contentBytes, err := io.ReadAll(tr) 41 if err != nil { 42 t.Fatal(err) 43 } 44 found[hdr.Name] = string(contentBytes) 45 } 46 } 47 48 assert.Equal(t, items, found) 49 }