-
Notifications
You must be signed in to change notification settings - Fork 0
/
address_test.go
140 lines (128 loc) · 3.73 KB
/
address_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
package address_test
import (
"crypto/rand"
"testing"
"github.com/stretchr/testify/require"
"github.com/spacemeshos/address"
)
func init() {
address.DefaultTestAddressConfig()
}
func TestAddress_NewAddress(t *testing.T) {
t.Parallel()
table := []struct {
name string
src string
err error
}{
{
name: "correct",
src: "stest1qqqqqqy0dd83jemjmfj3ghjndxm0ndh0z2rymaqyp0gu3",
},
{
name: "corect address, but no reserved space",
src: "stest1fejq2x3d79ukpkw06t7h6lndjuwzxdnj59npghsg43mh4",
err: address.ErrMissingReservedSpace,
},
{
name: "incorrect cahrset", // chars iob is not supported by bech32.
src: "stest1fejq2x3d79ukpkw06t7h6lniobuwzxdnj59nphsywyusj",
err: address.ErrDecodeBech32,
},
{
name: "missing hrp",
src: "1fejq2x3d79ukpkw06t7h6lniobuwzxdnj59nphsywyusj",
err: address.ErrDecodeBech32,
},
{
name: "to big length",
src: "stest1qw508d6e1qejxtdg4y5r3zarvax8wucu",
err: address.ErrWrongAddressLength,
},
{
name: "to small length",
src: "stest1qw504y5r3zarva2v6vda",
err: address.ErrWrongAddressLength,
},
{
name: "wrong network",
src: "sut1fejq2x3d79ukpkw06t7h6lndjuwzxdnj59npghsldfkky",
err: address.ErrUnsupportedNetwork,
},
}
for _, testCase := range table {
t.Run(testCase.name, func(t *testing.T) {
_, err := address.StringToAddress(testCase.src)
if testCase.err != nil {
require.ErrorContains(t, err, testCase.err.Error())
} else {
require.NoError(t, err)
}
})
}
}
func TestAddress_SetBytes(t *testing.T) {
t.Parallel()
t.Run("generate from correct string", func(t *testing.T) {
srcAddr, err := address.StringToAddress("stest1qqqqqqrs60l66w5uksxzmaznwq6xnhqfv56c28qlkm4a5")
require.NoError(t, err)
newAddr := address.GenerateAddress(srcAddr.Bytes())
require.NoError(t, err)
checkAddressesEqual(t, srcAddr, newAddr)
})
t.Run("generate from small byte slice", func(t *testing.T) {
newAddr := address.GenerateAddress([]byte{1})
require.NotEmpty(t, newAddr.String())
})
t.Run("generate from large byte slice length", func(t *testing.T) {
newAddr := address.GenerateAddress([]byte("12345678901234567890123456789012345678901234567890"))
require.NotEmpty(t, newAddr.String())
})
t.Run("generate from correct byte slice", func(t *testing.T) {
data := make([]byte, address.AddressLength)
for i := range data {
data[i] = byte(i)
}
newAddr := address.GenerateAddress(data)
srcAddr, err := address.StringToAddress(newAddr.String())
require.NoError(t, err)
checkAddressesEqual(t, newAddr, srcAddr)
})
}
func TestAddress_GenerateAddress(t *testing.T) {
t.Parallel()
t.Run("generate from public key", func(t *testing.T) {
srcAddr := address.GenerateAddress(RandomBytes(32))
newAddr, err := address.StringToAddress(srcAddr.String())
require.NoError(t, err)
checkAddressesEqual(t, srcAddr, newAddr)
})
t.Run("generate from string", func(t *testing.T) {
srcAddr := address.GenerateAddress([]byte("some random very very long string"))
newAddr, err := address.StringToAddress(srcAddr.String())
require.NoError(t, err)
checkAddressesEqual(t, srcAddr, newAddr)
})
}
func TestAddress_ReservedBytesOnTop(t *testing.T) {
for i := 0; i < 100; i++ {
addr := address.GenerateAddress(RandomBytes(i))
for j := 0; j < address.AddressReservedSpace; j++ {
require.Zero(t, addr.Bytes()[j])
}
}
}
func checkAddressesEqual(t *testing.T, addrA, addrB address.Address) {
require.Equal(t, addrA.Bytes(), addrB.Bytes())
require.Equal(t, addrA.String(), addrB.String())
require.Equal(t, addrA.IsEmpty(), addrB.IsEmpty())
}
// RandomBytes generates random data in bytes for testing.
func RandomBytes(size int) []byte {
b := make([]byte, size)
_, err := rand.Read(b)
if err != nil {
return nil
}
return b
}