1 parent
734d389a7c
commit
ff23b27713
11 files changed
+822
-15
No files matched your search
@@ -0,0 +1,133 @@
|
||||
package zipfile
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type zipFixture struct {
|
||||
name, body string
|
||||
mode fs.FileMode
|
||||
}
|
||||
|
||||
func archiveFixture(t *testing.T, entries ...zipFixture) []byte {
|
||||
t.Helper()
|
||||
var (
|
||||
out bytes.Buffer
|
||||
)
|
||||
writer := zip.NewWriter(&out)
|
||||
for _, entry := range entries {
|
||||
header := &zip.FileHeader{Name: entry.name, Method: zip.Deflate}
|
||||
if entry.mode != 0 {
|
||||
header.SetMode(entry.mode)
|
||||
}
|
||||
file, err := writer.CreateHeader(header)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = file.Write([]byte(entry.body)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out.Bytes()
|
||||
}
|
||||
|
||||
func TestExtractionOwnsSortedFilesAndCleanup(t *testing.T) {
|
||||
parent := t.TempDir()
|
||||
content := archiveFixture(t, zipFixture{name: "./", mode: fs.ModeDir | 0755}, zipFixture{name: "./nested/b.txt", body: "second"}, zipFixture{name: "a.txt", body: "first"})
|
||||
result, err := ExtractBytes(context.Background(), content, Options{TempDir: parent})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := result.Dir()
|
||||
files := result.Files()
|
||||
want := []string{filepath.Join(root, "a.txt"), filepath.Join(root, "nested", "b.txt")}
|
||||
if !reflect.DeepEqual(files, want) {
|
||||
t.Fatalf("files=%v", files)
|
||||
}
|
||||
data, err := os.ReadFile(files[0])
|
||||
if err != nil || string(data) != "first" {
|
||||
t.Fatalf("data=%q err=%v", data, err)
|
||||
}
|
||||
files[0] = "changed"
|
||||
if result.Files()[0] != want[0] {
|
||||
t.Fatal("caller changed owned list")
|
||||
}
|
||||
if err = result.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = result.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = os.Stat(root); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatal("temporary files retained")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailureCleansPartialExtraction(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
entries []zipFixture
|
||||
options Options
|
||||
}{
|
||||
{name: "traversal", entries: []zipFixture{{name: "good", body: "ok"}, {name: "../escape", body: "bad"}}},
|
||||
{name: "absolute", entries: []zipFixture{{name: "/escape", body: "bad"}}},
|
||||
{name: "backslash", entries: []zipFixture{{name: "..\\escape", body: "bad"}}},
|
||||
{name: "symlink", entries: []zipFixture{{name: "link", body: "../escape", mode: fs.ModeSymlink | 0777}}},
|
||||
{name: "duplicate", entries: []zipFixture{{name: "same", body: "one"}, {name: "same", body: "two"}}},
|
||||
{name: "bytes", entries: []zipFixture{{name: "one", body: "abc"}, {name: "two", body: "d"}}, options: Options{MaxBytes: 3}},
|
||||
{name: "entries", entries: []zipFixture{{name: "one"}, {name: "two"}}, options: Options{MaxEntries: 1}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
test.options.TempDir = t.TempDir()
|
||||
result, err := ExtractBytes(context.Background(), archiveFixture(t, test.entries...), test.options)
|
||||
if err == nil || result != nil {
|
||||
t.Fatal("invalid archive accepted")
|
||||
}
|
||||
left, err := os.ReadDir(test.options.TempDir)
|
||||
if err != nil || len(left) != 0 {
|
||||
t.Fatalf("partial extraction retained: %v %v", left, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLimitsAndCancellation(t *testing.T) {
|
||||
parent := t.TempDir()
|
||||
body := archiveFixture(t, zipFixture{name: "one", body: "abc"})
|
||||
result, err := ExtractBytes(context.Background(), body, Options{TempDir: parent, MaxBytes: 3, MaxEntries: 1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer result.Close()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if _, err = ExtractBytes(ctx, body, Options{TempDir: parent}); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("cancel cause lost: %v", err)
|
||||
}
|
||||
var (
|
||||
out bytes.Buffer
|
||||
)
|
||||
if _, err = (cancelWriter{ctx: ctx, output: &out}).Write([]byte("data")); !errors.Is(err, context.Canceled) || out.Len() != 0 {
|
||||
t.Fatal("canceled write proceeded")
|
||||
}
|
||||
if _, err = Extract(context.Background(), nil, Options{}); err == nil {
|
||||
t.Fatal("nil reader accepted")
|
||||
}
|
||||
if _, err = ExtractBytes(context.Background(), body, Options{MaxBytes: -1}); err == nil {
|
||||
t.Fatal("negative quota accepted")
|
||||
}
|
||||
if _, err = ExtractBytes(context.Background(), []byte("not a zip"), Options{}); !errors.Is(err, zip.ErrFormat) {
|
||||
t.Fatalf("open cause lost: %v", err)
|
||||
}
|
||||
}
|
||||