| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"net/url" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/minio/minio-go/v6" |
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
) |
| 11 |
|
| 12 |
// Hex-encoded 256-bit keys used in encryption tests. |
| 13 |
const ( |
| 14 |
testEncryptionKey = "6368616e676520746869732070617373776f726420746f206120736563726574" |
| 15 |
otherEncryptionKey = "746f74616c6c7920646966666572656e74206b65792075736564206865726521" |
| 16 |
) |
| 17 |
|
| 18 |
func testKey(t *testing.T, s string) []byte { |
| 19 |
key, err := hex.DecodeString(s) |
| 20 |
require.NoError(t, err) |
| 21 |
return key |
| 22 |
} |
| 23 |
|
| 24 |
func TestEncryptDecrypt(t *testing.T) { |
| 25 |
tests := map[string]struct { |
| 26 |
alg func([]byte) (converter, error) |
| 27 |
}{ |
| 28 |
"xchacha20-poly1305": {func(key []byte) (converter, error) { return NewXChaCha20Poly1305(key) }}, |
| 29 |
"aes-256-gcm": {func(key []byte) (converter, error) { return NewAES256GCM(key) }}, |
| 30 |
} |
| 31 |
|
| 32 |
for name, test := range tests { |
| 33 |
t.Run(name, func(t *testing.T) { |
| 34 |
plainIn := []byte{1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} |
| 35 |
|
| 36 |
// Make two converters. One for encryption and one for decryption. Could use |
| 37 |
// just one but this way we confirm the setup from a key is consistent |
| 38 |
enc, err := test.alg(testKey(t, testEncryptionKey)) |
| 39 |
require.NoError(t, err) |
| 40 |
dec, err := test.alg(testKey(t, testEncryptionKey)) |
| 41 |
require.NoError(t, err) |
| 42 |
|
| 43 |
// Encrypt the data |
| 44 |
ciphertext, err := enc.toStorage(plainIn) |
| 45 |
require.NoError(t, err) |
| 46 |
|
| 47 |
// Confirm the ciphertext is actually different than what went in |
| 48 |
require.NotEqual(t, plainIn, ciphertext) |
| 49 |
|
| 50 |
// Decrypt it |
| 51 |
plainOut, err := dec.fromStorage(ciphertext) |
| 52 |
require.NoError(t, err) |
| 53 |
|
| 54 |
// This should match the original data of course |
| 55 |
require.Equal(t, plainIn, plainOut) |
| 56 |
|
| 57 |
// Make another instance with a different key |
| 58 |
diffKey, err := test.alg(testKey(t, otherEncryptionKey)) |
| 59 |
require.NoError(t, err) |
| 60 |
|
| 61 |
// Try to decrypt the data, should get an error from AEAD algorithms |
| 62 |
_, err = diffKey.fromStorage(ciphertext) |
| 63 |
require.Error(t, err) |
| 64 |
|
| 65 |
// Keys need to be 256-bit, anything else should be rejected |
| 66 |
_, err = test.alg([]byte("too short")) |
| 67 |
require.Error(t, err) |
| 68 |
}) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestAES256GCMCompare(t *testing.T) { |
| 73 |
// Make three converters. Two with the same, one with a diff key |
| 74 |
enc1, err := NewAES256GCM(testKey(t, testEncryptionKey)) |
| 75 |
require.NoError(t, err) |
| 76 |
enc2, err := NewAES256GCM(testKey(t, testEncryptionKey)) |
| 77 |
require.NoError(t, err) |
| 78 |
diffKey, err := NewAES256GCM(testKey(t, otherEncryptionKey)) |
| 79 |
require.NoError(t, err) |
| 80 |
|
| 81 |
// Check equality method |
| 82 |
require.True(t, enc1.equal(enc2)) |
| 83 |
require.True(t, enc2.equal(enc1)) |
| 84 |
require.False(t, diffKey.equal(enc1)) |
| 85 |
require.False(t, enc1.equal(diffKey)) |
| 86 |
|
| 87 |
// A different algorithm with the same key must not be equal either |
| 88 |
diffAlg, err := NewXChaCha20Poly1305(testKey(t, testEncryptionKey)) |
| 89 |
require.NoError(t, err) |
| 90 |
require.False(t, enc1.equal(diffAlg)) |
| 91 |
require.False(t, diffAlg.equal(enc1)) |
| 92 |
} |
| 93 |
|
| 94 |
func TestAES256GCMExtension(t *testing.T) { |
| 95 |
enc1, err := NewAES256GCM(testKey(t, testEncryptionKey)) |
| 96 |
require.NoError(t, err) |
| 97 |
|
| 98 |
// Confirm that we have a key-handle in the file extension |
| 99 |
require.Equal(t, ".aes-256-gcm-50d64035", enc1.extension) |
| 100 |
|
| 101 |
// If algorithm and key are the same, the same key handle |
| 102 |
// (extension) should be produced every time |
| 103 |
enc2, err := NewAES256GCM(testKey(t, testEncryptionKey)) |
| 104 |
require.NoError(t, err) |
| 105 |
require.Equal(t, enc1.extension, enc2.extension) |
| 106 |
} |
| 107 |
|
| 108 |
// Indexes are always stored in plain form, encryption options need to be |
| 109 |
// rejected by index stores rather than silently ignored, for example when |
| 110 |
// a config entry matches a location used for both chunks and indexes. |
| 111 |
func TestIndexStoresRejectEncryption(t *testing.T) { |
| 112 |
opt := StoreOptions{Encryption: true, EncryptionKey: testEncryptionKey} |
| 113 |
|
| 114 |
tests := map[string]struct { |
| 115 |
location string |
| 116 |
newStore func(*url.URL) (IndexStore, error) |
| 117 |
}{ |
| 118 |
"sftp": {"sftp://host/store", func(u *url.URL) (IndexStore, error) { return NewSFTPIndexStore(u, opt) }}, |
| 119 |
"http": {"https://host/store", func(u *url.URL) (IndexStore, error) { return NewRemoteHTTPIndexStore(u, opt) }}, |
| 120 |
"s3": {"s3+https://host/bucket", func(u *url.URL) (IndexStore, error) { |
| 121 |
return NewS3IndexStore(u, nil, "", opt, minio.BucketLookupAuto) |
| 122 |
}}, |
| 123 |
"gs": {"gs://bucket/prefix", func(u *url.URL) (IndexStore, error) { return NewGCIndexStore(u, opt) }}, |
| 124 |
} |
| 125 |
|
| 126 |
for name, test := range tests { |
| 127 |
t.Run(name, func(t *testing.T) { |
| 128 |
u, err := url.Parse(test.location) |
| 129 |
require.NoError(t, err) |
| 130 |
_, err = test.newStore(u) |
| 131 |
require.ErrorContains(t, err, "not supported by index stores") |
| 132 |
}) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func TestStorageConvertersValidation(t *testing.T) { |
| 137 |
tests := map[string]struct { |
| 138 |
opt StoreOptions |
| 139 |
err string |
| 140 |
}{ |
| 141 |
"key without encryption flag": {StoreOptions{EncryptionKey: testEncryptionKey}, "without setting encryption to true"}, |
| 142 |
"algorithm without encryption flag": {StoreOptions{EncryptionAlgorithm: "aes-256-gcm"}, "without setting encryption to true"}, |
| 143 |
"encryption without key": {StoreOptions{Encryption: true}, "no encryption key configured"}, |
| 144 |
"invalid key encoding": {StoreOptions{Encryption: true, EncryptionKey: "not-hex"}, "invalid encryption key"}, |
| 145 |
"unsupported algorithm": {StoreOptions{Encryption: true, EncryptionKey: testEncryptionKey, EncryptionAlgorithm: "rot13"}, "unsupported encryption algorithm"}, |
| 146 |
"valid": {StoreOptions{Encryption: true, EncryptionKey: testEncryptionKey}, ""}, |
| 147 |
} |
| 148 |
|
| 149 |
for name, test := range tests { |
| 150 |
t.Run(name, func(t *testing.T) { |
| 151 |
_, err := test.opt.StorageConverters() |
| 152 |
if test.err == "" { |
| 153 |
require.NoError(t, err) |
| 154 |
} else { |
| 155 |
require.ErrorContains(t, err, test.err) |
| 156 |
} |
| 157 |
}) |
| 158 |
} |
| 159 |
} |