Debian Codegraph @

local_test.go

raw
1 package desync
2
3 import (
4 "context"
5 "io"
6 "os"
7 "testing"
8
9 "github.com/stretchr/testify/require"
10 )
11
12 func TestLocalStoreCompressed(t *testing.T) {
13 store := t.TempDir()
14
15 s, err := NewLocalStore(store, StoreOptions{})
16 require.NoError(t, err)
17
18 // Make up some data and store it
19 dataIn := []byte("some data")
20
21 chunkIn := NewChunk(dataIn)
22 id := chunkIn.ID()
23
24 // Store the chunk
25 err = s.StoreChunk(chunkIn)
26 require.NoError(t, err)
27
28 // Check it's in the store
29 hasChunk, err := s.HasChunk(id)
30 require.NoError(t, err)
31 require.True(t, hasChunk, "chunk not found in store")
32
33 // Pull the data the "official" way
34 chunkOut, err := s.GetChunk(id)
35 require.NoError(t, err)
36
37 dataOut, err := chunkOut.Data()
38 require.NoError(t, err)
39
40 // Compare the data that went in with what came out
41 require.Equal(t, dataIn, dataOut)
42
43 // Now let's look at the file in the store directly to make sure it's compressed
44 _, name := s.nameFromID(id)
45 b, err := os.ReadFile(name)
46 require.NoError(t, err)
47 require.NotEqual(t, dataIn, b, "chunk is not compressed")
48 }
49
50 func TestLocalStoreUncompressed(t *testing.T) {
51 store := t.TempDir()
52
53 s, err := NewLocalStore(store, StoreOptions{Uncompressed: true})
54 require.NoError(t, err)
55
56 // Make up some data and store it
57 dataIn := []byte("some data")
58
59 chunkIn := NewChunk(dataIn)
60 id := chunkIn.ID()
61
62 err = s.StoreChunk(chunkIn)
63 require.NoError(t, err)
64
65 // Check it's in the store
66 hasChunk, err := s.HasChunk(id)
67 require.NoError(t, err)
68 require.True(t, hasChunk, "chunk not found in store")
69
70 // Pull the data the "official" way
71 chunkOut, err := s.GetChunk(id)
72 require.NoError(t, err)
73
74 dataOut, err := chunkOut.Data()
75 require.NoError(t, err)
76
77 // Compare the data that went in with what came out
78 require.Equal(t, dataIn, dataOut)
79
80 // Now let's look at the file in the store directly to make sure it's uncompressed
81 _, name := s.nameFromID(id)
82 b, err := os.ReadFile(name)
83 require.NoError(t, err)
84
85 require.Equal(t, dataIn, b, "chunk is compressed")
86 }
87
88 func TestLocalStoreErrorHandling(t *testing.T) {
89 store := t.TempDir()
90
91 s, err := NewLocalStore(store, StoreOptions{})
92 require.NoError(t, err)
93
94 // Make up some data and store it
95 dataIn := []byte("some data")
96
97 chunkIn := NewChunk(dataIn)
98 id := chunkIn.ID()
99 err = s.StoreChunk(chunkIn)
100 require.NoError(t, err)
101
102 // Now put an invalid chunk into the store
103 idInvalid, err := ChunkIDFromString("1000000000000000000000000000000000000000000000000000000000000000")
104 require.NoError(t, err)
105
106 dirInvalid, nameInvalid := s.nameFromID(idInvalid)
107 _ = os.Mkdir(dirInvalid, 0755)
108 err = os.WriteFile(nameInvalid, []byte("invalid data"), 0644)
109 require.NoError(t, err)
110
111 // Also add a blank chunk
112 idBlank, err := ChunkIDFromString("2000000000000000000000000000000000000000000000000000000000000000")
113 require.NoError(t, err)
114
115 dirBlank, nameBlank := s.nameFromID(idBlank)
116 _ = os.Mkdir(dirBlank, 0755)
117 err = os.WriteFile(nameBlank, nil, 0644)
118 require.NoError(t, err)
119
120 // Let's see if we can retrieve the good chunk and get errors from the bad ones
121 _, err = s.GetChunk(id)
122 require.NoError(t, err)
123
124 _, err = s.GetChunk(idInvalid)
125 if _, ok := err.(ChunkInvalid); !ok {
126 t.Fatal(err)
127 }
128 _, err = s.GetChunk(idBlank)
129 if _, ok := err.(ChunkInvalid); !ok {
130 t.Fatal(err)
131 }
132
133 // Run the verify with repair enabled which should get rid of the invalid and blank chunks
134 err = s.Verify(context.Background(), 1, true, io.Discard)
135 require.NoError(t, err)
136
137 // Let's see if we can still retrieve the good chunk and get Not Found for the others
138 _, err = s.GetChunk(id)
139 require.NoError(t, err)
140
141 _, err = s.GetChunk(idInvalid)
142 if _, ok := err.(ChunkMissing); !ok {
143 t.Fatal(err)
144 }
145 _, err = s.GetChunk(idBlank)
146 if _, ok := err.(ChunkMissing); !ok {
147 t.Fatal(err)
148 }
149 }
150
151 // A read failure that isn't "file does not exist" must be reported to the
152 // caller, not turned into a misleading ChunkInvalid or, with SkipVerify, a
153 // chunk holding partial data.
154 func TestLocalStoreGetChunkReadError(t *testing.T) {
155 store := t.TempDir()
156
157 s, err := NewLocalStore(store, StoreOptions{SkipVerify: true})
158 require.NoError(t, err)
159
160 // Put a directory where the chunk file is expected, making os.ReadFile
161 // fail with an error other than IsNotExist on all platforms
162 id := NewChunk([]byte("some data")).ID()
163 _, p := s.nameFromID(id)
164 require.NoError(t, os.MkdirAll(p, 0755))
165
166 _, err = s.GetChunk(id)
167 require.Error(t, err)
168 require.NotErrorAs(t, err, &ChunkMissing{})
169 require.NotErrorAs(t, err, &ChunkInvalid{})
170 }
171
172 func TestLocalStoreUncompressedEncrypted(t *testing.T) {
173 store := t.TempDir()
174
175 s, err := NewLocalStore(store,
176 StoreOptions{
177 Uncompressed: true,
178 Encryption: true,
179 EncryptionKey: testEncryptionKey,
180 },
181 )
182 require.NoError(t, err)
183
184 // Make up some data and store it
185 dataIn := []byte("some data")
186
187 chunkIn := NewChunk(dataIn)
188 id := chunkIn.ID()
189
190 err = s.StoreChunk(chunkIn)
191 require.NoError(t, err)
192
193 // Check it's in the store
194 hasChunk, err := s.HasChunk(id)
195 require.NoError(t, err)
196 require.True(t, hasChunk, "chunk not found in store")
197
198 // Pull the data the "official" way
199 chunkOut, err := s.GetChunk(id)
200 require.NoError(t, err)
201
202 dataOut, err := chunkOut.Data()
203 require.NoError(t, err)
204
205 // Compare the data that went in with what came out
206 require.Equal(t, dataIn, dataOut)
207
208 // Now let's look at the file in the store directly to make sure it's actually
209 // encrypted, meaning it should not match the plain (uncompressed) text
210 _, name := s.nameFromID(id)
211 b, err := os.ReadFile(name)
212 require.NoError(t, err)
213 require.NotEqual(t, dataIn, b, "chunk is not encrypted")
214 }
215
216 func TestLocalStoreCompressedEncrypted(t *testing.T) {
217 store := t.TempDir()
218
219 s, err := NewLocalStore(store,
220 StoreOptions{
221 Uncompressed: false,
222 Encryption: true,
223 EncryptionKey: testEncryptionKey,
224 },
225 )
226 require.NoError(t, err)
227
228 // Make up some data and store it
229 dataIn := []byte("some data")
230
231 chunkIn := NewChunk(dataIn)
232 id := chunkIn.ID()
233
234 err = s.StoreChunk(chunkIn)
235 require.NoError(t, err)
236
237 // Check it's in the store
238 hasChunk, err := s.HasChunk(id)
239 require.NoError(t, err)
240 require.True(t, hasChunk, "chunk not found in store")
241
242 // Pull the data the "official" way
243 chunkOut, err := s.GetChunk(id)
244 require.NoError(t, err)
245
246 dataOut, err := chunkOut.Data()
247 require.NoError(t, err)
248
249 // Compare the data that went in with what came out
250 require.Equal(t, dataIn, dataOut)
251
252 // Now let's look at the file in the store directly and confirm it is
253 // compressed and encrypted (in that order!).
254 _, name := s.nameFromID(id)
255 b, err := os.ReadFile(name)
256 require.NoError(t, err)
257
258 // First decrypt it, using the correct key
259 dec, err := NewXChaCha20Poly1305(testKey(t, testEncryptionKey))
260 require.NoError(t, err)
261 decrypted, err := dec.fromStorage(b)
262 require.NoError(t, err)
263
264 // Now decompress
265 decompressed, err := Decompress(nil, decrypted)
266 require.NoError(t, err)
267
268 // And it should match the original content
269 require.Equal(t, dataIn, decompressed)
270 }
271
272 func TestLocalStoreKeyMismatch(t *testing.T) {
273 store := t.TempDir()
274
275 // Build 2 stores accessing the same files but with different keys
276 s1, err := NewLocalStore(store,
277 StoreOptions{
278 Encryption: true,
279 EncryptionKey: testEncryptionKey,
280 },
281 )
282 require.NoError(t, err)
283 s2, err := NewLocalStore(store,
284 StoreOptions{
285 Encryption: true,
286 EncryptionKey: otherEncryptionKey,
287 },
288 )
289 require.NoError(t, err)
290
291 // Make up some data and store it using the first key
292 dataIn := []byte("some data")
293
294 chunkIn := NewChunk(dataIn)
295 id := chunkIn.ID()
296
297 err = s1.StoreChunk(chunkIn)
298 require.NoError(t, err)
299
300 // Pull the data with the right key and compare it
301 chunkOut, err := s1.GetChunk(id)
302 require.NoError(t, err)
303 dataOut, err := chunkOut.Data()
304 require.NoError(t, err)
305 require.Equal(t, dataIn, dataOut)
306
307 // Try to get the chunk with a different key, expect a not-found
308 // since the chunk extensions are different for different keys.
309 _, err = s2.GetChunk(id)
310 require.Error(t, err)
311 require.ErrorAs(t, err, &ChunkMissing{})
312 }