118 lines
3.7 KiB
Go
118 lines
3.7 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func TestPrepareChatAttachmentsWritesPrivateFileAndPart(t *testing.T) {
|
|
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "uploads"))
|
|
data := base64.StdEncoding.EncodeToString([]byte("hello attachment"))
|
|
files, parts, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{
|
|
Name: "../../note.txt", Mime: "text/plain", Data: data,
|
|
}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(files) != 1 || len(parts) != 1 {
|
|
t.Fatalf("files=%d parts=%d", len(files), len(parts))
|
|
}
|
|
if files[0].Name != "note.txt" || parts[0].Name != "note.txt" || parts[0].Mime != "text/plain" {
|
|
t.Fatalf("attachment = %+v / %+v", files[0], parts[0])
|
|
}
|
|
path := strings.TrimPrefix(files[0].URI, "file://")
|
|
body, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(body) != "hello attachment" {
|
|
t.Fatalf("body = %q", body)
|
|
}
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info.Mode().Perm() != 0o600 {
|
|
t.Fatalf("mode = %o, want 0600", info.Mode().Perm())
|
|
}
|
|
if filepath.Dir(path) == filepath.Dir("/tmp") {
|
|
t.Fatalf("attachment escaped its private directory: %s", path)
|
|
}
|
|
}
|
|
|
|
func TestChatAttachmentStorageRetentionLeaseAndSymlink(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "uploads")
|
|
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
|
|
attachment := []protocol.ChatAttachment{{Name: "x", Data: base64.StdEncoding.EncodeToString([]byte("private"))}}
|
|
files, _, err := prepareChatAttachments("ses_test", attachment)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := strings.TrimPrefix(files[0].URI, "file://")
|
|
old := time.Now().Add(-chatAttachmentRetention - time.Hour)
|
|
if err := os.Chtimes(path, old, old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
more, _, err := prepareChatAttachments("ses_test", attachment)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer releaseChatAttachments(more)
|
|
if _, err := os.Stat(path); err != nil {
|
|
t.Fatal("queued/active file collected", err)
|
|
}
|
|
releaseChatAttachments(files)
|
|
last, _, err := prepareChatAttachments("ses_test", attachment)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer releaseChatAttachments(last)
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
t.Fatal("expired unleased file not collected", err)
|
|
}
|
|
link := filepath.Join(t.TempDir(), "link")
|
|
if err := os.Symlink(dir, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", link)
|
|
if _, _, err := prepareChatAttachments("ses_test", attachment); err == nil {
|
|
t.Fatal("symlink storage accepted")
|
|
}
|
|
}
|
|
|
|
func TestChatAttachmentStorageQuota(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.Chmod(dir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
|
|
file, err := os.Create(filepath.Join(dir, "quota"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := file.Truncate(chatAttachmentQuota); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
file.Close()
|
|
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "x", Data: "eA=="}}); err == nil || !strings.Contains(err.Error(), "quota") {
|
|
t.Fatalf("quota error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPrepareChatAttachmentsRejectsInvalidAndOversized(t *testing.T) {
|
|
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "uploads"))
|
|
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "x", Data: "not base64"}}); err == nil {
|
|
t.Fatal("invalid base64 accepted")
|
|
}
|
|
oversized := base64.StdEncoding.EncodeToString(make([]byte, maxChatAttachmentBytes+1))
|
|
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "big", Data: oversized}}); err == nil {
|
|
t.Fatal("oversized attachment accepted")
|
|
}
|
|
}
|