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 plain_separate
 20
 21import (
 22	"context"
 23	"errors"
 24	"testing"
 25
 26	"github.com/emersion/go-sasl"
 27	"github.com/foxcpp/maddy/framework/module"
 28)
 29
 30type mockAuth struct {
 31	db map[string]bool
 32}
 33
 34func (mockAuth) SASLMechanisms() []string {
 35	return []string{sasl.Plain, sasl.Login}
 36}
 37
 38func (m mockAuth) AuthPlain(username, _ string) error {
 39	ok := m.db[username]
 40	if !ok {
 41		return errors.New("invalid creds")
 42	}
 43	return nil
 44}
 45
 46type mockTable struct {
 47	db map[string]string
 48}
 49
 50func (m mockTable) Lookup(_ context.Context, a string) (string, bool, error) {
 51	b, ok := m.db[a]
 52	return b, ok, nil
 53}
 54
 55func 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	}
 65
 66	err := a.AuthPlain("user1", "aaa")
 67	if err != nil {
 68		t.Fatal("Unexpected error:", err)
 69	}
 70}
 71
 72func 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	}
 87
 88	err := a.AuthPlain("user1", "aaa")
 89	if err != nil {
 90		t.Fatal("Unexpected error:", err)
 91	}
 92}
 93
 94func 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	}
116
117	err := a.AuthPlain("user1", "aaa")
118	if err != nil {
119		t.Fatal("Unexpected error:", err)
120	}
121}
122
123func 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	}
150
151	err := a.AuthPlain("user1", "aaa")
152	if err != nil {
153		t.Fatal("Unexpected error:", err)
154	}
155}