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 modify
 20
 21import (
 22	"context"
 23	"reflect"
 24	"testing"
 25
 26	"github.com/foxcpp/maddy/framework/config"
 27	"github.com/foxcpp/maddy/internal/testutils"
 28)
 29
 30func testReplaceAddr(t *testing.T, modName string) {
 31	test := func(addr string, expectedMulti []string, aliases map[string][]string) {
 32		t.Helper()
 33
 34		mod, err := NewReplaceAddr(modName, "", nil, []string{"dummy"})
 35		if err != nil {
 36			t.Fatal(err)
 37		}
 38		m := mod.(*replaceAddr)
 39		if err := m.Init(config.NewMap(nil, config.Node{})); err != nil {
 40			t.Fatal(err)
 41		}
 42		m.table = testutils.MultiTable{M: aliases}
 43
 44		var actualMulti []string
 45		if modName == "modify.replace_sender" {
 46			var actual string
 47			actual, err = m.RewriteSender(context.Background(), addr)
 48			if err != nil {
 49				t.Fatal(err)
 50			}
 51			actualMulti = []string{actual}
 52		}
 53		if modName == "modify.replace_rcpt" {
 54			actualMulti, err = m.RewriteRcpt(context.Background(), addr)
 55			if err != nil {
 56				t.Fatal(err)
 57			}
 58		}
 59
 60		if !reflect.DeepEqual(actualMulti, expectedMulti) {
 61			t.Errorf("want %s, got %s", expectedMulti, actualMulti)
 62		}
 63	}
 64
 65	test("test@example.org", []string{"test@example.org"}, nil)
 66	test("postmaster", []string{"postmaster"}, nil)
 67	test("test@example.com", []string{"test@example.org"},
 68		map[string][]string{"test@example.com": []string{"test@example.org"}})
 69	test(`"\"test @ test\""@example.com`, []string{"test@example.org"},
 70		map[string][]string{`"\"test @ test\""@example.com`: []string{"test@example.org"}})
 71	test(`test@example.com`, []string{`"\"test @ test\""@example.org`},
 72		map[string][]string{`test@example.com`: []string{`"\"test @ test\""@example.org`}})
 73	test(`"\"test @ test\""@example.com`, []string{`"\"b @ b\""@example.com`},
 74		map[string][]string{`"\"test @ test\""`: []string{`"\"b @ b\""`}})
 75	test("TeSt@eXAMple.com", []string{"test@example.org"},
 76		map[string][]string{"test@example.com": []string{"test@example.org"}})
 77	test("test@example.com", []string{"test2@example.com"},
 78		map[string][]string{"test": []string{"test2"}})
 79	test("test@example.com", []string{"test2@example.org"},
 80		map[string][]string{"test": []string{"test2@example.org"}})
 81	test("postmaster", []string{"test2@example.org"},
 82		map[string][]string{"postmaster": []string{"test2@example.org"}})
 83	test("TeSt@examPLE.com", []string{"test2@example.com"},
 84		map[string][]string{"test": []string{"test2"}})
 85	test("test@example.com", []string{"test3@example.com"},
 86		map[string][]string{
 87			"test@example.com": []string{"test3@example.com"},
 88			"test":             []string{"test2"},
 89		})
 90	test("rcpt@E\u0301.example.com", []string{"rcpt@foo.example.com"},
 91		map[string][]string{
 92			"rcpt@\u00E9.example.com": []string{"rcpt@foo.example.com"},
 93		})
 94	test("E\u0301@foo.example.com", []string{"rcpt@foo.example.com"},
 95		map[string][]string{
 96			"\u00E9@foo.example.com": []string{"rcpt@foo.example.com"},
 97		})
 98
 99	if modName == "modify.replace_rcpt" {
100		//multiple aliases
101		test("test@example.com", []string{"test@example.org", "test@example.net"},
102			map[string][]string{"test@example.com": []string{"test@example.org", "test@example.net"}})
103		test("test@example.com", []string{"1@example.com", "2@example.com", "3@example.com"},
104			map[string][]string{"test@example.com": []string{"1@example.com", "2@example.com", "3@example.com"}})
105	}
106}
107
108func TestReplaceAddr_RewriteSender(t *testing.T) {
109	testReplaceAddr(t, "modify.replace_sender")
110}
111
112func TestReplaceAddr_RewriteRcpt(t *testing.T) {
113	testReplaceAddr(t, "modify.replace_rcpt")
114}