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 modify2021import (22 "context"23 "reflect"24 "testing"2526 "github.com/foxcpp/maddy/framework/config"27 "github.com/foxcpp/maddy/internal/testutils"28)2930func testReplaceAddr(t *testing.T, modName string) {31 test := func(addr string, expectedMulti []string, aliases map[string][]string) {32 t.Helper()3334 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}4344 var actualMulti []string45 if modName == "modify.replace_sender" {46 var actual string47 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 }5960 if !reflect.DeepEqual(actualMulti, expectedMulti) {61 t.Errorf("want %s, got %s", expectedMulti, actualMulti)62 }63 }6465 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 })9899 if modName == "modify.replace_rcpt" {100 //multiple aliases101 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}107108func TestReplaceAddr_RewriteSender(t *testing.T) {109 testReplaceAddr(t, "modify.replace_sender")110}111112func TestReplaceAddr_RewriteRcpt(t *testing.T) {113 testReplaceAddr(t, "modify.replace_rcpt")114}