forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plain_test.go
103 lines (84 loc) · 2.65 KB
/
plain_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/
package auth
import (
"bytes"
"context"
"encoding/base64"
"testing"
"github.com/ortuman/jackal/model"
memorystorage "github.com/ortuman/jackal/storage/memory"
"github.com/ortuman/jackal/xmpp"
"github.com/stretchr/testify/require"
)
func TestAuthPlainAuthentication(t *testing.T) {
var err error
testStm, s := authTestSetup(&model.User{Username: "mariana", Password: "1234"})
authr := NewPlain(testStm, s)
require.Equal(t, authr.Mechanism(), "PLAIN")
require.False(t, authr.UsesChannelBinding())
elem := xmpp.NewElementNamespace("auth", "urn:ietf:params:xml:ns:xmpp-sasl")
elem.SetAttribute("mechanism", "PLAIN")
_ = authr.ProcessElement(context.Background(), elem)
buf := new(bytes.Buffer)
buf.WriteByte(0)
buf.WriteString("mariana")
buf.WriteByte(0)
buf.WriteString("1234")
elem.SetText(base64.StdEncoding.EncodeToString(buf.Bytes()))
// storage error...
memorystorage.EnableMockedError()
require.Equal(t, authr.ProcessElement(context.Background(), elem), memorystorage.ErrMocked)
memorystorage.DisableMockedError()
// valid credentials...
err = authr.ProcessElement(context.Background(), elem)
require.Nil(t, err)
require.Equal(t, "mariana", authr.Username())
require.True(t, authr.Authenticated())
// already authenticated...
err = authr.ProcessElement(context.Background(), elem)
require.Nil(t, err)
// malformed request
authr.Reset()
elem.SetText("")
err = authr.ProcessElement(context.Background(), elem)
require.Equal(t, ErrSASLMalformedRequest, err)
// invalid payload
authr.Reset()
elem.SetText("bad formed base64")
err = authr.ProcessElement(context.Background(), elem)
require.Equal(t, ErrSASLIncorrectEncoding, err)
// invalid payload
buf.Reset()
buf.WriteByte(0)
buf.WriteString("mariana")
buf.WriteByte(0)
buf.WriteString("1234")
buf.WriteByte(0)
elem.SetText(base64.StdEncoding.EncodeToString(buf.Bytes()))
authr.Reset()
err = authr.ProcessElement(context.Background(), elem)
require.Equal(t, ErrSASLIncorrectEncoding, err)
// invalid user
buf.Reset()
buf.WriteByte(0)
buf.WriteString("ortuman")
buf.WriteByte(0)
buf.WriteString("1234")
elem.SetText(base64.StdEncoding.EncodeToString(buf.Bytes()))
authr.Reset()
err = authr.ProcessElement(context.Background(), elem)
require.Equal(t, ErrSASLNotAuthorized, err)
// incorrect password
buf.Reset()
buf.WriteByte(0)
buf.WriteString("mariana")
buf.WriteByte(0)
buf.WriteString("12345")
elem.SetText(base64.StdEncoding.EncodeToString(buf.Bytes()))
authr.Reset()
err = authr.ProcessElement(context.Background(), elem)
require.Equal(t, ErrSASLNotAuthorized, err)
}