Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
86 lines
2.7 KiB
Go
86 lines
2.7 KiB
Go
package processing
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"messenger/internal/attachment"
|
|
"messenger/internal/broker"
|
|
"messenger/internal/pipeline"
|
|
)
|
|
|
|
type stagedSource struct {
|
|
attachment.Blobs
|
|
body string
|
|
}
|
|
|
|
func (s stagedSource) Open(context.Context, string) (io.ReadCloser, error) {
|
|
return io.NopCloser(strings.NewReader(s.body)), nil
|
|
}
|
|
|
|
func TestReadOriginalValidatesPersistedSizeAndDigest(t *testing.T) {
|
|
h := sha256.Sum256([]byte("accepted"))
|
|
c := &pipeline.Claim{SizeBytes: 8, Hash: h[:], SourceKey: "staging/test"}
|
|
for _, bad := range []string{"accept", "altered!", "oversized bytes"} {
|
|
_, err := readOriginal(t.Context(), stagedSource{body: bad}, c)
|
|
if !errors.Is(err, errSourceMismatch) {
|
|
t.Fatalf("body %q: %v", bad, err)
|
|
}
|
|
}
|
|
got, err := readOriginal(t.Context(), stagedSource{body: "accepted"}, c)
|
|
if err != nil || string(got) != "accepted" {
|
|
t.Fatalf("valid original: %v", err)
|
|
}
|
|
}
|
|
|
|
type failingSource struct{ attachment.Blobs }
|
|
|
|
func (failingSource) Open(context.Context, string) (io.ReadCloser, error) {
|
|
return nil, fmt.Errorf("credentials=private-source-data: %w", context.DeadlineExceeded)
|
|
}
|
|
|
|
func TestFailureLogReportsStageAndReason(t *testing.T) {
|
|
var log strings.Builder
|
|
w := Worker{Blobs: failingSource{}, Log: slog.New(slog.NewJSONHandler(&log, nil))}
|
|
c := &pipeline.Claim{SizeBytes: 5, SourceKey: "sensitive-source-key"}
|
|
_, failure := w.writeObjects(t.Context(), c)
|
|
if failure == nil {
|
|
t.Fatal("expected failure reading source")
|
|
}
|
|
got := log.String()
|
|
for _, want := range []string{`"stage":"read_original"`, `"reason":"deadline_exceeded"`, `"error_code":"processing_failed"`, `"job_id":`, `"attempt":`} {
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("log missing %s: %s", want, got)
|
|
}
|
|
}
|
|
if strings.Contains(got, "private-source-data") || strings.Contains(got, "sensitive-source-key") {
|
|
t.Errorf("leaked source details: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestConsumerSetupRetryClassifiesFailureWithoutCredentials(t *testing.T) {
|
|
var logs strings.Builder
|
|
w := Worker{Log: slog.New(slog.NewJSONHandler(&logs, nil))}
|
|
w.Config.URL = "amqp://secret-user:secret-password@%invalidhost"
|
|
err := w.consume(t.Context())
|
|
if err == nil {
|
|
t.Fatal("invalid address connected")
|
|
}
|
|
logRetry(t.Context(), w.Log, "consumer", err)
|
|
got := logs.String()
|
|
if !strings.Contains(got, "broker_connect") || strings.Contains(got, "secret-") || strings.Contains(got, "invalidhost") {
|
|
t.Fatalf("unsafe or unclassified: %s", got)
|
|
}
|
|
logs.Reset()
|
|
logRetry(t.Context(), w.Log, "dispatcher", fmt.Errorf("sensitive URL: %w", broker.ErrPublish))
|
|
if got := logs.String(); !strings.Contains(got, "broker_publish") || strings.Contains(got, "sensitive") {
|
|
t.Fatalf("unsafe or unclassified: %s", got)
|
|
}
|
|
}
|