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 TestSplit(t *testing.T) {26 test := func(addr, mbox, domain string, fail bool) {27 t.Helper()2829 actualMbox, actualDomain, err := Split(addr)30 if err != nil && !fail {31 t.Errorf("%s: unexpected error: %v", addr, err)32 return33 }34 if err == nil && fail {35 t.Errorf("%s: expected error, got %s, %s", addr, actualMbox, actualDomain)36 return37 }3839 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 }4647 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)5455 // Not a valid address, but a special value for SMTP56 // should be handled separately where necessary.57 test("", "", "", true)5859 // A special SMTP value too, but permitted now.60 test("postmaster", "postmaster", "", false)61}6263func TestUnquoteMbox(t *testing.T) {64 test := func(inputMbox, expectedMbox string, fail bool) {65 t.Helper()6667 actualMbox, err := UnquoteMbox(inputMbox)68 if err != nil && !fail {69 t.Errorf("unexpected error: %v", err)70 return71 }72 if err == nil && fail {73 t.Errorf("expected error, got %s", actualMbox)74 return75 }7677 if actualMbox != expectedMbox {78 t.Errorf("wrong local part, want %s, got %s", actualMbox, actualMbox)79 }80 }8182 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}9394func TestQuoteMbox(t *testing.T) {95 test := func(inputMbox, expectedMbox string) {96 t.Helper()9798 actualMbox := QuoteMbox(inputMbox)99 if actualMbox != expectedMbox {100 t.Errorf("wrong local part, want %s, got %s", actualMbox, actualMbox)101 }102 }103104 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}