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 address
20
21import (
22	"testing"
23)
24
25func 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()
28
29		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}
40
41func 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}
51
52func 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}
63
64func 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	}
71
72	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}
80
81func 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}