forked from CrunchyData/postgres-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscram_test.go
More file actions
209 lines (176 loc) · 6.06 KB
/
Copy pathscram_test.go
File metadata and controls
209 lines (176 loc) · 6.06 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2021 - 2026 Crunchy Data Solutions, Inc.
//
// SPDX-License-Identifier: Apache-2.0
package password
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"testing"
)
func TestNewSCRAMPassword(t *testing.T) {
password := "datalake"
scram := NewSCRAMPassword(password)
if scram.password != password {
t.Errorf("plaintext password not set properly. expected %q actual %q", password, scram.password)
return
}
if scram.Iterations != scramDefaultIterations {
t.Errorf("iterations not set properly. expected %d actual %d", scramDefaultIterations, scram.Iterations)
return
}
if scram.SaltLength != scramDefaultSaltLength {
t.Errorf("salt length not set properly. expected %d actual %d", scramDefaultSaltLength, scram.SaltLength)
return
}
}
func TestScramGenerateSalt(t *testing.T) {
t.Run("valid", func(t *testing.T) {
saltLengths := []int{
scramDefaultSaltLength,
scramDefaultSaltLength * 2,
}
for _, saltLength := range saltLengths {
t.Run(fmt.Sprintf("salt length %d", saltLength), func(t *testing.T) {
salt, err := scramGenerateSalt(saltLength)
if err != nil {
t.Error(err)
}
if len(salt) != saltLength {
t.Errorf("expected: %d actual: %d", saltLength, len(salt))
}
})
}
})
t.Run("invalid", func(t *testing.T) {
saltLengths := []int{0, -1}
for _, saltLength := range saltLengths {
t.Run(fmt.Sprintf("salt length %d", saltLength), func(t *testing.T) {
if _, err := scramGenerateSalt(saltLength); err == nil {
t.Errorf("error expected for salt length of %d", saltLength)
}
})
}
})
}
func TestSCRAMBuild(t *testing.T) {
t.Run("scram-sha-256", func(t *testing.T) {
t.Run("valid", func(t *testing.T) {
// check a few different password combinations. note: the salt is kept the
// same so we can get a reproducible result
credentialList := []([]string){
[]string{`datalake`, `SCRAM-SHA-256$4096:aDFwcDBwNHJ0eTIwMjA=$xHkOo65LX9eBB8a6v+axqvs3+aMBTH0sCT7w/Nxzh5M=:PXuFoeJNuAGSeExskYSqkwUyiUJu8LPC9DgwDWQ9ARQ=`},
[]string{`øásis`, `SCRAM-SHA-256$4096:aDFwcDBwNHJ0eTIwMjA=$ySGUcYGGJXsigb0a24AfSqNRpGM+zqwlkfuzdlWCV9k=:GDITAfQzF7M9aJaP5OK04b6bT+XQ+wjU3qiGC2ERxeA=`},
[]string{`md53a0689aa9e31a50b5621971fc89f0c64`, `SCRAM-SHA-256$4096:aDFwcDBwNHJ0eTIwMjA=$R93U562i0T1ewqfMD3JhD/eTnvTsVBDq1wzkBWx0+WU=:p+dt112MXgpsvAshbNU6jTSMegApKRzb9VT18yiQ/HY=`},
[]string{`SCRAM-SHA-256$4096:aDFwcDBwNHJ0eTIwMjA=$xHkOo65LX9eBB8a6v+axqvs3+aMBTH0sCT7w/Nxzh5M=:PXuFoeJNuAGSeExskYSqkwUyiUJu8LPC9DgwDWQ9ARQ=`, `SCRAM-SHA-256$4096:aDFwcDBwNHJ0eTIwMjA=$s9HbNQBsfJwflGr4lvr4vEt/vvspp5Uu8IjWYLjMUMg=:3sUGJgo/70EQvjsma2I/RJsheqLhxN2rarUt7oqK6q8=`},
}
mockGenerateSalt := func(length int) ([]byte, error) {
// return the special salt
return []byte("h1pp0p4rty2020"), nil
}
// a credential is valid if it generates the specified md5 hash
for _, credentials := range credentialList {
t.Run(credentials[0], func(t *testing.T) {
scram := NewSCRAMPassword(credentials[0])
scram.generateSalt = mockGenerateSalt
hash, err := scram.Build()
if err != nil {
t.Error(err)
}
if hash != credentials[1] {
t.Errorf("expected: %q actual %q", credentials[1], hash)
}
})
}
})
t.Run("invalid", func(t *testing.T) {
// ensure the generate salt function returns an error
mockGenerateSalt := func(length int) ([]byte, error) {
return []byte{}, ErrSCRAMSaltLengthInvalid
}
t.Run("invalid salt generator value", func(t *testing.T) {
scram := NewSCRAMPassword("datalake")
scram.generateSalt = mockGenerateSalt
if _, err := scram.Build(); err == nil {
t.Errorf("error expected with invalid value to salt generator")
}
})
})
})
}
func TestSCRAMEncode(t *testing.T) {
t.Run("valid", func(t *testing.T) {
scram := SCRAMPassword{}
expected := "aGlwcG8="
actual := scram.encode([]byte("hippo"))
if expected != actual {
t.Errorf("expected: %s actual %s", expected, actual)
}
})
}
func TestSCRAMHash(t *testing.T) {
t.Run("valid", func(t *testing.T) {
scram := SCRAMPassword{}
expected, _ := hex.DecodeString("877cc977e7b033e10d6e0b0d666da1f463bc51b1de48869250a0347ec1b2b8b3")
actual := scram.hash(sha256.New, []byte("hippo"))
if !bytes.Equal(expected, actual) {
t.Errorf("expected: %x actual %x", expected, actual)
}
})
}
func TestSCRAMHMAC(t *testing.T) {
t.Run("valid", func(t *testing.T) {
scram := SCRAMPassword{}
expected, _ := hex.DecodeString("ac9872eb21043142c3bf073c9fa4caf9553940750ef7b85116905aaa456a2d07")
actual := scram.hmac(sha256.New, []byte("hippo"), []byte("datalake"))
if !bytes.Equal(expected, actual) {
t.Errorf("expected: %x actual %x", expected, actual)
}
})
}
func TestSCRAMIsASCII(t *testing.T) {
type stringStruct struct {
str string
isASCII bool
}
tests := []stringStruct{
{str: "hippo", isASCII: true},
{str: "híppo", isASCII: false},
{str: "こんにちは", isASCII: false},
}
for _, test := range tests {
t.Run(fmt.Sprintf("is ascii %q", test.str), func(t *testing.T) {
scram := SCRAMPassword{password: test.str}
if scram.isASCII() != test.isASCII {
t.Errorf("%q should be %t", test.str, test.isASCII)
}
})
}
}
func TestSCRAMSASLPrep(t *testing.T) {
type stringStruct struct {
password string
expected string
}
// some of the testing methodology for this is borrowed from:
//
// https://github.com/MagicStack/asyncpg/blob/master/tests/test_connect.py#L276-L287
tests := []stringStruct{
{password: "hippo", expected: "hippo"},
{password: "híppo", expected: "híppo"},
//nolint:gosec // G101: Test data for password normalization, not a credential.
{password: "こんにちは", expected: "こんにちは"},
{password: "hippo\u1680lake", expected: "hippo lake"},
{password: "hipp\ufe01o", expected: "hippo"},
{password: "hipp\u206ao", expected: "hipp\u206ao"},
}
for _, test := range tests {
t.Run(fmt.Sprintf("saslprep %q", test.password), func(t *testing.T) {
scram := SCRAMPassword{password: test.password}
if scram.saslPrep() != test.expected {
t.Errorf("%q should be %q", test.password, test.expected)
}
})
}
}