9
"github.com/stretchr/testify/assert"
10
"github.com/wal-g/wal-g/internal/crypto"
13
var pgpTestPrivateKey string
16
PrivateKeyFilePath = "./testdata/pgpTestPrivateKey"
17
PrivateKeyEnvFilePath = "./testdata/pgpTestPrivateKeyEnv"
20
func noPassphrase() (string, bool) {
24
func MockArmedCrypterFromEnv() crypto.Crypter {
25
pgpTestPrivateKeyBytes, err := os.ReadFile(PrivateKeyEnvFilePath)
29
pgpTestPrivateKey = string(pgpTestPrivateKeyBytes)
31
return CrypterFromKey(pgpTestPrivateKey, noPassphrase)
34
func MockArmedCrypterFromKeyPath() crypto.Crypter {
35
return CrypterFromKeyPath(PrivateKeyFilePath, noPassphrase)
38
func TestMockCrypterFromEnv(t *testing.T) {
39
MockArmedCrypterFromEnv()
42
func TestMockCrypterFromKeyPath(t *testing.T) {
43
MockArmedCrypterFromKeyPath()
46
func EncryptionCycle(t *testing.T, crypter crypto.Crypter) {
47
const someSecret = "so very secret thingy"
49
buf := new(bytes.Buffer)
50
encrypt, err := crypter.Encrypt(buf)
51
assert.NoErrorf(t, err, "Encryption error: %v", err)
53
encrypt.Write([]byte(someSecret))
56
decrypt, err := crypter.Decrypt(buf)
57
assert.NoErrorf(t, err, "Decryption error: %v", err)
59
decryptedBytes, err := io.ReadAll(decrypt)
60
assert.NoErrorf(t, err, "Decryption read error: %v", err)
62
assert.Equal(t, someSecret, string(decryptedBytes), "Decrypted text not equals open text")
65
func TestEncryptionCycleFromEnv(t *testing.T) {
66
EncryptionCycle(t, MockArmedCrypterFromEnv())
69
func TestEncryptionCycleFromKeyPath(t *testing.T) {
70
EncryptionCycle(t, MockArmedCrypterFromKeyPath())