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 testutils2021import (22 "context"2324 "github.com/emersion/go-message/textproto"25 "github.com/foxcpp/maddy/framework/buffer"26 "github.com/foxcpp/maddy/framework/config"27 "github.com/foxcpp/maddy/framework/module"28)2930type Check struct {31 InitErr error32 EarlyErr error33 ConnRes module.CheckResult34 SenderRes module.CheckResult35 RcptRes module.CheckResult36 BodyRes module.CheckResult3738 ConnCalls int39 SenderCalls int40 RcptCalls int41 BodyCalls int4243 UnclosedStates int4445 InstName string46}4748func (c *Check) CheckStateForMsg(ctx context.Context, msgMeta *module.MsgMetadata) (module.CheckState, error) {49 if c.InitErr != nil {50 return nil, c.InitErr51 }5253 c.UnclosedStates++54 return &checkState{msgMeta, c}, nil55}5657func (c *Check) Init(*config.Map) error {58 return nil59}6061func (c *Check) Name() string {62 return "test_check"63}6465func (c *Check) InstanceName() string {66 if c.InstName != "" {67 return c.InstName68 }69 return "test_check"70}7172func (c *Check) CheckConnection(ctx context.Context, state *module.ConnState) error {73 return c.EarlyErr74}7576type checkState struct {77 msgMeta *module.MsgMetadata78 check *Check79}8081func (cs *checkState) CheckConnection(ctx context.Context) module.CheckResult {82 cs.check.ConnCalls++83 return cs.check.ConnRes84}8586func (cs *checkState) CheckSender(ctx context.Context, from string) module.CheckResult {87 cs.check.SenderCalls++88 return cs.check.SenderRes89}9091func (cs *checkState) CheckRcpt(ctx context.Context, to string) module.CheckResult {92 cs.check.RcptCalls++93 return cs.check.RcptRes94}9596func (cs *checkState) CheckBody(ctx context.Context, header textproto.Header, body buffer.Buffer) module.CheckResult {97 cs.check.BodyCalls++98 return cs.check.BodyRes99}100101func (cs *checkState) Close() error {102 cs.check.UnclosedStates--103 return nil104}105106func init() {107 module.Register("test_check", func(_, _ string, _, _ []string) (module.Module, error) {108 return &Check{}, nil109 })110 module.RegisterInstance(&Check{}, nil)111}