-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnemonic_test.go
95 lines (82 loc) · 1.88 KB
/
mnemonic_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package gorfc1751
import (
"errors"
"fmt"
"math/rand"
"testing"
)
type testReader struct{}
func (p *testReader) Read(b []byte) (int, error) {
r := rand.New(rand.NewSource(1)) // nolint: gosec
return r.Read(b)
}
type testReaderWithError struct{}
func (p *testReaderWithError) Read(_ []byte) (int, error) {
return 0, errors.New("some error") // nolint: err113
}
func ExampleNewMnemonic() {
r := rand.New(rand.NewSource(1)) // nolint: gosec
fmt.Println(NewMnemonic(r, 128))
// Output: BARK TROD AMY UP LUG KNOB GAS WHEN NEWT POT KEY MEAN
}
func TestMnemonicPanics(t *testing.T) {
t.Parallel()
testPanicMnemonicBitSize(t, 32)
testPanicMnemonicBitSize(t, 65)
testPanicMnemonicBitSize(t, 224)
testPanicMnemonicReadError(t)
}
func testPanicMnemonicBitSize(t *testing.T, bitSize uint64) {
defer func() {
if r := recover(); r == nil {
t.Error("nothing panics")
return
}
}()
_ = NewMnemonic(&testReader{}, bitSize)
}
func testPanicMnemonicReadError(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("nothing panics")
return
}
}()
_ = NewMnemonic(&testReaderWithError{}, 64)
}
func TestNewMnemonic(t *testing.T) {
t.Parallel()
tests := []struct {
name string
have uint64
want string
}{
{
name: "test-1",
have: 64,
want: "BARK TROD AMY UP LUG KNOB",
},
{
name: "test-2",
have: 128,
want: "BARK TROD AMY UP LUG KNOB GAS WHEN NEWT POT KEY MEAN",
},
{
name: "test-3",
have: 192,
want: "BARK TROD AMY UP LUG KNOB GAS WHEN NEWT POT KEY MEAN HEAL RAM ROWS JET RIM MAE",
},
{
name: "test-4",
have: 256,
want: "BARK TROD AMY UP LUG KNOB GAS WHEN NEWT POT KEY MEAN HEAL RAM ROWS JET RIM MAE EMMA SEAR AN JUKE NOB LIN",
},
}
for _, tt := range tests {
got := NewMnemonic(&testReader{}, tt.have)
if got != tt.want {
t.Errorf("\ntest\t= %s\nwant\t= %s\ngot\t= %s", tt.name, tt.want, got)
continue
}
}
}