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 address2021import (22 "testing"23)2425func addrFuncTest(t *testing.T, f func(string) (string, error)) func(in, wantOut string, fail bool) {26 return func(in, wantOut string, fail bool) {27 t.Helper()2829 out, err := f(in)30 if err != nil {31 if !fail {32 t.Errorf("Expected failure, got none")33 }34 }35 if out != wantOut {36 t.Errorf("Wrong result: want '%s', got '%s'", wantOut, out)37 }38 }39}4041func TestForLookup(t *testing.T) {42 test := addrFuncTest(t, ForLookup)43 test("test@example.org", "test@example.org", false)44 test("E\u0301@example.org", "\u00E9@example.org", false)45 test("test@EXAMPLE.org", "test@example.org", false)46 test("test@xn--e1aybc.example.org", "test@тест.example.org", false)47 test("TEST@xn--99999999999.example.org", "test@xn--99999999999.example.org", true)48 test("tESt@", "test@", true)49 test("postmaster", "postmaster", false)50}5152func TestCleanDomain(t *testing.T) {53 test := addrFuncTest(t, CleanDomain)54 test("test@example.org", "test@example.org", false)55 test("whateveR@example.org", "whateveR@example.org", false)56 test("E\u0301@example.org", "E\u0301@example.org", false)57 test("test@EXAMPLE.org", "test@example.org", false)58 test("test@xn--e1aybc.example.org", "test@тест.example.org", false)59 test("TEST@xn--99999999999.example.org", "TEST@xn--99999999999.example.org", true)60 test("tESt@", "tESt@", true)61 test("postmaster", "postmaster", false)62}6364func TestEqual(t *testing.T) {65 test := func(in1, in2 string, wantEq bool) {66 eq := Equal(in1, in2)67 if eq != wantEq {68 t.Errorf("Want Equal(%s, %s) == %v, got %v", in1, in2, wantEq, eq)69 }70 }7172 test("test@example.org", "test@example.org", true)73 test("test2@example.org", "test@example.org", false)74 test("TEST2@example.org", "TesT2@example.org", true)75 test("E\u0301@example.org", "\u00E9@example.org", true)76 test("test@тест.example.org", "test@xn--e1aybc.example.org", true)77 test("test@xn--999999999999999.example.org", "test@xn--999999999999999.example.org", true)78 test("test@xn--999999999999.example.org", "test@xn--999999999999999.example.org", false)79}8081func TestIsASCII(t *testing.T) {82 if !IsASCII("hello") {83 t.Errorf("'hello' is ASCII")84 }85 if IsASCII("тест") {86 t.Errorf("'тест' is non-ASCII")87 }88}