wal-g

Форк
0
/
crypter_test.go 
71 строка · 1.6 Кб
1
package openpgp
2

3
import (
4
	"bytes"
5
	"io"
6
	"os"
7
	"testing"
8

9
	"github.com/stretchr/testify/assert"
10
	"github.com/wal-g/wal-g/internal/crypto"
11
)
12

13
var pgpTestPrivateKey string
14

15
const (
16
	PrivateKeyFilePath    = "./testdata/pgpTestPrivateKey"
17
	PrivateKeyEnvFilePath = "./testdata/pgpTestPrivateKeyEnv"
18
)
19

20
func noPassphrase() (string, bool) {
21
	return "", false
22
}
23

24
func MockArmedCrypterFromEnv() crypto.Crypter {
25
	pgpTestPrivateKeyBytes, err := os.ReadFile(PrivateKeyEnvFilePath)
26
	if err != nil {
27
		panic(err)
28
	}
29
	pgpTestPrivateKey = string(pgpTestPrivateKeyBytes)
30

31
	return CrypterFromKey(pgpTestPrivateKey, noPassphrase)
32
}
33

34
func MockArmedCrypterFromKeyPath() crypto.Crypter {
35
	return CrypterFromKeyPath(PrivateKeyFilePath, noPassphrase)
36
}
37

38
func TestMockCrypterFromEnv(t *testing.T) {
39
	MockArmedCrypterFromEnv()
40
}
41

42
func TestMockCrypterFromKeyPath(t *testing.T) {
43
	MockArmedCrypterFromKeyPath()
44
}
45

46
func EncryptionCycle(t *testing.T, crypter crypto.Crypter) {
47
	const someSecret = "so very secret thingy"
48

49
	buf := new(bytes.Buffer)
50
	encrypt, err := crypter.Encrypt(buf)
51
	assert.NoErrorf(t, err, "Encryption error: %v", err)
52

53
	encrypt.Write([]byte(someSecret))
54
	encrypt.Close()
55

56
	decrypt, err := crypter.Decrypt(buf)
57
	assert.NoErrorf(t, err, "Decryption error: %v", err)
58

59
	decryptedBytes, err := io.ReadAll(decrypt)
60
	assert.NoErrorf(t, err, "Decryption read error: %v", err)
61

62
	assert.Equal(t, someSecret, string(decryptedBytes), "Decrypted text not equals open text")
63
}
64

65
func TestEncryptionCycleFromEnv(t *testing.T) {
66
	EncryptionCycle(t, MockArmedCrypterFromEnv())
67
}
68

69
func TestEncryptionCycleFromKeyPath(t *testing.T) {
70
	EncryptionCycle(t, MockArmedCrypterFromKeyPath())
71
}
72

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.