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 auth
20
21import (
22	"errors"
23	"net"
24	"testing"
25
26	"github.com/foxcpp/maddy/framework/module"
27	"github.com/foxcpp/maddy/internal/testutils"
28)
29
30type mockAuth struct {
31	db map[string]bool
32}
33
34func (m mockAuth) AuthPlain(username, _ string) error {
35	ok := m.db[username]
36	if !ok {
37		return errors.New("invalid creds")
38	}
39	return nil
40}
41
42func 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	}
53
54	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	})
61
62	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 nil
68		})
69
70		_, _, err := srv.Next([]byte("\x00user1\x00aa"))
71		if err != nil {
72			t.Error("Unexpected error:", err)
73		}
74	})
75
76	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 nil
82		})
83
84		_, _, err := srv.Next([]byte("user1\x00user1\x00aa"))
85		if err != nil {
86			t.Error("Unexpected error:", err)
87		}
88	})
89}