-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbase58_test.go
More file actions
159 lines (147 loc) · 4.66 KB
/
base58_test.go
File metadata and controls
159 lines (147 loc) · 4.66 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2013-2014 The btcsuite developers
// Copyright (c) 2015-2025 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package base58
import (
"bytes"
"encoding/hex"
"strings"
"testing"
)
// hexToBytes is a wrapper around [hex.DecodeString] that panics if there is an
// error. It MUST only be used with hard coded values in the tests.
func hexToBytes(origHex string) []byte {
buf, err := hex.DecodeString(origHex)
if err != nil {
panic(err)
}
return buf
}
// TestBase58Coding ensures [Decode] and [Encode] produce the expected results
// for both strings and raw byte data converted from hex.
func TestBase58Coding(t *testing.T) {
t.Parallel()
tests := []struct {
decoded []byte
encoded string
}{
// String inputs.
{[]byte(""), ""},
{[]byte(" "), "Z"},
{[]byte("-"), "n"},
{[]byte("0"), "q"},
{[]byte("1"), "r"},
{[]byte("-1"), "4SU"},
{[]byte("11"), "4k8"},
{[]byte("abc"), "ZiCa"},
{[]byte("1234598760"), "3mJr7AoUXx2Wqd"},
{[]byte("abcdefghijklmnopqrstuvwxyz"), "3yxU3u1igY8WkgtjK92fbJQCd4BZiiT1v25f"},
{
[]byte("00000000000000000000000000000000000000000000000000000000000000"),
"3sN2THZeE9Eh9eYrwkvZqNstbHGvrxSAM7gXUXvyFQP8XvQLUqNCS27icwUeDT7ckHm4FUHM2mTVh1vbLmk7y",
},
{
bytes.Repeat([]byte("0"), 200),
"KdhzWGVLoe2Z7u7v8kU6dSjNhdK8HNqQbVswpifqRXqmC5a6eFUoTLjhu41kZtTc" +
"6Am7Dzp8FcpoMubGyeiAinFQzGavztm4nnAm65i72UDh3FsTLbkoJf5oVNvx" +
"VALvaqWzugRNxNEs75g75wyubjXGhFxk4etvhvfdxu7JiwhXk1cWwnLUPjMY" +
"DGMFi2BEd8qMkB2wE8ACUHwdk3hHYuwaYKbEpFzjVQZwsRQo8JoFYozwdrFB" +
"Ys1F5NW6AtTVKMefQ6MGpGCxcjsWw",
},
// Hex inputs.
{hexToBytes("61"), "2g"},
{hexToBytes("626262"), "a3gV"},
{hexToBytes("636363"), "aPEr"},
{
hexToBytes("73696d706c792061206c6f6e6720737472696e67"),
"2cFupjhnEsSn59qHXstmK2ffpLv2",
},
{
hexToBytes("00eb15231dfceb60925886b67d065299925915aeb172c06647"),
"1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L",
},
{hexToBytes("516b6fcd0f"), "ABnLTmg"},
{hexToBytes("bf4f89001e670274dd"), "3SEo3LWLoPntC"},
{hexToBytes("572e4794"), "3EFU7m"},
{hexToBytes("ecac89cad93923c02321"), "EJDM8drfXA6uyA"},
{hexToBytes("10c8511e"), "Rt5zm"},
{
hexToBytes("426018fe18ee72f798faacf8ed1efcd786577ad07f33124120fc9" +
"537ba97fbe5c5dbd46b66ebd88d11b650b06662914b12aa80ca90110d9b5" +
"7337c45ee224cf6ba1d9a3aaf92a232dfebc251c78753fe9f9215bfe7c43" +
"744"),
"XyJrepxxsECdexAWReSjewTiyL6Ekj5W22bSqxjZfCUsns6QpSHD8bKw33z1YrDZ" +
"yD3S1H4iQawvXMTfxfjm8SdyCo8W989jvzEj4qUdZwzjgkMaz7Jx2fCw",
},
{hexToBytes("0000000002060730"), "111141111"},
{hexToBytes("00"), "1"},
{hexToBytes("0000"), "11"},
{hexToBytes("000000"), "111"},
{hexToBytes("00000000"), "1111"},
{hexToBytes("0000000000"), "11111"},
{hexToBytes("00000000000000000000"), "1111111111"},
{hexToBytes("0000000000000000000000"), "11111111111"},
{hexToBytes("000000000000000000000000"), "111111111111"},
{hexToBytes("00000000000000000000000000"), "1111111111111"},
{hexToBytes("0000000000000000000000000000"), "11111111111111"},
}
for i, test := range tests {
if res := Encode(test.decoded); res != test.encoded {
t.Errorf("Encode test #%d failed: got: %q, want: %q", i, res,
test.encoded)
continue
}
if res := Decode(test.encoded); !bytes.Equal(res, test.decoded) {
t.Errorf("Decode test #%d failed: got: %q (%#[2]x), want: %q "+
"(%#[3]x)", i, res, test.decoded)
continue
}
}
}
// TestBase58DecodeInvalid ensures [Decode] produces an empty result when
// provided with invalid base58 encodings.
func TestBase58DecodeInvalid(t *testing.T) {
t.Parallel()
tests := []string{
"0", "O", "I", "l", "3mJr0", "O3yxU", "3sNI", "4kl8", "0OIl",
"!@#$%^&*()-_=+~`",
}
for i, test := range tests {
if res := Decode(test); !bytes.Equal(res, []byte("")) {
t.Errorf("Decode test #%d failed: got: %q, want: %q", i, res, test)
continue
}
}
}
// FuzzDecode provides a fuzzing target for [Decode].
func FuzzDecode(f *testing.F) {
f.Add("")
for i := 0; i < 256; i++ {
f.Add(string([]byte{byte(i)}))
}
f.Add("10")
f.Add("11")
f.Add("210")
f.Add("211")
f.Add("1110")
f.Add("1111")
f.Add("71111")
f.Add("11111111")
f.Add("21111111")
f.Add("211111111111")
f.Add("1111111111111111")
f.Add(strings.Repeat("1", 87))
f.Add("2" + strings.Repeat("1", 127))
f.Add("2111111111111111111111111")
f.Add("11111111111111111111111111111111")
f.Add("2111111111111111111111111111111111111111")
f.Add("2" + strings.Repeat("1", 156))
f.Add("7" + strings.Repeat("1", 169))
f.Add("1117" + strings.Repeat("1", 344))
f.Add("X" + strings.Repeat("1", 693))
f.Add(strings.Repeat("1", 128))
f.Fuzz(func(t *testing.T, input string) {
Decode(input)
})
}