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	"testing"
 23
 24	"github.com/foxcpp/maddy/framework/module"
 25	"github.com/foxcpp/maddy/internal/testutils"
 26)
 27
 28func 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	}
 45
 46	testutils.DoTestDelivery(t, &d, "whatever@whatever", []string{"whatever@whatever"})
 47
 48	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	}
 60
 61	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}
 65
 66func 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	}
 84
 85	testutils.DoTestDelivery(t, &d, "whatever@whatever", []string{"whatever@whatever"})
 86
 87	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	}
 96
 97	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}
101
102func 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	}
118
119	testutils.DoTestDelivery(t, &d, "whatever@whatever", []string{"whatever@whatever"})
120
121	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	}
133
134	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}