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 = map[string]string{ "A": "foo", "B": "bar", "a": "baz", } var testData = []struct { D []byte C *ClearSigned T string F map[string]string }{ {[]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 map[string]string) bool { for k,v := range f1 { if f2[k] != v { return false } } return len(f1) == len(f2) } func TestClearSigned(t *testing.T) { for _, x := range testData { c, err := DecodeClearSigned(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 := EncodeClearSigned(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 IssueDate: 2 MaturityDate: 3 Notes: some notes Nonce: 42 Server: 000000000000000000000000000000000000000c Drawee: 000000000000000000000000000000000000000d ` func TestDraft(t *testing.T) { d, err := ParseDraft([]byte(draftBody)) if err != nil { t.Errorf("parse %q draft failed: %s\n", draftBody, err) } s, err := RenderDraft(d) if err != nil { t.Errorf("render %v draft failed: %s\n", d, err) } if string(s) != draftBody { t.Errorf("parsed %#v, expected %#v\n", d, draftBody) } }