@@ -53,7 +53,7 @@ func TestOutOfRangeLeafRequests(t *testing.T) {
53
53
return // Expected for invalid ranges
54
54
}
55
55
// If we got a partial DAG, verify it's valid
56
- if err := partial .VerifyPartial (); err != nil {
56
+ if err := partial .Verify (); err != nil {
57
57
t .Errorf ("Invalid partial DAG returned for range %d-%d: %v" , tt .start , tt .end , err )
58
58
}
59
59
})
@@ -210,37 +210,40 @@ func TestInvalidPaths(t *testing.T) {
210
210
}
211
211
212
212
func TestBrokenDags (t * testing.T ) {
213
- tmpDir , err := ioutil .TempDir ("" , "test" )
214
- if err != nil {
215
- t .Fatalf ("Could not create temp directory: %s" , err )
216
- }
217
- defer os .RemoveAll (tmpDir )
213
+ // Create a valid DAG with known structure
214
+ dagBuilder := CreateDagBuilder ()
218
215
219
- // Create a valid DAG first
220
- err = ioutil .WriteFile (filepath .Join (tmpDir , "test.txt" ), []byte ("test content" ), 0644 )
216
+ // Create a file leaf
217
+ fileBuilder := CreateDagLeafBuilder ("test.txt" )
218
+ fileBuilder .SetType (FileLeafType )
219
+ fileBuilder .SetData ([]byte ("test content" ))
220
+ fileLeaf , err := fileBuilder .BuildLeaf (nil )
221
221
if err != nil {
222
- t .Fatalf ("Failed to create test file: %v" , err )
222
+ t .Fatalf ("Failed to build file leaf : %v" , err )
223
223
}
224
+ fileLeaf .SetLabel ("1" )
225
+ dagBuilder .AddLeaf (fileLeaf , nil )
224
226
225
- dag , err := CreateDag (tmpDir , false )
227
+ // Create a directory with the file
228
+ dirBuilder := CreateDagLeafBuilder ("testdir" )
229
+ dirBuilder .SetType (DirectoryLeafType )
230
+ dirBuilder .AddLink ("1" , fileLeaf .Hash )
231
+ dirLeaf , err := dirBuilder .BuildRootLeaf (dagBuilder , nil )
226
232
if err != nil {
227
- t .Fatalf ("Failed to create DAG : %v" , err )
233
+ t .Fatalf ("Failed to build directory leaf : %v" , err )
228
234
}
235
+ dagBuilder .AddLeaf (dirLeaf , nil )
236
+
237
+ dag := dagBuilder .BuildDag (dirLeaf .Hash )
229
238
230
239
t .Run ("missing_leaf" , func (t * testing.T ) {
231
240
brokenDag := & Dag {
232
241
Root : dag .Root ,
233
242
Leafs : make (map [string ]* DagLeaf ),
234
243
}
235
- // Copy all leaves except one
236
- var skippedOne bool
237
- for hash , leaf := range dag .Leafs {
238
- if ! skippedOne {
239
- skippedOne = true
240
- continue
241
- }
242
- brokenDag .Leafs [hash ] = leaf
243
- }
244
+ // Only copy the root leaf
245
+ brokenDag .Leafs [dag .Root ] = dag .Leafs [dag .Root ].Clone ()
246
+
244
247
if err := brokenDag .Verify (); err == nil {
245
248
t .Error ("Expected verification to fail for DAG with missing leaf" )
246
249
}
@@ -251,11 +254,18 @@ func TestBrokenDags(t *testing.T) {
251
254
Root : dag .Root ,
252
255
Leafs : make (map [string ]* DagLeaf ),
253
256
}
254
- // Copy all leaves but corrupt one's content
257
+ // Copy all leaves but corrupt file content
255
258
for hash , leaf := range dag .Leafs {
256
259
leafCopy := leaf .Clone ()
257
- if leaf .Type == FileLeafType || leaf .Type == ChunkLeafType {
258
- leafCopy .Content = append (leafCopy .Content , []byte ("corrupted" )... )
260
+ if leaf .Type == FileLeafType {
261
+ // Create a new leaf with corrupted content
262
+ builder := CreateDagLeafBuilder (leaf .ItemName )
263
+ builder .SetType (leaf .Type )
264
+ builder .SetData (append (leaf .Content , []byte ("corrupted" )... ))
265
+ corruptedLeaf , _ := builder .BuildLeaf (nil )
266
+ // Keep original hash but use corrupted content and hash
267
+ leafCopy .Content = corruptedLeaf .Content
268
+ leafCopy .ContentHash = corruptedLeaf .ContentHash
259
269
}
260
270
brokenDag .Leafs [hash ] = leafCopy
261
271
}
@@ -273,7 +283,12 @@ func TestBrokenDags(t *testing.T) {
273
283
for hash , leaf := range dag .Leafs {
274
284
leafCopy := leaf .Clone ()
275
285
if len (leafCopy .ClassicMerkleRoot ) > 0 {
276
- leafCopy .ClassicMerkleRoot = append (leafCopy .ClassicMerkleRoot , []byte ("corrupted" )... )
286
+ // Create a different merkle root by changing the content
287
+ builder := CreateDagLeafBuilder (leaf .ItemName )
288
+ builder .SetType (leaf .Type )
289
+ builder .AddLink ("invalid" , "invalid:hash" )
290
+ corruptedLeaf , _ := builder .BuildLeaf (nil )
291
+ leafCopy .ClassicMerkleRoot = corruptedLeaf .ClassicMerkleRoot
277
292
}
278
293
brokenDag .Leafs [hash ] = leafCopy
279
294
}
@@ -291,8 +306,13 @@ func TestBrokenDags(t *testing.T) {
291
306
for hash , leaf := range dag .Leafs {
292
307
leafCopy := leaf .Clone ()
293
308
if len (leafCopy .Links ) > 0 {
294
- // Add invalid link
295
- leafCopy .Links ["invalid" ] = "invalid:hash"
309
+ // Add invalid link while preserving CurrentLinkCount
310
+ builder := CreateDagLeafBuilder (leaf .ItemName )
311
+ builder .SetType (leaf .Type )
312
+ builder .AddLink ("invalid" , "invalid:hash" )
313
+ corruptedLeaf , _ := builder .BuildLeaf (nil )
314
+ leafCopy .Links = corruptedLeaf .Links
315
+ // CurrentLinkCount stays the same as it's part of the hash
296
316
}
297
317
brokenDag .Leafs [hash ] = leafCopy
298
318
}
0 commit comments