|
15 | 15 | package memblob |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "bytes" |
18 | 19 | "context" |
| 20 | + "crypto/md5" |
19 | 21 | "net/http" |
20 | 22 | "testing" |
21 | 23 |
|
@@ -76,6 +78,35 @@ func BenchmarkMemblob(b *testing.B) { |
76 | 78 | drivertest.RunBenchmarks(b, OpenBucket(nil)) |
77 | 79 | } |
78 | 80 |
|
| 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 | + |
79 | 110 | func TestOpenBucketFromURL(t *testing.T) { |
80 | 111 | tests := []struct { |
81 | 112 | URL string |
|
0 commit comments