Skip to content

Commit 57338a7

Browse files
authored
blob/memblob: fix Upload not generating correct MD5 attribute (#3722)
1 parent 9558213 commit 57338a7

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

blob/memblob/memblob.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ func (w *writer) Write(p []byte) (n int, err error) {
348348
}
349349

350350
func (w *writer) Upload(r io.Reader) error {
351+
if w.md5hash != nil {
352+
r = io.TeeReader(r, w.md5hash)
353+
}
351354
_, err := w.buf.ReadFrom(r)
352355
return err
353356
}

blob/memblob/memblob_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
package memblob
1616

1717
import (
18+
"bytes"
1819
"context"
20+
"crypto/md5"
1921
"net/http"
2022
"testing"
2123

@@ -76,6 +78,35 @@ func BenchmarkMemblob(b *testing.B) {
7678
drivertest.RunBenchmarks(b, OpenBucket(nil))
7779
}
7880

81+
// TestUploadMD5 verifies that blobs written via the Upload fast-path store the
82+
// correct MD5 attribute. Previously, writer.Upload wrote only to the buffer and
83+
// skipped feeding data through the md5 hasher, so the stored MD5 was always
84+
// the hash of an empty input rather than the actual content hash.
85+
func TestUploadMD5(t *testing.T) {
86+
ctx := context.Background()
87+
b := OpenBucket(nil)
88+
defer b.Close()
89+
90+
content := []byte("hello memblob upload md5")
91+
92+
// blob.Bucket.Upload uses the driver's Upload method when available.
93+
if err := b.Upload(ctx, "testkey", bytes.NewReader(content), &blob.WriterOptions{
94+
ContentType: "text/plain",
95+
}); err != nil {
96+
t.Fatalf("Upload: %v", err)
97+
}
98+
99+
attrs, err := b.Attributes(ctx, "testkey")
100+
if err != nil {
101+
t.Fatalf("Attributes: %v", err)
102+
}
103+
104+
want := md5.Sum(content)
105+
if !bytes.Equal(attrs.MD5, want[:]) {
106+
t.Errorf("MD5 after Upload = %x, want %x", attrs.MD5, want)
107+
}
108+
}
109+
79110
func TestOpenBucketFromURL(t *testing.T) {
80111
tests := []struct {
81112
URL string

0 commit comments

Comments
 (0)