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 "testing"2324 "github.com/foxcpp/maddy/framework/module"25 "github.com/foxcpp/maddy/internal/testutils"26)2728func TestMsgPipeline_Issue161(t *testing.T) {29 target := testutils.Target{}30 check1, check2 := testutils.Check{}, testutils.Check{}31 d := MsgPipeline{32 msgpipelineCfg: msgpipelineCfg{33 globalChecks: []module.Check{&check1},34 perSource: map[string]sourceBlock{},35 defaultSource: sourceBlock{36 checks: []module.Check{&check2},37 perRcpt: map[string]*rcptBlock{},38 defaultRcpt: &rcptBlock{39 targets: []module.DeliveryTarget{&target},40 },41 },42 },43 Log: testutils.Logger(t, "msgpipeline"),44 }4546 testutils.DoTestDelivery(t, &d, "whatever@whatever", []string{"whatever@whatever"})4748 if check2.ConnCalls != 1 {49 t.Errorf("CheckConnection called %d times", check2.ConnCalls)50 }51 if check2.SenderCalls != 1 {52 t.Errorf("CheckSender called %d times", check2.SenderCalls)53 }54 if check2.RcptCalls != 1 {55 t.Errorf("CheckRcpt called %d times", check2.RcptCalls)56 }57 if check2.BodyCalls != 1 {58 t.Errorf("CheckBody called %d times", check2.BodyCalls)59 }6061 if check1.UnclosedStates != 0 || check2.UnclosedStates != 0 {62 t.Fatalf("checks state objects leak or double-closed, alive counters: %v, %v", check1.UnclosedStates, check2.UnclosedStates)63 }64}6566func TestMsgPipeline_Issue161_2(t *testing.T) {67 target := testutils.Target{}68 check1, check2 := testutils.Check{}, testutils.Check{InstName: "check2"}69 d := MsgPipeline{70 msgpipelineCfg: msgpipelineCfg{71 globalChecks: []module.Check{&check1},72 perSource: map[string]sourceBlock{},73 defaultSource: sourceBlock{74 checks: []module.Check{&check1},75 perRcpt: map[string]*rcptBlock{},76 defaultRcpt: &rcptBlock{77 checks: []module.Check{&check2},78 targets: []module.DeliveryTarget{&target},79 },80 },81 },82 Log: testutils.Logger(t, "msgpipeline"),83 }8485 testutils.DoTestDelivery(t, &d, "whatever@whatever", []string{"whatever@whatever"})8687 if check2.ConnCalls != 1 {88 t.Errorf("CheckConnection called %d times", check2.ConnCalls)89 }90 if check2.SenderCalls != 1 {91 t.Errorf("CheckSender called %d times", check2.SenderCalls)92 }93 if check2.RcptCalls != 1 {94 t.Errorf("CheckRcpt called %d times", check2.RcptCalls)95 }9697 if check1.UnclosedStates != 0 || check2.UnclosedStates != 0 {98 t.Fatalf("checks state objects leak or double-closed, alive counters: %v, %v", check1.UnclosedStates, check2.UnclosedStates)99 }100}101102func TestMsgPipeline_Issue161_3(t *testing.T) {103 target := testutils.Target{}104 check1, check2 := testutils.Check{}, testutils.Check{}105 d := MsgPipeline{106 msgpipelineCfg: msgpipelineCfg{107 globalChecks: []module.Check{&check1, &check2},108 perSource: map[string]sourceBlock{},109 defaultSource: sourceBlock{110 perRcpt: map[string]*rcptBlock{},111 defaultRcpt: &rcptBlock{112 targets: []module.DeliveryTarget{&target},113 },114 },115 },116 Log: testutils.Logger(t, "msgpipeline"),117 }118119 testutils.DoTestDelivery(t, &d, "whatever@whatever", []string{"whatever@whatever"})120121 if check2.ConnCalls != 1 {122 t.Errorf("CheckConnection called %d times", check2.ConnCalls)123 }124 if check2.SenderCalls != 1 {125 t.Errorf("CheckSender called %d times", check2.SenderCalls)126 }127 if check2.RcptCalls != 1 {128 t.Errorf("CheckRcpt called %d times", check2.RcptCalls)129 }130 if check2.BodyCalls != 1 {131 t.Errorf("CheckBody called %d times", check2.BodyCalls)132 }133134 if check1.UnclosedStates != 0 || check2.UnclosedStates != 0 {135 t.Fatalf("checks state objects leak or double-closed, alive counters: %v, %v", check1.UnclosedStates, check2.UnclosedStates)136 }137}