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 auth2021import (22 "errors"23 "net"24 "testing"2526 "github.com/foxcpp/maddy/framework/module"27 "github.com/foxcpp/maddy/internal/testutils"28)2930type mockAuth struct {31 db map[string]bool32}3334func (m mockAuth) AuthPlain(username, _ string) error {35 ok := m.db[username]36 if !ok {37 return errors.New("invalid creds")38 }39 return nil40}4142func TestCreateSASL(t *testing.T) {43 a := SASLAuth{44 Log: testutils.Logger(t, "saslauth"),45 Plain: []module.PlainAuth{46 &mockAuth{47 db: map[string]bool{48 "user1": true,49 },50 },51 },52 }5354 t.Run("XWHATEVER", func(t *testing.T) {55 srv := a.CreateSASL("XWHATEVER", &net.TCPAddr{}, func(string, ContextData) error { return nil })56 _, _, err := srv.Next([]byte(""))57 if err == nil {58 t.Error("No error for XWHATEVER use")59 }60 })6162 t.Run("PLAIN", func(t *testing.T) {63 srv := a.CreateSASL("PLAIN", &net.TCPAddr{}, func(id string, data ContextData) error {64 if id != "user1" {65 t.Fatal("Wrong auth. identities passed to callback:", id)66 }67 return nil68 })6970 _, _, err := srv.Next([]byte("\x00user1\x00aa"))71 if err != nil {72 t.Error("Unexpected error:", err)73 }74 })7576 t.Run("PLAIN with authorization identity", func(t *testing.T) {77 srv := a.CreateSASL("PLAIN", &net.TCPAddr{}, func(id string, data ContextData) error {78 if id != "user1" {79 t.Fatal("Wrong authorization identity passed:", id)80 }81 return nil82 })8384 _, _, err := srv.Next([]byte("user1\x00user1\x00aa"))85 if err != nil {86 t.Error("Unexpected error:", err)87 }88 })89}