-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpgp_test.go
202 lines (150 loc) · 6.27 KB
/
pgp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package natscrypto
// Tests for the PGPEncrypter
import (
"fmt"
"io"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
)
func TestPGPEncrypterAccessors(t *testing.T) {
ec := NewPGPEncrypter()
assert.NotNil(t, ec.Identities)
e := GetEntity(t)
id := string(e.PrimaryKey.Fingerprint[:20])
assert.Panics(t, func() { ec.AddEntity(nil) })
ec.AddEntity(e)
assert.Equal(t, openpgp.EntityList{e}, ec.AllEntities)
assert.Equal(t, 1, len(ec.Identities))
assert.Equal(t, e, ec.Identities[id])
ec.RemoveID(id)
assert.Equal(t, openpgp.EntityList{}, ec.AllEntities)
assert.Equal(t, 0, len(ec.Identities))
e.PrimaryKey = nil
assert.Panics(t, func() { ec.AddEntity(e) })
}
func TestEncryptDecryptData(t *testing.T) {
signer := GetEntity(t)
signerID := string(signer.PrimaryKey.Fingerprint[:20])
recipient := GetEntity(t)
recipientID := string(recipient.PrimaryKey.Fingerprint[:20])
recipient.PrivateKey = nil
ec := NewPGPEncrypter()
ec.AddEntity(signer, recipient)
data, err := ec.EncryptData([]byte("Bonjour !"), []string{recipientID}, "unknown")
assert.Nil(t, data)
assert.Equal(t, "Unknown signer id: unknown", err.Error())
data, err = ec.EncryptData([]byte("Bonjour !"), []string{"jojo"}, signerID)
assert.Nil(t, data)
assert.Equal(t, "Unknown recipient id: jojo", err.Error())
data, err = ec.EncryptData([]byte("Bonjour !"), []string{signerID}, recipientID)
assert.Nil(t, data)
assert.Equal(t, "No private key for this identity: "+recipientID, err.Error())
data, err = ec.EncryptData([]byte("Bonjour !"), []string{recipientID}, signerID)
assert.Nil(t, err)
assert.NotEqual(t, "", data)
decData, decRecipients, decSigner, decErr := ec.DecryptData(data)
assert.Nil(t, decErr)
assert.Equal(t, "Bonjour !", string(decData))
assert.Equal(t, signerID, decSigner)
assert.Equal(t, []string{recipientID}, decRecipients)
}
func TestOneShotEntity(t *testing.T) {
entity := GetEntity(t)
entityID := string(entity.PrimaryKey.Fingerprint[:20])
ec := NewPGPEncrypter()
ec.AddOneShotEntity(entity)
assert.Equal(t, entity, ec.getEntity(entityID))
assert.Nil(t, ec.getEntity(entityID))
}
func failingEncrypt(io.Writer, []*openpgp.Entity, *openpgp.Entity, *openpgp.FileHints, *packet.Config) (io.WriteCloser, error) {
return nil, fmt.Errorf("Some error")
}
type WriteCloserWithFailingWrite struct{}
func (WriteCloserWithFailingWrite) Write([]byte) (int, error) { return 0, fmt.Errorf("Write failed") }
func (WriteCloserWithFailingWrite) Close() error { return nil }
type WriteCloserWithFailingClose struct{}
func (WriteCloserWithFailingClose) Write([]byte) (int, error) { return 0, nil }
func (WriteCloserWithFailingClose) Close() error { return fmt.Errorf("Close failed") }
func failingWriterWrite(io.Writer, []*openpgp.Entity, *openpgp.Entity, *openpgp.FileHints, *packet.Config) (io.WriteCloser, error) {
return WriteCloserWithFailingWrite{}, nil
}
func failingWriterClose(io.Writer, []*openpgp.Entity, *openpgp.Entity, *openpgp.FileHints, *packet.Config) (io.WriteCloser, error) {
return WriteCloserWithFailingClose{}, nil
}
func TestEncryptErrors(t *testing.T) {
openpgpEncrypt = failingEncrypt
defer func() { openpgpEncrypt = openpgp.Encrypt }()
signer := GetEntity(t)
signerID := string(signer.PrimaryKey.Fingerprint[:20])
recipient := GetEntity(t)
recipientID := string(recipient.PrimaryKey.Fingerprint[:20])
recipient.PrivateKey = nil
ec := NewPGPEncrypter()
ec.AddEntity(signer, recipient)
_, err := ec.EncryptData([]byte("Bonjour !"), []string{recipientID}, signerID)
assert.EqualError(t, err, "Some error")
openpgpEncrypt = failingWriterWrite
_, err = ec.EncryptData([]byte("Bonjour !"), []string{recipientID}, signerID)
assert.EqualError(t, err, "Write failed")
openpgpEncrypt = failingWriterClose
_, err = ec.EncryptData([]byte("Bonjour !"), []string{recipientID}, signerID)
assert.EqualError(t, err, "Close failed")
}
var readMessageEntity = GetEntity(nil)
type failingReader struct{}
func (failingReader) Read([]byte) (int, error) {
return 0, fmt.Errorf("Read failed")
}
func failingReadMessage(io.Reader, openpgp.KeyRing, openpgp.PromptFunction, *packet.Config) (*openpgp.MessageDetails, error) {
return nil, fmt.Errorf("ReadMessage failed")
}
func readMessageReturnsFaultyBodyReader(io.Reader, openpgp.KeyRing, openpgp.PromptFunction, *packet.Config) (*openpgp.MessageDetails, error) {
return &openpgp.MessageDetails{UnverifiedBody: failingReader{}}, nil
}
func readMessageUnsigned(io.Reader, openpgp.KeyRing, openpgp.PromptFunction, *packet.Config) (*openpgp.MessageDetails, error) {
return &openpgp.MessageDetails{
UnverifiedBody: strings.NewReader(""),
DecryptedWith: openpgp.Key{Entity: readMessageEntity},
}, nil
}
func readMessageUnknownSigner(io.Reader, openpgp.KeyRing, openpgp.PromptFunction, *packet.Config) (*openpgp.MessageDetails, error) {
return &openpgp.MessageDetails{
UnverifiedBody: strings.NewReader(""),
DecryptedWith: openpgp.Key{Entity: readMessageEntity},
IsSigned: true,
}, nil
}
func readMessageSignatureError(io.Reader, openpgp.KeyRing, openpgp.PromptFunction, *packet.Config) (*openpgp.MessageDetails, error) {
return &openpgp.MessageDetails{
UnverifiedBody: strings.NewReader(""),
DecryptedWith: openpgp.Key{Entity: readMessageEntity},
IsSigned: true,
SignatureError: fmt.Errorf("Signature Error"),
}, nil
}
func TestDecryptionErrors(t *testing.T) {
defer func() { openpgpEncrypt = openpgp.Encrypt }()
signer := GetEntity(t)
recipient := GetEntity(t)
recipient.PrivateKey = nil
ec := NewPGPEncrypter()
ec.AddEntity(signer, recipient)
openpgpReadMessage = failingReadMessage
_, _, _, err := ec.DecryptData([]byte(""))
assert.EqualError(t, err, "Error decrypting message: ReadMessage failed")
openpgpReadMessage = readMessageReturnsFaultyBodyReader
_, _, _, err = ec.DecryptData([]byte(""))
assert.EqualError(t, err, "Read failed")
openpgpReadMessage = readMessageUnsigned
_, _, _, err = ec.DecryptData([]byte(""))
assert.Equal(t, err, ErrUnsignedMessage)
openpgpReadMessage = readMessageUnknownSigner
_, _, _, err = ec.DecryptData([]byte(""))
assert.Equal(t, err, ErrUnknownSigner)
openpgpReadMessage = readMessageSignatureError
_, _, _, err = ec.DecryptData([]byte(""))
assert.Equal(t, "Signature Error", err.Error())
}