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 TestSplit(t *testing.T) {
 26	test := func(addr, mbox, domain string, fail bool) {
 27		t.Helper()
 28
 29		actualMbox, actualDomain, err := Split(addr)
 30		if err != nil && !fail {
 31			t.Errorf("%s: unexpected error: %v", addr, err)
 32			return
 33		}
 34		if err == nil && fail {
 35			t.Errorf("%s: expected error, got %s, %s", addr, actualMbox, actualDomain)
 36			return
 37		}
 38
 39		if actualMbox != mbox {
 40			t.Errorf("%s: wrong local part, want %s, got %s", addr, mbox, actualMbox)
 41		}
 42		if actualDomain != domain {
 43			t.Errorf("%s: wrong domain part, want %s, got %s", addr, domain, actualDomain)
 44		}
 45	}
 46
 47	test("simple@example.org", "simple", "example.org", false)
 48	test("simple@[1.2.3.4]", "simple", "[1.2.3.4]", false)
 49	test("simple@[IPv6:beef::1]", "simple", "[IPv6:beef::1]", false)
 50	test("@example.org", "", "", true)
 51	test("@", "", "", true)
 52	test("no-domain@", "", "", true)
 53	test("@no-local-part", "", "", true)
 54
 55	// Not a valid address, but a special value for SMTP
 56	// should be handled separately where necessary.
 57	test("", "", "", true)
 58
 59	// A special SMTP value too, but permitted now.
 60	test("postmaster", "postmaster", "", false)
 61}
 62
 63func TestUnquoteMbox(t *testing.T) {
 64	test := func(inputMbox, expectedMbox string, fail bool) {
 65		t.Helper()
 66
 67		actualMbox, err := UnquoteMbox(inputMbox)
 68		if err != nil && !fail {
 69			t.Errorf("unexpected error: %v", err)
 70			return
 71		}
 72		if err == nil && fail {
 73			t.Errorf("expected error, got %s", actualMbox)
 74			return
 75		}
 76
 77		if actualMbox != expectedMbox {
 78			t.Errorf("wrong local part, want %s, got %s", actualMbox, actualMbox)
 79		}
 80	}
 81
 82	test(`no\@no`, "", true)
 83	test("no@no", "", true)
 84	test(`no\"no`, "", true)
 85	test(`"no\"no"`, `no"no`, false)
 86	test(`"no@no"`, `no@no`, false)
 87	test(`"no no"`, `no no`, false)
 88	test(`"no\\no"`, `no\no`, false)
 89	test(`"no"no`, "", true)
 90	test(`postmaster`, "postmaster", false)
 91	test(`foo`, "foo", false)
 92}
 93
 94func TestQuoteMbox(t *testing.T) {
 95	test := func(inputMbox, expectedMbox string) {
 96		t.Helper()
 97
 98		actualMbox := QuoteMbox(inputMbox)
 99		if actualMbox != expectedMbox {
100			t.Errorf("wrong local part, want %s, got %s", actualMbox, actualMbox)
101		}
102	}
103
104	test(`no"no`, `"no\"no"`)
105	test(`no@no`, `"no@no"`)
106	test(`no no`, `"no no"`)
107	test(`no\no`, `"no\\no"`)
108	test("postmaster", `postmaster`)
109	test("foo", `foo`)
110}