searcherside/core/services/argon2id_hasher_test.go
Peter Kurfer 9ea9a8f658
Some checks failed
Go build / build (push) Failing after 1m58s
feat: continue basic setup
- setup ent scheme
- add command to create users
- document API
- add helpers to create migrations
- add command to run migrations
- add basic compose file
2024-06-19 21:19:37 +02:00

86 lines
1.8 KiB
Go

package services_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"code.icb4dc0.de/prskr/searcherside/core/services"
)
func TestArgon2IDHashAlgorithm_Hash(t *testing.T) {
type fields struct {
Argon2Params services.Argon2Params
}
type args struct {
password []byte
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "Basic test",
fields: fields{
Argon2Params: services.DefaultArgon2Params,
},
args: args{
password: []byte("hello_world1234!"),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &services.Argon2IDHashAlgorithm{
Argon2Params: tt.fields.Argon2Params,
}
got, err := a.Hash(tt.args.password)
if (err != nil) != tt.wantErr {
t.Errorf("Hash() error = %v, wantErr %v", err, tt.wantErr)
return
}
t.Log(string(got))
var pwh services.PasswordHash
assert.NoError(t, pwh.UnmarshalText(got))
assert.NotEmpty(t, pwh.Hash)
assert.NotEmpty(t, pwh.Salt)
assert.NotZero(t, pwh.Params)
})
}
}
func TestArgon2IDHashAlgorithm_Validate(t *testing.T) {
type args struct {
password []byte
hash []byte
}
tests := []struct {
name string
args args
wantErr assert.ErrorAssertionFunc
}{
{
name: "Happy path",
args: args{
hash: []byte("$argon2id$v=19$m=65536,t=3,p=2$Huar9oxLB/FrNHbY2EldpA$NfSo9dk+YEJ8AewO6y1GHoNyW8HBXo4CDdwQNXmeq4g"),
password: []byte("hello_world1234!"),
},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return assert.NoError(t, err, i)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var a services.Argon2IDHashAlgorithm
tt.wantErr(t, a.Validate(tt.args.password, tt.args.hash), fmt.Sprintf("Validate(%v, %v)", tt.args.password, tt.args.hash))
})
}
}