-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathedge_test.go
323 lines (293 loc) · 8.21 KB
/
edge_test.go
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
323
package dag
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func TestOutOfRangeLeafRequests(t *testing.T) {
// Create a simple DAG with known number of leaves
tmpDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatalf("Could not create temp directory: %s", err)
}
defer os.RemoveAll(tmpDir)
// Create 5 test files
for i := 0; i < 5; i++ {
err := ioutil.WriteFile(
filepath.Join(tmpDir, string(rune('a'+i))),
[]byte("test content"),
0644,
)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
}
dag, err := CreateDag(tmpDir, false)
if err != nil {
t.Fatalf("Failed to create DAG: %v", err)
}
tests := []struct {
name string
start int
end int
}{
{"beyond_size", 10, 15},
{"negative_start", -1, 3},
{"negative_end", 0, -1},
{"start_greater_than_end", 3, 2},
{"extremely_large", 1000000, 1000001},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
partial, err := dag.GetPartial(tt.start, tt.end)
if err != nil {
return // Expected for invalid ranges
}
// If we got a partial DAG, verify it's valid
if err := partial.Verify(); err != nil {
t.Errorf("Invalid partial DAG returned for range %d-%d: %v", tt.start, tt.end, err)
}
})
}
}
func TestSingleFileScenarios(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatalf("Could not create temp directory: %s", err)
}
defer os.RemoveAll(tmpDir)
// Test cases for different file sizes and content
tests := []struct {
name string
size int
content []byte
filename string
}{
{
name: "empty_file",
size: 0,
content: []byte{},
filename: "empty.txt",
},
{
name: "small_file",
size: 1024, // 1KB
filename: "small.txt",
},
{
name: "exact_chunk_size",
size: ChunkSize,
filename: "exact.txt",
},
{
name: "larger_than_chunk",
size: ChunkSize * 2,
filename: "large.txt",
},
{
name: "special_chars",
size: 1024,
filename: "special @#$%^&.txt",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filePath := filepath.Join(tmpDir, tt.filename)
// Generate content if not provided
content := tt.content
if len(content) == 0 && tt.size > 0 {
content = bytes.Repeat([]byte("a"), tt.size)
}
// Create the test file
err := ioutil.WriteFile(filePath, content, 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Create DAG from single file
dag, err := CreateDag(filePath, false)
if err != nil {
t.Fatalf("Failed to create DAG: %v", err)
}
// Verify DAG
if err := dag.Verify(); err != nil {
t.Errorf("DAG verification failed: %v", err)
}
// For files larger than chunk size, verify chunking
if tt.size > ChunkSize {
expectedChunks := (tt.size + ChunkSize - 1) / ChunkSize
var chunkCount int
for _, leaf := range dag.Leafs {
if leaf.Type == ChunkLeafType {
chunkCount++
}
}
if chunkCount != expectedChunks {
t.Errorf("Expected %d chunks, got %d", expectedChunks, chunkCount)
}
}
// For single file DAGs, verify content
rootLeaf := dag.Leafs[dag.Root]
if rootLeaf == nil {
t.Fatal("Could not find root leaf")
}
// Get and verify the content
recreated, err := dag.GetContentFromLeaf(rootLeaf)
if err != nil {
t.Fatalf("Failed to get content from leaf: %v", err)
}
// For debugging
t.Logf("Root leaf type: %s", rootLeaf.Type)
t.Logf("Root leaf links: %d", len(rootLeaf.Links))
t.Logf("Content sizes - Original: %d, Recreated: %d", len(content), len(recreated))
if !bytes.Equal(recreated, content) {
// Print first few bytes of both for comparison
maxLen := 50
origLen := len(content)
recLen := len(recreated)
if origLen < maxLen {
maxLen = origLen
}
if recLen < maxLen {
maxLen = recLen
}
t.Errorf("Recreated content does not match original.\nOriginal first %d bytes: %v\nRecreated first %d bytes: %v",
maxLen, content[:maxLen],
maxLen, recreated[:maxLen])
}
})
}
}
func TestInvalidPaths(t *testing.T) {
tests := []struct {
name string
path string
}{
{
name: "nonexistent_path",
path: "/path/that/does/not/exist",
},
{
name: "invalid_chars_windows",
path: strings.ReplaceAll(filepath.Join(os.TempDir(), "test<>:\"/\\|?*"), "/", string(filepath.Separator)),
},
{
name: "too_long_path",
path: strings.Repeat("a", 32768), // Exceeds most systems' PATH_MAX
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := CreateDag(tt.path, false)
if err == nil {
t.Error("Expected error for invalid path, got nil")
}
})
}
}
func TestBrokenDags(t *testing.T) {
// Create a valid DAG with known structure
dagBuilder := CreateDagBuilder()
// Create a file leaf
fileBuilder := CreateDagLeafBuilder("test.txt")
fileBuilder.SetType(FileLeafType)
fileBuilder.SetData([]byte("test content"))
fileLeaf, err := fileBuilder.BuildLeaf(nil)
if err != nil {
t.Fatalf("Failed to build file leaf: %v", err)
}
fileLeaf.SetLabel("1")
dagBuilder.AddLeaf(fileLeaf, nil)
// Create a directory with the file
dirBuilder := CreateDagLeafBuilder("testdir")
dirBuilder.SetType(DirectoryLeafType)
dirBuilder.AddLink("1", fileLeaf.Hash)
dirLeaf, err := dirBuilder.BuildRootLeaf(dagBuilder, nil)
if err != nil {
t.Fatalf("Failed to build directory leaf: %v", err)
}
dagBuilder.AddLeaf(dirLeaf, nil)
dag := dagBuilder.BuildDag(dirLeaf.Hash)
t.Run("missing_leaf", func(t *testing.T) {
brokenDag := &Dag{
Root: dag.Root,
Leafs: make(map[string]*DagLeaf),
}
// Only copy the root leaf
brokenDag.Leafs[dag.Root] = dag.Leafs[dag.Root].Clone()
if err := brokenDag.Verify(); err == nil {
t.Error("Expected verification to fail for DAG with missing leaf")
}
})
t.Run("corrupted_content", func(t *testing.T) {
brokenDag := &Dag{
Root: dag.Root,
Leafs: make(map[string]*DagLeaf),
}
// Copy all leaves but corrupt file content
for hash, leaf := range dag.Leafs {
leafCopy := leaf.Clone()
if leaf.Type == FileLeafType {
// Create a new leaf with corrupted content
builder := CreateDagLeafBuilder(leaf.ItemName)
builder.SetType(leaf.Type)
builder.SetData(append(leaf.Content, []byte("corrupted")...))
corruptedLeaf, _ := builder.BuildLeaf(nil)
// Keep original hash but use corrupted content and hash
leafCopy.Content = corruptedLeaf.Content
leafCopy.ContentHash = corruptedLeaf.ContentHash
}
brokenDag.Leafs[hash] = leafCopy
}
if err := brokenDag.Verify(); err == nil {
t.Error("Expected verification to fail for DAG with corrupted content")
}
})
t.Run("invalid_merkle_proof", func(t *testing.T) {
brokenDag := &Dag{
Root: dag.Root,
Leafs: make(map[string]*DagLeaf),
}
// Copy all leaves but corrupt merkle root
for hash, leaf := range dag.Leafs {
leafCopy := leaf.Clone()
if len(leafCopy.ClassicMerkleRoot) > 0 {
// Create a different merkle root by changing the content
builder := CreateDagLeafBuilder(leaf.ItemName)
builder.SetType(leaf.Type)
builder.AddLink("invalid", "invalid:hash")
corruptedLeaf, _ := builder.BuildLeaf(nil)
leafCopy.ClassicMerkleRoot = corruptedLeaf.ClassicMerkleRoot
}
brokenDag.Leafs[hash] = leafCopy
}
if err := brokenDag.Verify(); err == nil {
t.Error("Expected verification to fail for DAG with invalid merkle proof")
}
})
t.Run("broken_parent_child", func(t *testing.T) {
brokenDag := &Dag{
Root: dag.Root,
Leafs: make(map[string]*DagLeaf),
}
// Copy all leaves but modify parent-child relationship
for hash, leaf := range dag.Leafs {
leafCopy := leaf.Clone()
if len(leafCopy.Links) > 0 {
// Add invalid link while preserving CurrentLinkCount
builder := CreateDagLeafBuilder(leaf.ItemName)
builder.SetType(leaf.Type)
builder.AddLink("invalid", "invalid:hash")
corruptedLeaf, _ := builder.BuildLeaf(nil)
leafCopy.Links = corruptedLeaf.Links
// CurrentLinkCount stays the same as it's part of the hash
}
brokenDag.Leafs[hash] = leafCopy
}
if err := brokenDag.Verify(); err == nil {
t.Error("Expected verification to fail for DAG with broken parent-child relationship")
}
})
}