2020-11-29 14:26:42 +01:00
|
|
|
package group
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-04-12 01:05:48 +02:00
|
|
|
var key1 = ""
|
2024-01-02 18:36:09 +01:00
|
|
|
var pw1 = Password{
|
|
|
|
Type: "plain",
|
2024-04-12 01:05:48 +02:00
|
|
|
Key: &key1,
|
2024-01-02 18:36:09 +01:00
|
|
|
}
|
2024-04-12 01:05:48 +02:00
|
|
|
var key2 = "pass"
|
2024-01-02 18:36:09 +01:00
|
|
|
var pw2 = Password{
|
|
|
|
Type: "plain",
|
2024-04-12 01:05:48 +02:00
|
|
|
Key: &key2,
|
2024-01-02 18:36:09 +01:00
|
|
|
}
|
2024-04-12 01:05:48 +02:00
|
|
|
var key3 = "fe499504e8f144693fae828e8e371d50e019d0e4c84994fa03f7f445bd8a570a"
|
2020-11-29 14:26:42 +01:00
|
|
|
var pw3 = Password{
|
|
|
|
Type: "pbkdf2",
|
|
|
|
Hash: "sha-256",
|
2024-04-12 01:05:48 +02:00
|
|
|
Key: &key3,
|
2020-11-29 14:26:42 +01:00
|
|
|
Salt: "bcc1717851030776",
|
|
|
|
Iterations: 4096,
|
|
|
|
}
|
2024-04-12 01:05:48 +02:00
|
|
|
var key4 = "$2a$10$afOr2f33onT/nDFFyT3mbOq5FMSw1wWXfyTXQTBMbKvZpBkoD3Qwu"
|
2020-11-29 14:26:42 +01:00
|
|
|
var pw4 = Password{
|
2023-11-23 13:57:37 +01:00
|
|
|
Type: "bcrypt",
|
2024-04-12 01:05:48 +02:00
|
|
|
Key: &key4,
|
2023-11-23 13:57:37 +01:00
|
|
|
}
|
2024-01-02 18:36:09 +01:00
|
|
|
var pw5 = Password{}
|
|
|
|
var pw6 = Password{
|
2020-11-29 14:26:42 +01:00
|
|
|
Type: "bad",
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGood(t *testing.T) {
|
2024-01-02 18:36:09 +01:00
|
|
|
if match, err := pw1.Match(""); err != nil || !match {
|
|
|
|
t.Errorf("pw2 doesn't match (%v)", err)
|
|
|
|
}
|
2020-11-29 14:26:42 +01:00
|
|
|
if match, err := pw2.Match("pass"); err != nil || !match {
|
|
|
|
t.Errorf("pw2 doesn't match (%v)", err)
|
|
|
|
}
|
|
|
|
if match, err := pw3.Match("pass"); err != nil || !match {
|
|
|
|
t.Errorf("pw3 doesn't match (%v)", err)
|
|
|
|
}
|
2023-11-23 13:57:37 +01:00
|
|
|
if match, err := pw4.Match("pass"); err != nil || !match {
|
|
|
|
t.Errorf("pw4 doesn't match (%v)", err)
|
|
|
|
}
|
2020-11-29 14:26:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBad(t *testing.T) {
|
|
|
|
if match, err := pw2.Match("bad"); err != nil || match {
|
|
|
|
t.Errorf("pw2 matches")
|
|
|
|
}
|
|
|
|
if match, err := pw3.Match("bad"); err != nil || match {
|
|
|
|
t.Errorf("pw3 matches")
|
|
|
|
}
|
2023-11-23 13:57:37 +01:00
|
|
|
if match, err := pw4.Match("bad"); err != nil || match {
|
|
|
|
t.Errorf("pw4 matches")
|
|
|
|
}
|
2024-01-02 18:36:09 +01:00
|
|
|
if match, err := pw5.Match(""); err == nil || match {
|
|
|
|
t.Errorf("pw5 matches")
|
|
|
|
}
|
2023-11-23 13:57:37 +01:00
|
|
|
if match, err := pw5.Match("bad"); err == nil || match {
|
2024-01-02 18:36:09 +01:00
|
|
|
t.Errorf("pw5 matches")
|
|
|
|
}
|
|
|
|
if match, err := pw6.Match("bad"); err == nil || match {
|
|
|
|
t.Errorf("pw6 matches")
|
2020-11-29 14:26:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 01:05:48 +02:00
|
|
|
func TestEmptyKey(t *testing.T) {
|
|
|
|
for _, tpe := range []string{"", "plain", "pbkdf2", "bcrypt", "bad"} {
|
|
|
|
pw := Password{Type: tpe}
|
|
|
|
if match, err := pw.Match(""); err == nil || match {
|
|
|
|
t.Errorf("empty password of type %v didn't error", tpe)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 14:26:42 +01:00
|
|
|
func TestJSON(t *testing.T) {
|
|
|
|
plain, err := json.Marshal(pw2)
|
|
|
|
if err != nil || string(plain) != `"pass"` {
|
|
|
|
t.Errorf("Expected \"pass\", got %v", string(plain))
|
|
|
|
}
|
|
|
|
|
2023-11-23 13:57:37 +01:00
|
|
|
for _, pw := range []Password{pw1, pw2, pw3, pw4, pw5} {
|
2020-11-29 14:26:42 +01:00
|
|
|
j, err := json.Marshal(pw)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Marshal: %v", err)
|
|
|
|
}
|
|
|
|
if testing.Verbose() {
|
|
|
|
log.Printf("%v", string(j))
|
|
|
|
}
|
|
|
|
var pw2 Password
|
|
|
|
err = json.Unmarshal(j, &pw2)
|
|
|
|
if err != nil {
|
2024-01-02 18:36:09 +01:00
|
|
|
t.Errorf("Unmarshal: %v", err)
|
2020-11-29 14:26:42 +01:00
|
|
|
} else if !reflect.DeepEqual(pw, pw2) {
|
|
|
|
t.Errorf("Expected %v, got %v", pw, pw2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPlain(b *testing.B) {
|
2020-12-19 17:38:47 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-11-29 14:26:42 +01:00
|
|
|
match, err := pw2.Match("bad")
|
|
|
|
if err != nil || match {
|
|
|
|
b.Errorf("pw2 matched")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPBKDF2(b *testing.B) {
|
2020-12-19 17:38:47 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-11-29 14:26:42 +01:00
|
|
|
match, err := pw3.Match("bad")
|
|
|
|
if err != nil || match {
|
|
|
|
b.Errorf("pw3 matched")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-23 13:57:37 +01:00
|
|
|
|
|
|
|
func BenchmarkBCrypt(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
match, err := pw4.Match("bad")
|
|
|
|
if err != nil || match {
|
|
|
|
b.Errorf("pw3 matched")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|