mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-17 15:46:44 +00:00
feat: better handling of multipart messages
This commit is contained in:
@ -19,6 +19,7 @@ package message
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mime"
|
"mime"
|
||||||
@ -43,23 +44,17 @@ func Parse(r io.Reader, key, keyName string) (m *pmapi.Message, mimeMessage, pla
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
atts, attReaders, err := collectAttachments(p)
|
if m.Attachments, attReaders, err = collectAttachments(p); err != nil {
|
||||||
if err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m.Attachments = atts
|
|
||||||
|
|
||||||
richBody, plainBody, err := collectBodyParts(p)
|
if m.Body, plainBody, err = buildBodies(p); err != nil {
|
||||||
if err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m.Body = richBody
|
|
||||||
|
|
||||||
mimeType, err := determineMIMEType(p)
|
if m.MIMEType, err = determineMIMEType(p); err != nil {
|
||||||
if err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m.MIMEType = mimeType
|
|
||||||
|
|
||||||
if key != "" {
|
if key != "" {
|
||||||
attachPublicKey(p.Root(), key, keyName)
|
attachPublicKey(p.Root(), key, keyName)
|
||||||
@ -95,58 +90,116 @@ func collectAttachments(p *parser.Parser) (atts []*pmapi.Attachment, data []io.R
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// collectBodyParts returns a richtext body (used for normal sending)
|
func buildBodies(p *parser.Parser) (richBody, plainBody string, err error) {
|
||||||
// and a plaintext body (used for sending to recipients that prefer plaintext).
|
richParts, err := collectBodyParts(p, "text/html")
|
||||||
func collectBodyParts(p *parser.Parser) (richBody, plainBody string, err error) {
|
if err != nil {
|
||||||
var richParts, plainParts []string
|
|
||||||
|
|
||||||
w := p.NewWalker()
|
|
||||||
|
|
||||||
w.RegisterContentTypeHandler("text/plain").
|
|
||||||
OnEnter(func(p *parser.Part) error {
|
|
||||||
plainParts = append(plainParts, string(p.Body))
|
|
||||||
|
|
||||||
if !isAlternative(p) {
|
|
||||||
richParts = append(richParts, string(p.Body))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
w.RegisterContentTypeHandler("text/html").
|
|
||||||
OnEnter(func(p *parser.Part) error {
|
|
||||||
richParts = append(richParts, string(p.Body))
|
|
||||||
|
|
||||||
if !isAlternative(p) {
|
|
||||||
plain, htmlErr := html2text.FromString(string(p.Body))
|
|
||||||
if htmlErr != nil {
|
|
||||||
plain = string(p.Body)
|
|
||||||
}
|
|
||||||
plainParts = append(plainParts, plain)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if err = w.Walk(); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join(richParts, "\r\n"), strings.Join(plainParts, "\r\n"), nil
|
plainParts, err := collectBodyParts(p, "text/plain")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(richParts) != len(plainParts) {
|
||||||
|
return "", "", errors.New("unequal number of rich and plain parts")
|
||||||
|
}
|
||||||
|
|
||||||
|
richBuilder, plainBuilder := strings.Builder{}, strings.Builder{}
|
||||||
|
|
||||||
|
for i := 0; i < len(richParts); i++ {
|
||||||
|
_, _ = richBuilder.Write(richParts[i].Body)
|
||||||
|
_, _ = plainBuilder.Write(getPlainBody(plainParts[i]))
|
||||||
|
}
|
||||||
|
|
||||||
|
return richBuilder.String(), plainBuilder.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isAlternative(p *parser.Part) bool {
|
// collectBodyParts collects all body parts in the parse tree, preferring
|
||||||
parent := p.Parent()
|
// parts of the given content type if alternatives exist.
|
||||||
if parent == nil {
|
func collectBodyParts(p *parser.Parser, preferredContentType string) (parser.Parts, error) {
|
||||||
return false
|
v := parser.
|
||||||
}
|
NewVisitor(func(p *parser.Part, visit parser.Visit) (interface{}, error) {
|
||||||
|
childParts, err := collectChildParts(p, visit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
t, _, err := parent.Header.ContentType()
|
return joinChildParts(childParts), nil
|
||||||
|
}).
|
||||||
|
RegisterRule("multipart/alternative", func(p *parser.Part, visit parser.Visit) (interface{}, error) {
|
||||||
|
childParts, err := collectChildParts(p, visit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return bestChoice(childParts, preferredContentType)
|
||||||
|
}).
|
||||||
|
RegisterRule("text/plain", func(p *parser.Part, visit parser.Visit) (interface{}, error) {
|
||||||
|
return parser.Parts{p}, nil
|
||||||
|
}).
|
||||||
|
RegisterRule("text/html", func(p *parser.Part, visit parser.Visit) (interface{}, error) {
|
||||||
|
return parser.Parts{p}, nil
|
||||||
|
})
|
||||||
|
|
||||||
|
res, err := v.Visit(p.Root())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return t == "multipart/alternative"
|
return res.(parser.Parts), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func collectChildParts(p *parser.Part, visit parser.Visit) ([]parser.Parts, error) {
|
||||||
|
childParts := []parser.Parts{}
|
||||||
|
|
||||||
|
for _, child := range p.Children() {
|
||||||
|
res, err := visit(child)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
childParts = append(childParts, res.(parser.Parts))
|
||||||
|
}
|
||||||
|
|
||||||
|
return childParts, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func joinChildParts(childParts []parser.Parts) parser.Parts {
|
||||||
|
res := parser.Parts{}
|
||||||
|
|
||||||
|
for _, parts := range childParts {
|
||||||
|
res = append(res, parts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func bestChoice(childParts []parser.Parts, preferredContentType string) (parser.Parts, error) {
|
||||||
|
// If one of the parts has preferred content type, use that.
|
||||||
|
for i := len(childParts) - 1; i >= 0; i-- {
|
||||||
|
if allHaveContentType(childParts[i], preferredContentType) {
|
||||||
|
return childParts[i], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, choose the last one.
|
||||||
|
return childParts[len(childParts)-1], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func allHaveContentType(parts parser.Parts, contentType string) bool {
|
||||||
|
for _, part := range parts {
|
||||||
|
t, _, err := part.Header.ContentType()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if t != contentType {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func determineMIMEType(p *parser.Parser) (string, error) {
|
func determineMIMEType(p *parser.Parser) (string, error) {
|
||||||
@ -171,6 +224,29 @@ func determineMIMEType(p *parser.Parser) (string, error) {
|
|||||||
return "text/plain", nil
|
return "text/plain", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getPlainBody(part *parser.Part) []byte {
|
||||||
|
contentType, _, err := part.Header.ContentType()
|
||||||
|
if err != nil {
|
||||||
|
return part.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
switch contentType {
|
||||||
|
case "text/plain":
|
||||||
|
return part.Body
|
||||||
|
|
||||||
|
case "text/html":
|
||||||
|
text, err := html2text.FromReader(bytes.NewReader(part.Body))
|
||||||
|
if err != nil {
|
||||||
|
return part.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte(text)
|
||||||
|
|
||||||
|
default:
|
||||||
|
return part.Body
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func writeMIMEMessage(p *parser.Parser) (mime string, err error) {
|
func writeMIMEMessage(p *parser.Parser) (mime string, err error) {
|
||||||
writer := p.
|
writer := p.
|
||||||
NewWriter().
|
NewWriter().
|
||||||
|
|||||||
@ -63,7 +63,7 @@ func (p *Parser) parse(r io.Reader) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) enter() {
|
func (p *Parser) enter() {
|
||||||
p.stack = append(p.stack, &Part{parent: p.top()})
|
p.stack = append(p.stack, &Part{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) exit() {
|
func (p *Parser) exit() {
|
||||||
|
|||||||
@ -6,11 +6,12 @@ import (
|
|||||||
"github.com/emersion/go-message"
|
"github.com/emersion/go-message"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Parts []*Part
|
||||||
|
|
||||||
type Part struct {
|
type Part struct {
|
||||||
Header message.Header
|
Header message.Header
|
||||||
Body []byte
|
Body []byte
|
||||||
parent *Part
|
children Parts
|
||||||
children []*Part
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Part) Part(n int) (part *Part, err error) {
|
func (p *Part) Part(n int) (part *Part, err error) {
|
||||||
@ -21,58 +22,14 @@ func (p *Part) Part(n int) (part *Part, err error) {
|
|||||||
return p.children[n-1], nil
|
return p.children[n-1], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Part) Parts() (n int) {
|
func (p *Part) Children() Parts {
|
||||||
return len(p.children)
|
return p.children
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Part) Parent() *Part {
|
|
||||||
return p.parent
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Part) Siblings() []*Part {
|
|
||||||
if p.parent == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
siblings := []*Part{}
|
|
||||||
|
|
||||||
for _, sibling := range p.parent.children {
|
|
||||||
if sibling != p {
|
|
||||||
siblings = append(siblings, sibling)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return siblings
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Part) AddChild(child *Part) {
|
func (p *Part) AddChild(child *Part) {
|
||||||
p.children = append(p.children, child)
|
p.children = append(p.children, child)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Part) visit(w *Walker) (err error) {
|
|
||||||
hdl := p.getHandler(w)
|
|
||||||
|
|
||||||
if err = hdl.handleEnter(w, p); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, child := range p.children {
|
|
||||||
if err = child.visit(w); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return hdl.handleExit(w, p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Part) getHandler(w *Walker) handler {
|
|
||||||
if dispHandler := w.getDispHandler(p); dispHandler != nil {
|
|
||||||
return dispHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
return w.getTypeHandler(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Part) write(writer *message.Writer, w *Writer) (err error) {
|
func (p *Part) write(writer *message.Writer, w *Writer) (err error) {
|
||||||
if len(p.children) > 0 {
|
if len(p.children) > 0 {
|
||||||
for _, child := range p.children {
|
for _, child := range p.children {
|
||||||
|
|||||||
36
pkg/message/parser/testdata/multipart_alternative.eml
vendored
Normal file
36
pkg/message/parser/testdata/multipart_alternative.eml
vendored
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
To: pmbridgeietest@outlook.com
|
||||||
|
From: schizofrenic <schizofrenic@pm.me>
|
||||||
|
Subject: aoeuaoeu
|
||||||
|
Message-ID: <7dc32b61-b9cf-f2d3-8ec5-10e5b4a33ec1@pm.me>
|
||||||
|
Date: Thu, 30 Jul 2020 13:35:24 +0200
|
||||||
|
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0)
|
||||||
|
Gecko/20100101 Thunderbird/68.11.0
|
||||||
|
MIME-Version: 1.0
|
||||||
|
Content-Type: multipart/alternative;
|
||||||
|
boundary="------------22BC647264E52252E386881A"
|
||||||
|
Content-Language: en-US
|
||||||
|
|
||||||
|
This is a multi-part message in MIME format.
|
||||||
|
--------------22BC647264E52252E386881A
|
||||||
|
Content-Type: text/plain; charset=utf-8; format=flowed
|
||||||
|
Content-Transfer-Encoding: 7bit
|
||||||
|
|
||||||
|
*aoeuaoeu*
|
||||||
|
|
||||||
|
|
||||||
|
--------------22BC647264E52252E386881A
|
||||||
|
Content-Type: text/html; charset=utf-8
|
||||||
|
Content-Transfer-Encoding: 7bit
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p><b>aoeuaoeu</b><br>
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
--------------22BC647264E52252E386881A--
|
||||||
55
pkg/message/parser/visitor.go
Normal file
55
pkg/message/parser/visitor.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import "regexp"
|
||||||
|
|
||||||
|
type Visitor struct {
|
||||||
|
rules []*rule
|
||||||
|
fallback Rule
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewVisitor(fallback Rule) *Visitor {
|
||||||
|
return &Visitor{
|
||||||
|
fallback: fallback,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Visit func(*Part) (interface{}, error)
|
||||||
|
|
||||||
|
type Rule func(*Part, Visit) (interface{}, error)
|
||||||
|
|
||||||
|
type rule struct {
|
||||||
|
re string
|
||||||
|
fn Rule
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Visitor) RegisterRule(contentTypeRegex string, fn Rule) *Visitor {
|
||||||
|
v.rules = append(v.rules, &rule{
|
||||||
|
re: contentTypeRegex,
|
||||||
|
fn: fn,
|
||||||
|
})
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Visitor) Visit(p *Part) (interface{}, error) {
|
||||||
|
t, _, err := p.Header.ContentType()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if rule := v.getRuleForContentType(t); rule != nil {
|
||||||
|
return rule.fn(p, v.Visit)
|
||||||
|
}
|
||||||
|
|
||||||
|
return v.fallback(p, v.Visit)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Visitor) getRuleForContentType(contentType string) *rule {
|
||||||
|
for _, rule := range v.rules {
|
||||||
|
if regexp.MustCompile(rule.re).MatchString(contentType) {
|
||||||
|
return rule
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@ -23,7 +23,23 @@ func newWalker(root *Part) *Walker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *Walker) Walk() (err error) {
|
func (w *Walker) Walk() (err error) {
|
||||||
return w.root.visit(w)
|
return w.visitPart(w.root)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Walker) visitPart(p *Part) (err error) {
|
||||||
|
hdl := w.getHandler(p)
|
||||||
|
|
||||||
|
if err = hdl.handleEnter(w, p); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, child := range p.children {
|
||||||
|
if err = w.visitPart(child); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hdl.handleExit(w, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Walker) WithDefaultHandler(handler handler) *Walker {
|
func (w *Walker) WithDefaultHandler(handler handler) *Walker {
|
||||||
@ -46,6 +62,14 @@ func (w *Walker) RegisterContentDispositionHandler(contDisp string) *DispHandler
|
|||||||
return hdl
|
return hdl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Walker) getHandler(p *Part) handler {
|
||||||
|
if dispHandler := w.getDispHandler(p); dispHandler != nil {
|
||||||
|
return dispHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
return w.getTypeHandler(p)
|
||||||
|
}
|
||||||
|
|
||||||
// getTypeHandler returns the appropriate PartHandler to handle the given part.
|
// getTypeHandler returns the appropriate PartHandler to handle the given part.
|
||||||
// If no specialised handler exists, it returns the default handler.
|
// If no specialised handler exists, it returns the default handler.
|
||||||
func (w *Walker) getTypeHandler(p *Part) handler {
|
func (w *Walker) getTypeHandler(p *Part) handler {
|
||||||
|
|||||||
@ -34,8 +34,7 @@ func TestWalkerTypeHandler(t *testing.T) {
|
|||||||
|
|
||||||
walker := p.NewWalker()
|
walker := p.NewWalker()
|
||||||
|
|
||||||
walker.
|
walker.RegisterContentTypeHandler("text/html").
|
||||||
RegisterContentTypeHandler("text/html").
|
|
||||||
OnEnter(func(p *Part) (err error) {
|
OnEnter(func(p *Part) (err error) {
|
||||||
html = append(html, p.Body)
|
html = append(html, p.Body)
|
||||||
return
|
return
|
||||||
@ -54,8 +53,7 @@ func TestWalkerDispositionHandler(t *testing.T) {
|
|||||||
|
|
||||||
walker := p.NewWalker()
|
walker := p.NewWalker()
|
||||||
|
|
||||||
walker.
|
walker.RegisterContentDispositionHandler("attachment").
|
||||||
RegisterContentDispositionHandler("attachment").
|
|
||||||
OnEnter(func(p *Part, hdl PartHandlerFunc) (err error) {
|
OnEnter(func(p *Part, hdl PartHandlerFunc) (err error) {
|
||||||
attachments = append(attachments, p.Body)
|
attachments = append(attachments, p.Body)
|
||||||
return
|
return
|
||||||
@ -74,13 +72,11 @@ func TestWalkerDispositionAndTypeHandler(t *testing.T) {
|
|||||||
|
|
||||||
var enter, exit int
|
var enter, exit int
|
||||||
|
|
||||||
walker.
|
walker.RegisterContentTypeHandler("application/octet-stream").
|
||||||
RegisterContentTypeHandler("application/octet-stream").
|
|
||||||
OnEnter(func(p *Part) (err error) { enter++; return }).
|
OnEnter(func(p *Part) (err error) { enter++; return }).
|
||||||
OnExit(func(p *Part) (err error) { exit--; return })
|
OnExit(func(p *Part) (err error) { exit--; return })
|
||||||
|
|
||||||
walker.
|
walker.RegisterContentDispositionHandler("attachment").
|
||||||
RegisterContentDispositionHandler("attachment").
|
|
||||||
OnEnter(func(p *Part, hdl PartHandlerFunc) (err error) { _ = hdl(p); _ = hdl(p); return }).
|
OnEnter(func(p *Part, hdl PartHandlerFunc) (err error) { _ = hdl(p); _ = hdl(p); return }).
|
||||||
OnExit(func(p *Part, hdl PartHandlerFunc) (err error) { _ = hdl(p); _ = hdl(p); return })
|
OnExit(func(p *Part, hdl PartHandlerFunc) (err error) { _ = hdl(p); _ = hdl(p); return })
|
||||||
|
|
||||||
|
|||||||
@ -61,79 +61,79 @@ func readerToString(r io.Reader) string {
|
|||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextPlain(t *testing.T) {
|
func TestParseTextPlain(t *testing.T) {
|
||||||
f := f("text_plain.eml")
|
f := f("text_plain.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "body", m.Body)
|
assert.Equal(t, "body", m.Body)
|
||||||
assert.Equal(t, s("text_plain.mime"), mimeBody)
|
assert.Equal(t, s("text_plain.mime"), mimeMessage)
|
||||||
assert.Equal(t, "body", plainContents)
|
assert.Equal(t, "body", plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 0)
|
assert.Len(t, attReaders, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextPlainUTF8(t *testing.T) {
|
func TestParseTextPlainUTF8(t *testing.T) {
|
||||||
f := f("text_plain_utf8.eml")
|
f := f("text_plain_utf8.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "body", m.Body)
|
assert.Equal(t, "body", m.Body)
|
||||||
assert.Equal(t, s("text_plain_utf8.mime"), mimeBody)
|
assert.Equal(t, s("text_plain_utf8.mime"), mimeMessage)
|
||||||
assert.Equal(t, "body", plainContents)
|
assert.Equal(t, "body", plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 0)
|
assert.Len(t, attReaders, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextPlainLatin1(t *testing.T) {
|
func TestParseTextPlainLatin1(t *testing.T) {
|
||||||
f := f("text_plain_latin1.eml")
|
f := f("text_plain_latin1.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "ééééééé", m.Body)
|
assert.Equal(t, "ééééééé", m.Body)
|
||||||
assert.Equal(t, s("text_plain_latin1.mime"), mimeBody)
|
assert.Equal(t, s("text_plain_latin1.mime"), mimeMessage)
|
||||||
assert.Equal(t, "ééééééé", plainContents)
|
assert.Equal(t, "ééééééé", plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 0)
|
assert.Len(t, attReaders, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextPlainUnknownCharsetIsActuallyLatin1(t *testing.T) {
|
func TestParseTextPlainUnknownCharsetIsActuallyLatin1(t *testing.T) {
|
||||||
f := f("text_plain_unknown_latin1.eml")
|
f := f("text_plain_unknown_latin1.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "ééééééé", m.Body)
|
assert.Equal(t, "ééééééé", m.Body)
|
||||||
assert.Equal(t, s("text_plain_unknown_latin1.mime"), mimeBody)
|
assert.Equal(t, s("text_plain_unknown_latin1.mime"), mimeMessage)
|
||||||
assert.Equal(t, "ééééééé", plainContents)
|
assert.Equal(t, "ééééééé", plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 0)
|
assert.Len(t, attReaders, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextPlainUnknownCharsetIsActuallyLatin2(t *testing.T) {
|
func TestParseTextPlainUnknownCharsetIsActuallyLatin2(t *testing.T) {
|
||||||
f := f("text_plain_unknown_latin2.eml")
|
f := f("text_plain_unknown_latin2.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
@ -146,271 +146,271 @@ func TestParseMessageTextPlainUnknownCharsetIsActuallyLatin2(t *testing.T) {
|
|||||||
assert.NotEqual(t, []byte("řšřšřš"), expect)
|
assert.NotEqual(t, []byte("řšřšřš"), expect)
|
||||||
|
|
||||||
assert.Equal(t, string(expect), m.Body)
|
assert.Equal(t, string(expect), m.Body)
|
||||||
assert.Equal(t, s("text_plain_unknown_latin2.mime"), mimeBody)
|
assert.Equal(t, s("text_plain_unknown_latin2.mime"), mimeMessage)
|
||||||
assert.Equal(t, string(expect), plainContents)
|
assert.Equal(t, string(expect), plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 0)
|
assert.Len(t, attReaders, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextPlainAlready7Bit(t *testing.T) {
|
func TestParseTextPlainAlready7Bit(t *testing.T) {
|
||||||
f := f("text_plain_7bit.eml")
|
f := f("text_plain_7bit.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "body", m.Body)
|
assert.Equal(t, "body", m.Body)
|
||||||
assert.Equal(t, s("text_plain_7bit.mime"), mimeBody)
|
assert.Equal(t, s("text_plain_7bit.mime"), mimeMessage)
|
||||||
assert.Equal(t, "body", plainContents)
|
assert.Equal(t, "body", plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 0)
|
assert.Len(t, attReaders, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextPlainWithOctetAttachment(t *testing.T) {
|
func TestParseTextPlainWithOctetAttachment(t *testing.T) {
|
||||||
f := f("text_plain_octet_attachment.eml")
|
f := f("text_plain_octet_attachment.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "body", m.Body)
|
assert.Equal(t, "body", m.Body)
|
||||||
assert.Equal(t, s("text_plain_octet_attachment.mime"), mimeBody)
|
assert.Equal(t, s("text_plain_octet_attachment.mime"), mimeMessage)
|
||||||
assert.Equal(t, "body", plainContents)
|
assert.Equal(t, "body", plainBody)
|
||||||
|
|
||||||
require.Len(t, atts, 1)
|
require.Len(t, attReaders, 1)
|
||||||
assert.Equal(t, readerToString(atts[0]), "if you are reading this, hi!")
|
assert.Equal(t, readerToString(attReaders[0]), "if you are reading this, hi!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextPlainWithOctetAttachmentGoodFilename(t *testing.T) {
|
func TestParseTextPlainWithOctetAttachmentGoodFilename(t *testing.T) {
|
||||||
f := f("text_plain_octet_attachment_good_2231_filename.eml")
|
f := f("text_plain_octet_attachment_good_2231_filename.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "body", m.Body)
|
assert.Equal(t, "body", m.Body)
|
||||||
assert.Equal(t, s("text_plain_octet_attachment_good_2231_filename.mime"), mimeBody)
|
assert.Equal(t, s("text_plain_octet_attachment_good_2231_filename.mime"), mimeMessage)
|
||||||
assert.Equal(t, "body", plainContents)
|
assert.Equal(t, "body", plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 1)
|
assert.Len(t, attReaders, 1)
|
||||||
assert.Equal(t, readerToString(atts[0]), "if you are reading this, hi!")
|
assert.Equal(t, readerToString(attReaders[0]), "if you are reading this, hi!")
|
||||||
assert.Equal(t, "😁😂.txt", m.Attachments[0].Name)
|
assert.Equal(t, "😁😂.txt", m.Attachments[0].Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextPlainWithOctetAttachmentBadFilename(t *testing.T) {
|
func TestParseTextPlainWithOctetAttachmentBadFilename(t *testing.T) {
|
||||||
f := f("text_plain_octet_attachment_bad_2231_filename.eml")
|
f := f("text_plain_octet_attachment_bad_2231_filename.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "body", m.Body)
|
assert.Equal(t, "body", m.Body)
|
||||||
assert.Equal(t, s("text_plain_octet_attachment_bad_2231_filename.mime"), mimeBody)
|
assert.Equal(t, s("text_plain_octet_attachment_bad_2231_filename.mime"), mimeMessage)
|
||||||
assert.Equal(t, "body", plainContents)
|
assert.Equal(t, "body", plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 1)
|
assert.Len(t, attReaders, 1)
|
||||||
assert.Equal(t, readerToString(atts[0]), "if you are reading this, hi!")
|
assert.Equal(t, readerToString(attReaders[0]), "if you are reading this, hi!")
|
||||||
assert.Equal(t, "attachment.bin", m.Attachments[0].Name)
|
assert.Equal(t, "attachment.bin", m.Attachments[0].Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextPlainWithPlainAttachment(t *testing.T) {
|
func TestParseTextPlainWithPlainAttachment(t *testing.T) {
|
||||||
f := f("text_plain_plain_attachment.eml")
|
f := f("text_plain_plain_attachment.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "body", m.Body)
|
assert.Equal(t, "body", m.Body)
|
||||||
assert.Equal(t, s("text_plain_plain_attachment.mime"), mimeBody)
|
assert.Equal(t, s("text_plain_plain_attachment.mime"), mimeMessage)
|
||||||
assert.Equal(t, "body", plainContents)
|
assert.Equal(t, "body", plainBody)
|
||||||
|
|
||||||
require.Len(t, atts, 1)
|
require.Len(t, attReaders, 1)
|
||||||
assert.Equal(t, readerToString(atts[0]), "attachment")
|
assert.Equal(t, readerToString(attReaders[0]), "attachment")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextPlainWithImageInline(t *testing.T) {
|
func TestParseTextPlainWithImageInline(t *testing.T) {
|
||||||
f := f("text_plain_image_inline.eml")
|
f := f("text_plain_image_inline.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "body", m.Body)
|
assert.Equal(t, "body", m.Body)
|
||||||
assert.Equal(t, s("text_plain_image_inline.mime"), mimeBody)
|
assert.Equal(t, s("text_plain_image_inline.mime"), mimeMessage)
|
||||||
assert.Equal(t, "body", plainContents)
|
assert.Equal(t, "body", plainBody)
|
||||||
|
|
||||||
// The inline image is an 8x8 mic-dropping gopher.
|
// The inline image is an 8x8 mic-dropping gopher.
|
||||||
require.Len(t, atts, 1)
|
require.Len(t, attReaders, 1)
|
||||||
img, err := png.DecodeConfig(atts[0])
|
img, err := png.DecodeConfig(attReaders[0])
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, 8, img.Width)
|
assert.Equal(t, 8, img.Width)
|
||||||
assert.Equal(t, 8, img.Height)
|
assert.Equal(t, 8, img.Height)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageWithMultipleTextParts(t *testing.T) {
|
func TestParseWithMultipleTextParts(t *testing.T) {
|
||||||
f := f("multiple_text_parts.eml")
|
f := f("multiple_text_parts.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "body\nsome other part of the message", m.Body)
|
assert.Equal(t, "body\nsome other part of the message", m.Body)
|
||||||
assert.Equal(t, s("multiple_text_parts.mime"), mimeBody)
|
assert.Equal(t, s("multiple_text_parts.mime"), mimeMessage)
|
||||||
assert.Equal(t, "body\nsome other part of the message", plainContents)
|
assert.Equal(t, "body\nsome other part of the message", plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 0)
|
assert.Len(t, attReaders, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextHTML(t *testing.T) {
|
func TestParseTextHTML(t *testing.T) {
|
||||||
rand.Seed(0)
|
rand.Seed(0)
|
||||||
|
|
||||||
f := f("text_html.eml")
|
f := f("text_html.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> without attachment</body></html>", m.Body)
|
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> without attachment</body></html>", m.Body)
|
||||||
assert.Equal(t, s("text_html.mime"), mimeBody)
|
assert.Equal(t, s("text_html.mime"), mimeMessage)
|
||||||
assert.Equal(t, "This is body of *HTML mail* without attachment", plainContents)
|
assert.Equal(t, "This is body of *HTML mail* without attachment", plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 0)
|
assert.Len(t, attReaders, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextHTMLAlready7Bit(t *testing.T) {
|
func TestParseTextHTMLAlready7Bit(t *testing.T) {
|
||||||
rand.Seed(0)
|
rand.Seed(0)
|
||||||
|
|
||||||
f := f("text_html_7bit.eml")
|
f := f("text_html_7bit.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> without attachment</body></html>", m.Body)
|
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> without attachment</body></html>", m.Body)
|
||||||
assert.Equal(t, s("text_html_7bit.mime"), mimeBody)
|
assert.Equal(t, s("text_html_7bit.mime"), mimeMessage)
|
||||||
assert.Equal(t, "This is body of *HTML mail* without attachment", plainContents)
|
assert.Equal(t, "This is body of *HTML mail* without attachment", plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 0)
|
assert.Len(t, attReaders, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextHTMLWithOctetAttachment(t *testing.T) {
|
func TestParseTextHTMLWithOctetAttachment(t *testing.T) {
|
||||||
rand.Seed(0)
|
rand.Seed(0)
|
||||||
|
|
||||||
f := f("text_html_octet_attachment.eml")
|
f := f("text_html_octet_attachment.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", m.Body)
|
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", m.Body)
|
||||||
assert.Equal(t, s("text_html_octet_attachment.mime"), mimeBody)
|
assert.Equal(t, s("text_html_octet_attachment.mime"), mimeMessage)
|
||||||
assert.Equal(t, "This is body of *HTML mail* with attachment", plainContents)
|
assert.Equal(t, "This is body of *HTML mail* with attachment", plainBody)
|
||||||
|
|
||||||
require.Len(t, atts, 1)
|
require.Len(t, attReaders, 1)
|
||||||
assert.Equal(t, readerToString(atts[0]), "if you are reading this, hi!")
|
assert.Equal(t, readerToString(attReaders[0]), "if you are reading this, hi!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextHTMLWithPlainAttachment(t *testing.T) { // nolint[deadcode]
|
func TestParseTextHTMLWithPlainAttachment(t *testing.T) {
|
||||||
rand.Seed(0)
|
rand.Seed(0)
|
||||||
|
|
||||||
f := f("text_html_plain_attachment.eml")
|
f := f("text_html_plain_attachment.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
// BAD: plainContents should not be empty!
|
// BAD: plainBody should not be empty!
|
||||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", m.Body)
|
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", m.Body)
|
||||||
assert.Equal(t, s("text_html_plain_attachment.mime"), mimeBody)
|
assert.Equal(t, s("text_html_plain_attachment.mime"), mimeMessage)
|
||||||
assert.Equal(t, "This is body of *HTML mail* with attachment", plainContents)
|
assert.Equal(t, "This is body of *HTML mail* with attachment", plainBody)
|
||||||
|
|
||||||
require.Len(t, atts, 1)
|
require.Len(t, attReaders, 1)
|
||||||
assert.Equal(t, readerToString(atts[0]), "attachment")
|
assert.Equal(t, readerToString(attReaders[0]), "attachment")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextHTMLWithImageInline(t *testing.T) {
|
func TestParseTextHTMLWithImageInline(t *testing.T) {
|
||||||
rand.Seed(0)
|
rand.Seed(0)
|
||||||
|
|
||||||
f := f("text_html_image_inline.eml")
|
f := f("text_html_image_inline.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", m.Body)
|
assert.Equal(t, "<html><head></head><body>This is body of <b>HTML mail</b> with attachment</body></html>", m.Body)
|
||||||
assert.Equal(t, s("text_html_image_inline.mime"), mimeBody)
|
assert.Equal(t, s("text_html_image_inline.mime"), mimeMessage)
|
||||||
assert.Equal(t, "This is body of *HTML mail* with attachment", plainContents)
|
assert.Equal(t, "This is body of *HTML mail* with attachment", plainBody)
|
||||||
|
|
||||||
// The inline image is an 8x8 mic-dropping gopher.
|
// The inline image is an 8x8 mic-dropping gopher.
|
||||||
require.Len(t, atts, 1)
|
require.Len(t, attReaders, 1)
|
||||||
img, err := png.DecodeConfig(atts[0])
|
img, err := png.DecodeConfig(attReaders[0])
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, 8, img.Width)
|
assert.Equal(t, 8, img.Width)
|
||||||
assert.Equal(t, 8, img.Height)
|
assert.Equal(t, 8, img.Height)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageWithAttachedPublicKey(t *testing.T) { // nolint[deadcode]
|
func TestParseWithAttachedPublicKey(t *testing.T) {
|
||||||
f := f("text_plain.eml")
|
f := f("text_plain.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
// BAD: Public Key is not attached unless Content-Type is specified (not required)!
|
// BAD: Public Key is not attached unless Content-Type is specified (not required)!
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "publickey", "publickeyname")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "publickey", "publickeyname")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
assert.Equal(t, `"Receiver" <receiver@pm.me>`, m.ToList[0].String())
|
||||||
|
|
||||||
assert.Equal(t, "body", m.Body)
|
assert.Equal(t, "body", m.Body)
|
||||||
assert.Equal(t, s("text_plain_pubkey.mime"), mimeBody)
|
assert.Equal(t, s("text_plain_pubkey.mime"), mimeMessage)
|
||||||
assert.Equal(t, "body", plainContents)
|
assert.Equal(t, "body", plainBody)
|
||||||
|
|
||||||
// BAD: Public key not available as an attachment!
|
// BAD: Public key not available as an attachment!
|
||||||
require.Len(t, atts, 1)
|
require.Len(t, attReaders, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMessageTextHTMLWithEmbeddedForeignEncoding(t *testing.T) { // nolint[deadcode]
|
func TestParseTextHTMLWithEmbeddedForeignEncoding(t *testing.T) {
|
||||||
rand.Seed(0)
|
rand.Seed(0)
|
||||||
|
|
||||||
f := f("text_html_embedded_foreign_encoding.eml")
|
f := f("text_html_embedded_foreign_encoding.eml")
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
m, mimeBody, plainContents, atts, err := Parse(f, "", "")
|
m, mimeMessage, plainBody, attReaders, err := Parse(f, "", "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
assert.Equal(t, `"Sender" <sender@pm.me>`, m.Sender.String())
|
||||||
@ -418,8 +418,58 @@ func TestParseMessageTextHTMLWithEmbeddedForeignEncoding(t *testing.T) { // noli
|
|||||||
|
|
||||||
// BAD: Bridge does not detect the charset specified in the <meta> tag of the html.
|
// BAD: Bridge does not detect the charset specified in the <meta> tag of the html.
|
||||||
assert.Equal(t, `<html><head><meta charset="ISO-8859-2"></head><body>latin2 řšřš</body></html>`, m.Body)
|
assert.Equal(t, `<html><head><meta charset="ISO-8859-2"></head><body>latin2 řšřš</body></html>`, m.Body)
|
||||||
assert.Equal(t, s("text_html_embedded_foreign_encoding.mime"), mimeBody)
|
assert.Equal(t, s("text_html_embedded_foreign_encoding.mime"), mimeMessage)
|
||||||
assert.Equal(t, `latin2 řšřš`, plainContents)
|
assert.Equal(t, `latin2 řšřš`, plainBody)
|
||||||
|
|
||||||
assert.Len(t, atts, 0)
|
assert.Len(t, attReaders, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseMultipartAlternative(t *testing.T) {
|
||||||
|
rand.Seed(0)
|
||||||
|
|
||||||
|
f := f("multipart_alternative.eml")
|
||||||
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
|
m, _, plainBody, _, err := Parse(f, "", "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, `"schizofrenic" <schizofrenic@pm.me>`, m.Sender.String())
|
||||||
|
assert.Equal(t, `<pmbridgeietest@outlook.com>`, m.ToList[0].String())
|
||||||
|
|
||||||
|
assert.Equal(t, `<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<b>aoeuaoeu</b>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`, m.Body)
|
||||||
|
|
||||||
|
assert.Equal(t, "*aoeuaoeu*\n\n", plainBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseMultipartAlternativeNested(t *testing.T) {
|
||||||
|
rand.Seed(0)
|
||||||
|
|
||||||
|
f := f("multipart_alternative_nested.eml")
|
||||||
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
|
m, _, plainBody, _, err := Parse(f, "", "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, `"schizofrenic" <schizofrenic@pm.me>`, m.Sender.String())
|
||||||
|
assert.Equal(t, `<pmbridgeietest@outlook.com>`, m.ToList[0].String())
|
||||||
|
|
||||||
|
assert.Equal(t, `<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<b>multipart 2.2</b>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`, m.Body)
|
||||||
|
|
||||||
|
assert.Equal(t, "*multipart 2.1*\n\n", plainBody)
|
||||||
}
|
}
|
||||||
|
|||||||
30
pkg/message/testdata/multipart_alternative.eml
vendored
Normal file
30
pkg/message/testdata/multipart_alternative.eml
vendored
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
To: pmbridgeietest@outlook.com
|
||||||
|
From: schizofrenic <schizofrenic@pm.me>
|
||||||
|
Subject: aoeuaoeu
|
||||||
|
Date: Thu, 30 Jul 2020 13:35:24 +0200
|
||||||
|
MIME-Version: 1.0
|
||||||
|
Content-Type: multipart/alternative; boundary="------------22BC647264E52252E386881A"
|
||||||
|
Content-Language: en-US
|
||||||
|
|
||||||
|
This is a multi-part message in MIME format.
|
||||||
|
--------------22BC647264E52252E386881A
|
||||||
|
Content-Type: text/plain; charset=utf-8; format=flowed
|
||||||
|
Content-Transfer-Encoding: 7bit
|
||||||
|
|
||||||
|
*aoeuaoeu*
|
||||||
|
|
||||||
|
|
||||||
|
--------------22BC647264E52252E386881A
|
||||||
|
Content-Type: text/html; charset=utf-8
|
||||||
|
Content-Transfer-Encoding: 7bit
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<b>aoeuaoeu</b>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
--------------22BC647264E52252E386881A--
|
||||||
64
pkg/message/testdata/multipart_alternative_nested.eml
vendored
Normal file
64
pkg/message/testdata/multipart_alternative_nested.eml
vendored
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
To: pmbridgeietest@outlook.com
|
||||||
|
From: schizofrenic <schizofrenic@pm.me>
|
||||||
|
Subject: aoeuaoeu
|
||||||
|
Date: Thu, 30 Jul 2020 13:35:24 +0200
|
||||||
|
MIME-Version: 1.0
|
||||||
|
Content-Type: multipart/alternative; boundary="------------abcdefghijklmnopqrstuvwx"
|
||||||
|
Content-Language: en-US
|
||||||
|
|
||||||
|
This is a multi-part message in MIME format.
|
||||||
|
--------------abcdefghijklmnopqrstuvwx
|
||||||
|
Content-Type: multipart/alternative; boundary="------------22BC647264E52252E386881A"
|
||||||
|
Content-Language: en-US
|
||||||
|
|
||||||
|
This is a multi-part message in MIME format.
|
||||||
|
--------------22BC647264E52252E386881A
|
||||||
|
Content-Type: text/plain; charset=utf-8; format=flowed
|
||||||
|
Content-Transfer-Encoding: 7bit
|
||||||
|
|
||||||
|
*multipart 1.1*
|
||||||
|
|
||||||
|
|
||||||
|
--------------22BC647264E52252E386881A
|
||||||
|
Content-Type: text/html; charset=utf-8
|
||||||
|
Content-Transfer-Encoding: 7bit
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<b>multipart 1.2</b>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
--------------22BC647264E52252E386881A--
|
||||||
|
|
||||||
|
--------------abcdefghijklmnopqrstuvwx
|
||||||
|
Content-Type: multipart/alternative; boundary="------------22BC647264E52252E386881B"
|
||||||
|
Content-Language: en-US
|
||||||
|
|
||||||
|
This is a multi-part message in MIME format.
|
||||||
|
--------------22BC647264E52252E386881B
|
||||||
|
Content-Type: text/plain; charset=utf-8; format=flowed
|
||||||
|
Content-Transfer-Encoding: 7bit
|
||||||
|
|
||||||
|
*multipart 2.1*
|
||||||
|
|
||||||
|
|
||||||
|
--------------22BC647264E52252E386881B
|
||||||
|
Content-Type: text/html; charset=utf-8
|
||||||
|
Content-Transfer-Encoding: 7bit
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<b>multipart 2.2</b>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
--------------22BC647264E52252E386881B--
|
||||||
|
|
||||||
|
--------------abcdefghijklmnopqrstuvwx--
|
||||||
Reference in New Issue
Block a user