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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
// Copyright 2025 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package osutil
import (
"archive/tar"
"bytes"
"io"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTarDirectory(t *testing.T) {
dir := t.TempDir()
items := map[string]string{
"file1.txt": "first file content",
"dir/file2.txt": "second file content",
"empty.txt": "",
}
for path, content := range items {
fullPath := filepath.Join(dir, path)
dirPath := filepath.Dir(fullPath)
if err := MkdirAll(dirPath); err != nil {
t.Fatalf("mkdir %q failed: %v", dirPath, err)
}
if err := WriteFile(fullPath, []byte(content)); err != nil {
t.Fatalf("write file failed: %v", err)
}
}
var buf bytes.Buffer
err := tarDirectory(dir, &buf)
assert.NoError(t, err)
tr := tar.NewReader(&buf)
found := make(map[string]string)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
if hdr.Typeflag == tar.TypeReg {
contentBytes, err := io.ReadAll(tr)
if err != nil {
t.Fatal(err)
}
found[hdr.Name] = string(contentBytes)
}
}
assert.Equal(t, items, found)
}
|