Debian Codegraph @

converter_test.go

raw
1 package desync
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/require"
7 )
8
9 // Chunks converted between storage formats that share converter layers must
10 // only apply the difference, not rebuild the whole stack from plain data.
11 // Verified by using storage data that isn't valid zstd: any attempt to
12 // undo the shared compression layer would fail.
13 func TestChunkStorageSharedLayers(t *testing.T) {
14 enc, err := NewXChaCha20Poly1305(testKey(t, testEncryptionKey))
15 require.NoError(t, err)
16
17 compressed := []byte("not valid zstd data")
18
19 // Chunk from a compressed store, requested compressed+encrypted, as when
20 // a chunk-server with --encryption reads from a compressed upstream store.
21 // Only the encryption layer should be applied.
22 c, err := NewChunkFromStorage(ChunkID{}, compressed, Converters{Compressor{}}, true)
23 require.NoError(t, err)
24 encrypted, err := c.Storage(Converters{Compressor{}, enc})
25 require.NoError(t, err)
26 decrypted, err := enc.fromStorage(encrypted)
27 require.NoError(t, err)
28 require.Equal(t, compressed, decrypted)
29
30 // The reverse: a compressed+encrypted chunk written to a compressed store.
31 // Only the encryption layer should be removed.
32 c, err = NewChunkFromStorage(ChunkID{}, encrypted, Converters{Compressor{}, enc}, true)
33 require.NoError(t, err)
34 storage, err := c.Storage(Converters{Compressor{}})
35 require.NoError(t, err)
36 require.Equal(t, compressed, storage)
37
38 // Same modifiers should return the storage data as-is.
39 storage, err = c.Storage(Converters{Compressor{}, enc})
40 require.NoError(t, err)
41 require.Equal(t, encrypted, storage)
42
43 // Key rotation: convert between two encrypted formats that share the
44 // compression layer, as when re-encrypting a store with a new key.
45 // Only the encryption layer should be swapped.
46 otherEnc, err := NewXChaCha20Poly1305(testKey(t, otherEncryptionKey))
47 require.NoError(t, err)
48 reEncrypted, err := c.Storage(Converters{Compressor{}, otherEnc})
49 require.NoError(t, err)
50 decrypted, err = otherEnc.fromStorage(reEncrypted)
51 require.NoError(t, err)
52 require.Equal(t, compressed, decrypted)
53 }
54
55 // Converting a chunk to plain storage format goes through Data() which
56 // caches the result on the chunk, so that later consumers, such as reading
57 // a chunk back after storing it in an uncompressed cache, don't convert
58 // the same data again.
59 func TestChunkStoragePlainCached(t *testing.T) {
60 plain := []byte("some data")
61 compressed, err := Compress(plain)
62 require.NoError(t, err)
63
64 c, err := NewChunkFromStorage(ChunkID{}, compressed, Converters{Compressor{}}, true)
65 require.NoError(t, err)
66
67 storage, err := c.Storage(nil)
68 require.NoError(t, err)
69 require.Equal(t, plain, storage)
70 require.Equal(t, plain, c.data, "plain data not cached on the chunk")
71 }