From dfb5be349af98db984a0944f49896f454e4bc8a6 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Thu, 10 Apr 2025 15:08:06 +0200 Subject: pkg/osutil: add tar.gz generation helpers --- pkg/osutil/tar_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 pkg/osutil/tar_test.go (limited to 'pkg/osutil/tar_test.go') diff --git a/pkg/osutil/tar_test.go b/pkg/osutil/tar_test.go new file mode 100644 index 000000000..a0e5a22bd --- /dev/null +++ b/pkg/osutil/tar_test.go @@ -0,0 +1,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) +} -- cgit mrf-deployment