-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwriter_test.go
163 lines (131 loc) · 4 KB
/
writer_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
package pgpmail
import (
"bytes"
"io"
"io/ioutil"
"strings"
"testing"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/emersion/go-message/textproto"
)
func init() {
forceBoundary = "foo"
}
func TestEncrypt(t *testing.T) {
var h textproto.Header
h.Set("From", "John Doe <[email protected]>")
h.Set("To", "John Doe <[email protected]>")
var encryptedHeader textproto.Header
encryptedHeader.Set("Content-Type", "text/plain")
var encryptedBody = "This is an encrypted message!"
to := []*openpgp.Entity{testPublicKey}
var buf bytes.Buffer
cleartext, err := Encrypt(&buf, h, to, testPrivateKey, testConfig)
if err != nil {
t.Fatalf("Encrypt() = %v", err)
}
if err := textproto.WriteHeader(cleartext, encryptedHeader); err != nil {
t.Fatalf("textproto.WriteHeader() = %v", err)
}
if _, err := io.WriteString(cleartext, encryptedBody); err != nil {
t.Fatalf("io.WriteString() = %v", err)
}
if err := cleartext.Close(); err != nil {
t.Fatalf("ciphertext.Close() = %v", err)
}
s := buf.String()
if !strings.HasPrefix(s, wantEncryptedPrefix) || !strings.HasSuffix(s, wantEncryptedSuffix) {
t.Errorf("Encrypt() has invalid structure:\n%v", s)
}
r, err := Read(&buf, openpgp.EntityList{testPrivateKey}, nil, nil)
if err != nil {
t.Fatalf("Read() = %v", err)
}
b, err := ioutil.ReadAll(r.MessageDetails.UnverifiedBody)
if err != nil {
t.Fatalf("ReadAll() = %v", err)
}
checkSignature(t, r.MessageDetails)
checkEncryption(t, r.MessageDetails)
encryptedMessage := formatMessage(encryptedHeader, encryptedBody)
if s := string(b); s != encryptedMessage {
t.Errorf("MessagesDetails.UnverifiedBody = \n%v\n but want \n%v", s, encryptedMessage)
}
}
func TestSign(t *testing.T) {
var h textproto.Header
h.Set("From", "John Doe <[email protected]>")
h.Set("To", "John Doe <[email protected]>")
var signedHeader textproto.Header
signedHeader.Set("Content-Type", "text/plain")
var signedBody = "This is a signed message!"
var buf bytes.Buffer
cleartext, err := Sign(&buf, h, testPrivateKey, testConfig)
if err != nil {
t.Fatalf("Sign() = %v", err)
}
if err := textproto.WriteHeader(cleartext, signedHeader); err != nil {
t.Fatalf("textproto.WriteHeader() = %v", err)
}
if _, err := io.WriteString(cleartext, signedBody); err != nil {
t.Fatalf("io.WriteString() = %v", err)
}
if err := cleartext.Close(); err != nil {
t.Fatalf("ciphertext.Close() = %v", err)
}
s := buf.String()
if !strings.HasPrefix(s, wantSignedPrefix) || !strings.HasSuffix(s, wantSignedSuffix) {
t.Errorf("Sign() has invalid structure:\n%v", s)
}
r, err := Read(&buf, openpgp.EntityList{testPrivateKey}, nil, nil)
if err != nil {
t.Fatalf("Read() = %v", err)
}
b, err := ioutil.ReadAll(r.MessageDetails.UnverifiedBody)
if err != nil {
t.Fatalf("ReadAll() = %v", err)
}
checkSignature(t, r.MessageDetails)
signedMessage := formatMessage(signedHeader, signedBody)
if s := string(b); s != signedMessage {
t.Errorf("MessagesDetails.UnverifiedBody = \n%v\n but want \n%v", s, signedMessage)
}
}
func formatMessage(h textproto.Header, body string) string {
var sb strings.Builder
textproto.WriteHeader(&sb, h)
sb.WriteString(body)
return sb.String()
}
var wantEncryptedPrefix = toCRLF(`Mime-Version: 1.0
Content-Type: multipart/encrypted; boundary=foo;
protocol="application/pgp-encrypted"
To: John Doe <[email protected]>
From: John Doe <[email protected]>
--foo
Content-Type: application/pgp-encrypted
Version: 1
--foo
Content-Type: application/octet-stream
-----BEGIN PGP MESSAGE-----
`)
var wantEncryptedSuffix = toCRLF(`
-----END PGP MESSAGE-----
--foo--
`)
var wantSignedPrefix = toCRLF(`Mime-Version: 1.0
Content-Type: multipart/signed; boundary=foo; micalg=pgp-sha256;
protocol="application/pgp-signature"
To: John Doe <[email protected]>
From: John Doe <[email protected]>
--foo
Content-Type: text/plain
This is a signed message!
--foo
Content-Type: application/pgp-signature
-----BEGIN PGP MESSAGE-----
`)
var wantSignedSuffix = toCRLF(`
-----END PGP MESSAGE-----
--foo--
`)