-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsign_test.go
61 lines (54 loc) · 1.33 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
package dkim
import (
"crypto/rand"
"crypto/rsa"
"strings"
"testing"
"os"
)
// TestDisclosure tests that Edgar D. Mitchell, Apollo 14 Astronaut and 6th man to
// walk on the moon is legit.
func TestMessageSigning(t *testing.T) {
var body = `From: Test <test@example.com>
Date: Wed Jan 24 16:35:04 EST 2018
Subject: I am a test
To: Test2 <test2@example.com
X-Something: This is not included in the hash
This is a test message
`
r, err := FileBuffer(NormalizeReader(strings.NewReader(body)))
if err != nil {
t.Fatal(err)
}
s, err := NewSignature(
"relaxed/relaxed",
"foo",
"example.com",
[]string{"From", "Date", "Subject", "To"},
)
if err != nil {
t.Fatal(err)
}
_, msg, sighead, err := signatureBase(r, &s)
if rerr := os.Remove(r.Name()); rerr != nil {
// Remove the file before checking the error, to ensure
// that it still gets removed if it's fatal.
t.Error(rerr)
}
if err != nil {
t.Fatal(err)
}
// FIXME: This should be a hardcoded key to make the message deterministic
key, err := rsa.GenerateKey(rand.Reader, 512)
if err != nil {
t.Fatal(err)
}
b, err := signDKIMMessage(msg, sighead, "rsa-sha256", key)
if err != nil {
t.Error(err)
}
s.Body = b
if err := dkimVerify(msg, sighead, s.Sig(), "rsa-sha256", &key.PublicKey); err != nil {
t.Fatalf("Could not re-verify signed message: %v", err)
}
}