120
121
t.Errorf("recovered message incorrect got '%s', want '%s'", messageBuf.Bytes(), message)
125
var testEncryptionTests = []struct {
130
testKeys1And2PrivateHex,
134
testKeys1And2PrivateHex,
138
dsaElGamalTestKeysHex,
142
dsaElGamalTestKeysHex,
147
func TestEncryption(t *testing.T) {
148
for i, test := range testEncryptionTests {
149
kring, _ := ReadKeyRing(readerFromHex(test.keyRingHex))
151
passphrase := []byte("passphrase")
152
for _, entity := range kring {
153
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted {
154
err := entity.PrivateKey.Decrypt(passphrase)
156
t.Errorf("#%d: failed to decrypt key", i)
159
for _, subkey := range entity.Subkeys {
160
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted {
161
err := subkey.PrivateKey.Decrypt(passphrase)
163
t.Errorf("#%d: failed to decrypt subkey", i)
174
buf := new(bytes.Buffer)
175
w, err := Encrypt(buf, kring[:1], signed, nil /* no hints */ )
177
t.Errorf("#%d: error in Encrypt: %s", i, err)
181
const message = "testing"
182
_, err = w.Write([]byte(message))
184
t.Errorf("#%d: error writing plaintext: %s", i, err)
189
t.Errorf("#%d: error closing WriteCloser: %s", i, err)
193
md, err := ReadMessage(buf, kring, nil /* no prompt */ )
195
t.Errorf("#%d: error reading message: %s", i, err)
200
expectedKeyId := kring[0].signingKey().PublicKey.KeyId
201
if md.SignedByKeyId != expectedKeyId {
202
t.Errorf("#%d: message signed by wrong key id, got: %d, want: %d", i, *md.SignedBy, expectedKeyId)
204
if md.SignedBy == nil {
205
t.Errorf("#%d: failed to find the signing Entity", i)
209
plaintext, err := ioutil.ReadAll(md.UnverifiedBody)
211
t.Errorf("#%d: error reading encrypted contents: %s", i, err)
215
expectedKeyId := kring[0].encryptionKey().PublicKey.KeyId
216
if len(md.EncryptedToKeyIds) != 1 || md.EncryptedToKeyIds[0] != expectedKeyId {
217
t.Errorf("#%d: expected message to be encrypted to %v, but got %#v", i, expectedKeyId, md.EncryptedToKeyIds)
220
if string(plaintext) != message {
221
t.Errorf("#%d: got: %s, want: %s", i, string(plaintext), message)
225
if md.SignatureError != nil {
226
t.Errorf("#%d: signature error: %s", i, err)
228
if md.Signature == nil {
229
t.Error("signature missing")