-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcrypto_test.go
199 lines (181 loc) · 5.07 KB
/
crypto_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package faker
import (
"fmt"
"strings"
"testing"
)
var (
bannedBitcoin = []string{"O", "I", "l", "0"}
validBitcoinPrefix = map[string]string{
"p2pkh": "1",
"p2sh": "3",
"bech32": "bc1",
}
validEthPrefix = "0x"
)
type GeneratorMock struct {
local int
}
func (g GeneratorMock) Intn(_ int) int {
return g.local
}
func (g GeneratorMock) Int() int {
return g.local
}
type TestCaseAlnum struct {
desc string
localInt int
assert func(t *testing.T, a int, b int)
}
type TestCaseRandomBitcoin struct {
desc string
localInt int
expectedSubstring string
}
func TestIsInExclusionZone(t *testing.T) {
c := New().Crypto()
for _, address := range bannedBitcoin {
Expect(t, true, c.isInExclusionZone(int(rune(address[0]))))
}
// take any banned rune and + 1 it to get a valid character
Expect(t, false, c.isInExclusionZone(int(rune(bannedBitcoin[0][0]))+1))
}
func TestGenerateBicoinAddress(t *testing.T) {
c := New().Crypto()
length := c.Faker.IntBetween(5, 10)
Expect(t, length+1, len(c.generateBicoinAddress(length, "a", c.Faker)))
}
func TestP2PKHAddress(t *testing.T) {
c := New().Crypto()
addr := c.P2PKHAddress()
Expect(t, true, len(addr) >= bitcoinMin)
Expect(t, true, len(addr) <= bitcoinMax)
Expect(t, true, strings.HasPrefix(addr, validBitcoinPrefix["p2pkh"]))
for i := 0; i < len(bannedBitcoin); i++ {
Expect(t, true, !strings.Contains(addr, bannedBitcoin[i]))
}
}
func TestP2PKHAddressWithLength(t *testing.T) {
c := New().Crypto()
length := c.Faker.IntBetween(26, 62)
addr := c.P2PKHAddressWithLength(length)
Expect(t, true, len(addr) == length)
Expect(t, true, strings.HasPrefix(addr, validBitcoinPrefix["p2pkh"]))
}
func TestP2SHAddress(t *testing.T) {
c := New().Crypto()
addr := c.P2SHAddress()
Expect(t, true, len(addr) >= bitcoinMin)
Expect(t, true, len(addr) <= bitcoinMax)
Expect(t, true, strings.HasPrefix(addr, validBitcoinPrefix["p2sh"]))
for i := 0; i < len(bannedBitcoin); i++ {
Expect(t, true, !strings.Contains(addr, bannedBitcoin[i]))
}
}
func TestP2SHAddressWithLength(t *testing.T) {
c := New().Crypto()
length := c.Faker.IntBetween(26, 62)
addr := c.P2SHAddressWithLength(length)
Expect(t, true, len(addr) == length)
Expect(t, true, strings.HasPrefix(addr, validBitcoinPrefix["p2sh"]))
}
func TestBech32Address(t *testing.T) {
c := New().Crypto()
addr := c.Bech32Address()
Expect(t, true, len(addr) >= bitcoinMin)
Expect(t, true, len(addr) <= bitcoinMax)
Expect(t, true, strings.HasPrefix(addr, validBitcoinPrefix["bech32"]))
for i := 0; i < len(bannedBitcoin); i++ {
Expect(t, true, !strings.Contains(addr, bannedBitcoin[i]))
}
}
func TestBech32AddressWithLength(t *testing.T) {
c := New().Crypto()
length := c.Faker.IntBetween(26, 62)
addr := c.Bech32AddressWithLength(length)
Expect(t, true, len(addr) == length)
Expect(t, true, strings.HasPrefix(addr, validBitcoinPrefix["bech32"]))
}
func TestEtheriumAddress(t *testing.T) {
c := New().Crypto()
addr := c.EtheriumAddress()
Expect(t, true, len(addr) == ethLen)
Expect(t, true, strings.HasPrefix(addr, ethPrefix))
}
func TestAlgorithmRange(t *testing.T) {
for k, tc := range []TestCaseAlnum{
{
// The Description of the test case
desc: "Test Get Digit 0-9",
localInt: 0,
// Our anticipated result
assert: func(t *testing.T, a int, b int) {
Expect(t, true, a == int('0'))
Expect(t, true, b == int('9'))
},
},
{
desc: "Test Get Uppercase A-Z",
localInt: 1,
assert: func(t *testing.T, a int, b int) {
Expect(t, true, a == int('A'))
Expect(t, true, b == int('Z'))
},
},
{
desc: "Test Get Lowercase a-z",
localInt: 2,
assert: func(t *testing.T, a int, b int) {
Expect(t, true, a == int('a'))
Expect(t, true, b == int('z'))
},
},
} {
t.Run(fmt.Sprintf("case=%d/description=%s", k, tc.desc), func(t *testing.T) {
// Use our mock here instead of using a seed.
gen := GeneratorMock{}
gen.local = tc.localInt
// populate the generator with our mock as it is an interface.
c := Faker{Generator: gen}
a, b := c.Crypto().algorithmRange()
tc.assert(t, a, b)
})
}
}
func TestRandomBitcoin(t *testing.T) {
for k, tc := range []TestCaseRandomBitcoin{
{
// The Description of the test case
desc: "Test Get Bech32",
localInt: 0,
// Our anticipated result
expectedSubstring: "bc1",
},
{
// The Description of the test case
desc: "Test Get P2SH",
localInt: 1,
// Our anticipated result
expectedSubstring: "3",
},
{
// The Description of the test case
desc: "Test Get P2PKH",
localInt: 2,
// Our anticipated result
expectedSubstring: "1",
},
} {
t.Run(fmt.Sprintf("case=%d/description=%s", k, tc.desc), func(t *testing.T) {
// Use our mock here instead of using a seed.
gen := GeneratorMock{}
gen.local = tc.localInt
// populate the generator with our mock as it is an interface.
c := Faker{Generator: gen}
rs := c.Crypto().BitcoinAddress()
Expect(t, true, strings.HasPrefix(rs, tc.expectedSubstring))
Expect(t, true, len(rs) >= bitcoinMin)
Expect(t, true, len(rs) <= bitcoinMax)
})
}
}