OLD | NEW |
(Empty) | |
| 1 // Copyright 2011 The Go Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style |
| 3 // license that can be found in the LICENSE file. |
| 4 |
| 5 package openpgp |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "crypto/rand" |
| 10 "io" |
| 11 "io/ioutil" |
| 12 "testing" |
| 13 "time" |
| 14 ) |
| 15 |
| 16 func TestSignDetached(t *testing.T) { |
| 17 kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 18 out := bytes.NewBuffer(nil) |
| 19 message := bytes.NewBufferString(signedInput) |
| 20 err := DetachSign(out, kring[0], message) |
| 21 if err != nil { |
| 22 t.Error(err) |
| 23 } |
| 24 |
| 25 testDetachedSignature(t, kring, out, signedInput, "check", testKey1KeyId
) |
| 26 } |
| 27 |
| 28 func TestSignTextDetached(t *testing.T) { |
| 29 kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 30 out := bytes.NewBuffer(nil) |
| 31 message := bytes.NewBufferString(signedInput) |
| 32 err := DetachSignText(out, kring[0], message) |
| 33 if err != nil { |
| 34 t.Error(err) |
| 35 } |
| 36 |
| 37 testDetachedSignature(t, kring, out, signedInput, "check", testKey1KeyId
) |
| 38 } |
| 39 |
| 40 func TestSignDetachedDSA(t *testing.T) { |
| 41 kring, _ := ReadKeyRing(readerFromHex(dsaTestKeyPrivateHex)) |
| 42 out := bytes.NewBuffer(nil) |
| 43 message := bytes.NewBufferString(signedInput) |
| 44 err := DetachSign(out, kring[0], message) |
| 45 if err != nil { |
| 46 t.Error(err) |
| 47 } |
| 48 |
| 49 testDetachedSignature(t, kring, out, signedInput, "check", testKey3KeyId
) |
| 50 } |
| 51 |
| 52 func TestNewEntity(t *testing.T) { |
| 53 if testing.Short() { |
| 54 return |
| 55 } |
| 56 |
| 57 e, err := NewEntity(rand.Reader, time.Now(), "Test User", "test", "test@
example.com") |
| 58 if err != nil { |
| 59 t.Errorf("failed to create entity: %s", err) |
| 60 return |
| 61 } |
| 62 |
| 63 w := bytes.NewBuffer(nil) |
| 64 if err := e.SerializePrivate(w); err != nil { |
| 65 t.Errorf("failed to serialize entity: %s", err) |
| 66 return |
| 67 } |
| 68 serialized := w.Bytes() |
| 69 |
| 70 el, err := ReadKeyRing(w) |
| 71 if err != nil { |
| 72 t.Errorf("failed to reparse entity: %s", err) |
| 73 return |
| 74 } |
| 75 |
| 76 if len(el) != 1 { |
| 77 t.Errorf("wrong number of entities found, got %d, want 1", len(e
l)) |
| 78 } |
| 79 |
| 80 w = bytes.NewBuffer(nil) |
| 81 if err := e.SerializePrivate(w); err != nil { |
| 82 t.Errorf("failed to serialize entity second time: %s", err) |
| 83 return |
| 84 } |
| 85 |
| 86 if !bytes.Equal(w.Bytes(), serialized) { |
| 87 t.Errorf("results differed") |
| 88 } |
| 89 } |
| 90 |
| 91 func TestSymmetricEncryption(t *testing.T) { |
| 92 buf := new(bytes.Buffer) |
| 93 plaintext, err := SymmetricallyEncrypt(buf, []byte("testing"), nil) |
| 94 if err != nil { |
| 95 t.Errorf("error writing headers: %s", err) |
| 96 return |
| 97 } |
| 98 message := []byte("hello world\n") |
| 99 _, err = plaintext.Write(message) |
| 100 if err != nil { |
| 101 t.Errorf("error writing to plaintext writer: %s", err) |
| 102 } |
| 103 err = plaintext.Close() |
| 104 if err != nil { |
| 105 t.Errorf("error closing plaintext writer: %s", err) |
| 106 } |
| 107 |
| 108 md, err := ReadMessage(buf, nil, func(keys []Key, symmetric bool) ([]byt
e, error) { |
| 109 return []byte("testing"), nil |
| 110 }) |
| 111 if err != nil { |
| 112 t.Errorf("error rereading message: %s", err) |
| 113 } |
| 114 messageBuf := bytes.NewBuffer(nil) |
| 115 _, err = io.Copy(messageBuf, md.UnverifiedBody) |
| 116 if err != nil { |
| 117 t.Errorf("error rereading message: %s", err) |
| 118 } |
| 119 if !bytes.Equal(message, messageBuf.Bytes()) { |
| 120 t.Errorf("recovered message incorrect got '%s', want '%s'", mess
ageBuf.Bytes(), message) |
| 121 } |
| 122 } |
| 123 |
| 124 var testEncryptionTests = []struct { |
| 125 keyRingHex string |
| 126 isSigned bool |
| 127 }{ |
| 128 { |
| 129 testKeys1And2PrivateHex, |
| 130 false, |
| 131 }, |
| 132 { |
| 133 testKeys1And2PrivateHex, |
| 134 true, |
| 135 }, |
| 136 { |
| 137 dsaElGamalTestKeysHex, |
| 138 false, |
| 139 }, |
| 140 { |
| 141 dsaElGamalTestKeysHex, |
| 142 true, |
| 143 }, |
| 144 } |
| 145 |
| 146 func TestEncryption(t *testing.T) { |
| 147 for i, test := range testEncryptionTests { |
| 148 kring, _ := ReadKeyRing(readerFromHex(test.keyRingHex)) |
| 149 |
| 150 passphrase := []byte("passphrase") |
| 151 for _, entity := range kring { |
| 152 if entity.PrivateKey != nil && entity.PrivateKey.Encrypt
ed { |
| 153 err := entity.PrivateKey.Decrypt(passphrase) |
| 154 if err != nil { |
| 155 t.Errorf("#%d: failed to decrypt key", i
) |
| 156 } |
| 157 } |
| 158 for _, subkey := range entity.Subkeys { |
| 159 if subkey.PrivateKey != nil && subkey.PrivateKey
.Encrypted { |
| 160 err := subkey.PrivateKey.Decrypt(passphr
ase) |
| 161 if err != nil { |
| 162 t.Errorf("#%d: failed to decrypt
subkey", i) |
| 163 } |
| 164 } |
| 165 } |
| 166 } |
| 167 |
| 168 var signed *Entity |
| 169 if test.isSigned { |
| 170 signed = kring[0] |
| 171 } |
| 172 |
| 173 buf := new(bytes.Buffer) |
| 174 w, err := Encrypt(buf, kring[:1], signed, nil /* no hints */ ) |
| 175 if err != nil { |
| 176 t.Errorf("#%d: error in Encrypt: %s", i, err) |
| 177 continue |
| 178 } |
| 179 |
| 180 const message = "testing" |
| 181 _, err = w.Write([]byte(message)) |
| 182 if err != nil { |
| 183 t.Errorf("#%d: error writing plaintext: %s", i, err) |
| 184 continue |
| 185 } |
| 186 err = w.Close() |
| 187 if err != nil { |
| 188 t.Errorf("#%d: error closing WriteCloser: %s", i, err) |
| 189 continue |
| 190 } |
| 191 |
| 192 md, err := ReadMessage(buf, kring, nil /* no prompt */ ) |
| 193 if err != nil { |
| 194 t.Errorf("#%d: error reading message: %s", i, err) |
| 195 continue |
| 196 } |
| 197 |
| 198 if test.isSigned { |
| 199 expectedKeyId := kring[0].signingKey().PublicKey.KeyId |
| 200 if md.SignedByKeyId != expectedKeyId { |
| 201 t.Errorf("#%d: message signed by wrong key id, g
ot: %d, want: %d", i, *md.SignedBy, expectedKeyId) |
| 202 } |
| 203 if md.SignedBy == nil { |
| 204 t.Errorf("#%d: failed to find the signing Entity
", i) |
| 205 } |
| 206 } |
| 207 |
| 208 plaintext, err := ioutil.ReadAll(md.UnverifiedBody) |
| 209 if err != nil { |
| 210 t.Errorf("#%d: error reading encrypted contents: %s", i,
err) |
| 211 continue |
| 212 } |
| 213 |
| 214 expectedKeyId := kring[0].encryptionKey().PublicKey.KeyId |
| 215 if len(md.EncryptedToKeyIds) != 1 || md.EncryptedToKeyIds[0] !=
expectedKeyId { |
| 216 t.Errorf("#%d: expected message to be encrypted to %v, b
ut got %#v", i, expectedKeyId, md.EncryptedToKeyIds) |
| 217 } |
| 218 |
| 219 if string(plaintext) != message { |
| 220 t.Errorf("#%d: got: %s, want: %s", i, string(plaintext),
message) |
| 221 } |
| 222 |
| 223 if test.isSigned { |
| 224 if md.SignatureError != nil { |
| 225 t.Errorf("#%d: signature error: %s", i, md.Signa
tureError) |
| 226 } |
| 227 if md.Signature == nil { |
| 228 t.Error("signature missing") |
| 229 } |
| 230 } |
| 231 } |
| 232 } |
OLD | NEW |