Skip to content

Commit

Permalink
cov
Browse files Browse the repository at this point in the history
Signed-off-by: Lixia (Sylvia) Lei <lixlei@microsoft.com>
  • Loading branch information
Wwwsylvia committed Jan 13, 2025
1 parent e5a0559 commit 73c0aee
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions content/file/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.
package file

import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"errors"
Expand Down Expand Up @@ -242,3 +244,120 @@ func Test_extractTarGzip_Error(t *testing.T) {
}
})
}

func Test_extractTarDirectory(t *testing.T) {
tests := []struct {
name string
tarData []byte
wantFiles map[string]string // map of file paths to their expected contents
wantErr bool
}{
{
name: "valid files",
tarData: createTar(t, []tarEntry{
{name: "base/", mode: os.ModeDir | 0777},
{name: "base/test.txt", content: "hello world", mode: 0666},
{name: "base/file_symlink", linkname: "base/test.txt", mode: os.ModeSymlink | 0666},
}),
wantFiles: map[string]string{
"base/test.txt": "hello world",
"base/file_symlink": "hello world",
},
wantErr: false,
},
{
name: "non-regular files",
tarData: createTar(t, []tarEntry{
{name: "something", isNonRegular: true},
}),
wantErr: true,
},
{
name: "invalid tar header",
tarData: []byte("random data"),
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
dirName := "base"
dirPath := filepath.Join(tempDir, dirName)
buf := make([]byte, 1024)

if err := extractTarDirectory(dirPath, dirName, bytes.NewReader(tt.tarData), buf); (err != nil) != tt.wantErr {
t.Fatalf("extractTarDirectory() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr {
for path, wantContent := range tt.wantFiles {
filePath := filepath.Join(tempDir, path)
fi, err := os.Lstat(filePath)
if err != nil {
t.Fatalf("failed to stat file %s: %v", filePath, err)
}

if fi.Mode()&os.ModeSymlink != 0 {
filePath, err = os.Readlink(filePath)
if err != nil {
t.Fatalf("failed to read link %s: %v", filePath, err)
}
if !filepath.IsAbs(filePath) {
filePath = filepath.Join(tempDir, filePath)
}
}
gotContent, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("failed to read file %s: %v", filePath, err)
}
if string(gotContent) != wantContent {
t.Errorf("file content = %s, want %s", gotContent, wantContent)
}
}
}
})
}
}

type tarEntry struct {
name string
content string
linkname string
mode os.FileMode
isNonRegular bool
isHardLink bool
}

func createTar(t *testing.T, entries []tarEntry) []byte {
var buf bytes.Buffer
tw := tar.NewWriter(&buf)

for _, entry := range entries {
hdr := &tar.Header{
Name: entry.name,
Mode: int64(entry.mode.Perm()),
Size: int64(len(entry.content)),
}
if entry.isNonRegular {
hdr.Typeflag = tar.TypeBlock
} else if entry.isHardLink {
hdr.Typeflag = tar.TypeLink
hdr.Linkname = entry.linkname
} else if entry.mode&os.ModeSymlink != 0 {
hdr.Typeflag = tar.TypeSymlink
hdr.Linkname = entry.linkname
}

if err := tw.WriteHeader(hdr); err != nil {
t.Fatalf("failed to write tar header: %v", err)
}
if _, err := tw.Write([]byte(entry.content)); err != nil {
t.Fatalf("failed to write tar content: %v", err)
}
}

if err := tw.Close(); err != nil {
t.Fatalf("failed to close tar writer: %v", err)
}
return buf.Bytes()
}

0 comments on commit 73c0aee

Please sign in to comment.