Skip to content

Commit 6f325bf

Browse files
committed
chore: add a non-random test to reserve sample
1 parent 1fd7740 commit 6f325bf

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

pkg/storer/sample_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/ethersphere/bee/v2/pkg/cac"
1314
"github.com/ethersphere/bee/v2/pkg/postage"
1415

1516
postagetesting "github.com/ethersphere/bee/v2/pkg/postage/testing"
@@ -309,6 +310,63 @@ func assertValidSample(t *testing.T, sample storer.Sample, minRadius uint8, anch
309310
}
310311
}
311312

313+
// TestSampleVectorCAC is a deterministic test vector that verifies the chunk
314+
// address and transformed address produced by MakeSampleUsingChunks for a
315+
// single hardcoded CAC chunk and anchor. It guards against regressions in the
316+
// BMT hashing or sampling pipeline.
317+
func TestSampleVectorCAC(t *testing.T) {
318+
t.Parallel()
319+
320+
// Chunk content: 4096 bytes with repeating pattern i%256.
321+
chunkContent := make([]byte, swarm.ChunkSize)
322+
for i := range chunkContent {
323+
chunkContent[i] = byte(i % 256)
324+
}
325+
326+
ch, err := cac.New(chunkContent)
327+
if err != nil {
328+
t.Fatal(err)
329+
}
330+
331+
// Attach a hardcoded (but otherwise irrelevant) stamp so that
332+
// MakeSampleUsingChunks can read ch.Stamp() without panicking.
333+
batchID := make([]byte, 32)
334+
for i := range batchID {
335+
batchID[i] = byte(i + 1)
336+
}
337+
sig := make([]byte, 65)
338+
for i := range sig {
339+
sig[i] = byte(i + 1)
340+
}
341+
ch = ch.WithStamp(postage.NewStamp(batchID, make([]byte, 8), make([]byte, 8), sig))
342+
343+
// Anchor: exactly 32 bytes, constant across runs.
344+
anchor := []byte("swarm-test-anchor-deterministic!")
345+
346+
sample, err := storer.MakeSampleUsingChunks([]swarm.Chunk{ch}, anchor)
347+
if err != nil {
348+
t.Fatal(err)
349+
}
350+
351+
if len(sample.Items) != 1 {
352+
t.Fatalf("expected 1 sample item, got %d", len(sample.Items))
353+
}
354+
355+
item := sample.Items[0]
356+
357+
const (
358+
wantChunkAddr = "902406053a7a2f3a17f16097e1d0b4b6a4abeae6b84968f5503ae621f9522e16"
359+
wantTransformedAddr = "9dee91d1ed794460474ffc942996bd713176731db4581a3c6470fe9862905a60"
360+
)
361+
362+
if got := item.ChunkAddress.String(); got != wantChunkAddr {
363+
t.Errorf("chunk address mismatch:\n got: %s\n want: %s", got, wantChunkAddr)
364+
}
365+
if got := item.TransformedAddress.String(); got != wantTransformedAddr {
366+
t.Errorf("transformed address mismatch:\n got: %s\n want: %s", got, wantTransformedAddr)
367+
}
368+
}
369+
312370
func assertSampleNoErrors(t *testing.T, sample storer.Sample) {
313371
t.Helper()
314372

0 commit comments

Comments
 (0)