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 testutils
 20
 21import (
 22	"context"
 23
 24	"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)
 29
 30type Check struct {
 31	InitErr   error
 32	EarlyErr  error
 33	ConnRes   module.CheckResult
 34	SenderRes module.CheckResult
 35	RcptRes   module.CheckResult
 36	BodyRes   module.CheckResult
 37
 38	ConnCalls   int
 39	SenderCalls int
 40	RcptCalls   int
 41	BodyCalls   int
 42
 43	UnclosedStates int
 44
 45	InstName string
 46}
 47
 48func (c *Check) CheckStateForMsg(ctx context.Context, msgMeta *module.MsgMetadata) (module.CheckState, error) {
 49	if c.InitErr != nil {
 50		return nil, c.InitErr
 51	}
 52
 53	c.UnclosedStates++
 54	return &checkState{msgMeta, c}, nil
 55}
 56
 57func (c *Check) Init(*config.Map) error {
 58	return nil
 59}
 60
 61func (c *Check) Name() string {
 62	return "test_check"
 63}
 64
 65func (c *Check) InstanceName() string {
 66	if c.InstName != "" {
 67		return c.InstName
 68	}
 69	return "test_check"
 70}
 71
 72func (c *Check) CheckConnection(ctx context.Context, state *module.ConnState) error {
 73	return c.EarlyErr
 74}
 75
 76type checkState struct {
 77	msgMeta *module.MsgMetadata
 78	check   *Check
 79}
 80
 81func (cs *checkState) CheckConnection(ctx context.Context) module.CheckResult {
 82	cs.check.ConnCalls++
 83	return cs.check.ConnRes
 84}
 85
 86func (cs *checkState) CheckSender(ctx context.Context, from string) module.CheckResult {
 87	cs.check.SenderCalls++
 88	return cs.check.SenderRes
 89}
 90
 91func (cs *checkState) CheckRcpt(ctx context.Context, to string) module.CheckResult {
 92	cs.check.RcptCalls++
 93	return cs.check.RcptRes
 94}
 95
 96func (cs *checkState) CheckBody(ctx context.Context, header textproto.Header, body buffer.Buffer) module.CheckResult {
 97	cs.check.BodyCalls++
 98	return cs.check.BodyRes
 99}
100
101func (cs *checkState) Close() error {
102	cs.check.UnclosedStates--
103	return nil
104}
105
106func init() {
107	module.Register("test_check", func(_, _ string, _, _ []string) (module.Module, error) {
108		return &Check{}, nil
109	})
110	module.RegisterInstance(&Check{}, nil)
111}