Skip to content

Commit c20a99a

Browse files
committed
Add test
1 parent 9c44be5 commit c20a99a

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

server/auth_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package server
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestErrAccessDenied(t *testing.T) {
11+
require.True(t, errors.Is(ErrAccessDenied, ErrAccessDenied))
12+
require.True(t, errors.Is(ErrAccessDeniedNoPassword, ErrAccessDenied))
13+
require.False(t, errors.Is(ErrAccessDenied, ErrAccessDeniedNoPassword))
14+
}
15+
16+
func TestCompareNativePasswordAuthData_EmptyPassword(t *testing.T) {
17+
tests := []struct {
18+
name string
19+
clientAuthData []byte
20+
serverPassword string
21+
wantErr error
22+
}{
23+
{
24+
name: "empty client auth, empty server password",
25+
clientAuthData: []byte{},
26+
serverPassword: "",
27+
wantErr: nil,
28+
},
29+
{
30+
name: "empty client auth, non-empty server password",
31+
clientAuthData: []byte{},
32+
serverPassword: "secret",
33+
wantErr: ErrAccessDeniedNoPassword,
34+
},
35+
}
36+
37+
for _, tt := range tests {
38+
t.Run(tt.name, func(t *testing.T) {
39+
c := &Conn{
40+
credential: Credential{Password: tt.serverPassword},
41+
}
42+
err := c.compareNativePasswordAuthData(tt.clientAuthData, c.credential)
43+
if tt.wantErr == nil {
44+
require.NoError(t, err)
45+
} else {
46+
require.ErrorIs(t, err, tt.wantErr)
47+
}
48+
})
49+
}
50+
}
51+
52+
func TestCompareSha256PasswordAuthData_EmptyPassword(t *testing.T) {
53+
tests := []struct {
54+
name string
55+
clientAuthData []byte
56+
serverPassword string
57+
wantErr error
58+
}{
59+
{
60+
name: "empty client auth, empty server password",
61+
clientAuthData: []byte{},
62+
serverPassword: "",
63+
wantErr: nil,
64+
},
65+
{
66+
name: "empty client auth, non-empty server password",
67+
clientAuthData: []byte{},
68+
serverPassword: "secret",
69+
wantErr: ErrAccessDeniedNoPassword,
70+
},
71+
}
72+
73+
for _, tt := range tests {
74+
t.Run(tt.name, func(t *testing.T) {
75+
c := &Conn{
76+
credential: Credential{Password: tt.serverPassword},
77+
}
78+
err := c.compareSha256PasswordAuthData(tt.clientAuthData, c.credential)
79+
if tt.wantErr == nil {
80+
require.NoError(t, err)
81+
} else {
82+
require.ErrorIs(t, err, tt.wantErr)
83+
}
84+
})
85+
}
86+
}
87+
88+
func TestCompareCacheSha2PasswordAuthData_EmptyPassword(t *testing.T) {
89+
tests := []struct {
90+
name string
91+
clientAuthData []byte
92+
serverPassword string
93+
wantErr error
94+
}{
95+
{
96+
name: "empty client auth, empty server password",
97+
clientAuthData: []byte{},
98+
serverPassword: "",
99+
wantErr: nil,
100+
},
101+
{
102+
name: "empty client auth, non-empty server password",
103+
clientAuthData: []byte{},
104+
serverPassword: "secret",
105+
wantErr: ErrAccessDeniedNoPassword,
106+
},
107+
}
108+
109+
for _, tt := range tests {
110+
t.Run(tt.name, func(t *testing.T) {
111+
c := &Conn{
112+
credential: Credential{Password: tt.serverPassword},
113+
}
114+
err := c.compareCacheSha2PasswordAuthData(tt.clientAuthData)
115+
if tt.wantErr == nil {
116+
require.NoError(t, err)
117+
} else {
118+
require.ErrorIs(t, err, tt.wantErr)
119+
}
120+
})
121+
}
122+
}

0 commit comments

Comments
 (0)