-
Notifications
You must be signed in to change notification settings - Fork 0
/
passwd_test.go
93 lines (79 loc) · 2.29 KB
/
passwd_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
package passwd_test
import (
"encoding/json"
"github.com/pkg-id/passwd"
passwdBcrypt "github.com/pkg-id/passwd/bcrypt"
"golang.org/x/crypto/bcrypt"
"strings"
"testing"
)
func TestPassword_Value(t *testing.T) {
passwd.SetHashComparer(passwdBcrypt.DefaultCost)
pwd := passwd.Password("pass1234")
value, err := pwd.Value()
if err != nil {
t.Errorf("value: expected no error, but got an error: %v", err)
}
bytes := []byte(value.(string))
err = bcrypt.CompareHashAndPassword(bytes, []byte("pass1234"))
if err != nil {
t.Errorf("compare: expected no error, but got an error: %v", err)
}
err = bcrypt.CompareHashAndPassword(bytes, []byte("pass1235"))
if err == nil {
t.Errorf("compare: expected an error")
}
}
func TestPassword_Scan(t *testing.T) {
pwd := passwd.Password("pass1234")
value, err := pwd.Value()
if err != nil {
t.Errorf("value: expected no error, but got an error: %v", err)
}
src := []byte(value.(string))
var scanned passwd.Password
if err := scanned.Scan(src); err != nil {
t.Errorf("scan: expected no error, but got an error: %v", err)
}
}
func TestPassword_Scan_WhenNil(t *testing.T) {
var scanned passwd.Password
if err := scanned.Scan(nil); err != nil {
t.Errorf("scan: expected no error, but got an error: %v", err)
}
if scanned != "" {
t.Errorf("scan: expect empty string when given src nil")
}
}
func TestPassword_Scan_WhenTypeUnsupported(t *testing.T) {
var scanned passwd.Password
if err := scanned.Scan(1234); err == nil {
t.Fatalf("scan: expect an error")
}
}
func TestPassword_Compare(t *testing.T) {
pwd := passwd.Password("pass1234")
value, err := pwd.Value()
if err != nil {
t.Errorf("value: expected no error, but got an error: %v", err)
}
src := value.(string)
var scanned passwd.Password
if err := scanned.Scan(src); err != nil {
t.Errorf("scan: expected no error, but got an error: %v", err)
}
if err := scanned.Compare("pass1234"); err != nil {
t.Errorf("compare sanned: expected no error, but got an error: %v", err)
}
}
func TestPassword_MarshalJSON(t *testing.T) {
pwd := passwd.Password("pass1234")
b, err := json.Marshal(pwd)
if err != nil {
t.Errorf("marshal: expected no error, but got an error: %v", err)
}
raw := strings.TrimSpace(string(b))
if raw != "\"FILTERED\"" {
t.Errorf("expect FILTERED, but got %s!", raw)
}
}