~evanj/security

ref: 9b9bc6682997dd34024d4ffa662bc3c83087fc1d security/security_test.go -rw-r--r-- 1.8 KiB
9b9bc668Evan M Jones Feat(go.mod): Move from git.evanjon.es to git.sr.ht/~evanj 1 year, 10 months ago
                                                                                
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
package security_test

import (
	"testing"

	"git.sr.ht/~evanj/security"
	"github.com/Pallinder/go-randomdata"
	"gopkg.in/go-playground/assert.v1"
)

var sec = security.Default("SUPER_SECRET_VALUE")
var someSalt = randomdata.SillyName()

func TestCreateAndComparePassword(t *testing.T) {
	t.Parallel()
	pass := randomdata.SillyName()
	hash, err := sec.HashCreate(someSalt, pass)
	assert.Equal(t, err, nil)
	err = sec.HashCompare(someSalt, pass, hash)
	assert.Equal(t, err, nil)
}

func TestHashBadInput(t *testing.T) {
	t.Parallel()
	hash, err := sec.HashCreate("", "")
	assert.Equal(t, err, nil)
	err = sec.HashCompare("", "", hash)
	assert.Equal(t, err, nil)
}

func TestCreateAndComparePasswordBad(t *testing.T) {
	t.Parallel()
	pass := randomdata.SillyName()
	tocompare := randomdata.SillyName()
	hash, err := sec.HashCreate(someSalt, pass)
	assert.Equal(t, err, nil)
	err = sec.HashCompare(someSalt, tocompare, hash)
	assert.NotEqual(t, err, nil)
}

func TestHashSalt(t *testing.T) {
	t.Parallel()
	pass := randomdata.SillyName()
	otherSalt := randomdata.SillyName()
	hash, err := sec.HashCreate(someSalt, pass)
	assert.Equal(t, err, nil)
	err = sec.HashCompare(someSalt, pass, hash)
	assert.Equal(t, err, nil)
	err = sec.HashCompare(otherSalt, pass, hash)
	assert.NotEqual(t, err, nil)
}

func TestToken(t *testing.T) {
	t.Parallel()
	type User struct{ Name string }
	user := User{randomdata.SillyName()}
	token, err := sec.TokenCreate(security.TokenMap{"Name": user.Name})
	assert.Equal(t, err, nil)
	val, err := sec.TokenFrom(token)
	assert.Equal(t, err, nil)
	name, ok := val["Name"].(string)
	assert.Equal(t, ok, true)
	user2 := User{name}
	assert.Equal(t, user, user2)
}

func TestTokenDontPanic(t *testing.T) {
	t.Parallel()
	_, err := sec.TokenFrom("")
	assert.NotEqual(t, err, nil)
}