From a466f6eaefcf51b03ba79711213425a31580ea54 Mon Sep 17 00:00:00 2001 From: yann300 Date: Tue, 2 Jan 2018 17:57:34 +0100 Subject: [PATCH] refactor testing --- remix-solidity/test/decoder/localDecoder.js | 32 +----- .../test/decoder/localsTests/int.js | 4 +- .../test/decoder/localsTests/misc.js | 4 +- .../test/decoder/localsTests/misc2.js | 4 +- .../test/decoder/localsTests/structArray.js | 4 +- .../test/decoder/stateTests/mapping.js | 75 +++++++++++++ remix-solidity/test/decoder/storageDecoder.js | 101 +----------------- remix-solidity/test/decoder/vmCall.js | 34 +++++- 8 files changed, 119 insertions(+), 139 deletions(-) create mode 100644 remix-solidity/test/decoder/stateTests/mapping.js diff --git a/remix-solidity/test/decoder/localDecoder.js b/remix-solidity/test/decoder/localDecoder.js index 5c9fe73ed6..a3188b365e 100644 --- a/remix-solidity/test/decoder/localDecoder.js +++ b/remix-solidity/test/decoder/localDecoder.js @@ -4,11 +4,8 @@ var compiler = require('solc') var intLocal = require('./contracts/intLocal') var miscLocal = require('./contracts/miscLocal') var structArrayLocal = require('./contracts/structArrayLocal') -var VM = require('ethereumjs-vm') -var utileth = require('ethereumjs-util') var remixLib = require('remix-lib') -var Web3Providers = remixLib.vm.Web3Providers -var global = remixLib.global +var vmCall = require('./vmCall') var intLocalTest = require('./localsTests/int') var miscLocalTest = require('./localsTests/misc') var misc2LocalTest = require('./localsTests/misc2') @@ -18,36 +15,11 @@ var compilerInput = remixLib.helpers.compiler.compilerInput tape('solidity', function (t) { t.test('local decoder', function (st) { var privateKey = new Buffer('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', 'hex') - var address = utileth.privateToAddress(privateKey) - var vm = initVM(st, address) + var vm = vmCall.initVM(st, privateKey) test(st, vm, privateKey) }) }) -/* - Init VM / Send Transaction -*/ -function initVM (st, address) { - var vm = new VM({ - enableHomestead: true, - activatePrecompiles: true - }) - vm.stateManager.putAccountBalance(address, 'f00000000000000001', function cb () {}) - var web3Providers = new Web3Providers() - web3Providers.addVM('VM', vm) - web3Providers.get('VM', function (error, obj) { - if (error) { - var mes = 'provider TEST not defined' - console.log(mes) - st.fail(mes) - } else { - global.web3 = obj - st.end() - } - }) - return vm -} - function test (st, vm, privateKey) { var output = compiler.compileStandardWrapper(compilerInput(intLocal.contract)) output = JSON.parse(output) diff --git a/remix-solidity/test/decoder/localsTests/int.js b/remix-solidity/test/decoder/localsTests/int.js index 771f71e092..7b829039b2 100644 --- a/remix-solidity/test/decoder/localsTests/int.js +++ b/remix-solidity/test/decoder/localsTests/int.js @@ -2,7 +2,7 @@ var remixCore = require('remix-core') var TraceManager = remixCore.trace.TraceManager var CodeManager = remixCore.code.CodeManager -var vmSendTx = require('../vmCall') +var vmCall = require('../vmCall') var remixLib = require('remix-lib') var traceHelper = remixLib.helpers.trace @@ -13,7 +13,7 @@ var EventManager = remixLib.EventManager var helper = require('./helper') module.exports = function (st, vm, privateKey, contractBytecode, compilationResult, cb) { - vmSendTx(vm, {nonce: 0, privateKey: privateKey}, null, 0, contractBytecode, function (error, txHash) { + vmCall.sendTx(vm, {nonce: 0, privateKey: privateKey}, null, 0, contractBytecode, function (error, txHash) { if (error) { st.fail(error) } else { diff --git a/remix-solidity/test/decoder/localsTests/misc.js b/remix-solidity/test/decoder/localsTests/misc.js index 92a1405a02..008d703927 100644 --- a/remix-solidity/test/decoder/localsTests/misc.js +++ b/remix-solidity/test/decoder/localsTests/misc.js @@ -2,7 +2,7 @@ var remixCore = require('remix-core') var TraceManager = remixCore.trace.TraceManager var CodeManager = remixCore.code.CodeManager -var vmSendTx = require('../vmCall') +var vmCall = require('../vmCall') var remixLib = require('remix-lib') var traceHelper = remixLib.helpers.trace var global = remixLib.global @@ -12,7 +12,7 @@ var EventManager = remixLib.EventManager var helper = require('./helper') module.exports = function (st, vm, privateKey, contractBytecode, compilationResult, cb) { - vmSendTx(vm, {nonce: 0, privateKey: privateKey}, null, 0, contractBytecode, function (error, txHash) { + vmCall.sendTx(vm, {nonce: 0, privateKey: privateKey}, null, 0, contractBytecode, function (error, txHash) { if (error) { st.fail(error) } else { diff --git a/remix-solidity/test/decoder/localsTests/misc2.js b/remix-solidity/test/decoder/localsTests/misc2.js index 063292e729..ffd5b04832 100644 --- a/remix-solidity/test/decoder/localsTests/misc2.js +++ b/remix-solidity/test/decoder/localsTests/misc2.js @@ -2,7 +2,7 @@ var remixCore = require('remix-core') var TraceManager = remixCore.trace.TraceManager var CodeManager = remixCore.code.CodeManager -var vmSendTx = require('../vmCall') +var vmCall = require('../vmCall') var remixLib = require('remix-lib') var traceHelper = remixLib.helpers.trace var global = remixLib.global @@ -12,7 +12,7 @@ var EventManager = remixLib.EventManager var helper = require('./helper') module.exports = function (st, vm, privateKey, contractBytecode, compilationResult, cb) { - vmSendTx(vm, {nonce: 0, privateKey: privateKey}, null, 0, contractBytecode, function (error, txHash) { + vmCall.sendTx(vm, {nonce: 0, privateKey: privateKey}, null, 0, contractBytecode, function (error, txHash) { if (error) { st.fail(error) } else { diff --git a/remix-solidity/test/decoder/localsTests/structArray.js b/remix-solidity/test/decoder/localsTests/structArray.js index 6ff46607a7..ef8a28e44e 100644 --- a/remix-solidity/test/decoder/localsTests/structArray.js +++ b/remix-solidity/test/decoder/localsTests/structArray.js @@ -2,7 +2,7 @@ var remixCore = require('remix-core') var TraceManager = remixCore.trace.TraceManager var CodeManager = remixCore.code.CodeManager -var vmSendTx = require('../vmCall') +var vmCall = require('../vmCall') var remixLib = require('remix-lib') var traceHelper = remixLib.helpers.trace var global = remixLib.global @@ -12,7 +12,7 @@ var EventManager = remixLib.EventManager var helper = require('./helper') module.exports = function (st, vm, privateKey, contractBytecode, compilationResult, cb) { - vmSendTx(vm, {nonce: 0, privateKey: privateKey}, null, 0, contractBytecode, function (error, txHash) { + vmCall.sendTx(vm, {nonce: 0, privateKey: privateKey}, null, 0, contractBytecode, function (error, txHash) { if (error) { st.fail(error) } else { diff --git a/remix-solidity/test/decoder/stateTests/mapping.js b/remix-solidity/test/decoder/stateTests/mapping.js new file mode 100644 index 0000000000..3aac8010c5 --- /dev/null +++ b/remix-solidity/test/decoder/stateTests/mapping.js @@ -0,0 +1,75 @@ +var remixLib = require('remix-lib') +var compilerInput = remixLib.helpers.compiler.compilerInput +var compiler = require('solc') +var stateDecoder = require('../../../src/decoder/stateDecoder') +var vmCall = require('../vmCall') + +module.exports = function testMappingStorage (st, cb) { + var mappingStorage = require('../contracts/mappingStorage') + var privateKey = new Buffer('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', 'hex') + var vm = vmCall.initVM(st, privateKey) + var output = compiler.compileStandardWrapper(compilerInput(mappingStorage.contract)) + output = JSON.parse(output) + vmCall.sendTx(vm, {nonce: 0, privateKey: privateKey}, null, 0, output.contracts['test.sol']['SimpleMappingState'].evm.bytecode.object, function (error, txHash) { + if (error) { + console.log(error) + st.end(error) + } else { + remixLib.global.web3.eth.getTransaction(txHash, (error, tx) => { + if (error) { + st.end(error) + } else { + testMapping(st, vm, privateKey, tx.contractAddress, output, (error) => { + st.end(error) + cb() + }) + } + }) + } + }) +} + +function testMapping (st, vm, privateKey, contractAddress, output, cb) { + vmCall.sendTx(vm, {nonce: 1, privateKey: privateKey}, contractAddress, 0, '2fd0a83a00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001074686973206973206120737472696e6700000000000000000000000000000000', + function (error, txHash) { + if (error) { + console.log(error) + st.end(error) + } else { + console.log(txHash) + remixLib.global.web3.eth.getTransaction(txHash, (error, tx) => { + if (error) { + st.end(error) + } else { + var TraceManager = require('remix-core').trace.TraceManager + var traceManager = new TraceManager() + traceManager.resolveTrace(tx, () => { + var StorageResolver = require('remix-core').storage.StorageResolver + var StorageViewer = require('remix-core').storage.StorageViewer + var storageViewer = new StorageViewer({ + stepIndex: 199, + tx: tx, + address: contractAddress + }, new StorageResolver(), traceManager) + var stateVars = stateDecoder.extractStateVariables('SimpleMappingState', output.sources) + stateDecoder.decodeState(stateVars, storageViewer).then((result) => { + console.log('ok', JSON.stringify(result)) + st.equal(result['_num'].value, '1') + st.equal(result['_num'].type, 'uint256') + st.equal(result['_iBreakSolidityState'].type, 'mapping(string => uint256)') + st.equal(result['_iBreakSolidityState'].value['74686973206973206120737472696e67'].value, '1') + st.equal(result['_iBreakSolidityState'].value['74686973206973206120737472696e67'].type, 'uint256') + st.equal(result['_iBreakSolidityStateInt'].type, 'mapping(uint256 => uint256)') + st.equal(result['_iBreakSolidityStateInt'].value['0000000000000000000000000000000000000000000000000000000000000001'].value, '1') + st.equal(result['_iBreakSolidityStateInt'].value['0000000000000000000000000000000000000000000000000000000000000001'].type, 'uint256') + // st.end() + }, (reason) => { + console.log('fail') + st.end(reason) + }) + }) + } + }) + } + }) +} diff --git a/remix-solidity/test/decoder/storageDecoder.js b/remix-solidity/test/decoder/storageDecoder.js index 9888a8025a..1c943e47fd 100644 --- a/remix-solidity/test/decoder/storageDecoder.js +++ b/remix-solidity/test/decoder/storageDecoder.js @@ -5,6 +5,7 @@ var stateDecoder = require('../../src/decoder/stateDecoder') var MockStorageResolver = require('./mockStorageResolver') var remixLib = require('remix-lib') var compilerInput = remixLib.helpers.compiler.compilerInput +var testMappingStorage = require('./stateTests/mapping') tape('solidity', function (t) { t.test('storage decoder', function (st) { @@ -273,103 +274,3 @@ function testStructArrayStorage (st, cb) { cb() }) } - -function testMappingStorage (st, cb) { - var mappingStorage = require('./contracts/mappingStorage') - var vmSendTx = require('./vmCall') - var privateKey = new Buffer('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', 'hex') - var vm = initVM(st, privateKey) - var output = compiler.compileStandardWrapper(compilerInput(mappingStorage.contract)) - output = JSON.parse(output) - vmSendTx(vm, {nonce: 0, privateKey: privateKey}, null, 0, output.contracts['test.sol']['SimpleMappingState'].evm.bytecode.object, function (error, txHash) { - if (error) { - console.log(error) - st.end(error) - } else { - remixLib.global.web3.eth.getTransaction(txHash, (error, tx) => { - if (error) { - st.end(error) - } else { - testMapping(st, vm, privateKey, tx.contractAddress, output, (error) => { - st.end(error) - }) - } - }) - } - }) -} - -/* - Init VM / Send Transaction -*/ -function initVM (st, privateKey) { - var remixLib = require('remix-lib') - var utileth = require('ethereumjs-util') - var VM = require('ethereumjs-vm') - var Web3Providers = remixLib.vm.Web3Providers - var address = utileth.privateToAddress(privateKey) - var vm = new VM({ - enableHomestead: true, - activatePrecompiles: true - }) - vm.stateManager.putAccountBalance(address, 'f00000000000000001', function cb () {}) - var web3Providers = new Web3Providers() - web3Providers.addVM('VM', vm) - web3Providers.get('VM', function (error, obj) { - if (error) { - var mes = 'provider TEST not defined' - console.log(mes) - st.fail(mes) - } else { - remixLib.global.web3 = obj - st.end() - } - }) - return vm -} - -function testMapping (st, vm, privateKey, contractAddress, output, cb) { - var vmSendTx = require('./vmCall') - vmSendTx(vm, {nonce: 1, privateKey: privateKey}, contractAddress, 0, '2fd0a83a00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001074686973206973206120737472696e6700000000000000000000000000000000', - function (error, txHash) { - if (error) { - console.log(error) - st.end(error) - } else { - console.log(txHash) - remixLib.global.web3.eth.getTransaction(txHash, (error, tx) => { - if (error) { - st.end(error) - } else { - var TraceManager = require('remix-core').trace.TraceManager - var traceManager = new TraceManager() - traceManager.resolveTrace(tx, () => { - var StorageResolver = require('remix-core').storage.StorageResolver - var StorageViewer = require('remix-core').storage.StorageViewer - var storageViewer = new StorageViewer({ - stepIndex: 199, - tx: tx, - address: contractAddress - }, new StorageResolver(), traceManager) - var stateVars = stateDecoder.extractStateVariables('SimpleMappingState', output.sources) - stateDecoder.decodeState(stateVars, storageViewer).then((result) => { - console.log('ok', JSON.stringify(result)) - st.equal(result['_num'].value, '1') - st.equal(result['_num'].type, 'uint256') - st.equal(result['_iBreakSolidityState'].type, 'mapping(string => uint256)') - st.equal(result['_iBreakSolidityState'].value['74686973206973206120737472696e67'].value, '1') - st.equal(result['_iBreakSolidityState'].value['74686973206973206120737472696e67'].type, 'uint256') - st.equal(result['_iBreakSolidityStateInt'].type, 'mapping(uint256 => uint256)') - st.equal(result['_iBreakSolidityStateInt'].value['0000000000000000000000000000000000000000000000000000000000000001'].value, '1') - st.equal(result['_iBreakSolidityStateInt'].value['0000000000000000000000000000000000000000000000000000000000000001'].type, 'uint256') - // st.end() - }, (reason) => { - console.log('fail') - st.end(reason) - }) - }) - } - }) - } - }) -} diff --git a/remix-solidity/test/decoder/vmCall.js b/remix-solidity/test/decoder/vmCall.js index dc7906960a..0faf7a483a 100644 --- a/remix-solidity/test/decoder/vmCall.js +++ b/remix-solidity/test/decoder/vmCall.js @@ -29,4 +29,36 @@ function sendTx (vm, from, to, value, data, cb) { }) } -module.exports = sendTx +/* + Init VM / Send Transaction +*/ +function initVM (st, privateKey) { + var remixLib = require('remix-lib') + var utileth = require('ethereumjs-util') + var VM = require('ethereumjs-vm') + var Web3Providers = remixLib.vm.Web3Providers + var address = utileth.privateToAddress(privateKey) + var vm = new VM({ + enableHomestead: true, + activatePrecompiles: true + }) + vm.stateManager.putAccountBalance(address, 'f00000000000000001', function cb () {}) + var web3Providers = new Web3Providers() + web3Providers.addVM('VM', vm) + web3Providers.get('VM', function (error, obj) { + if (error) { + var mes = 'provider TEST not defined' + console.log(mes) + st.fail(mes) + } else { + remixLib.global.web3 = obj + st.end() + } + }) + return vm +} + +module.exports = { + sendTx: sendTx, + initVM: initVM +}