38 lines
932 B
Go
38 lines
932 B
Go
package processing
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"errors"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"messenger/internal/attachment"
|
|
"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{"truncated", "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)
|
|
}
|
|
}
|