internal/mailin/fuzz_test.go

v1.39.0
gitbay/internal/mailin/fuzz_test.go history · blame · raw

35 lines · 1138 bytes

 1package mailin
 2
 3import (
 4	"bytes"
 5	"net/mail"
 6	"net/textproto"
 7	"testing"
 8	"time"
 9
10	"gitbay.org/gitbay/internal/mailreply"
11)
12
13// FuzzReply runs what an inbound message goes through before any
14// account is looked up: header parsing, token extraction and
15// verification, body extraction and quote stripping.
16func FuzzReply(f *testing.F) {
17	f.Add([]byte("From: a@b\r\nTo: reply+abc@x.example\r\nContent-Type: multipart/alternative; boundary=b\r\n\r\n--b\r\nContent-Type: text/plain\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\nhi=\r\n\r\n> q\r\n--b--\r\n"))
18	f.Add([]byte("Subject: x\r\n\r\nOn Mon wrote:\r\n> a\r\n-- \r\nsig\r\n"))
19	key := [][]byte{[]byte("0123456789abcdef0123456789abcdef")}
20	f.Fuzz(func(t *testing.T, raw []byte) {
21		msg, err := mail.ReadMessage(bytes.NewReader(raw))
22		if err != nil {
23			return
24		}
25		automatic(msg.Header)
26		if tok := findToken(msg.Header, "reply@x.example"); tok != "" {
27			mailreply.Verify(key, tok, fuzzNow)
28		}
29		if s, err := textBody(textproto.MIMEHeader(msg.Header), msg.Body, 1<<16); err == nil {
30			stripQuoted(s)
31		}
32	})
33}
34
35var fuzzNow = time.Date(2026, 9, 29, 0, 0, 0, 0, time.UTC)