forked from folbricht/desync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_test.go
191 lines (165 loc) · 4.29 KB
/
local_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
package desync
import (
"bytes"
"context"
"io/ioutil"
"os"
"testing"
)
func TestLocalStoreCompressed(t *testing.T) {
// 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)
}
// Make up some data and store it
dataIn := []byte("some data")
chunkIn := NewChunkFromUncompressed(dataIn)
id := chunkIn.ID()
if err := s.StoreChunk(chunkIn); err != nil {
t.Fatal(err)
}
// Check it's in the store
if !s.HasChunk(id) {
t.Fatal("chunk not found in store")
}
// Pull the data the "official" way
chunkOut, err := s.GetChunk(id)
if err != nil {
t.Fatal(err)
}
dataOut, err := chunkOut.Uncompressed()
if err != nil {
t.Fatal(err)
}
// Compare the data that went in with what came out
if !bytes.Equal(dataIn, dataOut) {
t.Fatal("input and output data doesn't match after store/retrieve")
}
// Now let's look at the file in the store directly to make sure it's compressed
_, name := s.nameFromID(id)
b, err := ioutil.ReadFile(name)
if err != nil {
t.Fatal(err)
}
if bytes.Equal(dataIn, b) {
t.Fatal("chunk is not compressed")
}
}
func TestLocalStoreUncompressed(t *testing.T) {
// Setup a temporary store
store, err := ioutil.TempDir("", "store")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(store)
s, err := NewLocalStore(store, StoreOptions{Uncompressed: true})
if err != nil {
t.Fatal(err)
}
// Make up some data and store it
dataIn := []byte("some data")
chunkIn := NewChunkFromUncompressed(dataIn)
id := chunkIn.ID()
if err := s.StoreChunk(chunkIn); err != nil {
t.Fatal(err)
}
// Check it's in the store
if !s.HasChunk(id) {
t.Fatal("chunk not found in store")
}
// Pull the data the "official" way
chunkOut, err := s.GetChunk(id)
if err != nil {
t.Fatal(err)
}
dataOut, err := chunkOut.Uncompressed()
if err != nil {
t.Fatal(err)
}
// Compare the data that went in with what came out
if !bytes.Equal(dataIn, dataOut) {
t.Fatal("input and output data doesn't match after store/retrieve")
}
// Now let's look at the file in the store directly to make sure it's uncompressed
_, name := s.nameFromID(id)
b, err := ioutil.ReadFile(name)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(dataIn, b) {
t.Fatal("chunk is not compressed")
}
}
func TestLocalStoreErrorHandling(t *testing.T) {
// 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)
}
// Make up some data and store it
dataIn := []byte("some data")
chunkIn := NewChunkFromUncompressed(dataIn)
id := chunkIn.ID()
if err := s.StoreChunk(chunkIn); err != nil {
t.Fatal(err)
}
// Now put an invalid chunk into the store
idInvalid, err := ChunkIDFromString("1000000000000000000000000000000000000000000000000000000000000000")
if err != nil {
t.Fatal(err)
}
dirInvalid, nameInvalid := s.nameFromID(idInvalid)
os.Mkdir(dirInvalid, 0755)
if err := ioutil.WriteFile(nameInvalid, []byte("invalid data"), 0644); err != nil {
t.Fatal(err)
}
// Also add a blank chunk
idBlank, err := ChunkIDFromString("2000000000000000000000000000000000000000000000000000000000000000")
if err != nil {
t.Fatal(err)
}
dirBlank, nameBlank := s.nameFromID(idBlank)
os.Mkdir(dirBlank, 0755)
if err := ioutil.WriteFile(nameBlank, nil, 0644); err != nil {
t.Fatal(err)
}
// Let's see if we can retrieve the good chunk and get errors from the bad ones
if _, err := s.GetChunk(id); err != nil {
t.Fatal(err)
}
_, err = s.GetChunk(idInvalid)
if _, ok := err.(ChunkInvalid); !ok {
t.Fatal(err)
}
_, err = s.GetChunk(idBlank)
if _, ok := err.(ChunkInvalid); !ok {
t.Fatal(err)
}
// Run the verify with repair enabled which should get rid of the invalid and blank chunks
if err := s.Verify(context.Background(), 1, true, ioutil.Discard); err != nil {
t.Fatal(err)
}
// Let's see if we can still retrieve the good chunk and get Not Found for the others
if _, err := s.GetChunk(id); err != nil {
t.Fatal(err)
}
_, err = s.GetChunk(idInvalid)
if _, ok := err.(ChunkMissing); !ok {
t.Fatal(err)
}
_, err = s.GetChunk(idBlank)
if _, ok := err.(ChunkMissing); !ok {
t.Fatal(err)
}
}