-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
60 lines (47 loc) · 1.15 KB
/
main_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
package main_test
import (
"crypto/sha256"
"encoding/base64"
"regexp"
"strconv"
"testing"
main "github.com/fillmore-labs/pgpasswd"
)
const (
expr = `^([^$:]+)\$([0-9]+):([^$:]+)\$([^$:]+):([^$:]+)$`
)
const (
namePos int = iota + 1
iterPos
saltPos
storedKeyPos
serverKeyPos
)
func TestComputeAuth(t *testing.T) { //nolint:cyclop
t.Parallel()
r := regexp.MustCompile(expr)
dec := base64.StdEncoding.DecodeString
auth, err := main.ScramSHA256Auth("password")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
m := r.FindStringSubmatch(auth)
if len(m) != 6 {
t.Fatal("Wrong format")
}
if m[namePos] != main.ScramSHA256Name {
t.Error("Wrong identifier")
}
if iter, err := strconv.Atoi(m[iterPos]); err != nil || iter != main.IterCount {
t.Error("Unexpected iteration count")
}
if salt, err := dec(m[saltPos]); err != nil || len(salt) != 16 {
t.Error("Unexpected salt")
}
if storedKey, err := dec(m[storedKeyPos]); err != nil || len(storedKey) != sha256.Size {
t.Error("Unexpected stored key")
}
if serverKey, err := dec(m[serverKeyPos]); err != nil || len(serverKey) != sha256.Size {
t.Error("Unexpected server key")
}
}