maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

  1/*
  2Maddy Mail Server - Composable all-in-one email server.
  3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
  4
  5This program is free software: you can redistribute it and/or modify
  6it under the terms of the GNU General Public License as published by
  7the Free Software Foundation, either version 3 of the License, or
  8(at your option) any later version.
  9
 10This program is distributed in the hope that it will be useful,
 11but WITHOUT ANY WARRANTY; without even the implied warranty of
 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13GNU General Public License for more details.
 14
 15You should have received a copy of the GNU General Public License
 16along with this program.  If not, see <https://www.gnu.org/licenses/>.
 17*/
 18
 19package msgpipeline
 20
 21import (
 22	"errors"
 23	"testing"
 24
 25	"github.com/foxcpp/maddy/framework/module"
 26	"github.com/foxcpp/maddy/internal/modify"
 27	"github.com/foxcpp/maddy/internal/testutils"
 28)
 29
 30type multipleErrs map[string]error
 31
 32func (m multipleErrs) SetStatus(rcptTo string, err error) {
 33	m[rcptTo] = err
 34}
 35
 36func TestMsgPipeline_BodyNonAtomic(t *testing.T) {
 37	err := errors.New("go away")
 38
 39	target := testutils.Target{
 40		PartialBodyErr: map[string]error{
 41			"tester@example.org": err,
 42		},
 43	}
 44	d := MsgPipeline{
 45		msgpipelineCfg: msgpipelineCfg{
 46			perSource: map[string]sourceBlock{},
 47			defaultSource: sourceBlock{
 48				perRcpt: map[string]*rcptBlock{},
 49				defaultRcpt: &rcptBlock{
 50					targets: []module.DeliveryTarget{&target},
 51				},
 52			},
 53		},
 54		Log: testutils.Logger(t, "msgpipeline"),
 55	}
 56
 57	c := multipleErrs{}
 58	testutils.DoTestDeliveryNonAtomic(t, c, &d, "sender@example.org", []string{"tester@example.org", "tester2@example.org"})
 59
 60	if c["tester@example.org"] == nil {
 61		t.Fatalf("no error for tester@example.org")
 62	}
 63	if c["tester@example.org"].Error() != err.Error() {
 64		t.Errorf("wrong error for tester@example.org: %v", err)
 65	}
 66}
 67
 68func TestMsgPipeline_BodyNonAtomic_ModifiedRcpt(t *testing.T) {
 69	err := errors.New("go away")
 70
 71	target := testutils.Target{
 72		PartialBodyErr: map[string]error{
 73			"tester-alias@example.org": err,
 74		},
 75	}
 76	d := MsgPipeline{
 77		msgpipelineCfg: msgpipelineCfg{
 78			globalModifiers: modify.Group{
 79				Modifiers: []module.Modifier{
 80					testutils.Modifier{
 81						InstName: "test_modifier",
 82						RcptTo: map[string][]string{
 83							"tester@example.org": []string{"tester-alias@example.org"},
 84						},
 85					},
 86				},
 87			},
 88			perSource: map[string]sourceBlock{},
 89			defaultSource: sourceBlock{
 90				perRcpt: map[string]*rcptBlock{},
 91				defaultRcpt: &rcptBlock{
 92					targets: []module.DeliveryTarget{&target},
 93				},
 94			},
 95		},
 96		Log: testutils.Logger(t, "msgpipeline"),
 97	}
 98
 99	c := multipleErrs{}
100	testutils.DoTestDeliveryNonAtomic(t, c, &d, "sender@example.org", []string{"tester@example.org"})
101
102	if c["tester@example.org"] == nil {
103		t.Fatalf("no error for tester@example.org")
104	}
105	if c["tester@example.org"].Error() != err.Error() {
106		t.Errorf("wrong error for tester@example.org: %v", err)
107	}
108}
109
110func TestMsgPipeline_BodyNonAtomic_ExpandAtomic(t *testing.T) {
111	err := errors.New("go away")
112
113	target, target2 := testutils.Target{
114		PartialBodyErr: map[string]error{
115			"tester@example.org": err,
116		},
117	}, testutils.Target{
118		BodyErr: err,
119	}
120	d := MsgPipeline{
121		msgpipelineCfg: msgpipelineCfg{
122			perSource: map[string]sourceBlock{},
123			defaultSource: sourceBlock{
124				perRcpt: map[string]*rcptBlock{},
125				defaultRcpt: &rcptBlock{
126					targets: []module.DeliveryTarget{&target, &target2},
127				},
128			},
129		},
130		Log: testutils.Logger(t, "msgpipeline"),
131	}
132
133	c := multipleErrs{}
134	testutils.DoTestDeliveryNonAtomic(t, c, &d, "sender@example.org", []string{"tester@example.org", "tester2@example.org"})
135
136	if c["tester@example.org"] == nil {
137		t.Fatalf("no error for tester@example.org")
138	}
139	if c["tester@example.org"].Error() != err.Error() {
140		t.Errorf("wrong error for tester@example.org: %v", err)
141	}
142	if c["tester2@example.org"] == nil {
143		t.Fatalf("no error for tester@example.org")
144	}
145	if c["tester2@example.org"].Error() != err.Error() {
146		t.Errorf("wrong error for tester@example.org: %v", err)
147	}
148}