forked from nymsio/pgpmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign_test.go
130 lines (112 loc) · 3.51 KB
/
sign_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
package pgpmail
import "testing"
func TestSign(t *testing.T) {
k, _ := testKeys.GetSecretKey("user1@example.com")
td := new(TestData)
td.Body = "This is a test message.\n"
m := td.Message()
m.SetHeader(ctHeader, "text/plain; charset=us-ascii")
sigBodyPart := createBodyMimePart(m)
sig, _ := createSignature(sigBodyPart.rawContent, k, openpgpConfig)
if sig != testExpectedSigUser1 {
t.Error("Signature is not expected value")
}
}
var testExpectedSigUser1 = `-----BEGIN PGP SIGNATURE-----
wpwEAQEIABAFAgAAAAAJEGG1Vm0p3l+0AACTkAQAymMahe+auV36Jn5kg3fDiAs+
rZmmg8SqpA4aYq2fDRLv1zLFDcjePKPUqR+isgFKpJL/Lrb5CrSo5aBdJZ3cNWeT
Rpp8Z/p0yrINVGWG+fIRk2ahZX9eqhcIUxWs1NQUgLtNz3XV7fB8NN05pc7mc8w5
AX9kg9PkbMQkDzBnS/k=
=x06Q
-----END PGP SIGNATURE-----`
func TestMimeSign(t *testing.T) {
k, err := testKeys.GetSecretKey("user1@example.com")
if err != nil {
t.Error("error looking up secret key for user1@example.com: " + err.Error())
}
td := new(TestData)
td.From = "user1@example.com"
td.Body = "This is a test message.\n"
m := td.Message()
ss := m.Sign(testKeys, "")
if ss.Code != StatusSignedOnly {
t.Errorf("status is not expected value: %v", ss)
}
if err != nil {
t.Error("unexpected error signing test message: " + err.Error())
}
status := m.Verify(testKeys)
if status.Code != VerifySigValid {
t.Error("Signature did not verify")
}
if status.SignerKeyId != k.PrimaryKey.KeyId {
t.Error("Signature keyid does not match original signing key")
}
m = td.Message()
m.Sign(testKeys, "")
p := m.mpContent.parts[0]
p.rawContent = p.rawContent[1:]
status = m.Verify(testKeys)
if status.Code != VerifySigInvalid {
t.Error("Signature did not fail on corrupted text as expected")
}
}
func TestClearSign(t *testing.T) {
testClearsignMessage(t, clearsignData)
testClearsignMessage(t, clearsignData2)
}
func testClearsignMessage(t *testing.T, msg string) {
td := new(TestData)
td.Body = msg
m := td.Message()
status := m.Verify(testKeys)
k, _ := testKeys.GetPublicKey("user1@example.com")
if status.Code != VerifySigValid {
t.Error("Clearsign signature did not verify")
}
if status.SignerKeyId != k.PrimaryKey.KeyId {
t.Error("Signature keyid does not match expected signing key")
}
}
var clearsignData = `
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
This is a clearsign test message.
-----BEGIN PGP SIGNATURE-----
iJwEAQEKAAYFAlPoatUACgkQYbVWbSneX7SzsgP/Wt+eQ3gKR6OI6cN5iE9tbQur
+oQcYrtb/8SnDNClf4d+R/Ksif4JiI6zYrawz/HSC50XoaMjsmT7SPsoCi2r2zY7
C7Y1RWvall1BorUjefRqySf3Qm3AYHSbKR2S9ZZEeBMc8BXazrNlB5kpsimRAefw
QWLmlo+NjFTMK4Yf1gA=
=AsQ/
-----END PGP SIGNATURE-----
`
var clearsignData2 = `
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
This is a clearsigned message body with leading newlines.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2
Comment: GPGTools - https://gpgtools.org
iJwEAQEKAAYFAlPqEnoACgkQYbVWbSneX7TGlQP9F91/xLKB/OhF9IhcMH2g2c18
eUgPBUhg46T5a95zk6D0NoxvuSVp5o6XESYs7gg7XXweQGAHDu/cZTJxMBbRX6gU
AOTv4yh3X64MXmnoh/FvggE82QhNzzbj1nkAaVA5psAiMdT1U91VSyVEkhoQdnGC
P+qli0eg7HL/fPwl44A=
=4Hco
-----END PGP SIGNATURE-----
`
func TestUnsignedMessage(t *testing.T) {
td := new(TestData)
td.Body = "This message is not signed."
m := td.Message()
status := m.Verify(testKeys)
if status.Code != VerifyNotSigned {
t.Error("Unsigned message did not return VerifyUnsigned as expected")
}
td = new(TestData)
td.Parts = []string{plainPart, htmlPart}
m = td.Message()
status = m.Verify(testKeys)
if status.Code != VerifyNotSigned {
t.Error("Unsigned message did not return VerifyUnsigned as expected")
}
}