"testing"
)
-const D1 = `-----BEGIN PGP SIGNED MESSAGE-----
+var signedData = []struct {
+ text string
+ ok bool
+ hash string
+ body string
+ sig string
+}{
+ {
+ `-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
-Content-Type: text/plain.epoint.cert; charset=utf-8
-
-A: foo
-B: bar
-a: baz
+body
-----BEGIN PGP SIGNATURE-----
-
sig
------END PGP SIGNATURE-----
-`
-const D2 = `-----BEGIN PGP SIGNED MESSAGE-----
+`, true, "SHA1", "body", "-----BEGIN PGP SIGNATURE-----\nsig\n"},
+ {
+ `-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
+Hash: SHA256
-Content-Type: text/plain.epoint.cert; charset=utf-8
-
-A: foo
-B: bar
-a: baz
-
+- body
-----BEGIN PGP SIGNATURE-----
-
sig
------END PGP SIGNATURE-----
-`
-
-var C1 = &ClearSigned{
- Hash: "SHA1",
- Body: []byte(`Content-Type: text/plain.epoint.cert; charset=utf-8
-
-A: foo
-B: bar
-a: baz`),
- ArmoredSignature: []byte(`-----BEGIN PGP SIGNATURE-----
-
-sig
------END PGP SIGNATURE-----
-`)}
-
-var C2 = &ClearSigned{
- Hash: "SHA1",
- Body: []byte(`Content-Type: text/plain.epoint.cert; charset=utf-8
-
-A: foo
-B: bar
-a: baz
-`),
- ArmoredSignature: []byte(`-----BEGIN PGP SIGNATURE-----
-
-sig
------END PGP SIGNATURE-----
-`)}
-
-var F = []Field{
- {"A", "foo"},
- {"B", "bar"},
- {"a", "baz"},
+`, false, "", "body", "-----BEGIN PGP SIGNATURE-----\nsig\n"},
}
-var testData = []struct {
- D []byte
- C *ClearSigned
- T string
- F []Field
-}{
- {[]byte(D1), C1, "cert", F},
- {[]byte(D2), C2, "cert", F},
-}
-
-func eqClearSigned(c1, c2 *ClearSigned) bool {
- return c1.Hash == c2.Hash &&
- string(c1.Body) == string(c2.Body) &&
- string(c1.ArmoredSignature) == string(c2.ArmoredSignature)
-}
-
-func eqFields(f1, f2 []Field) bool {
- if len(f1) != len(f2) {
- return false
- }
- for i, v := range f1 {
- if f2[i].Key != v.Key && f2[i].Value != v.Value {
- return false
- }
- }
- return true
-}
-
-func TestClearSigned(t *testing.T) {
- for _, x := range testData {
- c, err := DecodeClearSigned(x.D)
+func TestSigned(t *testing.T) {
+ for _, x := range signedData {
+ c, err := ParseSigned([]byte(x.text))
if err != nil {
- t.Errorf("decoding %#v failed: %s\n", x.D, err)
+ t.Errorf("parsing %q failed: %s\n", x.text, err)
continue
}
- if !eqClearSigned(c, x.C) {
- t.Errorf("expected: %#v, got %#v\n", x.C, c)
+ if string(c.Body) != x.body {
+ t.Errorf("expected: %q, got %q\n", x.body, c.Body)
+ }
+ if string(c.Signature) != x.sig {
+ t.Errorf("expected: %q, got %q\n", x.sig, c.Signature)
}
}
- for _, x := range testData {
- d, err := EncodeClearSigned(x.C)
+ for _, x := range signedData {
+ if !x.ok {
+ continue
+ }
+ c := &Signed{x.hash, []byte(x.body), []byte(x.sig)}
+ s, err := FormatSigned(c)
if err != nil {
- t.Errorf("encoding %#v failed: %s\n", x.C, err)
+ t.Errorf("formating %#v failed: %s\n", c, err)
continue
}
- if string(d) != string(x.D) {
- t.Errorf("expected: %#v, got %#v\n", x.D, d)
+ if string(s) != x.text {
+ t.Errorf("expected: %q, got %q\n", x.text, s)
}
}
}
-func TestParse(t *testing.T) {
- for _, x := range testData {
- tt, f, err := ParseBody(x.C.Body)
+var docData = []struct {
+ text string
+ ok bool
+ t string
+ k []string
+ v []string
+}{
+ {
+ `Content-Type: text/vnd.epoint.draft; charset=utf-8
+
+K1: v1
+K2-Foo: v 2
+K3: v 3
+`, true, "Draft", []string{"K1", "K2-Foo", "K3"}, []string{"v1", "v 2", "v 3"}},
+ {
+ `Content-Type: text/vnd.epoint.debit; charset=utf-8
+
+K1: v1
+K2-Foo: v 2
+K3: v 3
+`, true, "DebitCert", []string{"K1", "K2-Foo", "K3"}, []string{" v1", "v 2", "v 3"}},
+ {
+ `Content-Type: text/vnd.epoint.credit; charset=utf-8
+
+K1: v1
+K2-Foo: v 2
+K3: v
+ 3
+`, false, "CreditCert", []string{"K1", "K2-Foo", "K3"}, []string{"v1", "v 2", "v 3"}},
+}
+
+func TestDocument(t *testing.T) {
+ for _, x := range docData {
+ doc, err := ParseDocument([]byte(x.text))
if err != nil {
- t.Errorf("parsing %s failed: %s\n", x.C.Body, err)
+ t.Errorf("parsing %q failed: %s\n", x.text, err)
continue
}
- if !eqFields(f, x.F) {
- t.Errorf("expected fields %#v, got %#v\n", x.F, f)
+ if string(doc.Type) != x.t {
+ t.Errorf("expected: %q, got %q\n", x.t, doc.Type)
}
- if tt != x.T {
- t.Errorf("expected type %s, got %s\n", x.T, tt)
+ if len(doc.Fields) != len(x.k) {
+ t.Errorf("expected: %d fields, got %d\n", len(x.k), len(doc.Fields))
+ }
+ for i, k := range x.k {
+ if doc.Fields[k] != x.v[i] {
+ t.Errorf("value of %s expected to be %s, got %s\n", k, x.v[i], doc.Fields[k])
+ }
+ }
+ }
+ for _, x := range docData {
+ if !x.ok {
+ continue
+ }
+ doc := new(Document)
+ doc.Type = x.t
+ doc.Fields = make(map[string]string)
+ doc.Order = x.k
+ for i, k := range x.k {
+ doc.Fields[k] = x.v[i]
+ }
+ s, err := FormatDocument(doc)
+ if err != nil {
+ t.Errorf("formating %#v failed: %s\n", doc, err)
+ continue
+ }
+ if string(s) != x.text {
+ t.Errorf("expected: %q, got %q\n", x.text, s)
}
}
}
-const draftBody = `Content-Type: text/plain.epoint.draft; charset=utf-8
+const draftBody = `Content-Type: text/vnd.epoint.draft; charset=utf-8
Drawer: 000000000000000000000000000000000000000A
Beneficiary: 000000000000000000000000000000000000000B
Amount: 1
Denomination: half euro
-Issue-Date: 2011-11-13T12:20:35Z
-Maturity-Date: 2011-12-27T09:18:46Z
-Notes: some notes
+Issuer: 000000000000000000000000000000000000000D
+Authorized-By: 000000000000000000000000000000000000000C
+Maturity-Date: 2011-11-13T12:20:35Z
+Expiry-Date: 2011-12-27T09:18:46Z
Nonce: 42
-Server: 000000000000000000000000000000000000000C
-Drawee: 000000000000000000000000000000000000000D
+Notes: some notes
`
func TestDraft(t *testing.T) {
- d, err := ParseDraft([]byte(draftBody))
+ doc, err := ParseDocument([]byte(draftBody))
+ if err != nil {
+ t.Errorf("ParseDocument failed: %s\n", err)
+ return
+ }
+ iv, err := ParseStruct(doc)
if err != nil {
- t.Errorf("ParseDraft failed: %s\n", err)
+ t.Errorf("ParseStruct %v failed: %s\n", doc, err)
+ return
+ }
+ d, ok := iv.(*Draft)
+ if !ok {
+ t.Errorf("expected *Draft got %#v\n", iv)
return
}
- s, err := RenderDraft(d)
+ doc, err = FormatStruct(d)
if err != nil {
- t.Errorf("render %v draft failed: %s\n", d, err)
+ t.Errorf("format %v draft failed: %s\n", d, err)
+ return
+ }
+ s, err := FormatDocument(doc)
+ if err != nil {
+ t.Errorf("format %v doc failed: %s\n", doc, err)
+ return
}
if string(s) != draftBody {
t.Errorf("parsed %#v\nexpected: %s\ngot: %s\n", d, draftBody, s)
}
}
-const certBody = `Content-Type: text/plain.epoint.cert; charset=utf-8
+const debitBody = `Content-Type: text/vnd.epoint.debit; charset=utf-8
-Holder: 000000000000000000000000000000000000000A
+Holder: 0000000000000000000000000000000000000009
Serial: 13
-Date: 2011-11-01T10:29:38Z
Balance: 23
Denomination: half euro
Issuer: 000000000000000000000000000000000000000B
+Date: 2011-11-13T12:20:35Z
+Difference: 1
+Draft: 000000000000000000000000000000000000000D
+Beneficiary: 000000000000000000000000000000000000000E
+Authorized-By: 000000000000000000000000000000000000000A
+Notes: -
Last-Debit-Serial: 0
Last-Credit-Serial: 12
Last-Cert: 000000000000000000000000000000000000000C
-Difference: 1
-Draft: 000000000000000000000000000000000000000D
-Drawer: 000000000000000000000000000000000000000E
-Drawer-Serial: 2
-Drawer-Cert: 000000000000000000000000000000000000000F
-References: 000000000000000000000000000000000000000C 000000000000000000000000000000000000000F
+References: 000000000000000000000000000000000000000C
+ 000000000000000000000000000000000000000F
`
func TestCert(t *testing.T) {
- c, err := ParseCert([]byte(certBody))
+ doc, err := ParseDocument([]byte(debitBody))
+ if err != nil {
+ t.Errorf("ParseDocument failed: %s\n", err)
+ return
+ }
+ iv, err := ParseStruct(doc)
+ if err != nil {
+ t.Errorf("ParseStruct %v failed: %s\n", doc, err)
+ return
+ }
+ d, ok := iv.(*DebitCert)
+ if !ok {
+ t.Errorf("expected *DebitCert got %#v\n", iv)
+ return
+ }
+ doc, err = FormatStruct(d)
if err != nil {
- t.Errorf("ParseCert failed: %s\n", err)
+ t.Errorf("format %v draft failed: %s\n", d, err)
return
}
- s, err := RenderCert(c)
+ s, err := FormatDocument(doc)
if err != nil {
- t.Errorf("render %v cert failed: %s\n", c, err)
+ t.Errorf("format %v doc failed: %s\n", doc, err)
+ return
}
- if string(s) != certBody {
- t.Errorf("parsed %#v\nexpected: %s\ngot: %s\n", c, certBody, s)
+ if string(s) != debitBody {
+ t.Errorf("parsed %#v\nexpected: %s\ngot: %s\n", d, debitBody, s)
}
}