passforios-gopenpgp/crypto/mime_test.go

80 lines
1.9 KiB
Go
Raw Normal View History

package crypto
import (
"github.com/stretchr/testify/assert"
2019-05-13 14:07:18 +02:00
"github.com/ProtonMail/gopenpgp/internal"
2018-09-19 11:52:14 +02:00
"io/ioutil"
"testing"
2018-09-05 14:56:06 +02:00
)
// Corresponding key in testdata/mime_privateKey
const privateKeyPassword = "test"
2018-09-05 14:56:06 +02:00
// define call back interface
type Callbacks struct {
Testing *testing.T
2018-09-05 14:56:06 +02:00
}
func (t *Callbacks) OnBody(body string, mimetype string) {
2019-05-14 16:08:25 +00:00
assert.Exactly(t.Testing, readTestFile("mime_decryptedBody", false), body)
2018-09-05 14:56:06 +02:00
}
func (t Callbacks) OnAttachment(headers string, data []byte) {
assert.Exactly(t.Testing, 1, data)
2018-09-05 14:56:06 +02:00
}
func (t Callbacks) OnEncryptedHeaders(headers string) {
assert.Exactly(t.Testing, "", headers)
2018-09-05 14:56:06 +02:00
}
func (t Callbacks) OnVerified(verified int) {
2018-09-05 14:56:06 +02:00
}
func (t Callbacks) OnError(err error) {
t.Testing.Fatal("Error in decrypting MIME message: ", err)
2018-09-05 14:56:06 +02:00
}
func TestDecrypt(t *testing.T) {
callbacks := Callbacks{
Testing: t,
}
2019-05-14 16:08:25 +00:00
block, err := internal.Unarmor(readTestFile("mime_publicKey", false))
if err != nil {
t.Fatal("Cannot unarmor public key: ", err)
}
publicKeyUnarmored, _ := ioutil.ReadAll(block.Body)
2019-05-14 16:08:25 +00:00
block, err = internal.Unarmor(readTestFile("mime_privateKey", false))
if err != nil {
t.Fatal("Cannot unarmor private key: ", err)
}
privateKeyUnarmored, _ := ioutil.ReadAll(block.Body)
pmCrypto.DecryptMIMEMessage(
2019-05-14 16:08:25 +00:00
readTestFile("mime_pgpMessage", false),
pmCrypto.BuildKeyRingNoError(publicKeyUnarmored),
pmCrypto.BuildKeyRingNoError(privateKeyUnarmored),
privateKeyPassword,
&callbacks,
pmCrypto.GetTimeUnix())
}
func TestParse(t *testing.T) {
2019-05-14 16:08:25 +00:00
body, _, atts, attHeaders, err := pmCrypto.parseMIME(readTestFile("mime_testMessage", false), nil)
if err != nil {
t.Error("Expected no error while parsing message, got:", err)
}
_ = atts
_ = attHeaders
bodyData, _ := body.GetBody()
2019-05-14 16:08:25 +00:00
assert.Exactly(t, readTestFile("mime_decodedBody", true), bodyData)
assert.Exactly(t, readTestFile("mime_decodedBodyHeaders", false), body.GetHeaders())
assert.Exactly(t, 2, len(atts))
}