-
Notifications
You must be signed in to change notification settings - Fork 3
/
file_sharder_test.go
169 lines (148 loc) · 4.39 KB
/
file_sharder_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
package rsutils
import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"
)
func TestEncode(t *testing.T) {
testFile := getTestFile(t, "input3")
dataShards := 2
fixtureMd := getMetadata()
var parityBuffer bytes.Buffer
md, err := Encode(testFile, dataShards, []io.Writer{&parityBuffer})
if err != nil {
t.Fatal(err)
}
if md.Size != fixtureMd.Size {
t.Errorf("Expected md.Size == %d, got %d", fixtureMd.Size, md.Size)
}
if md.DataShards != fixtureMd.DataShards || md.ParityShards != fixtureMd.ParityShards {
t.Errorf("Incorrect metadata ds/ps: got %d/%d, expected %d/%d", md.DataShards, md.ParityShards, fixtureMd.DataShards, fixtureMd.ParityShards)
}
if len(md.Hashes) != len(fixtureMd.Hashes) {
t.Errorf("Incorrect number of hashes: got %d, expected %d", len(md.Hashes), len(fixtureMd.Hashes))
}
for i := range md.Hashes {
if md.Hashes[i] != fixtureMd.Hashes[i] {
t.Errorf("Incorrect metadata hash: got '%s', expected %s", md.Hashes[i], fixtureMd.Hashes[i])
}
}
}
func TestOpen(t *testing.T) {
// successful
fixtureMd := getMetadata()
testFile := getTestFile(t, "input3")
parityFile := getTestFile(t, "parity1")
_, err := Open(testFile, []*os.File{parityFile}, fixtureMd)
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
// insufficient parity files
expectedErrMsg := "Cannot open encoded files: need 1 parity shards, got 0"
_, err = Open(testFile, []*os.File{}, fixtureMd)
if err == nil || err.Error() != expectedErrMsg {
t.Errorf("Expected error '%s', got '%s'", expectedErrMsg, err)
}
}
func TestDecode(t *testing.T) {
readSize := 64
expectedOutput := `The Tyger, by William Blake!
Tyger Tyger, burning bright,
In th`
tests := []struct {
name string
dataFileName string
parityFileName string
output []byte
expectedErrorMsg string
}{
{"successful read", "input3", "parity1", []byte(expectedOutput), ""},
{"success - corrupt data", "input4_corrupt", "parity1", []byte(expectedOutput), ""},
{"success - corrupt parity", "input3", "parity2_corrupt", []byte(expectedOutput), ""},
{"failure - corrupt data+parity", "input4_corrupt", "parity2_corrupt", make([]byte, readSize), "Cannot repair data: 2 shards corrupt, only have 1 parity shards"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dataInput := cloneFileTmp(t, getTestFile(t, tt.dataFileName)).(*os.File)
parityInput := cloneFileTmp(t, getTestFile(t, tt.parityFileName)).(*os.File)
md := getMetadata()
decoder, err := Open(dataInput, []*os.File{parityInput}, md)
if err != nil {
t.Fatal(err)
}
buf := make([]byte, readSize)
n, err := decoder.Read(buf)
if err != nil {
if n != 0 {
t.Errorf("Expected to read 0 bytes, got %d", n)
}
if !bytes.Equal(buf, tt.output) {
t.Errorf("Expected empty output, got '%v'", buf)
}
if err.Error() != tt.expectedErrorMsg {
t.Errorf("Expected error '%s', got '%s'", tt.expectedErrorMsg, err)
}
return
}
if n != readSize {
t.Errorf("Expected to read 20 bytes, got %d", n)
}
if err != nil {
t.Errorf("Expected read to succeed, got error: %s", err)
}
if !bytes.Equal(buf, tt.output) {
t.Errorf("Expected output '%s', but got '%s'", tt.output, buf)
}
})
}
}
func TestFileSharderUnevenInputE2E(t *testing.T) {
unevenFileSize := 547
dataShards := 2
parityOutput, err := ioutil.TempFile("", "rsutils")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
parityOutput.Close()
os.Remove(parityOutput.Name())
})
unevenFile, err := os.Open("testdata/uneven_input1")
if err != nil {
t.Fatal(err)
}
md, err := Encode(unevenFile, dataShards, []io.Writer{parityOutput})
if err != nil {
t.Fatalf("Expected nil error, got %s", err)
}
_, err = unevenFile.Seek(0, os.SEEK_SET)
if err != nil {
t.Fatal(err)
}
_, err = parityOutput.Seek(0, os.SEEK_SET)
if err != nil {
t.Fatal(err)
}
decoder, err := Open(unevenFile, []*os.File{parityOutput}, md)
if err != nil {
t.Fatalf("Expected nil err, got %s", err)
}
contents := make([]byte, unevenFileSize)
_, err = decoder.Read(contents)
if err != nil {
t.Fatalf("Expected nil err, got %s", err)
}
_, err = unevenFile.Seek(0, os.SEEK_SET)
if err != nil {
t.Fatal(err)
}
expectedContents, err := ioutil.ReadAll(unevenFile)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(contents, expectedContents) {
t.Errorf("Expected output\n%s\nBut got:\n%s", expectedContents, contents)
}
}