passforios-gopenpgp/crypto/mime_test.go

74 lines
1.7 KiB
Go
Raw Normal View History

package crypto
import (
2018-09-19 11:52:14 +02:00
"testing"
"github.com/stretchr/testify/assert"
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,
}
privateKeyRing, _ := BuildKeyRingArmored(readTestFile("mime_privateKey", false))
err = privateKeyRing.UnlockWithPassphrase(privateKeyPassword)
if err != nil {
t.Fatal("Cannot unlock private key:", err)
}
message, err := NewPGPMessageFromArmored(readTestFile("mime_pgpMessage", false))
if err != nil {
t.Fatal("Cannot decode armored message:", err)
}
privateKeyRing.DecryptMIMEMessage(
message,
nil,
&callbacks,
GetUnixTime())
}
func TestParse(t *testing.T) {
body, atts, attHeaders, err := 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))
}