internal: moved testutil method to internal package

This commit is contained in:
Vsevolod Brekelov 2019-12-04 15:24:49 +03:00
parent 2d42b14a1d
commit c1f39d5c7b
8 changed files with 85 additions and 85 deletions

View file

@ -5,9 +5,9 @@ import (
"github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/testutil"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/internal/random"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/smartcontract"
"github.com/CityOfZion/neo-go/pkg/vm/opcode" "github.com/CityOfZion/neo-go/pkg/vm/opcode"
@ -16,7 +16,7 @@ import (
func TestPutGetAndDecode(t *testing.T) { func TestPutGetAndDecode(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
serializable := &TestSerializable{field: testutil.RandomString(4)} serializable := &TestSerializable{field: random.String(4)}
hash := []byte{1} hash := []byte{1}
err := dao.Put(serializable, hash) err := dao.Put(serializable, hash)
require.NoError(t, err) require.NoError(t, err)
@ -41,7 +41,7 @@ func (t *TestSerializable) DecodeBinary(reader *io.BinReader) {
func TestGetAccountStateOrNew_New(t *testing.T) { func TestGetAccountStateOrNew_New(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint160() hash := random.Uint160()
createdAccount, err := dao.GetAccountStateOrNew(hash) createdAccount, err := dao.GetAccountStateOrNew(hash)
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, createdAccount) require.NotNil(t, createdAccount)
@ -52,7 +52,7 @@ func TestGetAccountStateOrNew_New(t *testing.T) {
func TestPutAndGetAccountStateOrNew(t *testing.T) { func TestPutAndGetAccountStateOrNew(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint160() hash := random.Uint160()
accountState := &state.Account{ScriptHash: hash} accountState := &state.Account{ScriptHash: hash}
err := dao.PutAccountState(accountState) err := dao.PutAccountState(accountState)
require.NoError(t, err) require.NoError(t, err)
@ -63,7 +63,7 @@ func TestPutAndGetAccountStateOrNew(t *testing.T) {
func TestPutAndGetAssetState(t *testing.T) { func TestPutAndGetAssetState(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
id := testutil.RandomUint256() id := random.Uint256()
assetState := &state.Asset{ID: id, Owner: keys.PublicKey{}} assetState := &state.Asset{ID: id, Owner: keys.PublicKey{}}
err := dao.PutAssetState(assetState) err := dao.PutAssetState(assetState)
require.NoError(t, err) require.NoError(t, err)
@ -98,7 +98,7 @@ func TestDeleteContractState(t *testing.T) {
func TestGetUnspentCoinStateOrNew_New(t *testing.T) { func TestGetUnspentCoinStateOrNew_New(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint256() hash := random.Uint256()
unspentCoinState, err := dao.GetUnspentCoinStateOrNew(hash) unspentCoinState, err := dao.GetUnspentCoinStateOrNew(hash)
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, unspentCoinState) require.NotNil(t, unspentCoinState)
@ -109,7 +109,7 @@ func TestGetUnspentCoinStateOrNew_New(t *testing.T) {
func TestGetUnspentCoinState_Err(t *testing.T) { func TestGetUnspentCoinState_Err(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint256() hash := random.Uint256()
gotUnspentCoinState, err := dao.GetUnspentCoinState(hash) gotUnspentCoinState, err := dao.GetUnspentCoinState(hash)
require.Error(t, err) require.Error(t, err)
require.Nil(t, gotUnspentCoinState) require.Nil(t, gotUnspentCoinState)
@ -117,7 +117,7 @@ func TestGetUnspentCoinState_Err(t *testing.T) {
func TestPutGetUnspentCoinState(t *testing.T) { func TestPutGetUnspentCoinState(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint256() hash := random.Uint256()
unspentCoinState := &UnspentCoinState{states:[]state.Coin{}} unspentCoinState := &UnspentCoinState{states:[]state.Coin{}}
err := dao.PutUnspentCoinState(hash, unspentCoinState) err := dao.PutUnspentCoinState(hash, unspentCoinState)
require.NoError(t, err) require.NoError(t, err)
@ -128,7 +128,7 @@ func TestPutGetUnspentCoinState(t *testing.T) {
func TestGetSpentCoinStateOrNew_New(t *testing.T) { func TestGetSpentCoinStateOrNew_New(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint256() hash := random.Uint256()
spentCoinState, err := dao.GetSpentCoinsOrNew(hash) spentCoinState, err := dao.GetSpentCoinsOrNew(hash)
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, spentCoinState) require.NotNil(t, spentCoinState)
@ -139,7 +139,7 @@ func TestGetSpentCoinStateOrNew_New(t *testing.T) {
func TestPutAndGetSpentCoinState(t *testing.T) { func TestPutAndGetSpentCoinState(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint256() hash := random.Uint256()
spentCoinState := &SpentCoinState{items:make(map[uint16]uint32)} spentCoinState := &SpentCoinState{items:make(map[uint16]uint32)}
err := dao.PutSpentCoinState(hash, spentCoinState) err := dao.PutSpentCoinState(hash, spentCoinState)
require.NoError(t, err) require.NoError(t, err)
@ -150,7 +150,7 @@ func TestPutAndGetSpentCoinState(t *testing.T) {
func TestGetSpentCoinState_Err(t *testing.T) { func TestGetSpentCoinState_Err(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint256() hash := random.Uint256()
spentCoinState, err := dao.GetSpentCoinState(hash) spentCoinState, err := dao.GetSpentCoinState(hash)
require.Error(t, err) require.Error(t, err)
require.Nil(t, spentCoinState) require.Nil(t, spentCoinState)
@ -158,7 +158,7 @@ func TestGetSpentCoinState_Err(t *testing.T) {
func TestDeleteSpentCoinState(t *testing.T) { func TestDeleteSpentCoinState(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint256() hash := random.Uint256()
spentCoinState := &SpentCoinState{items:make(map[uint16]uint32)} spentCoinState := &SpentCoinState{items:make(map[uint16]uint32)}
err := dao.PutSpentCoinState(hash, spentCoinState) err := dao.PutSpentCoinState(hash, spentCoinState)
require.NoError(t, err) require.NoError(t, err)
@ -229,7 +229,7 @@ func TestGetValidators(t *testing.T) {
func TestPutGetAppExecResult(t *testing.T) { func TestPutGetAppExecResult(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint256() hash := random.Uint256()
appExecResult := &state.AppExecResult{TxHash: hash, Events:[]state.NotificationEvent{}} appExecResult := &state.AppExecResult{TxHash: hash, Events:[]state.NotificationEvent{}}
err := dao.PutAppExecResult(appExecResult) err := dao.PutAppExecResult(appExecResult)
require.NoError(t, err) require.NoError(t, err)
@ -240,7 +240,7 @@ func TestPutGetAppExecResult(t *testing.T) {
func TestPutGetStorageItem(t *testing.T) { func TestPutGetStorageItem(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint160() hash := random.Uint160()
key := []byte{0} key := []byte{0}
storageItem := &state.StorageItem{Value: []uint8{}} storageItem := &state.StorageItem{Value: []uint8{}}
err := dao.PutStorageItem(hash, key, storageItem) err := dao.PutStorageItem(hash, key, storageItem)
@ -251,7 +251,7 @@ func TestPutGetStorageItem(t *testing.T) {
func TestDeleteStorageItem(t *testing.T) { func TestDeleteStorageItem(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint160() hash := random.Uint160()
key := []byte{0} key := []byte{0}
storageItem := &state.StorageItem{Value: []uint8{}} storageItem := &state.StorageItem{Value: []uint8{}}
err := dao.PutStorageItem(hash, key, storageItem) err := dao.PutStorageItem(hash, key, storageItem)
@ -264,7 +264,7 @@ func TestDeleteStorageItem(t *testing.T) {
func TestGetBlock_NotExists(t *testing.T) { func TestGetBlock_NotExists(t *testing.T) {
dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())}
hash := testutil.RandomUint256() hash := random.Uint256()
block, err := dao.GetBlock(hash) block, err := dao.GetBlock(hash)
require.Error(t, err) require.Error(t, err)
require.Nil(t, block) require.Nil(t, block)

View file

@ -6,9 +6,9 @@ import (
"github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/testutil"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/internal/random"
"github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/smartcontract"
"github.com/CityOfZion/neo-go/pkg/smartcontract/trigger" "github.com/CityOfZion/neo-go/pkg/smartcontract/trigger"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
@ -349,10 +349,10 @@ func createVMAndAssetState(t *testing.T) (*vm.VM, *state.Asset, *interopContext)
Available: 2, Available: 2,
Precision: 1, Precision: 1,
FeeMode: 1, FeeMode: 1,
FeeAddress: testutil.RandomUint160(), FeeAddress: random.Uint160(),
Owner: keys.PublicKey{X: big.NewInt(1), Y: big.NewInt(1)}, Owner: keys.PublicKey{X: big.NewInt(1), Y: big.NewInt(1)},
Admin: testutil.RandomUint160(), Admin: random.Uint160(),
Issuer: testutil.RandomUint160(), Issuer: random.Uint160(),
Expiration: 10, Expiration: 10,
IsFrozen: false, IsFrozen: false,
} }
@ -368,11 +368,11 @@ func createVMAndContractState(t *testing.T) (*vm.VM, *state.Contract, *interopCo
ParamList: []smartcontract.ParamType{smartcontract.StringType, smartcontract.IntegerType, smartcontract.Hash160Type}, ParamList: []smartcontract.ParamType{smartcontract.StringType, smartcontract.IntegerType, smartcontract.Hash160Type},
ReturnType: smartcontract.ArrayType, ReturnType: smartcontract.ArrayType,
Properties: smartcontract.HasStorage, Properties: smartcontract.HasStorage,
Name: testutil.RandomString(10), Name: random.String(10),
CodeVersion: testutil.RandomString(10), CodeVersion: random.String(10),
Author: testutil.RandomString(10), Author: random.String(10),
Email: testutil.RandomString(10), Email: random.String(10),
Description: testutil.RandomString(10), Description: random.String(10),
} }
context := newInteropContext(trigger.Application, newTestChain(t), storage.NewMemoryStore(), nil, nil) context := newInteropContext(trigger.Application, newTestChain(t), storage.NewMemoryStore(), nil, nil)
@ -404,14 +404,14 @@ func createVMAndTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interopCont
}) })
inputs := append(tx.Inputs, transaction.Input{ inputs := append(tx.Inputs, transaction.Input{
PrevHash: testutil.RandomUint256(), PrevHash: random.Uint256(),
PrevIndex: 1, PrevIndex: 1,
}) })
outputs := append(tx.Outputs, transaction.Output{ outputs := append(tx.Outputs, transaction.Output{
AssetID: testutil.RandomUint256(), AssetID: random.Uint256(),
Amount: 10, Amount: 10,
ScriptHash: testutil.RandomUint160(), ScriptHash: random.Uint160(),
Position: 1, Position: 1,
}) })

View file

@ -3,14 +3,14 @@ package core
import ( import (
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/core/testutil" "github.com/CityOfZion/neo-go/pkg/internal/random"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestEncodeDecodeSpentCoinState(t *testing.T) { func TestEncodeDecodeSpentCoinState(t *testing.T) {
spent := &SpentCoinState{ spent := &SpentCoinState{
txHash: testutil.RandomUint256(), txHash: random.Uint256(),
txHeight: 1001, txHeight: 1001,
items: map[uint16]uint32{ items: map[uint16]uint32{
1: 3, 1: 3,

View file

@ -3,8 +3,8 @@ package state
import ( import (
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/core/testutil"
"github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/internal/random"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -17,12 +17,12 @@ func TestDecodeEncodeAccountState(t *testing.T) {
votes = make([]*keys.PublicKey, n) votes = make([]*keys.PublicKey, n)
) )
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
asset := testutil.RandomUint256() asset := random.Uint256()
for j := 0; j < i+1; j++ { for j := 0; j < i+1; j++ {
balances[asset] = append(balances[asset], UnspentBalance{ balances[asset] = append(balances[asset], UnspentBalance{
Tx: testutil.RandomUint256(), Tx: random.Uint256(),
Index: uint16(testutil.RandomInt(0, 65535)), Index: uint16(random.Int(0, 65535)),
Value: util.Fixed8(int64(testutil.RandomInt(1, 10000))), Value: util.Fixed8(int64(random.Int(1, 10000))),
}) })
} }
k, err := keys.NewPrivateKey() k, err := keys.NewPrivateKey()
@ -32,7 +32,7 @@ func TestDecodeEncodeAccountState(t *testing.T) {
a := &Account{ a := &Account{
Version: 0, Version: 0,
ScriptHash: testutil.RandomUint160(), ScriptHash: random.Uint160(),
IsFrozen: true, IsFrozen: true,
Votes: votes, Votes: votes,
Balances: balances, Balances: balances,
@ -58,8 +58,8 @@ func TestDecodeEncodeAccountState(t *testing.T) {
} }
func TestAccountStateBalanceValues(t *testing.T) { func TestAccountStateBalanceValues(t *testing.T) {
asset1 := testutil.RandomUint256() asset1 := random.Uint256()
asset2 := testutil.RandomUint256() asset2 := random.Uint256()
as := Account{Balances: make(map[util.Uint256][]UnspentBalance)} as := Account{Balances: make(map[util.Uint256][]UnspentBalance)}
ref := 0 ref := 0
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {

View file

@ -3,9 +3,9 @@ package state
import ( import (
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/core/testutil"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/internal/random"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -13,7 +13,7 @@ import (
func TestEncodeDecodeAssetState(t *testing.T) { func TestEncodeDecodeAssetState(t *testing.T) {
asset := &Asset{ asset := &Asset{
ID: testutil.RandomUint256(), ID: random.Uint256(),
AssetType: transaction.Token, AssetType: transaction.Token,
Name: "super cool token", Name: "super cool token",
Amount: util.Fixed8(1000000), Amount: util.Fixed8(1000000),
@ -21,8 +21,8 @@ func TestEncodeDecodeAssetState(t *testing.T) {
Precision: 0, Precision: 0,
FeeMode: feeMode, FeeMode: feeMode,
Owner: keys.PublicKey{}, Owner: keys.PublicKey{},
Admin: testutil.RandomUint160(), Admin: random.Uint160(),
Issuer: testutil.RandomUint160(), Issuer: random.Uint160(),
Expiration: 10, Expiration: 10,
IsFrozen: false, IsFrozen: false,
} }

View file

@ -3,14 +3,14 @@ package state
import ( import (
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/core/testutil" "github.com/CityOfZion/neo-go/pkg/internal/random"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestEncodeDecodeNotificationEvent(t *testing.T) { func TestEncodeDecodeNotificationEvent(t *testing.T) {
event := &NotificationEvent{ event := &NotificationEvent{
ScriptHash: testutil.RandomUint160(), ScriptHash: random.Uint160(),
Item: nil, Item: nil,
} }
@ -26,7 +26,7 @@ func TestEncodeDecodeNotificationEvent(t *testing.T) {
func TestEncodeDecodeAppExecResult(t *testing.T) { func TestEncodeDecodeAppExecResult(t *testing.T) {
appExecResult := &AppExecResult{ appExecResult := &AppExecResult{
TxHash: testutil.RandomUint256(), TxHash: random.Uint256(),
Trigger: 1, Trigger: 1,
VMState: "Hault", VMState: "Hault",
GasConsumed: 10, GasConsumed: 10,

View file

@ -1,40 +0,0 @@
package testutil
import (
"math/rand"
"time"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/util"
)
// RandomString returns a random string with the n as its length.
func RandomString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = byte(RandomInt(65, 90))
}
return string(b)
}
// RandomInt returns a random integer in [min,max).
func RandomInt(min, max int) int {
return min + rand.Intn(max-min)
}
// RandomUint256 returns a random Uint256.
func RandomUint256() util.Uint256 {
str := RandomString(20)
return hash.Sha256([]byte(str))
}
// RandomUint160 returns a random Uint160.
func RandomUint160() util.Uint160 {
str := RandomString(20)
return hash.RipeMD160([]byte(str))
}
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}

View file

@ -0,0 +1,40 @@
package random
import (
"math/rand"
"time"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/util"
)
// String returns a random string with the n as its length.
func String(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = byte(Int(65, 90))
}
return string(b)
}
// Int returns a random integer in [min,max).
func Int(min, max int) int {
return min + rand.Intn(max-min)
}
// Uint256 returns a random Uint256.
func Uint256() util.Uint256 {
str := String(20)
return hash.Sha256([]byte(str))
}
// Uint160 returns a random Uint160.
func Uint160() util.Uint160 {
str := String(20)
return hash.RipeMD160([]byte(str))
}
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}