-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathstorage_test.go
More file actions
322 lines (280 loc) · 8.48 KB
/
Copy pathstorage_test.go
File metadata and controls
322 lines (280 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2024 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package storage_test
import (
"bytes"
"encoding/hex"
"errors"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/v2/pkg/cac"
"github.com/ethersphere/bee/v2/pkg/crypto"
"github.com/ethersphere/bee/v2/pkg/postage"
postagetesting "github.com/ethersphere/bee/v2/pkg/postage/testing"
"github.com/ethersphere/bee/v2/pkg/soc"
"github.com/ethersphere/bee/v2/pkg/storage"
testingc "github.com/ethersphere/bee/v2/pkg/storage/testing"
"github.com/ethersphere/bee/v2/pkg/swarm"
)
func TestIdentityAddress(t *testing.T) {
t.Run("single owner chunk", func(t *testing.T) {
t.Parallel()
// Create a single owner chunk (SOC)
owner := common.HexToAddress("8d3766440f0d7b949a5e32995d09619a7f86e632")
// signature of hash(id + chunk address of foo)
sig, err := hex.DecodeString("5acd384febc133b7b245e5ddc62d82d2cded9182d2716126cd8844509af65a053deb418208027f548e3e88343af6f84a8772fb3cebc0a1833a0ea7ec0c1348311b")
if err != nil {
t.Fatal(err)
}
id := make([]byte, swarm.HashSize)
copy(id, []byte("id"))
payload := []byte("foo")
ch, err := cac.New(payload)
if err != nil {
t.Fatal(err)
}
sch, err := soc.NewSigned(id, ch, owner.Bytes(), sig)
if err != nil {
t.Fatal(err)
}
schChunk, err := sch.Chunk()
if err != nil {
t.Fatal(err)
}
schAddress, err := sch.Address()
if err != nil {
t.Fatal(err)
}
idAddr, err := storage.IdentityAddress(schChunk)
if err != nil {
t.Fatalf("IdentityAddress returned error: %v", err)
}
if idAddr.IsZero() {
t.Fatalf("expected non-zero address, got zero address")
}
if idAddr.Equal(schAddress) {
t.Fatalf("expected identity address to be different from SOC address")
}
})
t.Run("content addressed chunk", func(t *testing.T) {
t.Parallel()
// Create a content addressed chunk (CAC)
data := []byte("data")
cacChunk, err := cac.New(data)
if err != nil {
t.Fatalf("create content addressed chunk: %v", err)
}
// Call IdentityAddress with the CAC
addr, err := storage.IdentityAddress(cacChunk)
if err != nil {
t.Fatalf("IdentityAddress returned error: %v", err)
}
// Verify the address matches the CAC address
if !addr.Equal(cacChunk.Address()) {
t.Fatalf("expected address %s, got %s", cacChunk.Address(), addr)
}
})
}
func TestChunkSum(t *testing.T) {
t.Parallel()
t.Run("cac is deterministic and 16 bytes", func(t *testing.T) {
t.Parallel()
ch := testingc.GenerateTestRandomChunk()
sum, err := storage.ChunkSum(ch)
if err != nil {
t.Fatal(err)
}
if len(sum) != storage.ChunkSumSize {
t.Fatalf("expected sum length %d, got %d", storage.ChunkSumSize, len(sum))
}
again, err := storage.ChunkSum(ch)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(sum, again) {
t.Fatal("expected deterministic sum")
}
})
t.Run("divergent socs share address but produce different sums", func(t *testing.T) {
t.Parallel()
privKey, err := crypto.GenerateSecp256k1Key()
if err != nil {
t.Fatal(err)
}
signer := crypto.NewDefaultSigner(privKey)
id := make([]byte, swarm.HashSize)
inner1, err := cac.New([]byte("content-one"))
if err != nil {
t.Fatal(err)
}
inner2, err := cac.New([]byte("content-two"))
if err != nil {
t.Fatal(err)
}
soc1, err := soc.New(id, inner1).Sign(signer)
if err != nil {
t.Fatal(err)
}
soc2, err := soc.New(id, inner2).Sign(signer)
if err != nil {
t.Fatal(err)
}
// same owner and id yield the same SOC address despite different content
if !soc1.Address().Equal(soc2.Address()) {
t.Fatalf("expected equal soc addresses, got %s and %s", soc1.Address(), soc2.Address())
}
// stamp both with the same stamp so batchID and stampHash are identical;
// only the folded wrapped-CAC address should differ the sums.
stamp := postagetesting.MustNewStamp()
sum1, err := storage.ChunkSum(soc1.WithStamp(stamp))
if err != nil {
t.Fatal(err)
}
sum2, err := storage.ChunkSum(soc2.WithStamp(stamp))
if err != nil {
t.Fatal(err)
}
if bytes.Equal(sum1, sum2) {
t.Fatal("divergent socs must produce different sums")
}
})
t.Run("cac sum survives stamp marshal round-trip", func(t *testing.T) {
t.Parallel()
ch := testingc.GenerateTestRandomChunk()
s1, err := storage.ChunkSum(ch)
if err != nil {
t.Fatal(err)
}
raw, err := ch.Stamp().MarshalBinary()
if err != nil {
t.Fatal(err)
}
st := new(postage.Stamp)
if err := st.UnmarshalBinary(raw); err != nil {
t.Fatal(err)
}
s2, err := storage.ChunkSum(swarm.NewChunk(ch.Address(), ch.Data()).WithStamp(st))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(s1, s2) {
t.Fatal("sum must be stable across stamp marshal round-trip")
}
})
}
// FuzzChunkSum exercises the checksum computation with fully arbitrary chunk
// data and stamp fields. Pullsync recomputes the sum on delivered chunks
// before their validity is checked, so this path processes attacker-controlled
// bytes and must never panic. It also pins the equivalence of ChunkSum and
// ChunkSumFromParts whenever a sum is computable at all.
func FuzzChunkSum(f *testing.F) {
inner, err := cac.New([]byte("seed payload"))
if err != nil {
f.Fatal(err)
}
validSoc := testingc.GenerateTestRandomSoChunk(f, inner)
f.Add(validSoc.Data(), []byte("batch"), []byte("index"), []byte("ts"), []byte("sig"))
f.Add([]byte{}, []byte{}, []byte{}, []byte{}, []byte{})
f.Add(make([]byte, swarm.ChunkWithSpanSize), make([]byte, 32), make([]byte, 8), make([]byte, 8), make([]byte, 65))
f.Fuzz(func(t *testing.T, data, batchID, index, ts, sig []byte) {
if len(data) > swarm.SocMaxChunkSize {
t.Skip()
}
hasher := swarm.NewHasher()
_, _ = hasher.Write(data)
ch := swarm.NewChunk(swarm.NewAddress(hasher.Sum(nil)), data).
WithStamp(postage.NewStamp(batchID, index, ts, sig))
sum, err := storage.ChunkSum(ch) // must not panic on any input
if err != nil {
return
}
if len(sum) != storage.ChunkSumSize {
t.Fatalf("sum length %d, want %d", len(sum), storage.ChunkSumSize)
}
stampHash, err := ch.Stamp().Hash()
if err != nil {
t.Fatalf("sum computed but stamp hash failed: %v", err)
}
fromParts, err := storage.ChunkSumFromParts(ch.Stamp().BatchID(), stampHash, ch)
if err != nil {
t.Fatalf("sum computed but parts variant failed: %v", err)
}
if !bytes.Equal(sum, fromParts) {
t.Fatal("ChunkSum and ChunkSumFromParts disagree")
}
})
}
func TestDivergentSocChunkWins(t *testing.T) {
t.Parallel()
privKey, err := crypto.GenerateSecp256k1Key()
if err != nil {
t.Fatal(err)
}
signer := crypto.NewDefaultSigner(privKey)
id := make([]byte, swarm.HashSize)
newSOC := func(data string) swarm.Chunk {
t.Helper()
inner, err := cac.New([]byte(data))
if err != nil {
t.Fatal(err)
}
ch, err := soc.New(id, inner).Sign(signer)
if err != nil {
t.Fatal(err)
}
return ch.WithStamp(postagetesting.MustNewStamp())
}
lower, higher := newSOC("content-one"), newSOC("content-two")
lowerInner, err := soc.UnwrapCAC(lower)
if err != nil {
t.Fatal(err)
}
higherInner, err := soc.UnwrapCAC(higher)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(lowerInner.Address().Bytes(), higherInner.Address().Bytes()) > 0 {
lower, higher = higher, lower
}
t.Run("lower wrapped address wins", func(t *testing.T) {
t.Parallel()
wins, err := storage.DivergentSocChunkWins(higher, lower)
if err != nil {
t.Fatal(err)
}
if !wins {
t.Fatal("expected the chunk wrapping the lower cac address to win")
}
})
t.Run("tie-break is antisymmetric", func(t *testing.T) {
t.Parallel()
wins, err := storage.DivergentSocChunkWins(lower, higher)
if err != nil {
t.Fatal(err)
}
if wins {
t.Fatal("expected the chunk wrapping the higher cac address to lose")
}
})
t.Run("a chunk does not displace itself", func(t *testing.T) {
t.Parallel()
wins, err := storage.DivergentSocChunkWins(lower, lower)
if err != nil {
t.Fatal(err)
}
if wins {
t.Fatal("expected an identical chunk not to win")
}
})
t.Run("content addressed chunks cannot diverge", func(t *testing.T) {
t.Parallel()
cac := testingc.GenerateTestRandomChunk()
if _, err := storage.DivergentSocChunkWins(cac, lower); !errors.Is(err, storage.ErrUnknownChunkType) {
t.Fatalf("expected ErrUnknownChunkType, got %v", err)
}
if _, err := storage.DivergentSocChunkWins(lower, cac); !errors.Is(err, storage.ErrUnknownChunkType) {
t.Fatalf("expected ErrUnknownChunkType, got %v", err)
}
})
}