forked from multiversx/mx-chain-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserAccount_test.go
158 lines (116 loc) · 3.98 KB
/
userAccount_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
package state_test
import (
"math/big"
"testing"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go/state"
"github.com/stretchr/testify/assert"
)
func TestNewUserAccount_NilAddressContainerShouldErr(t *testing.T) {
t.Parallel()
acc, err := state.NewUserAccount(nil)
assert.True(t, check.IfNil(acc))
assert.Equal(t, state.ErrNilAddress, err)
}
func TestNewUserAccount_OkParamsShouldWork(t *testing.T) {
t.Parallel()
acc, err := state.NewUserAccount(make([]byte, 32))
assert.Nil(t, err)
assert.False(t, check.IfNil(acc))
}
func TestUserAccount_AddToBalanceInsufficientFundsShouldErr(t *testing.T) {
t.Parallel()
acc, _ := state.NewUserAccount(make([]byte, 32))
value := big.NewInt(-1)
err := acc.AddToBalance(value)
assert.Equal(t, state.ErrInsufficientFunds, err)
}
func TestUserAccount_SubFromBalanceInsufficientFundsShouldErr(t *testing.T) {
t.Parallel()
acc, _ := state.NewUserAccount(make([]byte, 32))
value := big.NewInt(1)
err := acc.SubFromBalance(value)
assert.Equal(t, state.ErrInsufficientFunds, err)
}
func TestUserAccount_GetBalance(t *testing.T) {
t.Parallel()
acc, _ := state.NewUserAccount(make([]byte, 32))
balance := big.NewInt(100)
subFromBalance := big.NewInt(20)
_ = acc.AddToBalance(balance)
assert.Equal(t, balance, acc.GetBalance())
_ = acc.SubFromBalance(subFromBalance)
assert.Equal(t, big.NewInt(0).Sub(balance, subFromBalance), acc.GetBalance())
}
func TestUserAccount_AddToDeveloperReward(t *testing.T) {
t.Parallel()
acc, _ := state.NewUserAccount(make([]byte, 32))
reward := big.NewInt(10)
acc.AddToDeveloperReward(reward)
assert.Equal(t, reward, acc.GetDeveloperReward())
}
func TestUserAccount_ClaimDeveloperRewardsWrongAddressShouldErr(t *testing.T) {
t.Parallel()
acc, _ := state.NewUserAccount(make([]byte, 32))
val, err := acc.ClaimDeveloperRewards([]byte("wrong address"))
assert.Nil(t, val)
assert.Equal(t, state.ErrOperationNotPermitted, err)
}
func TestUserAccount_ClaimDeveloperRewards(t *testing.T) {
t.Parallel()
acc, _ := state.NewUserAccount(make([]byte, 32))
reward := big.NewInt(10)
acc.AddToDeveloperReward(reward)
val, err := acc.ClaimDeveloperRewards(acc.OwnerAddress)
assert.Nil(t, err)
assert.Equal(t, reward, val)
assert.Equal(t, big.NewInt(0), acc.GetDeveloperReward())
}
func TestUserAccount_ChangeOwnerAddressWrongAddressShouldErr(t *testing.T) {
t.Parallel()
acc, _ := state.NewUserAccount(make([]byte, 32))
err := acc.ChangeOwnerAddress([]byte("wrong address"), []byte{})
assert.Equal(t, state.ErrOperationNotPermitted, err)
}
func TestUserAccount_ChangeOwnerAddressInvalidAddressShouldErr(t *testing.T) {
t.Parallel()
acc, _ := state.NewUserAccount(make([]byte, 32))
err := acc.ChangeOwnerAddress(acc.OwnerAddress, []byte("new address"))
assert.Equal(t, state.ErrInvalidAddressLength, err)
}
func TestUserAccount_ChangeOwnerAddress(t *testing.T) {
t.Parallel()
newAddress := make([]byte, 32)
acc, _ := state.NewUserAccount(make([]byte, 32))
err := acc.ChangeOwnerAddress(acc.OwnerAddress, newAddress)
assert.Nil(t, err)
assert.Equal(t, newAddress, acc.GetOwnerAddress())
}
func TestUserAccount_SetOwnerAddress(t *testing.T) {
t.Parallel()
newAddress := []byte("new address")
acc, _ := state.NewUserAccount(make([]byte, 32))
acc.SetOwnerAddress(newAddress)
assert.Equal(t, newAddress, acc.GetOwnerAddress())
}
func TestUserAccount_SetAndGetNonce(t *testing.T) {
t.Parallel()
acc, _ := state.NewUserAccount(make([]byte, 32))
nonce := uint64(5)
acc.IncreaseNonce(nonce)
assert.Equal(t, nonce, acc.GetNonce())
}
func TestUserAccount_SetAndGetCodeHash(t *testing.T) {
t.Parallel()
acc, _ := state.NewUserAccount(make([]byte, 32))
codeHash := []byte("code hash")
acc.SetCodeHash(codeHash)
assert.Equal(t, codeHash, acc.GetCodeHash())
}
func TestUserAccount_SetAndGetRootHash(t *testing.T) {
t.Parallel()
acc, _ := state.NewUserAccount(make([]byte, 32))
rootHash := []byte("root hash")
acc.SetRootHash(rootHash)
assert.Equal(t, rootHash, acc.GetRootHash())
}