Debian Codegraph @

lib/crypto/gpg/writer_test.go

raw
1 package gpg
2
3 import (
4 "bytes"
5 "io"
6 "strings"
7 "testing"
8
9 "git.sr.ht/~rjarry/aerc/lib/crypto/gpg/gpgbin"
10 "git.sr.ht/~rjarry/aerc/models"
11 "github.com/emersion/go-message/textproto"
12 )
13
14 func init() {
15 forceBoundary = "foo"
16 }
17
18 type writerTestCase struct {
19 name string
20 method string
21 body string
22 to []string
23 expectedErr string
24 }
25
26 func TestWriter(t *testing.T) {
27 initGPGtest(t)
28 importSecretKey()
29 importPublicKey()
30 importOwnertrust()
31
32 testCases := []writerTestCase{
33 {
34 name: "Encrypt",
35 method: "encrypt",
36 body: "This is an encrypted message!\r\n",
37 to: []string{"[email protected]"},
38 },
39 {
40 name: "Sign",
41 method: "sign",
42 body: "This is a signed message!\r\n",
43 to: []string{"[email protected]"},
44 },
45 {
46 name: "Encrypt to untrusted",
47 method: "encrypt",
48 body: "This is an encrypted message!\r\n",
49 to: []string{"[email protected]"},
50 expectedErr: "gpg: failure to encrypt: gpg: public key of [email protected] is not trusted. check public key(s)",
51 },
52 }
53 var h textproto.Header
54 h.Set("From", "John Doe <[email protected]>")
55 h.Set("To", "John Doe <[email protected]>")
56
57 var header textproto.Header
58 header.Set("Content-Type", "text/plain")
59
60 from := "[email protected]"
61
62 var err error
63 for _, tc := range testCases {
64 t.Logf("Test case: %s", tc.name)
65 var (
66 buf bytes.Buffer
67 cleartext io.WriteCloser
68 )
69 switch tc.method {
70 case "encrypt":
71 cleartext, err = Encrypt(&buf, h, tc.to, from)
72 if err != nil {
73 t.Fatalf("Encrypt() = %v", err)
74 }
75 case "sign":
76 cleartext, err = Sign(&buf, h, from)
77 if err != nil {
78 t.Fatalf("Encrypt() = %v", err)
79 }
80 }
81 if err = textproto.WriteHeader(cleartext, header); err != nil {
82 t.Fatalf("textproto.WriteHeader() = %v", err)
83 }
84 if _, err = io.WriteString(cleartext, tc.body); err != nil {
85 t.Fatalf("io.WriteString() = %v", err)
86 }
87 if err = cleartext.Close(); err != nil {
88 if err.Error() == tc.expectedErr {
89 continue
90 }
91 t.Fatalf("ciphertext.Close() = %v", err)
92 }
93 if tc.expectedErr != "" {
94 t.Fatalf("Expected error %v, but got %v", tc.expectedErr, err)
95 }
96 switch tc.method {
97 case "encrypt":
98 validateEncrypt(t, buf)
99 case "sign":
100 validateSign(t, buf)
101 }
102 }
103 }
104
105 func validateEncrypt(t *testing.T, buf bytes.Buffer) {
106 md, err := gpgbin.Decrypt(&buf)
107 if err != nil {
108 t.Errorf("Encrypt error: could not decrypt test encryption")
109 }
110 var body bytes.Buffer
111 io.Copy(&body, md.Body)
112 if s := body.String(); s != wantEncrypted {
113 t.Errorf("Encrypt() = \n%v\n but want \n%v", s, wantEncrypted)
114 }
115 }
116
117 func validateSign(t *testing.T, buf bytes.Buffer) {
118 parts := strings.Split(buf.String(), "\r\n--foo\r\n")
119 msg := strings.NewReader(parts[1])
120 sig := strings.NewReader(parts[2])
121 md, err := gpgbin.Verify(msg, sig)
122 if err != nil {
123 t.Fatalf("gpg.Verify() = %v", err)
124 }
125
126 deepEqual(t, "Sign", md, &wantSigned)
127 }
128
129 var wantEncrypted = toCRLF(`Content-Type: text/plain
130
131 This is an encrypted message!
132 `)
133
134 var wantSignedBody = toCRLF(`Content-Type: text/plain
135
136 This is a signed message!
137 `)
138
139 var wantSigned = models.MessageDetails{
140 IsEncrypted: false,
141 IsSigned: true,
142 SignedBy: "John Doe (This is a test key) <[email protected]>",
143 SignedByKeyId: 3490876580878068068,
144 SignatureError: "",
145 DecryptedWith: "",
146 DecryptedWithKeyId: 0,
147 Body: strings.NewReader(wantSignedBody),
148 Micalg: "pgp-sha256",
149 }