From c1f39d5c7bf22970fc777f7a6000a0f01e8ad30b Mon Sep 17 00:00:00 2001 From: Vsevolod Brekelov Date: Wed, 4 Dec 2019 15:24:49 +0300 Subject: [PATCH] internal: moved testutil method to internal package --- pkg/core/dao_test.go | 32 +++++++++--------- pkg/core/interop_neo_test.go | 24 +++++++------- pkg/core/spent_coin_state_test.go | 4 +-- pkg/core/state/account_test.go | 16 ++++----- pkg/core/state/asset_test.go | 8 ++--- pkg/core/state/notification_event_test.go | 6 ++-- pkg/core/testutil/random_util.go | 40 ----------------------- pkg/internal/random/random_util.go | 40 +++++++++++++++++++++++ 8 files changed, 85 insertions(+), 85 deletions(-) delete mode 100644 pkg/core/testutil/random_util.go create mode 100644 pkg/internal/random/random_util.go diff --git a/pkg/core/dao_test.go b/pkg/core/dao_test.go index 7c014cfe7..5aa44fe9f 100644 --- a/pkg/core/dao_test.go +++ b/pkg/core/dao_test.go @@ -5,9 +5,9 @@ import ( "github.com/CityOfZion/neo-go/pkg/core/state" "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/crypto/keys" + "github.com/CityOfZion/neo-go/pkg/internal/random" "github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/vm/opcode" @@ -16,7 +16,7 @@ import ( func TestPutGetAndDecode(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - serializable := &TestSerializable{field: testutil.RandomString(4)} + serializable := &TestSerializable{field: random.String(4)} hash := []byte{1} err := dao.Put(serializable, hash) require.NoError(t, err) @@ -41,7 +41,7 @@ func (t *TestSerializable) DecodeBinary(reader *io.BinReader) { func TestGetAccountStateOrNew_New(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint160() + hash := random.Uint160() createdAccount, err := dao.GetAccountStateOrNew(hash) require.NoError(t, err) require.NotNil(t, createdAccount) @@ -52,7 +52,7 @@ func TestGetAccountStateOrNew_New(t *testing.T) { func TestPutAndGetAccountStateOrNew(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint160() + hash := random.Uint160() accountState := &state.Account{ScriptHash: hash} err := dao.PutAccountState(accountState) require.NoError(t, err) @@ -63,7 +63,7 @@ func TestPutAndGetAccountStateOrNew(t *testing.T) { func TestPutAndGetAssetState(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - id := testutil.RandomUint256() + id := random.Uint256() assetState := &state.Asset{ID: id, Owner: keys.PublicKey{}} err := dao.PutAssetState(assetState) require.NoError(t, err) @@ -98,7 +98,7 @@ func TestDeleteContractState(t *testing.T) { func TestGetUnspentCoinStateOrNew_New(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint256() + hash := random.Uint256() unspentCoinState, err := dao.GetUnspentCoinStateOrNew(hash) require.NoError(t, err) require.NotNil(t, unspentCoinState) @@ -109,7 +109,7 @@ func TestGetUnspentCoinStateOrNew_New(t *testing.T) { func TestGetUnspentCoinState_Err(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint256() + hash := random.Uint256() gotUnspentCoinState, err := dao.GetUnspentCoinState(hash) require.Error(t, err) require.Nil(t, gotUnspentCoinState) @@ -117,7 +117,7 @@ func TestGetUnspentCoinState_Err(t *testing.T) { func TestPutGetUnspentCoinState(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint256() + hash := random.Uint256() unspentCoinState := &UnspentCoinState{states:[]state.Coin{}} err := dao.PutUnspentCoinState(hash, unspentCoinState) require.NoError(t, err) @@ -128,7 +128,7 @@ func TestPutGetUnspentCoinState(t *testing.T) { func TestGetSpentCoinStateOrNew_New(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint256() + hash := random.Uint256() spentCoinState, err := dao.GetSpentCoinsOrNew(hash) require.NoError(t, err) require.NotNil(t, spentCoinState) @@ -139,7 +139,7 @@ func TestGetSpentCoinStateOrNew_New(t *testing.T) { func TestPutAndGetSpentCoinState(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint256() + hash := random.Uint256() spentCoinState := &SpentCoinState{items:make(map[uint16]uint32)} err := dao.PutSpentCoinState(hash, spentCoinState) require.NoError(t, err) @@ -150,7 +150,7 @@ func TestPutAndGetSpentCoinState(t *testing.T) { func TestGetSpentCoinState_Err(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint256() + hash := random.Uint256() spentCoinState, err := dao.GetSpentCoinState(hash) require.Error(t, err) require.Nil(t, spentCoinState) @@ -158,7 +158,7 @@ func TestGetSpentCoinState_Err(t *testing.T) { func TestDeleteSpentCoinState(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint256() + hash := random.Uint256() spentCoinState := &SpentCoinState{items:make(map[uint16]uint32)} err := dao.PutSpentCoinState(hash, spentCoinState) require.NoError(t, err) @@ -229,7 +229,7 @@ func TestGetValidators(t *testing.T) { func TestPutGetAppExecResult(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint256() + hash := random.Uint256() appExecResult := &state.AppExecResult{TxHash: hash, Events:[]state.NotificationEvent{}} err := dao.PutAppExecResult(appExecResult) require.NoError(t, err) @@ -240,7 +240,7 @@ func TestPutGetAppExecResult(t *testing.T) { func TestPutGetStorageItem(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint160() + hash := random.Uint160() key := []byte{0} storageItem := &state.StorageItem{Value: []uint8{}} err := dao.PutStorageItem(hash, key, storageItem) @@ -251,7 +251,7 @@ func TestPutGetStorageItem(t *testing.T) { func TestDeleteStorageItem(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint160() + hash := random.Uint160() key := []byte{0} storageItem := &state.StorageItem{Value: []uint8{}} err := dao.PutStorageItem(hash, key, storageItem) @@ -264,7 +264,7 @@ func TestDeleteStorageItem(t *testing.T) { func TestGetBlock_NotExists(t *testing.T) { dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} - hash := testutil.RandomUint256() + hash := random.Uint256() block, err := dao.GetBlock(hash) require.Error(t, err) require.Nil(t, block) diff --git a/pkg/core/interop_neo_test.go b/pkg/core/interop_neo_test.go index 9cf07369e..ffe48a92f 100644 --- a/pkg/core/interop_neo_test.go +++ b/pkg/core/interop_neo_test.go @@ -6,9 +6,9 @@ import ( "github.com/CityOfZion/neo-go/pkg/core/state" "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/crypto/keys" + "github.com/CityOfZion/neo-go/pkg/internal/random" "github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/smartcontract/trigger" "github.com/CityOfZion/neo-go/pkg/util" @@ -349,10 +349,10 @@ func createVMAndAssetState(t *testing.T) (*vm.VM, *state.Asset, *interopContext) Available: 2, Precision: 1, FeeMode: 1, - FeeAddress: testutil.RandomUint160(), + FeeAddress: random.Uint160(), Owner: keys.PublicKey{X: big.NewInt(1), Y: big.NewInt(1)}, - Admin: testutil.RandomUint160(), - Issuer: testutil.RandomUint160(), + Admin: random.Uint160(), + Issuer: random.Uint160(), Expiration: 10, 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}, ReturnType: smartcontract.ArrayType, Properties: smartcontract.HasStorage, - Name: testutil.RandomString(10), - CodeVersion: testutil.RandomString(10), - Author: testutil.RandomString(10), - Email: testutil.RandomString(10), - Description: testutil.RandomString(10), + Name: random.String(10), + CodeVersion: random.String(10), + Author: random.String(10), + Email: random.String(10), + Description: random.String(10), } 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{ - PrevHash: testutil.RandomUint256(), + PrevHash: random.Uint256(), PrevIndex: 1, }) outputs := append(tx.Outputs, transaction.Output{ - AssetID: testutil.RandomUint256(), + AssetID: random.Uint256(), Amount: 10, - ScriptHash: testutil.RandomUint160(), + ScriptHash: random.Uint160(), Position: 1, }) diff --git a/pkg/core/spent_coin_state_test.go b/pkg/core/spent_coin_state_test.go index 0e3b65474..ec8176130 100644 --- a/pkg/core/spent_coin_state_test.go +++ b/pkg/core/spent_coin_state_test.go @@ -3,14 +3,14 @@ package core import ( "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/stretchr/testify/assert" ) func TestEncodeDecodeSpentCoinState(t *testing.T) { spent := &SpentCoinState{ - txHash: testutil.RandomUint256(), + txHash: random.Uint256(), txHeight: 1001, items: map[uint16]uint32{ 1: 3, diff --git a/pkg/core/state/account_test.go b/pkg/core/state/account_test.go index e59215f3b..3981222f7 100644 --- a/pkg/core/state/account_test.go +++ b/pkg/core/state/account_test.go @@ -3,8 +3,8 @@ package state import ( "testing" - "github.com/CityOfZion/neo-go/pkg/core/testutil" "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/util" "github.com/stretchr/testify/assert" @@ -17,12 +17,12 @@ func TestDecodeEncodeAccountState(t *testing.T) { votes = make([]*keys.PublicKey, n) ) for i := 0; i < n; i++ { - asset := testutil.RandomUint256() + asset := random.Uint256() for j := 0; j < i+1; j++ { balances[asset] = append(balances[asset], UnspentBalance{ - Tx: testutil.RandomUint256(), - Index: uint16(testutil.RandomInt(0, 65535)), - Value: util.Fixed8(int64(testutil.RandomInt(1, 10000))), + Tx: random.Uint256(), + Index: uint16(random.Int(0, 65535)), + Value: util.Fixed8(int64(random.Int(1, 10000))), }) } k, err := keys.NewPrivateKey() @@ -32,7 +32,7 @@ func TestDecodeEncodeAccountState(t *testing.T) { a := &Account{ Version: 0, - ScriptHash: testutil.RandomUint160(), + ScriptHash: random.Uint160(), IsFrozen: true, Votes: votes, Balances: balances, @@ -58,8 +58,8 @@ func TestDecodeEncodeAccountState(t *testing.T) { } func TestAccountStateBalanceValues(t *testing.T) { - asset1 := testutil.RandomUint256() - asset2 := testutil.RandomUint256() + asset1 := random.Uint256() + asset2 := random.Uint256() as := Account{Balances: make(map[util.Uint256][]UnspentBalance)} ref := 0 for i := 0; i < 10; i++ { diff --git a/pkg/core/state/asset_test.go b/pkg/core/state/asset_test.go index 790ba4f68..5a0a1daed 100644 --- a/pkg/core/state/asset_test.go +++ b/pkg/core/state/asset_test.go @@ -3,9 +3,9 @@ package state import ( "testing" - "github.com/CityOfZion/neo-go/pkg/core/testutil" "github.com/CityOfZion/neo-go/pkg/core/transaction" "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/util" "github.com/stretchr/testify/assert" @@ -13,7 +13,7 @@ import ( func TestEncodeDecodeAssetState(t *testing.T) { asset := &Asset{ - ID: testutil.RandomUint256(), + ID: random.Uint256(), AssetType: transaction.Token, Name: "super cool token", Amount: util.Fixed8(1000000), @@ -21,8 +21,8 @@ func TestEncodeDecodeAssetState(t *testing.T) { Precision: 0, FeeMode: feeMode, Owner: keys.PublicKey{}, - Admin: testutil.RandomUint160(), - Issuer: testutil.RandomUint160(), + Admin: random.Uint160(), + Issuer: random.Uint160(), Expiration: 10, IsFrozen: false, } diff --git a/pkg/core/state/notification_event_test.go b/pkg/core/state/notification_event_test.go index 7b41859c3..0e92ae898 100644 --- a/pkg/core/state/notification_event_test.go +++ b/pkg/core/state/notification_event_test.go @@ -3,14 +3,14 @@ package state import ( "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/stretchr/testify/assert" ) func TestEncodeDecodeNotificationEvent(t *testing.T) { event := &NotificationEvent{ - ScriptHash: testutil.RandomUint160(), + ScriptHash: random.Uint160(), Item: nil, } @@ -26,7 +26,7 @@ func TestEncodeDecodeNotificationEvent(t *testing.T) { func TestEncodeDecodeAppExecResult(t *testing.T) { appExecResult := &AppExecResult{ - TxHash: testutil.RandomUint256(), + TxHash: random.Uint256(), Trigger: 1, VMState: "Hault", GasConsumed: 10, diff --git a/pkg/core/testutil/random_util.go b/pkg/core/testutil/random_util.go deleted file mode 100644 index c58832e89..000000000 --- a/pkg/core/testutil/random_util.go +++ /dev/null @@ -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()) -} diff --git a/pkg/internal/random/random_util.go b/pkg/internal/random/random_util.go new file mode 100644 index 000000000..d07e4da50 --- /dev/null +++ b/pkg/internal/random/random_util.go @@ -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()) +}