Files
vHost/backend/internal/httpapi/upload_batch_test.go
T

57 lines
2.2 KiB
Go

package httpapi
import (
"mime/multipart"
"testing"
)
func TestBatchUploadLimitsBeforeWriting(t *testing.T) {
const mb = 1 << 20
for _, test := range []struct {
name string
files []*multipart.FileHeader
ok bool
}{
{"exact total", []*multipart.FileHeader{{Filename: "a", Size: 20 * mb}, {Filename: "b", Size: 10 * mb}}, true},
{"over total", []*multipart.FileHeader{{Filename: "a", Size: 20 * mb}, {Filename: "b", Size: 10*mb + 1}}, false},
{"over single", []*multipart.FileHeader{{Filename: "a", Size: 20*mb + 1}}, false},
{"duplicate normalized name", []*multipart.FileHeader{{Filename: "a"}, {Filename: "folder/a"}}, false},
{"empty", nil, false},
} {
t.Run(test.name, func(t *testing.T) {
_, err := batchUploadNames(test.files, 20*mb)
if (err == nil) != test.ok {
t.Fatalf("unexpected error %v", err)
}
})
}
files := make([]*multipart.FileHeader, 101)
for i := range files {
files[i] = &multipart.FileHeader{Filename: "x"}
}
if _, err := batchUploadNames(files, 20*mb); err == nil {
t.Fatal("unbounded file count accepted")
}
}
func TestFolderUploadPathsAreRootedAndUnambiguous(t *testing.T) {
headers := []*multipart.FileHeader{{Filename: "index.php", Size: 5}, {Filename: "index.php", Size: 4}}
paths := []string{"site/a/index.php", "site/b/index.php"}
names, err := batchUploadPaths(headers, 20<<20, paths)
if err != nil || names[0] != paths[0] || names[1] != paths[1] {
t.Fatalf("folder paths lost: %v %v", names, err)
}
for _, name := range []string{"../index.php", "/index.php", "site/../index.php", "site//index.php", "site/./index.php", `site\index.php`, "C:/index.php", "site/.sea-runtime/index.php", "site/.sea-trash/index.php", "site/.user.ini/index.php", "site/other.php"} {
if _, err = batchUploadPaths(headers[:1], 20<<20, []string{name}); err == nil {
t.Errorf("unsafe path accepted: %q", name)
}
}
if _, err = batchUploadPaths(headers, 20<<20, paths[:1]); err == nil {
t.Fatal("path count mismatch accepted")
}
conflicting := []*multipart.FileHeader{{Filename: "site"}, {Filename: "index.php"}}
if _, err = batchUploadPaths(conflicting, 20<<20, []string{"site", "site/index.php"}); err == nil {
t.Fatal("file/directory collision accepted")
}
}