-
Notifications
You must be signed in to change notification settings - Fork 0
/
base58_1_test.go
171 lines (140 loc) · 3.91 KB
/
base58_1_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
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
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) 2017-2020 Denis Subbotin, Philip Schlump,
// Nika Jones, Steven Allen, MoonFruit
// Copyright (c) 2022 Teal.Finance contributors
//
// This file is a modified copy from https://github.com/mr-tron/base58
// The source code has been adapted to support other bases.
// This file is now part of BaseXX under the terms of the MIT License.
// SPDX-License-Identifier: MIT
// See the LICENSE file or https://opensource.org/licenses/MIT
package base58
import (
"encoding/hex"
"math/rand"
"testing"
"time"
mrTronBase58 "github.com/mr-tron/base58"
)
type testValues struct {
dec []byte
enc string
}
var tstEncoding = NewEncoding(btcDigits[:Radix])
const n = 8192 // power of two to speed up the % modulo
var testPairs = make([]testValues, 0, n)
func init() {
// If we do not seed the prng - it will default to a seed of (1)
// https://golang.org/pkg/math/rand/#Seed
rand.Seed(time.Now().UTC().UnixNano())
}
func initTestPairs() {
if len(testPairs) > 0 {
return
}
// pre-make the test pairs, so it doesn't take up benchmark time...
for i := 0; i < n; i++ {
data := make([]byte, 32)
rand.Read(data)
testPairs = append(testPairs, testValues{dec: data, enc: StdEncoding.EncodeToString(data)})
}
}
func randEncoding() *Encoding {
// Permutes [0, 127] and returns the first XX elements according to the BaseXX.
var randomness [128]byte
rand.Read(randomness[:])
var bts [128]byte
for i, r := range randomness {
j := int(r) % (i + 1)
bts[i] = bts[j]
bts[j] = byte(i)
}
return NewEncoding(string(bts[:Radix]))
}
var btcDigits = "" +
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" +
" !0OIl()*+[\\]^_`{|}~;:#$<=>%&',-./?@"
func TestInvalidEncodingTooShort(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet being too short did not occur")
}
}()
_ = NewEncoding(btcDigits[:Radix-1]) // too short
}
func TestInvalidEncodingTooLong(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet being too long did not occur")
}
}()
_ = NewEncoding(btcDigits) // too long
}
func TestInvalidEncodingNon127(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet containing non-ascii chars did not occur")
}
}()
_ = NewEncoding("\xFF" + btcDigits[:Radix-1]) // good length
}
func TestInvalidEncodingDup(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet containing duplicate chars did not occur")
}
}()
_ = NewEncoding(btcDigits[:1] + btcDigits[:Radix-1]) // good length, but 1st char duplicated
}
func TestFastEqTrivialEncodingAndDecoding(t *testing.T) {
for k := 0; k < 10; k++ {
testEncDecLoop(t, randEncoding())
}
testEncDecLoop(t, BTCEncoding)
testEncDecLoop(t, FlickrEncoding)
testEncDecLoop(t, tstEncoding)
}
func testEncDecLoop(t *testing.T, enc *Encoding) {
t.Helper()
for j := 1; j < 256; j++ {
b := make([]byte, j)
for i := 0; i < 100; i++ {
rand.Read(b)
fe := enc.EncodeToString(b)
fd, err := enc.DecodeString(fe)
if err != nil {
t.Errorf("fast error: %v", err)
}
if hex.EncodeToString(b) != hex.EncodeToString(fd) {
t.Errorf("decoding err: %s != %s", hex.EncodeToString(b), hex.EncodeToString(fd))
}
}
}
}
func BenchmarkEncode(b *testing.B) {
initTestPairs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
StdEncoding.EncodeToString(testPairs[i%n].dec)
}
}
func BenchmarkEncodeMrTronBase58(b *testing.B) {
initTestPairs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mrTronBase58.Encode(testPairs[i%n].dec)
}
}
func BenchmarkDecode(b *testing.B) {
initTestPairs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = StdEncoding.DecodeString(testPairs[i%n].enc)
}
}
func BenchmarkDecodeMrTronBase58(b *testing.B) {
initTestPairs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = mrTronBase58.Decode(testPairs[i%n].enc)
}
}