passforios-gopenpgp/crypto/mime_test.go
wussler e65ed17b41
Refactor api (#6)
* Refactor library, remove duplicates

* Rebuild structure to use Messages and Signature models

* Use PGPSplitMessage

* Remove signature model

* Various fixes

* Add helpers with tests

* Fixes, add some docs, add tests

* Add attachment helpers

* Add helpers Symmetric encryption

* Edit docs + examples

* Rename kr to keyRing

* Various fixes for documentation

* Edit JSON handling functions, add decrypt keyring via token

* Add proposal changes doc

* Fix CI

* Drop *Message functions, join CleartextMessage and BinaryMessage

* Change canonicalization and trimming only to text signatures

* Add cleartextsignature, detach signature from message model, move helpers

* Documentation, remove optional parameters

* Move verification to separate model

* Don't return message in VerifyDetached

* Update table of contents in readme

* Appease golint

* Run go fmt

* Rename Encrypt/DecryptMessageWithPassword to ..WithToken

These functions shouldn't be used with user-provided passwords,
as they don't do any key-stretching.

* Change key generation usernames
2019-06-03 17:00:01 +02:00

87 lines
2.1 KiB
Go

package crypto
import (
"github.com/ProtonMail/gopenpgp/internal"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
)
// Corresponding key in testdata/mime_privateKey
const privateKeyPassword = "test"
// define call back interface
type Callbacks struct {
Testing *testing.T
}
func (t *Callbacks) OnBody(body string, mimetype string) {
assert.Exactly(t.Testing, readTestFile("mime_decryptedBody", false), body)
}
func (t Callbacks) OnAttachment(headers string, data []byte) {
assert.Exactly(t.Testing, 1, data)
}
func (t Callbacks) OnEncryptedHeaders(headers string) {
assert.Exactly(t.Testing, "", headers)
}
func (t Callbacks) OnVerified(verified int) {
}
func (t Callbacks) OnError(err error) {
t.Testing.Fatal("Error in decrypting MIME message: ", err)
}
func TestDecrypt(t *testing.T) {
callbacks := Callbacks{
Testing: t,
}
block, err := internal.Unarmor(readTestFile("mime_publicKey", false))
if err != nil {
t.Fatal("Cannot unarmor public key: ", err)
}
publicKeyUnarmored, _ := ioutil.ReadAll(block.Body)
block, err = internal.Unarmor(readTestFile("mime_privateKey", false))
if err != nil {
t.Fatal("Cannot unarmor private key:", err)
}
privateKeyUnarmored, _ := ioutil.ReadAll(block.Body)
privateKeyRing, _ := pgp.BuildKeyRing(privateKeyUnarmored)
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,
pgp.BuildKeyRingNoError(publicKeyUnarmored),
&callbacks,
pgp.GetUnixTime())
}
func TestParse(t *testing.T) {
body, _, atts, attHeaders, err := pgp.parseMIME(readTestFile("mime_testMessage", false), nil)
if err != nil {
t.Error("Expected no error while parsing message, got:", err)
}
_ = atts
_ = attHeaders
bodyData, _ := body.GetBody()
assert.Exactly(t, readTestFile("mime_decodedBody", true), bodyData)
assert.Exactly(t, readTestFile("mime_decodedBodyHeaders", false), body.GetHeaders())
assert.Exactly(t, 2, len(atts))
}