Files
skald/skaldctl/skaldctl_test.go
2026-07-09 11:15:35 -04:00

124 lines
2.8 KiB
Go

package main
import (
"bufio"
"bytes"
"encoding/json"
"strings"
"testing"
"git.stormux.org/storm/skald/hall"
)
func TestMakePassword(t *testing.T) {
doit := func(pw hall.Password) {
ok, _ := pw.Match("secret")
if !ok {
t.Errorf("%v didn't match", pw)
}
ok, _ = pw.Match("notsecret")
if ok {
t.Errorf("%v did match", pw)
}
}
pw, err := makePassword("secret", "pbkdf2", 4096, 32, 8, 0)
if err != nil {
t.Errorf("PBKDF2: %v", err)
}
doit(pw)
pw, err = makePassword("secret", "bcrypt", 0, 0, 0, 10)
if err != nil {
t.Errorf("bcrypt: %v", err)
}
doit(pw)
pw, err = makePassword("", "wildcard", 0, 0, 0, 0)
if err != nil {
t.Errorf("Wildcard: %v", err)
}
ok, _ := pw.Match("notsecretatall")
if !ok {
t.Errorf("Wildcard didn't match")
}
}
func TestInteractiveHall(t *testing.T) {
input := strings.NewReader(strings.Join([]string{
"city-watch",
"City Watch",
"An audio hall",
"yes",
"",
"",
"no",
"no",
"vetinari",
"",
"yes",
"",
}, "\n"))
passwords := []string{"secret", "secret", "guest", "guest"}
readPassword := func(string) (string, error) {
password := passwords[0]
passwords = passwords[1:]
return password, nil
}
var output bytes.Buffer
name, desc, err := interactiveHall(
bufio.NewReader(input), &output, readPassword, "",
)
if err != nil {
t.Fatalf("interactiveHall: %v", err)
}
if name != "city-watch" {
t.Fatalf("name = %q", name)
}
if desc.DisplayName != "City Watch" || desc.Description != "An audio hall" {
t.Fatalf("description = %#v", desc)
}
if !desc.Public || !desc.AllowRecording || !desc.Autolock {
t.Fatalf("common settings = %#v", desc)
}
user := desc.Users["vetinari"]
if ok, err := user.Password.Match("secret"); !ok || err != nil {
t.Fatalf("operator password match = %v, %v", ok, err)
}
if user.Permissions.String() != "op" {
t.Fatalf("operator permissions = %v", user.Permissions)
}
if desc.WildcardUser == nil {
t.Fatal("wildcard user is nil")
}
if ok, err := desc.WildcardUser.Password.Match("guest"); !ok || err != nil {
t.Fatalf("wildcard password match = %v, %v", ok, err)
}
}
func TestFormatPermissions(t *testing.T) {
tests := []struct{ j, v, p string }{
{`"op"`, "op", "[mopt]"},
{`"present"`, "present", "[mp]"},
{`"observe"`, "observe", "[]"},
{`"admin"`, "admin", "[a]"},
{`["message", "present", "token"]`, "[mpt]", "[mpt]"},
{`[]`, "[]", "[]"},
}
for _, test := range tests {
var p hall.Permissions
err := json.Unmarshal([]byte(test.j), &p)
if err != nil {
t.Errorf("Unmarshal %#v: %v", test.j, err)
continue
}
v := formatPermissions(p)
if v != test.v {
t.Errorf("Expected %v, got %v", test.v, v)
}
pp := formatRawPermissions(p.Permissions(nil))
if pp != test.p {
t.Errorf("Expected %v, got %v", test.p, pp)
}
}
}