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) {
2019-05-14 18:05:01 +02:00
var pgp = GopenPGP{}
2019-05-14 15:39:35 +02:00
const password = "my secret password"
// Encrypt data with password
2019-05-14 18:05:01 +02:00
armor, err := pgp.EncryptMessageWithPassword("my message", password)
2019-05-14 15:39:35 +02:00
if err != nil {
t.Fatal("Expected no error when encrypting, got:", err)
}
// Decrypt data with wrong password
2019-05-14 18:05:01 +02:00
_, err = pgp.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 18:05:01 +02:00
text, err := pgp.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) {
2019-05-14 18:05:01 +02:00
var pgp = GopenPGP{}
2019-05-14 15:39:35 +02:00
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
2019-05-14 18:05:01 +02:00
armor, err := pgp.EncryptMessage(message, testPublicKeyRing, testPrivateKeyRing, testMailboxPassword, false)
2019-05-14 15:39:35 +02:00
if err != nil {
t.Fatal("Expected no error when encrypting, got:", err)
}
2019-05-14 18:05:01 +02:00
plainText, err := pgp.DecryptMessage(armor, testPrivateKeyRing, testMailboxPassword)
2019-05-14 15:39:35 +02:00
if err != nil {
t.Fatal("Expected no error when decrypting, got:", err)
}
assert.Exactly(t, message, plainText)
}