initial document design
[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 = map[string]string{
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 map[string]string
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 map[string]string) bool {
85         for k,v := range f1 {
86                 if f2[k] != v {
87                         return false
88                 }
89         }
90         return len(f1) == len(f2)
91 }
92
93 func TestClearSigned(t *testing.T) {
94         for _, x := range testData {
95                 c, err := DecodeClearSigned(x.D)
96                 if err != nil {
97                         t.Errorf("decoding %#v failed: %s\n", x.D, err)
98                         continue
99                 }
100                 if !eqClearSigned(c, x.C) {
101                         t.Errorf("expected: %#v, got %#v\n", x.C, c)
102                 }
103         }
104         for _, x := range testData {
105                 d, err := EncodeClearSigned(x.C)
106                 if err != nil {
107                         t.Errorf("encoding %#v failed: %s\n", x.C, err)
108                         continue
109                 }
110                 if string(d) != string(x.D) {
111                         t.Errorf("expected: %#v, got %#v\n", x.D, d)
112                 }
113         }
114 }
115
116 func TestParse(t *testing.T) {
117         for _, x := range testData {
118                 tt, f, err := ParseBody(x.C.Body)
119                 if err != nil {
120                         t.Errorf("parsing %s failed: %s\n", x.C.Body, err)
121                         continue
122                 }
123                 if !eqFields(f, x.F) {
124                         t.Errorf("expected fields %#v, got %#v\n", x.F, f)
125                 }
126                 if tt != x.T {
127                         t.Errorf("expected type %s, got %s\n", x.T, tt)
128                 }
129         }
130 }
131
132 const draftBody = `Content-Type: text/plain.epoint.draft; charset=utf-8
133
134 Drawer: 000000000000000000000000000000000000000a
135 Beneficiary: 000000000000000000000000000000000000000b
136 Amount: 1
137 Denomination: half euro
138 IssueDate: 2
139 MaturityDate: 3
140 Notes: some notes
141 Nonce: 42
142 Server: 000000000000000000000000000000000000000c
143 Drawee: 000000000000000000000000000000000000000d
144 `
145
146 func TestDraft(t *testing.T) {
147         d, err := ParseDraft([]byte(draftBody))
148         if err != nil {
149                 t.Errorf("parse %q draft failed: %s\n", draftBody, err)
150         }
151         s, err := RenderDraft(d)
152         if err != nil {
153                 t.Errorf("render %v draft failed: %s\n", d, err)
154         }
155         if string(s) != draftBody {
156                 t.Errorf("parsed %#v, expected %#v\n", d, draftBody)
157         }
158 }