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 plain_separate2021import (22 "context"23 "errors"24 "testing"2526 "github.com/emersion/go-sasl"27 "github.com/foxcpp/maddy/framework/module"28)2930type mockAuth struct {31 db map[string]bool32}3334func (mockAuth) SASLMechanisms() []string {35 return []string{sasl.Plain, sasl.Login}36}3738func (m mockAuth) AuthPlain(username, _ string) error {39 ok := m.db[username]40 if !ok {41 return errors.New("invalid creds")42 }43 return nil44}4546type mockTable struct {47 db map[string]string48}4950func (m mockTable) Lookup(_ context.Context, a string) (string, bool, error) {51 b, ok := m.db[a]52 return b, ok, nil53}5455func TestPlainSplit_NoUser(t *testing.T) {56 a := Auth{57 passwd: []module.PlainAuth{58 mockAuth{59 db: map[string]bool{60 "user1": true,61 },62 },63 },64 }6566 err := a.AuthPlain("user1", "aaa")67 if err != nil {68 t.Fatal("Unexpected error:", err)69 }70}7172func TestPlainSplit_NoUser_MultiPass(t *testing.T) {73 a := Auth{74 passwd: []module.PlainAuth{75 mockAuth{76 db: map[string]bool{77 "user2": true,78 },79 },80 mockAuth{81 db: map[string]bool{82 "user1": true,83 },84 },85 },86 }8788 err := a.AuthPlain("user1", "aaa")89 if err != nil {90 t.Fatal("Unexpected error:", err)91 }92}9394func TestPlainSplit_UserPass(t *testing.T) {95 a := Auth{96 userTbls: []module.Table{97 mockTable{98 db: map[string]string{99 "user1": "",100 },101 },102 },103 passwd: []module.PlainAuth{104 mockAuth{105 db: map[string]bool{106 "user2": true,107 },108 },109 mockAuth{110 db: map[string]bool{111 "user1": true,112 },113 },114 },115 }116117 err := a.AuthPlain("user1", "aaa")118 if err != nil {119 t.Fatal("Unexpected error:", err)120 }121}122123func TestPlainSplit_MultiUser_Pass(t *testing.T) {124 a := Auth{125 userTbls: []module.Table{126 mockTable{127 db: map[string]string{128 "userWH": "",129 },130 },131 mockTable{132 db: map[string]string{133 "user1": "",134 },135 },136 },137 passwd: []module.PlainAuth{138 mockAuth{139 db: map[string]bool{140 "user2": true,141 },142 },143 mockAuth{144 db: map[string]bool{145 "user1": true,146 },147 },148 },149 }150151 err := a.AuthPlain("user1", "aaa")152 if err != nil {153 t.Fatal("Unexpected error:", err)154 }155}