package document import ( "testing" ) const D1 = `-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Content-Type: text/plain.epoint.cert; charset=utf-8 A: foo B: bar a: baz -----BEGIN PGP SIGNATURE----- sig -----END PGP SIGNATURE----- ` const D2 = `-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Content-Type: text/plain.epoint.cert; charset=utf-8 A: foo B: bar a: baz -----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"}, } 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 := ParseClearSigned(x.D) if err != nil { t.Errorf("decoding %#v failed: %s\n", x.D, err) continue } if !eqClearSigned(c, x.C) { t.Errorf("expected: %#v, got %#v\n", x.C, c) } } for _, x := range testData { d, err := FormatClearSigned(x.C) if err != nil { t.Errorf("encoding %#v failed: %s\n", x.C, err) continue } if string(d) != string(x.D) { t.Errorf("expected: %#v, got %#v\n", x.D, d) } } } func TestParse(t *testing.T) { for _, x := range testData { tt, f, err := ParseBody(x.C.Body) if err != nil { t.Errorf("parsing %s failed: %s\n", x.C.Body, err) continue } if !eqFields(f, x.F) { t.Errorf("expected fields %#v, got %#v\n", x.F, f) } if tt != x.T { t.Errorf("expected type %s, got %s\n", x.T, tt) } } } const draftBody = `Content-Type: text/plain.epoint.draft; charset=utf-8 Drawer: 000000000000000000000000000000000000000A Beneficiary: 000000000000000000000000000000000000000B Amount: 1 Denomination: half euro Issuer: 000000000000000000000000000000000000000D Authorized-By: 000000000000000000000000000000000000000C Date: 2011-11-13T12:20:35Z Maturity-Date: 2011-11-13T12:20:35Z Expiry-Date: 2011-12-27T09:18:46Z Notes: some notes Nonce: 42 ` func TestDraft(t *testing.T) { d, err := ParseDraft([]byte(draftBody)) if err != nil { t.Errorf("ParseDraft failed: %s\n", err) return } s, err := FormatDraft(d) if err != nil { t.Errorf("render %v draft failed: %s\n", d, err) } 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 Holder: 0000000000000000000000000000000000000009 Serial: 13 Date: 2011-11-01T10:29:38Z Balance: 23 Denomination: half euro Issuer: 000000000000000000000000000000000000000B Authorized-By: 000000000000000000000000000000000000000A Last-Debit-Serial: 0 Last-Credit-Serial: 12 Last-Cert: 000000000000000000000000000000000000000C Difference: 1 Draft: 000000000000000000000000000000000000000D Drawer: 000000000000000000000000000000000000000E Drawer-Cert: 000000000000000000000000000000000000000F Notes: - References: 000000000000000000000000000000000000000C 000000000000000000000000000000000000000F ` func TestCert(t *testing.T) { c, err := ParseCert([]byte(certBody)) if err != nil { t.Errorf("ParseCert failed: %s\n", err) return } s, err := FormatCert(c) if err != nil { t.Errorf("render %v cert failed: %s\n", c, err) } if string(s) != certBody { t.Errorf("parsed %#v\nexpected: %s\ngot: %s\n", c, certBody, s) } }