forked from folbricht/desync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemble_test.go
293 lines (269 loc) · 7.47 KB
/
assemble_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
package desync
import (
"bytes"
"context"
"crypto/md5"
"crypto/rand"
"io"
"io/ioutil"
"os"
"testing"
)
func TestExtract(t *testing.T) {
// Make a test file that's guaranteed to have duplicate chunks.
b, err := ioutil.ReadFile("testdata/chunker.input")
if err != nil {
t.Fatal(err)
}
for i := 0; i < 4; i++ { // Replicate it a few times to make sure we get dupes
b = append(b, b...)
}
b = append(b, make([]byte, 2*ChunkSizeMaxDefault)...) // want to have at least one null-chunk in the input
in, err := ioutil.TempFile("", "in")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(in.Name())
if _, err := io.Copy(in, bytes.NewReader(b)); err != nil {
t.Fatal(err)
}
in.Close()
// Record the checksum of the input file, used to compare to the output later
inSum := md5.Sum(b)
// Chunk the file to get an index
index, _, err := IndexFromFile(
context.Background(),
in.Name(),
10,
ChunkSizeMinDefault, ChunkSizeAvgDefault, ChunkSizeMaxDefault,
nil,
)
if err != nil {
t.Fatal(err)
}
// Chop up the input file into a (temporary) local store
store, err := ioutil.TempDir("", "store")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(store)
s, err := NewLocalStore(store, StoreOptions{})
if err != nil {
t.Fatal(err)
}
if err := ChopFile(context.Background(), in.Name(), index.Chunks, s, 10, nil); err != nil {
t.Fatal(err)
}
// Make a blank store - used to test a case where no chunk *should* be requested
blankstore, err := ioutil.TempDir("", "blankstore")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(blankstore)
bs, err := NewLocalStore(blankstore, StoreOptions{})
if err != nil {
t.Fatal(err)
}
// Prepare output files for each test - first a non-existing one
out1, err := ioutil.TempFile("", "out1")
if err != nil {
t.Fatal(err)
}
os.Remove(out1.Name())
// This one is a complete file matching what we exepct at the end
out2, err := ioutil.TempFile("", "out2")
if err != nil {
t.Fatal(err)
}
if _, err := io.Copy(out2, bytes.NewReader(b)); err != nil {
t.Fatal(err)
}
out2.Close()
defer os.Remove(out2.Name())
// Incomplete or damaged file that has most but not all data
out3, err := ioutil.TempFile("", "out3")
if err != nil {
t.Fatal(err)
}
b[0] ^= 0xff // flip some bits
b[len(b)-1] ^= 0xff
b = append(b, 0) // make it longer
if _, err := io.Copy(out3, bytes.NewReader(b)); err != nil {
t.Fatal(err)
}
out3.Close()
defer os.Remove(out3.Name())
// At this point we have the data needed for the test setup
// in - Temp file that represents the original input file
// inSub - MD5 of the input file
// index - Index file for the input file
// s - Local store containing the chunks needed to rebuild the input file
// bs - A blank local store, all GetChunk fail on it
// out1 - Just a non-existing file that gets assembled
// out2 - The output file already fully complete, no GetChunk should be needed
// out3 - Partial/damaged file with most, but not all data correct
// seedIndex + seedFile - Seed file to help assemble the input
tests := map[string]struct {
outfile string
store Store
seed []Seed
}{
"extract to new file": {outfile: out1.Name(), store: s},
"extract to complete file": {outfile: out2.Name(), store: bs},
"extract to incomplete file": {outfile: out3.Name(), store: s},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
defer os.Remove(test.outfile)
if _, err := AssembleFile(context.Background(), test.outfile, index, test.store, nil, 10, nil); err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(test.outfile)
if err != nil {
t.Fatal(err)
}
outSum := md5.Sum(b)
if inSum != outSum {
t.Fatal("checksum of extracted file doesn't match expected")
}
})
}
}
func TestSeed(t *testing.T) {
// Prepare different types of data slices that'll be used to assemble target
// and seed files with varying amount of duplication
data1, err := ioutil.ReadFile("testdata/chunker.input")
if err != nil {
t.Fatal(err)
}
null := make([]byte, 4*ChunkSizeMaxDefault)
rand1 := make([]byte, 4*ChunkSizeMaxDefault)
rand.Read(rand1)
rand2 := make([]byte, 4*ChunkSizeMaxDefault)
rand.Read(rand2)
// Setup a temporary store
store, err := ioutil.TempDir("", "store")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(store)
s, err := NewLocalStore(store, StoreOptions{})
if err != nil {
t.Fatal(err)
}
// Define tests with files with different content, by building files out
// of sets of byte slices to create duplication or not between the target and
// its seeds
tests := map[string]struct {
target [][]byte
seeds [][][]byte
}{
"extract without seed": {
target: [][]byte{rand1, rand2},
seeds: nil},
"extract all null file": {
target: [][]byte{null, null, null, null, null},
seeds: nil},
"extract repetitive file": {
target: [][]byte{data1, data1, data1, data1, data1},
seeds: nil},
"extract with single file seed": {
target: [][]byte{data1, null, null, rand1, null},
seeds: [][][]byte{
{data1, null, rand2, rand2, data1},
},
},
"extract with multiple file seeds": {
target: [][]byte{null, null, rand1, null, data1},
seeds: [][][]byte{
{rand2, null, rand2, rand2, data1},
{data1, null, rand2, rand2, data1},
{rand2},
},
},
"extract with identical file seed": {
target: [][]byte{data1, null, rand1, null, data1},
seeds: [][][]byte{
{data1, null, rand1, null, data1},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
// Build the destination file so we can chunk it
dst, err := ioutil.TempFile("", "dst")
if err != nil {
t.Fatal(err)
}
dstBytes := join(test.target...)
if _, err := io.Copy(dst, bytes.NewReader(dstBytes)); err != nil {
t.Fatal(err)
}
dst.Close()
defer os.Remove(dst.Name())
// Record the checksum of the target file, used to compare to the output later
dstSum := md5.Sum(dstBytes)
// Chunk the file to get an index
dstIndex, _, err := IndexFromFile(
context.Background(),
dst.Name(),
10,
ChunkSizeMinDefault, ChunkSizeAvgDefault, ChunkSizeMaxDefault,
nil,
)
if err != nil {
t.Fatal(err)
}
// Chop up the input file into the store
if err := ChopFile(context.Background(), dst.Name(), dstIndex.Chunks, s, 10, nil); err != nil {
t.Fatal(err)
}
// Build the seed files and indexes then populate the array of seeds
var seeds []Seed
for _, f := range test.seeds {
seedFile, err := ioutil.TempFile("", "seed")
if err != nil {
t.Fatal(err)
}
if _, err := io.Copy(seedFile, bytes.NewReader(join(f...))); err != nil {
t.Fatal(err)
}
seedFile.Close()
defer os.Remove(seedFile.Name())
seedIndex, _, err := IndexFromFile(
context.Background(),
seedFile.Name(),
10,
ChunkSizeMinDefault, ChunkSizeAvgDefault, ChunkSizeMaxDefault,
nil,
)
if err != nil {
t.Fatal(err)
}
seed, err := NewIndexSeed(dst.Name(), seedFile.Name(), seedIndex)
if err != nil {
t.Fatal(err)
}
seeds = append(seeds, seed)
}
if _, err := AssembleFile(context.Background(), dst.Name(), dstIndex, s, seeds, 10, nil); err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(dst.Name())
if err != nil {
t.Fatal(err)
}
outSum := md5.Sum(b)
if dstSum != outSum {
t.Fatal("checksum of extracted file doesn't match expected")
}
})
}
}
func join(slices ...[]byte) []byte {
var out []byte
for _, b := range slices {
out = append(out, b...)
}
return out
}