~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/crypto/openpgp/write_test.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
        "crypto/rand"
10
10
        "os"
11
11
        "io"
 
12
        "io/ioutil"
12
13
        "testing"
13
14
        "time"
14
15
)
120
121
                t.Errorf("recovered message incorrect got '%s', want '%s'", messageBuf.Bytes(), message)
121
122
        }
122
123
}
 
124
 
 
125
var testEncryptionTests = []struct {
 
126
        keyRingHex string
 
127
        isSigned   bool
 
128
}{
 
129
        {
 
130
                testKeys1And2PrivateHex,
 
131
                false,
 
132
        },
 
133
        {
 
134
                testKeys1And2PrivateHex,
 
135
                true,
 
136
        },
 
137
        {
 
138
                dsaElGamalTestKeysHex,
 
139
                false,
 
140
        },
 
141
        {
 
142
                dsaElGamalTestKeysHex,
 
143
                true,
 
144
        },
 
145
}
 
146
 
 
147
func TestEncryption(t *testing.T) {
 
148
        for i, test := range testEncryptionTests {
 
149
                kring, _ := ReadKeyRing(readerFromHex(test.keyRingHex))
 
150
 
 
151
                passphrase := []byte("passphrase")
 
152
                for _, entity := range kring {
 
153
                        if entity.PrivateKey != nil && entity.PrivateKey.Encrypted {
 
154
                                err := entity.PrivateKey.Decrypt(passphrase)
 
155
                                if err != nil {
 
156
                                        t.Errorf("#%d: failed to decrypt key", i)
 
157
                                }
 
158
                        }
 
159
                        for _, subkey := range entity.Subkeys {
 
160
                                if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted {
 
161
                                        err := subkey.PrivateKey.Decrypt(passphrase)
 
162
                                        if err != nil {
 
163
                                                t.Errorf("#%d: failed to decrypt subkey", i)
 
164
                                        }
 
165
                                }
 
166
                        }
 
167
                }
 
168
 
 
169
                var signed *Entity
 
170
                if test.isSigned {
 
171
                        signed = kring[0]
 
172
                }
 
173
 
 
174
                buf := new(bytes.Buffer)
 
175
                w, err := Encrypt(buf, kring[:1], signed, nil /* no hints */ )
 
176
                if err != nil {
 
177
                        t.Errorf("#%d: error in Encrypt: %s", i, err)
 
178
                        continue
 
179
                }
 
180
 
 
181
                const message = "testing"
 
182
                _, err = w.Write([]byte(message))
 
183
                if err != nil {
 
184
                        t.Errorf("#%d: error writing plaintext: %s", i, err)
 
185
                        continue
 
186
                }
 
187
                err = w.Close()
 
188
                if err != nil {
 
189
                        t.Errorf("#%d: error closing WriteCloser: %s", i, err)
 
190
                        continue
 
191
                }
 
192
 
 
193
                md, err := ReadMessage(buf, kring, nil /* no prompt */ )
 
194
                if err != nil {
 
195
                        t.Errorf("#%d: error reading message: %s", i, err)
 
196
                        continue
 
197
                }
 
198
 
 
199
                if test.isSigned {
 
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)
 
203
                        }
 
204
                        if md.SignedBy == nil {
 
205
                                t.Errorf("#%d: failed to find the signing Entity", i)
 
206
                        }
 
207
                }
 
208
 
 
209
                plaintext, err := ioutil.ReadAll(md.UnverifiedBody)
 
210
                if err != nil {
 
211
                        t.Errorf("#%d: error reading encrypted contents: %s", i, err)
 
212
                        continue
 
213
                }
 
214
 
 
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)
 
218
                }
 
219
 
 
220
                if string(plaintext) != message {
 
221
                        t.Errorf("#%d: got: %s, want: %s", i, string(plaintext), message)
 
222
                }
 
223
 
 
224
                if test.isSigned {
 
225
                        if md.SignatureError != nil {
 
226
                                t.Errorf("#%d: signature error: %s", i, err)
 
227
                        }
 
228
                        if md.Signature == nil {
 
229
                                t.Error("signature missing")
 
230
                        }
 
231
                }
 
232
        }
 
233
}