You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
4.2 KiB
111 lines
4.2 KiB
package wordkey
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The lists numbered for dice and their SHA-256, as lists/README.md records
|
|
// them: the English one is the file of the EFF, byte for byte.
|
|
var diceListHashes = map[string]string{
|
|
"en": "addd35536511597a02fa0a9ff1e5284677b8883b83e986e43f15a3db996b903e",
|
|
"es": "611f779a33df74587e9dfb486bb0185a9dfd4d1384e75993a21247ad31cefddb",
|
|
}
|
|
|
|
func TestDiceNumber(t *testing.T) {
|
|
for i, want := range map[int]string{0: "11111", 1: "11112", 5: "11116", 6: "11121", 35: "11166", 36: "11211", 1295: "16666", 1296: "21111", 7775: "66666"} {
|
|
if got, err := DiceNumber(i); err != nil || got != want {
|
|
t.Errorf("DiceNumber(%d) = %q, %v, want %q", i, got, err, want)
|
|
}
|
|
}
|
|
for _, i := range []int{-1, 7776} {
|
|
if _, err := DiceNumber(i); err == nil || err.Error() != fmt.Sprintf("wordkey: no dice give position %d of a list of 7776 words", i) {
|
|
t.Errorf("DiceNumber(%d): %v", i, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDiceWord(t *testing.T) {
|
|
en, _ := List("en")
|
|
es, _ := List("es")
|
|
// Every position, there and back.
|
|
for i := range en {
|
|
n, _ := DiceNumber(i)
|
|
if w, err := DiceWord(en, n); err != nil || w != en[i] {
|
|
t.Fatalf("DiceWord(%s) = %q, %v, want %q", n, w, err, en[i])
|
|
}
|
|
}
|
|
for _, c := range []struct {
|
|
list []string
|
|
dice, want string
|
|
}{
|
|
{en, "11111", "abacus"},
|
|
{en, "11112", "abdomen"},
|
|
{en, "35214", "jovial"},
|
|
{en, "66666", "zoom"},
|
|
{es, "11111", "abad"},
|
|
{es, "35214", "glaciar"},
|
|
{es, "66666", "útil"},
|
|
} {
|
|
if w, err := DiceWord(c.list, c.dice); err != nil || w != c.want {
|
|
t.Errorf("DiceWord(%s) = %q, %v, want %q", c.dice, w, err, c.want)
|
|
}
|
|
}
|
|
for _, dice := range []string{"", "1111", "111111", "11110", "11117", "a1111", "1111 ", string(rune(0xff11)) + "1111"} {
|
|
want := fmt.Sprintf("wordkey: %q is not five dice: five digits from 1 to 6", dice)
|
|
if _, err := DiceWord(en, dice); err == nil || err.Error() != want {
|
|
t.Errorf("DiceWord(%q): %v, want %q", dice, err, want)
|
|
}
|
|
}
|
|
if _, err := DiceWord(en[:2048], "11111"); err == nil || err.Error() != "wordkey: dice draw from a list of 7776 words, not 2048" {
|
|
t.Errorf("DiceWord of 2048 words: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDiceWords(t *testing.T) {
|
|
en, _ := List("en")
|
|
words, err := DiceWords(en, " 11111 11112\t11113\n11114 11115 11116 11121 ")
|
|
if err != nil || strings.Join(words, " ") != "abacus abdomen abdominal abide abiding ability ablaze" {
|
|
t.Fatalf("DiceWords = %q, %v", words, err)
|
|
}
|
|
if err := Check(Normalize(strings.Join(words, " "))); err != nil {
|
|
t.Errorf("the words of the dice: %v", err)
|
|
}
|
|
for _, c := range []struct{ dice, want string }{
|
|
{"11111 11112 11113 11114 11115", "wordkey: a key of words needs at least 6 words, not 5"},
|
|
{"", "wordkey: a key of words needs at least 6 words, not 0"},
|
|
{"11111 11112 11113 11114 11115 1116", `wordkey: "1116" is not five dice: five digits from 1 to 6`},
|
|
{"11111 11112 11113 11114 11115 11111", `wordkey: the dice 11111 give "abacus" a second time; roll them again`},
|
|
{"11111,11112 11113 11114 11115 11116 11121", `wordkey: "11111,11112" is not five dice: five digits from 1 to 6`},
|
|
} {
|
|
if _, err := DiceWords(en, c.dice); err == nil || err.Error() != c.want {
|
|
t.Errorf("DiceWords(%q): %v, want %q", c.dice, err, c.want)
|
|
}
|
|
}
|
|
if _, err := DiceWords(en[1:], "11111 11112 11113 11114 11115 11116"); err == nil || err.Error() != "wordkey: dice draw from a list of 7776 words, not 7775" {
|
|
t.Errorf("DiceWords of 7775 words: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDiceList(t *testing.T) {
|
|
for lang, want := range diceListHashes {
|
|
list, _ := List(lang)
|
|
text, err := DiceList(list)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := fmt.Sprintf("%x", sha256.Sum256([]byte(text))); got != want {
|
|
t.Errorf("DiceList(%s): SHA-256 %s, want %s", lang, got, want)
|
|
}
|
|
lines := strings.Split(strings.TrimSuffix(text, "\n"), "\n")
|
|
if len(lines) != DiceListSize || lines[0] != "11111\t"+list[0] || lines[7775] != "66666\t"+list[7775] {
|
|
t.Errorf("DiceList(%s): %d lines, %q … %q", lang, len(lines), lines[0], lines[len(lines)-1])
|
|
}
|
|
}
|
|
if _, err := DiceList(make([]string, 2048)); err == nil || err.Error() != "wordkey: dice draw from a list of 7776 words, not 2048" {
|
|
t.Errorf("DiceList of 2048 words: %v", err)
|
|
}
|
|
}
|