2022-09-10 19:23:48 +00:00
|
|
|
package auth
|
2022-09-08 18:33:57 +00:00
|
|
|
|
|
|
|
import (
|
2022-12-31 06:34:03 +00:00
|
|
|
"encoding/base64"
|
2022-09-10 19:23:48 +00:00
|
|
|
"golang.org/x/crypto/sha3"
|
|
|
|
"os"
|
2022-09-08 18:33:57 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuthPasswordHashBad(t *testing.T) {
|
2022-09-10 19:23:48 +00:00
|
|
|
os.RemoveAll("test-auth-token")
|
|
|
|
os.Setenv("KEYBOARD_AUTH_TOKEN_FILE", "test-auth-token")
|
|
|
|
os.WriteFile("test-auth-token", []byte("junk"), 0644)
|
2022-09-08 18:33:57 +00:00
|
|
|
t.Log("TestAuthPasswordHash")
|
2022-09-10 19:23:48 +00:00
|
|
|
|
2022-09-08 18:33:57 +00:00
|
|
|
password := "wrong password"
|
|
|
|
|
2022-12-31 06:34:03 +00:00
|
|
|
result := CheckAuthToken(password)
|
2022-09-10 19:23:48 +00:00
|
|
|
if result == nil {
|
|
|
|
t.Errorf("Expected error, got nil")
|
|
|
|
}
|
|
|
|
os.RemoveAll("test-auth-token")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthPasswordEmpty(t *testing.T) {
|
|
|
|
os.RemoveAll("test-auth-token")
|
|
|
|
os.Setenv("KEYBOARD_AUTH_TOKEN_FILE", "test-auth-token")
|
|
|
|
os.WriteFile("test-auth-token", []byte("c0067d4af4e87f00dbac63b6156828237059172d1bbeac67427345d6a9fda484"), 0644)
|
|
|
|
t.Log("TestAuthPasswordHash")
|
|
|
|
|
|
|
|
password := ""
|
|
|
|
|
2022-12-31 06:34:03 +00:00
|
|
|
result := CheckAuthToken(password)
|
2022-09-10 19:23:48 +00:00
|
|
|
if result == nil {
|
|
|
|
t.Errorf("Expected error, got nil")
|
|
|
|
}
|
|
|
|
os.RemoveAll("test-auth-token")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthPasswordHashGood(t *testing.T) {
|
|
|
|
os.RemoveAll("test-auth-token")
|
|
|
|
os.Setenv("KEYBOARD_AUTH_TOKEN_FILE", "test-auth-token")
|
|
|
|
//os.WriteFile("test-auth-token", []byte("c0067d4af4e87f00dbac63b6156828237059172d1bbeac67427345d6a9fda484"), 0644)
|
|
|
|
expectedHash := sha3.Sum256([]byte("password"))
|
|
|
|
fo, err := os.Create("test-auth-token")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fo.Write(expectedHash[:])
|
|
|
|
t.Log("TestAuthPasswordHash")
|
|
|
|
|
2022-12-31 06:34:03 +00:00
|
|
|
password := base64.StdEncoding.EncodeToString([]byte("password"))
|
2022-09-10 19:23:48 +00:00
|
|
|
|
2022-12-31 06:34:03 +00:00
|
|
|
result := CheckAuthToken(password)
|
2022-09-10 19:23:48 +00:00
|
|
|
if result != nil {
|
|
|
|
t.Errorf("Expected nil, got error")
|
2022-09-08 18:33:57 +00:00
|
|
|
}
|
2022-09-10 19:23:48 +00:00
|
|
|
os.RemoveAll("test-auth-token")
|
2022-09-08 18:33:57 +00:00
|
|
|
}
|