passforios-gopenpgp/crypto/message_test.go

50 lines
1.5 KiB
Go
Raw Normal View History

2019-05-14 15:39:35 +02:00
package crypto
import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestMessageEncryptionWithPassword(t *testing.T) {
var pmCrypto = PmCrypto{}
const password = "my secret password"
// Encrypt data with password
armor, err := pmCrypto.EncryptMessageWithPassword("my message", password)
if err != nil {
t.Fatal("Expected no error when encrypting, got:", err)
}
// Decrypt data with wrong password
2019-05-14 16:08:25 +00:00
_, err = pmCrypto.DecryptMessageWithPassword(armor, "wrong password")
2019-05-14 15:39:35 +02:00
assert.NotNil(t, err)
// Decrypt data with the good password
2019-05-14 16:08:25 +00:00
text, err := pmCrypto.DecryptMessageWithPassword(armor, password)
2019-05-14 15:39:35 +02:00
if err != nil {
t.Fatal("Expected no error when decrypting, got:", err)
}
assert.Exactly(t, "my message", text)
}
func TestMessageEncryption(t *testing.T) {
var pmCrypto = PmCrypto{}
var (
message = "plain text"
)
2019-05-14 16:08:25 +00:00
testPrivateKeyRing, err = ReadArmoredKeyRing(strings.NewReader(readTestFile("keyring_privateKey", false)))
_ = testPrivateKeyRing.Unlock([]byte(testMailboxPassword))
testPublicKeyRing, _ = ReadArmoredKeyRing(strings.NewReader(readTestFile("keyring_publicKey", false)))
2019-05-14 15:39:35 +02:00
armor, err := pmCrypto.EncryptMessage(message, testPublicKeyRing, testPrivateKeyRing, testMailboxPassword, false)
if err != nil {
t.Fatal("Expected no error when encrypting, got:", err)
}
plainText, err := pmCrypto.DecryptMessage(armor, testPrivateKeyRing, testMailboxPassword)
if err != nil {
t.Fatal("Expected no error when decrypting, got:", err)
}
assert.Exactly(t, message, plainText)
}