-
Notifications
You must be signed in to change notification settings - Fork 13
/
codec_test.go
117 lines (109 loc) · 2.65 KB
/
codec_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package coincodec
import (
"bytes"
"errors"
"testing"
"github.com/wealdtech/go-slip44"
)
func TestToBytes(t *testing.T) {
tests := []struct {
name string
input string
coinType uint32
output []byte
err error
}{
{
name: "Empty",
input: "",
coinType: slip44.ETHER,
err: errors.New("empty input"),
},
{
name: "Unknown",
input: "unknown",
coinType: 6543253,
err: errors.New("unhandled coin type"),
},
{
name: "Good",
input: "0x0102030405060708090a0B0c0d0e0f1011121314",
coinType: slip44.ETHER,
output: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := ToBytes(test.input, test.coinType)
if test.err != nil {
if err == nil {
t.Fatalf("Missing expected error: expected %v", test.err)
}
if test.err.Error() != err.Error() {
t.Fatalf("Unexpected error value: expected %v, received %v", test.err, err)
}
} else {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !bytes.Equal(test.output, output) {
t.Fatalf("Unexpected output: expected %x, received %x", test.output, output)
}
}
})
}
}
func TestToString(t *testing.T) {
tests := []struct {
name string
input []byte
coinType uint32
output string
err error
}{
{
name: "Nil",
input: nil,
coinType: slip44.ETHER,
err: errors.New("empty input"),
},
{
name: "Empty",
input: []byte{},
coinType: slip44.ETHER,
err: errors.New("empty input"),
},
{
name: "Unknown",
input: []byte{0x01, 0x02, 0x03, 0x04},
coinType: 6543253,
err: errors.New("unhandled coin type"),
},
{
name: "Good",
input: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14},
coinType: slip44.ETHER,
output: "0x0102030405060708090a0B0c0d0e0f1011121314",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := ToString(test.input, test.coinType)
if test.err != nil {
if err == nil {
t.Fatalf("Missing expected error: expected %v", test.err)
}
if test.err.Error() != err.Error() {
t.Fatalf("Unexpected error value: expected %v, received %v", test.err, err)
}
} else {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if test.output != output {
t.Fatalf("Unexpected output: expected %x, received %x", test.output, output)
}
}
})
}
}