1/*2Maddy Mail Server - Composable all-in-one email server.3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors45This program is free software: you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation, either version 3 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program. If not, see <https://www.gnu.org/licenses/>.17*/1819package msgpipeline2021import (22 "errors"23 "testing"2425 "github.com/foxcpp/maddy/framework/module"26 "github.com/foxcpp/maddy/internal/modify"27 "github.com/foxcpp/maddy/internal/testutils"28)2930type multipleErrs map[string]error3132func (m multipleErrs) SetStatus(rcptTo string, err error) {33 m[rcptTo] = err34}3536func TestMsgPipeline_BodyNonAtomic(t *testing.T) {37 err := errors.New("go away")3839 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 }5657 c := multipleErrs{}58 testutils.DoTestDeliveryNonAtomic(t, c, &d, "sender@example.org", []string{"tester@example.org", "tester2@example.org"})5960 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}6768func TestMsgPipeline_BodyNonAtomic_ModifiedRcpt(t *testing.T) {69 err := errors.New("go away")7071 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 }9899 c := multipleErrs{}100 testutils.DoTestDeliveryNonAtomic(t, c, &d, "sender@example.org", []string{"tester@example.org"})101102 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}109110func TestMsgPipeline_BodyNonAtomic_ExpandAtomic(t *testing.T) {111 err := errors.New("go away")112113 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 }132133 c := multipleErrs{}134 testutils.DoTestDeliveryNonAtomic(t, c, &d, "sender@example.org", []string{"tester@example.org", "tester2@example.org"})135136 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}