@@ -2,6 +2,7 @@ package sasl_test
22
33import (
44 "bytes"
5+ "errors"
56 "testing"
67
78 "github.com/emersion/go-sasl"
@@ -34,3 +35,95 @@ func TestNewLoginClient(t *testing.T) {
3435 t .Error ("Invalid initial response:" , resp )
3536 }
3637}
38+
39+ func TestNewLoginServer (t * testing.T ) {
40+ var authenticated = false
41+ s := sasl .NewLoginServer (func (username , password string ) error {
42+ if username != "tim" {
43+ return errors .New ("Invalid username: " + username )
44+ }
45+ if password != "tanstaaftanstaaf" {
46+ return errors .New ("Invalid password: " + password )
47+ }
48+
49+ authenticated = true
50+ return nil
51+ })
52+
53+ challenge , done , err := s .Next (nil )
54+ if err != nil {
55+ t .Fatal ("Error while starting server:" , err )
56+ }
57+ if done {
58+ t .Fatal ("Done after starting server" )
59+ }
60+ if string (challenge ) != "Username:" {
61+ t .Error ("Invalid first challenge:" , challenge )
62+ }
63+
64+ challenge , done , err = s .Next ([]byte ("tim" ))
65+ if err != nil {
66+ t .Fatal ("Error while sending username:" , err )
67+ }
68+ if done {
69+ t .Fatal ("Done after sending username" )
70+ }
71+ if string (challenge ) != "Password:" {
72+ t .Error ("Invalid challenge after sending username:" , challenge )
73+ }
74+
75+ challenge , done , err = s .Next ([]byte ("tanstaaftanstaaf" ))
76+ if err != nil {
77+ t .Fatal ("Error while sending password:" , err )
78+ }
79+ if ! done {
80+ t .Fatal ("Authentication not finished after sending password" )
81+ }
82+ if len (challenge ) > 0 {
83+ t .Error ("Invalid non-empty final challenge:" , challenge )
84+ }
85+
86+ if ! authenticated {
87+ t .Error ("Not authenticated" )
88+ }
89+
90+ // Tests with initial response field, as per RFC4422 section 3
91+ authenticated = false
92+ s = sasl .NewLoginServer (func (username , password string ) error {
93+ if username != "tim" {
94+ return errors .New ("Invalid username: " + username )
95+ }
96+ if password != "tanstaaftanstaaf" {
97+ return errors .New ("Invalid password: " + password )
98+ }
99+
100+ authenticated = true
101+ return nil
102+ })
103+
104+ challenge , done , err = s .Next ([]byte ("tim" ))
105+ if err != nil {
106+ t .Fatal ("Error while sending username:" , err )
107+ }
108+ if done {
109+ t .Fatal ("Done after sending username" )
110+ }
111+ if string (challenge ) != "Password:" {
112+ t .Error ("Invalid challenge after sending username:" , string (challenge ))
113+ }
114+
115+ challenge , done , err = s .Next ([]byte ("tanstaaftanstaaf" ))
116+ if err != nil {
117+ t .Fatal ("Error while sending password:" , err )
118+ }
119+ if ! done {
120+ t .Fatal ("Authentication not finished after sending password" )
121+ }
122+ if len (challenge ) > 0 {
123+ t .Error ("Invalid non-empty final challenge:" , challenge )
124+ }
125+
126+ if ! authenticated {
127+ t .Error ("Not authenticated" )
128+ }
129+ }
0 commit comments