forked from mtusi2026/mtusi2026lab4
525 lines
18 KiB
Go
525 lines
18 KiB
Go
package application_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/binary"
|
|
"errors"
|
|
"hash/crc32"
|
|
"image"
|
|
"image/color"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"io"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"mtusi2026lab4/internal/adapter/memory"
|
|
. "mtusi2026lab4/internal/application"
|
|
"mtusi2026lab4/internal/domain"
|
|
)
|
|
|
|
type fakeObjectStore struct {
|
|
mu sync.Mutex
|
|
objects map[string][]byte
|
|
putError error
|
|
blockDelete bool
|
|
putCalls int
|
|
deleteCalls int
|
|
lastPutKey string
|
|
}
|
|
|
|
type alwaysFailReader struct{}
|
|
|
|
func (alwaysFailReader) Read([]byte) (int, error) {
|
|
return 0, errors.New("injected interrupted upload")
|
|
}
|
|
|
|
type finalizeFailureRepository struct {
|
|
Repository
|
|
}
|
|
|
|
func (repository *finalizeFailureRepository) MarkAttachmentReady(
|
|
context.Context,
|
|
string,
|
|
time.Time,
|
|
) (domain.Attachment, error) {
|
|
return domain.Attachment{}, errors.New("injected metadata finalize failure")
|
|
}
|
|
|
|
type commitThenDisconnectRepository struct {
|
|
Repository
|
|
hideReadback bool
|
|
}
|
|
|
|
func (repository *commitThenDisconnectRepository) MarkAttachmentReady(
|
|
ctx context.Context,
|
|
attachmentID string,
|
|
updatedAt time.Time,
|
|
) (domain.Attachment, error) {
|
|
if _, err := repository.Repository.MarkAttachmentReady(ctx, attachmentID, updatedAt); err != nil {
|
|
return domain.Attachment{}, err
|
|
}
|
|
return domain.Attachment{}, errors.New("injected connection loss after commit")
|
|
}
|
|
|
|
func (repository *commitThenDisconnectRepository) GetAttachment(
|
|
ctx context.Context,
|
|
attachmentID string,
|
|
actorID string,
|
|
) (domain.Attachment, error) {
|
|
if repository.hideReadback {
|
|
return domain.Attachment{}, errors.New("injected database outage during readback")
|
|
}
|
|
return repository.Repository.GetAttachment(ctx, attachmentID, actorID)
|
|
}
|
|
|
|
func newFakeObjectStore() *fakeObjectStore {
|
|
return &fakeObjectStore{objects: make(map[string][]byte)}
|
|
}
|
|
|
|
func (store *fakeObjectStore) Put(_ context.Context, object ObjectPut) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.putCalls++
|
|
store.lastPutKey = object.Key
|
|
if store.putError != nil {
|
|
return store.putError
|
|
}
|
|
payload, err := io.ReadAll(object.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if int64(len(payload)) != object.SizeBytes {
|
|
return errors.New("declared object size differs")
|
|
}
|
|
store.objects[object.Key] = payload
|
|
return nil
|
|
}
|
|
|
|
func (store *fakeObjectStore) Get(_ context.Context, key string) (ObjectContent, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
payload, exists := store.objects[key]
|
|
if !exists {
|
|
return ObjectContent{}, errors.New("object not found")
|
|
}
|
|
copyOfPayload := append([]byte(nil), payload...)
|
|
return ObjectContent{
|
|
Body: io.NopCloser(bytes.NewReader(copyOfPayload)),
|
|
SizeBytes: int64(len(copyOfPayload)),
|
|
}, nil
|
|
}
|
|
|
|
func (store *fakeObjectStore) Delete(ctx context.Context, key string) error {
|
|
store.mu.Lock()
|
|
store.deleteCalls++
|
|
blockDelete := store.blockDelete
|
|
store.mu.Unlock()
|
|
if blockDelete {
|
|
<-ctx.Done()
|
|
return ctx.Err()
|
|
}
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
delete(store.objects, key)
|
|
return nil
|
|
}
|
|
|
|
func newAttachmentService(
|
|
t *testing.T,
|
|
objectStore ObjectStore,
|
|
) (*Service, *memory.MemoryStore, string, string) {
|
|
t.Helper()
|
|
legacyStore := memory.NewMemoryStore()
|
|
repository := memory.NewMemoryRepository(legacyStore)
|
|
user, err := repository.CreateUser(context.Background(), "uploader", "Uploader", "hash")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
chat, err := repository.CreateChat(context.Background(), "Images", []string{user.ID})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
codec, err := NewCursorCodec("attachment-test-key-with-more-than-thirty-two-bytes")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
service, err := NewService(repository, codec, time.Hour, WithObjectStore(objectStore))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return service, legacyStore, user.ID, chat.ID
|
|
}
|
|
|
|
func encodeTestImage(t *testing.T, contentType string) []byte {
|
|
t.Helper()
|
|
var payload bytes.Buffer
|
|
img := image.NewRGBA(image.Rect(0, 0, 2, 2))
|
|
img.Set(0, 0, color.RGBA{R: 255, A: 255})
|
|
var err error
|
|
if contentType == "image/png" {
|
|
err = png.Encode(&payload, img)
|
|
} else {
|
|
err = jpeg.Encode(&payload, img, nil)
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return payload.Bytes()
|
|
}
|
|
|
|
func corruptPNGAfterPrefix(t *testing.T) []byte {
|
|
t.Helper()
|
|
img := image.NewNRGBA(image.Rect(0, 0, 512, 512))
|
|
state := uint32(0x12345678)
|
|
for index := range img.Pix {
|
|
state ^= state << 13
|
|
state ^= state >> 17
|
|
state ^= state << 5
|
|
img.Pix[index] = byte(state)
|
|
}
|
|
var payload bytes.Buffer
|
|
if err := png.Encode(&payload, img); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
corrupted := payload.Bytes()
|
|
if len(corrupted) <= 70_000 {
|
|
t.Fatalf("test PNG is unexpectedly small: %d", len(corrupted))
|
|
}
|
|
corrupted[70_000] ^= 0xff
|
|
return corrupted
|
|
}
|
|
|
|
func jpegWithoutScanData(t *testing.T) []byte {
|
|
t.Helper()
|
|
payload := encodeTestImage(t, "image/jpeg")
|
|
sos := bytes.Index(payload, []byte{0xff, 0xda})
|
|
if sos < 0 || sos+4 > len(payload) {
|
|
t.Fatal("encoded JPEG has no SOS marker")
|
|
}
|
|
segmentLength := int(binary.BigEndian.Uint16(payload[sos+2 : sos+4]))
|
|
endOfHeader := sos + 2 + segmentLength
|
|
if endOfHeader > len(payload) {
|
|
t.Fatal("invalid SOS segment length in test JPEG")
|
|
}
|
|
return append([]byte(nil), payload[:endOfHeader]...)
|
|
}
|
|
|
|
func TestUploadAttachmentStoresValidatedImage(t *testing.T) {
|
|
objectStore := newFakeObjectStore()
|
|
service, metadataStore, actorID, chatID := newAttachmentService(t, objectStore)
|
|
payload := encodeTestImage(t, "image/png")
|
|
|
|
attachment, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
|
ActorID: actorID,
|
|
ChatID: chatID,
|
|
Filename: "кот.png",
|
|
DeclaredContentType: "image/png",
|
|
Body: bytes.NewReader(payload),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if attachment.Status != domain.AttachmentStatusReady {
|
|
t.Fatalf("status = %q, want ready", attachment.Status)
|
|
}
|
|
if attachment.ObjectKey == "" || strings.Contains(attachment.ObjectKey, attachment.Filename) {
|
|
t.Fatalf("object key must be server-generated: %q", attachment.ObjectKey)
|
|
}
|
|
if attachment.Width != 2 || attachment.Height != 2 || len(attachment.SHA256) != 64 {
|
|
t.Fatalf("unexpected image metadata: %+v", attachment)
|
|
}
|
|
if !bytes.Equal(objectStore.objects[attachment.ObjectKey], payload) {
|
|
t.Fatal("stored object differs from uploaded bytes")
|
|
}
|
|
stored, err := metadataStore.GetAttachmentByID(attachment.ID)
|
|
if err != nil || stored.Status != domain.AttachmentStatusReady {
|
|
t.Fatalf("ready metadata was not persisted: %+v, %v", stored, err)
|
|
}
|
|
}
|
|
|
|
func TestUploadAttachmentRejectsInvalidInputBeforeStorage(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
filename string
|
|
declaredType string
|
|
payload []byte
|
|
wantError error
|
|
}{
|
|
{name: "unsafe filename", filename: "../image.png", declaredType: "image/png", payload: encodeTestImage(t, "image/png"), wantError: ErrInvalidInput},
|
|
{name: "unsupported declared type", filename: "image.gif", declaredType: "image/gif", payload: []byte("GIF89a"), wantError: ErrUnsupportedMediaType},
|
|
{name: "MIME substitution", filename: "image.jpg", declaredType: "image/jpeg", payload: encodeTestImage(t, "image/png"), wantError: ErrUnsupportedMediaType},
|
|
{name: "corrupt image", filename: "image.png", declaredType: "image/png", payload: []byte("not a png"), wantError: ErrInvalidImage},
|
|
{name: "PNG header without image data", filename: "truncated.png", declaredType: "image/png", payload: pngConfigurationOnly(1, 1), wantError: ErrInvalidImage},
|
|
{name: "PNG corruption after 64 KiB", filename: "corrupted.png", declaredType: "image/png", payload: corruptPNGAfterPrefix(t), wantError: ErrInvalidImage},
|
|
{name: "JPEG without scan data", filename: "truncated.jpg", declaredType: "image/jpeg", payload: jpegWithoutScanData(t), wantError: ErrInvalidImage},
|
|
{name: "too large", filename: "image.png", declaredType: "image/png", payload: bytes.Repeat([]byte{0}, domain.MaxAttachmentSizeBytes+1), wantError: ErrPayloadTooLarge},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
objectStore := newFakeObjectStore()
|
|
service, _, actorID, chatID := newAttachmentService(t, objectStore)
|
|
_, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
|
ActorID: actorID,
|
|
ChatID: chatID,
|
|
Filename: testCase.filename,
|
|
DeclaredContentType: testCase.declaredType,
|
|
Body: bytes.NewReader(testCase.payload),
|
|
})
|
|
if !errors.Is(err, testCase.wantError) {
|
|
t.Fatalf("error = %v, want %v", err, testCase.wantError)
|
|
}
|
|
if objectStore.putCalls != 0 {
|
|
t.Fatalf("invalid upload reached object storage %d times", objectStore.putCalls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUploadAttachmentCompensatesObjectStoreFailure(t *testing.T) {
|
|
objectStore := newFakeObjectStore()
|
|
objectStore.putError = errors.New("injected put failure")
|
|
service, metadataStore, actorID, chatID := newAttachmentService(t, objectStore)
|
|
|
|
_, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
|
ActorID: actorID,
|
|
ChatID: chatID,
|
|
Filename: "image.jpg",
|
|
DeclaredContentType: "image/jpeg",
|
|
Body: bytes.NewReader(encodeTestImage(t, "image/jpeg")),
|
|
})
|
|
if !errors.Is(err, ErrStorageUnavailable) {
|
|
t.Fatalf("error = %v, want ErrStorageUnavailable", err)
|
|
}
|
|
if objectStore.deleteCalls != 1 {
|
|
t.Fatalf("compensating deletes = %d, want 1", objectStore.deleteCalls)
|
|
}
|
|
|
|
objectStore.mu.Lock()
|
|
keyParts := strings.Split(objectStore.lastPutKey, "/")
|
|
objectStore.mu.Unlock()
|
|
if len(keyParts) != 4 {
|
|
t.Fatalf("unexpected generated object key: %q", objectStore.lastPutKey)
|
|
}
|
|
failedAttachment, lookupErr := metadataStore.GetAttachmentByID(keyParts[2])
|
|
if lookupErr != nil {
|
|
t.Fatal(lookupErr)
|
|
}
|
|
if failedAttachment.Status != domain.AttachmentStatusFailed || failedAttachment.ErrorCode == nil {
|
|
t.Fatalf("failed upload metadata was not finalized: %+v", failedAttachment)
|
|
}
|
|
}
|
|
|
|
func TestUploadAttachmentMarksFailedBeforeBlockedObjectCleanup(t *testing.T) {
|
|
objectStore := newFakeObjectStore()
|
|
objectStore.putError = errors.New("injected put failure")
|
|
objectStore.blockDelete = true
|
|
service, metadataStore, actorID, chatID := newAttachmentService(t, objectStore)
|
|
|
|
startedAt := time.Now()
|
|
_, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
|
ActorID: actorID, ChatID: chatID, Filename: "blocked-cleanup.png",
|
|
DeclaredContentType: "image/png", Body: bytes.NewReader(encodeTestImage(t, "image/png")),
|
|
})
|
|
if !errors.Is(err, ErrStorageUnavailable) {
|
|
t.Fatalf("error = %v, want ErrStorageUnavailable", err)
|
|
}
|
|
if elapsed := time.Since(startedAt); elapsed > 2*time.Second {
|
|
t.Fatalf("bounded cleanup took %s", elapsed)
|
|
}
|
|
keyParts := strings.Split(objectStore.lastPutKey, "/")
|
|
failed, lookupErr := metadataStore.GetAttachmentByID(keyParts[2])
|
|
if lookupErr != nil {
|
|
t.Fatal(lookupErr)
|
|
}
|
|
if failed.Status != domain.AttachmentStatusFailed || failed.ErrorCode == nil {
|
|
t.Fatalf("blocked object cleanup consumed DB cleanup context: %+v", failed)
|
|
}
|
|
}
|
|
|
|
func TestUploadAttachmentRejectsInterruptedBodyBeforeMetadata(t *testing.T) {
|
|
objectStore := newFakeObjectStore()
|
|
service, metadataStore, actorID, chatID := newAttachmentService(t, objectStore)
|
|
payload := encodeTestImage(t, "image/png")
|
|
|
|
_, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
|
ActorID: actorID,
|
|
ChatID: chatID,
|
|
Filename: "interrupted.png",
|
|
DeclaredContentType: "image/png",
|
|
Body: io.MultiReader(bytes.NewReader(payload[:8]), alwaysFailReader{}),
|
|
})
|
|
if !errors.Is(err, ErrInvalidInput) {
|
|
t.Fatalf("error = %v, want ErrInvalidInput", err)
|
|
}
|
|
if objectStore.putCalls != 0 {
|
|
t.Fatal("interrupted upload reached object storage")
|
|
}
|
|
if _, lookupErr := metadataStore.GetAttachmentByID("missing"); !errors.Is(lookupErr, domain.ErrNotFound) {
|
|
t.Fatalf("unexpected metadata lookup result: %v", lookupErr)
|
|
}
|
|
}
|
|
|
|
func TestUploadAttachmentPreservesObjectOnMetadataFinalizeFailure(t *testing.T) {
|
|
legacyStore := memory.NewMemoryStore()
|
|
repository := memory.NewMemoryRepository(legacyStore)
|
|
user, err := repository.CreateUser(context.Background(), "finalize", "Finalize", "hash")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
chat, err := repository.CreateChat(context.Background(), "Finalize", []string{user.ID})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
codec, err := NewCursorCodec("finalize-test-key-with-more-than-thirty-two-bytes")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
objectStore := newFakeObjectStore()
|
|
service, err := NewService(
|
|
&finalizeFailureRepository{Repository: repository},
|
|
codec,
|
|
time.Hour,
|
|
WithObjectStore(objectStore),
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
|
ActorID: user.ID,
|
|
ChatID: chat.ID,
|
|
Filename: "finalize.png",
|
|
DeclaredContentType: "image/png",
|
|
Body: bytes.NewReader(encodeTestImage(t, "image/png")),
|
|
})
|
|
if err == nil {
|
|
t.Fatal("metadata finalize failure was hidden")
|
|
}
|
|
if objectStore.deleteCalls != 0 || len(objectStore.objects) != 1 {
|
|
t.Fatalf("ambiguous object must be preserved: deletes=%d objects=%d", objectStore.deleteCalls, len(objectStore.objects))
|
|
}
|
|
keyParts := strings.Split(objectStore.lastPutKey, "/")
|
|
pending, lookupErr := legacyStore.GetAttachmentByID(keyParts[2])
|
|
if lookupErr != nil {
|
|
t.Fatal(lookupErr)
|
|
}
|
|
if pending.Status != domain.AttachmentStatusPending || pending.ErrorCode != nil {
|
|
t.Fatalf("ambiguous metadata must remain reconcilable: %+v", pending)
|
|
}
|
|
}
|
|
|
|
func TestUploadAttachmentRecoversWhenCommitResponseIsLost(t *testing.T) {
|
|
legacyStore := memory.NewMemoryStore()
|
|
baseRepository := memory.NewMemoryRepository(legacyStore)
|
|
user, err := baseRepository.CreateUser(context.Background(), "commit-lost", "Commit Lost", "hash")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
chat, err := baseRepository.CreateChat(context.Background(), "Commit Lost", []string{user.ID})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
codec, err := NewCursorCodec("commit-lost-test-key-with-more-than-thirty-two-bytes")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
objectStore := newFakeObjectStore()
|
|
repository := &commitThenDisconnectRepository{Repository: baseRepository}
|
|
service, err := NewService(repository, codec, time.Hour, WithObjectStore(objectStore))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
attachment, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
|
ActorID: user.ID, ChatID: chat.ID, Filename: "commit.png",
|
|
DeclaredContentType: "image/png", Body: bytes.NewReader(encodeTestImage(t, "image/png")),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("committed ready attachment was not recovered: %v", err)
|
|
}
|
|
if attachment.Status != domain.AttachmentStatusReady || len(objectStore.objects) != 1 || objectStore.deleteCalls != 0 {
|
|
t.Fatalf("lost commit response damaged attachment: attachment=%+v deletes=%d objects=%d",
|
|
attachment, objectStore.deleteCalls, len(objectStore.objects))
|
|
}
|
|
}
|
|
|
|
func TestUploadAttachmentNeverDeletesObjectAfterAmbiguousCommit(t *testing.T) {
|
|
legacyStore := memory.NewMemoryStore()
|
|
baseRepository := memory.NewMemoryRepository(legacyStore)
|
|
user, err := baseRepository.CreateUser(context.Background(), "commit-outage", "Commit Outage", "hash")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
chat, err := baseRepository.CreateChat(context.Background(), "Commit Outage", []string{user.ID})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
codec, err := NewCursorCodec("commit-outage-test-key-with-more-than-thirty-two-bytes")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
objectStore := newFakeObjectStore()
|
|
repository := &commitThenDisconnectRepository{Repository: baseRepository, hideReadback: true}
|
|
service, err := NewService(repository, codec, time.Hour, WithObjectStore(objectStore))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
|
ActorID: user.ID, ChatID: chat.ID, Filename: "commit.png",
|
|
DeclaredContentType: "image/png", Body: bytes.NewReader(encodeTestImage(t, "image/png")),
|
|
})
|
|
if err == nil {
|
|
t.Fatal("database outage after commit was hidden")
|
|
}
|
|
keyParts := strings.Split(objectStore.lastPutKey, "/")
|
|
stored, lookupErr := legacyStore.GetAttachmentByID(keyParts[2])
|
|
if lookupErr != nil {
|
|
t.Fatal(lookupErr)
|
|
}
|
|
if stored.Status != domain.AttachmentStatusReady || len(objectStore.objects) != 1 || objectStore.deleteCalls != 0 {
|
|
t.Fatalf("committed ready metadata lost its original: metadata=%+v deletes=%d objects=%d",
|
|
stored, objectStore.deleteCalls, len(objectStore.objects))
|
|
}
|
|
}
|
|
|
|
func TestUploadAttachmentRejectsPixelLimitBeforeFullDecode(t *testing.T) {
|
|
objectStore := newFakeObjectStore()
|
|
service, _, actorID, chatID := newAttachmentService(t, objectStore)
|
|
|
|
_, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
|
ActorID: actorID,
|
|
ChatID: chatID,
|
|
Filename: "huge.png",
|
|
DeclaredContentType: "image/png",
|
|
Body: bytes.NewReader(pngConfigurationOnly(10_000, 4_001)),
|
|
})
|
|
if !errors.Is(err, ErrPayloadTooLarge) {
|
|
t.Fatalf("error = %v, want ErrPayloadTooLarge", err)
|
|
}
|
|
if objectStore.putCalls != 0 {
|
|
t.Fatalf("oversized decoded image reached object storage %d times", objectStore.putCalls)
|
|
}
|
|
}
|
|
|
|
func pngConfigurationOnly(width uint32, height uint32) []byte {
|
|
payload := make([]byte, 33)
|
|
copy(payload[0:8], []byte{137, 80, 78, 71, 13, 10, 26, 10})
|
|
binary.BigEndian.PutUint32(payload[8:12], 13)
|
|
copy(payload[12:16], "IHDR")
|
|
binary.BigEndian.PutUint32(payload[16:20], width)
|
|
binary.BigEndian.PutUint32(payload[20:24], height)
|
|
payload[24] = 8
|
|
payload[25] = 6
|
|
payload[26] = 0
|
|
payload[27] = 0
|
|
payload[28] = 0
|
|
binary.BigEndian.PutUint32(payload[29:33], crc32.ChecksumIEEE(payload[12:29]))
|
|
return payload
|
|
}
|