This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_test.go
131 lines (118 loc) · 2.88 KB
/
parse_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
package autocrypt
import (
"net/mail"
"os"
"strings"
"testing"
"golang.org/x/crypto/openpgp"
pgperrors "golang.org/x/crypto/openpgp/errors"
)
var (
// table of errors. rest is nil
errTable = map[string]error{
"unknown-type.eml": ErrUnknownType,
"rsa2048-unknown-critical.eml": ErrUnknownAttr,
"no_autocrypt.eml": ErrNoHeader,
// TODO known breakage
"25519-simple.eml": pgperrors.UnsupportedError("public key type: 22"),
}
stateTable = map[string]func(*Header) bool{
"no_autocrypt.eml": func(h *Header) bool {
return h == nil
},
"unknown-type.eml": func(h *Header) bool {
return h == nil
},
"rsa4096-simple.eml": func(h *Header) bool {
return h != nil &&
h.To == "[email protected]" &&
h.Key != nil &&
checkKey(h.Key) &&
!h.PreferEncrypted &&
h.Type == TypeOpenPGP &&
h.Uncritical == nil
},
"rsa2048-explicit-type.eml": func(h *Header) bool {
return h != nil &&
h.To == "[email protected]" &&
h.Key != nil &&
checkKey(h.Key) &&
!h.PreferEncrypted &&
h.Type == TypeOpenPGP &&
h.Uncritical == nil
},
"rsa2048-unknown-critical.eml": func(h *Header) bool {
// due to error in parse
return h == nil
},
"rsa2048-simple-to-bot.eml": func(h *Header) bool {
return h != nil &&
h.To == "[email protected]" &&
h.Key != nil &&
checkKey(h.Key) &&
!h.PreferEncrypted &&
h.Type == TypeOpenPGP &&
h.Uncritical == nil
},
"25519-simple.eml": func(h *Header) bool {
// TODO known breakage
return h == nil
},
"rsa2048-simple.eml": func(h *Header) bool {
return h != nil &&
h.To == "[email protected]" &&
h.Key != nil &&
checkKey(h.Key) &&
!h.PreferEncrypted &&
h.Type == TypeOpenPGP &&
h.Uncritical == nil
},
"rsa2048-unknown-non-critical.eml": func(h *Header) bool {
return h != nil &&
h.To == "[email protected]" &&
h.Key != nil &&
checkKey(h.Key) &&
!h.PreferEncrypted &&
h.Type == TypeOpenPGP &&
h.Uncritical != nil &&
h.Uncritical["_monkey"] == "ignore"
},
}
)
func TestParse(t *testing.T) {
dir, err := os.Open("data")
if err != nil {
t.Fatal(err)
}
emls, err := dir.Readdir(0)
if err != nil {
t.Fatal(err)
}
for _, emlFI := range emls {
emlName := emlFI.Name()
if !strings.HasSuffix(emlName, ".eml") {
continue
}
emlFile, err := os.Open("data/" + emlName)
if err != nil {
t.Fatal(err)
}
msg, err := mail.ReadMessage(emlFile)
if err != nil {
t.Fatal(err)
}
hdr, err := ParseHeader(msg.Header)
if err != errTable[emlName] {
t.Fatal(emlName, err)
}
t.Logf("%s:\n%v\n", emlName, hdr)
}
}
func checkKey(e *openpgp.Entity) bool {
return e.PrimaryKey != nil &&
e.PrivateKey == nil &&
len(e.Subkeys) == 1 &&
e.Subkeys[0].PublicKey != nil &&
e.Subkeys[0].PrivateKey == nil &&
e.Subkeys[0].Sig != nil
}