extend cert with server and notes
[epoint] / document / document_test.go
1 package document
2
3 import (
4         "testing"
5 )
6
7 const D1 = `-----BEGIN PGP SIGNED MESSAGE-----
8 Hash: SHA1
9
10 Content-Type: text/plain.epoint.cert; charset=utf-8
11
12 A: foo
13 B: bar
14 a: baz
15 -----BEGIN PGP SIGNATURE-----
16
17 sig
18 -----END PGP SIGNATURE-----
19 `
20 const D2 = `-----BEGIN PGP SIGNED MESSAGE-----
21 Hash: SHA1
22
23 Content-Type: text/plain.epoint.cert; charset=utf-8
24
25 A: foo
26 B: bar
27 a: baz
28
29 -----BEGIN PGP SIGNATURE-----
30
31 sig
32 -----END PGP SIGNATURE-----
33 `
34
35 var C1 = &ClearSigned{
36         Hash: "SHA1",
37         Body: []byte(`Content-Type: text/plain.epoint.cert; charset=utf-8
38
39 A: foo
40 B: bar
41 a: baz`),
42         ArmoredSignature: []byte(`-----BEGIN PGP SIGNATURE-----
43
44 sig
45 -----END PGP SIGNATURE-----
46 `)}
47
48 var C2 = &ClearSigned{
49         Hash: "SHA1",
50         Body: []byte(`Content-Type: text/plain.epoint.cert; charset=utf-8
51
52 A: foo
53 B: bar
54 a: baz
55 `),
56         ArmoredSignature: []byte(`-----BEGIN PGP SIGNATURE-----
57
58 sig
59 -----END PGP SIGNATURE-----
60 `)}
61
62 var F = []Field{
63         {"A", "foo"},
64         {"B", "bar"},
65         {"a", "baz"},
66 }
67
68 var testData = []struct {
69         D []byte
70         C *ClearSigned
71         T string
72         F []Field
73 }{
74         {[]byte(D1), C1, "cert", F},
75         {[]byte(D2), C2, "cert", F},
76 }
77
78 func eqClearSigned(c1, c2 *ClearSigned) bool {
79         return c1.Hash == c2.Hash &&
80                 string(c1.Body) == string(c2.Body) &&
81                 string(c1.ArmoredSignature) == string(c2.ArmoredSignature)
82 }
83
84 func eqFields(f1, f2 []Field) bool {
85         if len(f1) != len(f2) {
86                 return false
87         }
88         for i, v := range f1 {
89                 if f2[i].Key != v.Key && f2[i].Value != v.Value {
90                         return false
91                 }
92         }
93         return true
94 }
95
96 func TestClearSigned(t *testing.T) {
97         for _, x := range testData {
98                 c, err := DecodeClearSigned(x.D)
99                 if err != nil {
100                         t.Errorf("decoding %#v failed: %s\n", x.D, err)
101                         continue
102                 }
103                 if !eqClearSigned(c, x.C) {
104                         t.Errorf("expected: %#v, got %#v\n", x.C, c)
105                 }
106         }
107         for _, x := range testData {
108                 d, err := EncodeClearSigned(x.C)
109                 if err != nil {
110                         t.Errorf("encoding %#v failed: %s\n", x.C, err)
111                         continue
112                 }
113                 if string(d) != string(x.D) {
114                         t.Errorf("expected: %#v, got %#v\n", x.D, d)
115                 }
116         }
117 }
118
119 func TestParse(t *testing.T) {
120         for _, x := range testData {
121                 tt, f, err := ParseBody(x.C.Body)
122                 if err != nil {
123                         t.Errorf("parsing %s failed: %s\n", x.C.Body, err)
124                         continue
125                 }
126                 if !eqFields(f, x.F) {
127                         t.Errorf("expected fields %#v, got %#v\n", x.F, f)
128                 }
129                 if tt != x.T {
130                         t.Errorf("expected type %s, got %s\n", x.T, tt)
131                 }
132         }
133 }
134
135 const draftBody = `Content-Type: text/plain.epoint.draft; charset=utf-8
136
137 Drawer: 000000000000000000000000000000000000000A
138 Beneficiary: 000000000000000000000000000000000000000B
139 Amount: 1
140 Denomination: half euro
141 Issue-Date: 2011-11-13T12:20:35Z
142 Maturity-Date: 2011-12-27T09:18:46Z
143 Notes: some notes
144 Nonce: 42
145 Server: 000000000000000000000000000000000000000C
146 Drawee: 000000000000000000000000000000000000000D
147 `
148
149 func TestDraft(t *testing.T) {
150         d, err := ParseDraft([]byte(draftBody))
151         if err != nil {
152                 t.Errorf("ParseDraft failed: %s\n", err)
153                 return
154         }
155         s, err := RenderDraft(d)
156         if err != nil {
157                 t.Errorf("render %v draft failed: %s\n", d, err)
158         }
159         if string(s) != draftBody {
160                 t.Errorf("parsed %#v\nexpected: %s\ngot: %s\n", d, draftBody, s)
161         }
162 }
163
164 const certBody = `Content-Type: text/plain.epoint.cert; charset=utf-8
165
166 Holder: 0000000000000000000000000000000000000009
167 Serial: 13
168 Date: 2011-11-01T10:29:38Z
169 Balance: 23
170 Denomination: half euro
171 Server: 000000000000000000000000000000000000000A
172 Issuer: 000000000000000000000000000000000000000B
173 Last-Debit-Serial: 0
174 Last-Credit-Serial: 12
175 Last-Cert: 000000000000000000000000000000000000000C
176 Difference: 1
177 Draft: 000000000000000000000000000000000000000D
178 Drawer: 000000000000000000000000000000000000000E
179 Drawer-Serial: 2
180 Drawer-Cert: 000000000000000000000000000000000000000F
181 Notes: -
182 References: 000000000000000000000000000000000000000C 000000000000000000000000000000000000000F
183 `
184
185 func TestCert(t *testing.T) {
186         c, err := ParseCert([]byte(certBody))
187         if err != nil {
188                 t.Errorf("ParseCert failed: %s\n", err)
189                 return
190         }
191         s, err := RenderCert(c)
192         if err != nil {
193                 t.Errorf("render %v cert failed: %s\n", c, err)
194         }
195         if string(s) != certBody {
196                 t.Errorf("parsed %#v\nexpected: %s\ngot: %s\n", c, certBody, s)
197         }
198 }