remix-project mirror
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
remix-project/libs/remix-lib/test/util.ts

111 lines
64 KiB

7 years ago
'use strict'
import tape from 'tape'
import * as util from '../src/util'
2 years ago
import { ERC721 } from './data/ERC721'
import { sampleERC721 } from './data/sampleERC721'
7 years ago
tape('Util', function (t) {
t.test('lowerbound', function (st) {
st.plan(7)
let array = [2, 5, 8, 9, 45, 56, 78]
let lowerBound = util.findLowerBound(10, array)
7 years ago
st.equal(lowerBound, 3)
lowerBound = util.findLowerBound(3, array)
st.equal(lowerBound, 0)
lowerBound = util.findLowerBound(100, array)
st.equal(lowerBound, 6)
lowerBound = util.findLowerBound(1, array)
st.equal(lowerBound, -1)
lowerBound = util.findLowerBound(45, array)
st.equal(lowerBound, 4)
array = [2, 5, 8, 9, 9, 45, 56, 78]
lowerBound = util.findLowerBound(9, array)
st.equal(lowerBound, 4)
lowerBound = util.findLowerBound(9, [])
st.equal(lowerBound, -1)
})
})
tape('util.groupBy on valid input', function (t) {
t.plan(1)
const result = util.groupBy([
{category: 'GAS', name: 'a'},
{category: 'SEC', name: 'b'},
{category: 'GAS', name: 'c'}
], 'category')
const expectedResult = {
'GAS': [
{category: 'GAS', name: 'a'},
{category: 'GAS', name: 'c'}
],
'SEC': [
{category: 'SEC', name: 'b'}
]
}
t.deepEqual(result, expectedResult)
})
tape('util.concatWithSeperator valid output', function (t) {
t.plan(4)
t.notEqual(util.concatWithSeperator(['a', 'b', 'c'], ','), 'a, b, c', 'Concat with comma should not produce spaces')
t.equal(util.concatWithSeperator(['a', 'b', 'c'], ','), 'a,b,c', 'Concat with comma should not produce spaces')
t.equal(util.concatWithSeperator(['a', 'b', 'c'], ', '), 'a, b, c', 'Concat with comma space should not produce trailing comma')
t.equal(util.concatWithSeperator(['a', 'b', 'c'], '+'), 'a+b+c', 'Concat with plus')
})
tape('util.escapeRegExp', function (t) {
t.plan(3)
const original = 'function (uint256) returns (bool)'
t.equal(util.escapeRegExp('abcd'), 'abcd', 'String with no regex')
t.equal(util.escapeRegExp(original), 'function \\(uint256\\) returns \\(bool\\)', 'function string with regex')
t.ok(new RegExp(util.escapeRegExp(original)).test(original), 'should still test for original string')
})
7 years ago
tape('util.compareByteCode', function (t) {
2 years ago
t.plan(5)
2 years ago
const address = 'c2a9cef5420203c2672f0e4325cca774893cca98'
const nullAddress = '0000000000000000000000000000000000000000'
const deployedLibraryByteCode = '0x73c2a9cef5420203c2672f0e4325cca774893cca983014608060405260043610610058576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063f26ea02c1461005d575b600080fd5b81801561006957600080fd5b506100886004803603810190808035906020019092919050505061008a565b005b600081600101600060648110151561009e57fe5b600502016002018190555060008160010160006064811015156100bd57fe5b600502016004018190555060008160010160006064811015156100dc57fe5b6005020160030181905550600081600001819055506001816101f501819055816101f601819055506064816101f70181905550505600a165627a7a723058203a6f106db7413fd9cad962bc12ba2327799d6b1334335f7bb854eab04200b3bf0029'
7 years ago
t.ok(util.compareByteCode(deployedLibraryByteCode, deployedLibraryByteCode.replace(address, nullAddress)), 'library bytecode should be the same')
// ballot on ropsten - 0x96d87AB604AEC7FB26C2E046CA5e6eBBB9D8b74D
const code1 = '0x608060405234801561001057600080fd5b50600436106100885760003560e01c8063609ff1bd1161005b578063609ff1bd146101925780639e7b8d61146101b0578063a3ec138d146101f4578063e2ba53f01461029157610088565b80630121b93f1461008d578063013cf08b146100bb5780632e4176cf146101045780635c19a95c1461014e575b600080fd5b6100b9600480360360208110156100a357600080fd5b81019080803590602001909291905050506102af565b005b6100e7600480360360208110156100d157600080fd5b810190808035906020019092919050505061044c565b604051808381526020018281526020019250505060405180910390f35b61010c61047d565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6101906004803603602081101561016457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506104a2565b005b61019a6108be565b6040518082815260200191505060405180910390f35b6101f2600480360360208110156101c657600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610935565b005b6102366004803603602081101561020a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610b36565b60405180858152602001841515151581526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200182815260200194505050505060405180910390f35b610299610b93565b6040518082815260200191505060405180910390f35b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000154141561036d576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f486173206e6f20726967687420746f20766f746500000000000000000000000081525060200191505060405180910390fd5b8060010160009054906101000a900460ff16156103f2576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252600e8152602001807f416c726561647920766f7465642e00000000000000000000000000000000000081525060200191505060405180910390fd5b60018160010160006101000a81548160ff02191690831515021790555081816002018190555080600001546002838154811061042a57fe5b9060005260206000209060020201600101600082825401925050819055505050565b6002818154811061045957fe5b90600052602060002090600202016000915090508060000154908060010154905082565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002090508060010160009054906101000a900460ff161561056a576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260128152602001807f596f7520616c726561647920766f7465642e000000000000000000000000000081525060200191505060405180910390fd5b3373ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141561060c576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601e8152602001807f53656c662d64656c65676174696f6e20697320646973616c6c6f7765642e000081525060200191505060405180910390fd5b5b600073ffffffffffffffffffffffffffffffffffffffff16600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160019054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146107af57600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160019054906101000a900473ffffffffffffffffffffffffffffffffffffffff1691503373ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156107aa576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f466f756e64206c6f6f7020696e2064656c65676174696f6e2e0000000000000081525060200191505060405180910390fd5b61060d565b60018160010160006101000a81548160ff021916908315150217905550818160010160016101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373
const code2 = '0x608060405234801561001057600080fd5b50600436106100885760003560e01c8063609ff1bd1161005b578063609ff1bd146101925780639e7b8d61146101b0578063a3ec138d146101f4578063e2ba53f01461029157610088565b80630121b93f1461008d578063013cf08b146100bb5780632e4176cf146101045780635c19a95c1461014e575b600080fd5b6100b9600480360360208110156100a357600080fd5b81019080803590602001909291905050506102af565b005b6100e7600480360360208110156100d157600080fd5b810190808035906020019092919050505061044c565b604051808381526020018281526020019250505060405180910390f35b61010c61047d565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6101906004803603602081101561016457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506104a2565b005b61019a6108be565b6040518082815260200191505060405180910390f35b6101f2600480360360208110156101c657600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610935565b005b6102366004803603602081101561020a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610b36565b60405180858152602001841515151581526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200182815260200194505050505060405180910390f35b610299610b93565b6040518082815260200191505060405180910390f35b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020905060008160000154141561036d576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f486173206e6f20726967687420746f20766f746500000000000000000000000081525060200191505060405180910390fd5b8060010160009054906101000a900460ff16156103f2576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252600e8152602001807f416c726561647920766f7465642e00000000000000000000000000000000000081525060200191505060405180910390fd5b60018160010160006101000a81548160ff02191690831515021790555081816002018190555080600001546002838154811061042a57fe5b9060005260206000209060020201600101600082825401925050819055505050565b6002818154811061045957fe5b90600052602060002090600202016000915090508060000154908060010154905082565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002090508060010160009054906101000a900460ff161561056a576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260128152602001807f596f7520616c726561647920766f7465642e000000000000000000000000000081525060200191505060405180910390fd5b3373ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141561060c576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601e8152602001807f53656c662d64656c65676174696f6e20697320646973616c6c6f7765642e000081525060200191505060405180910390fd5b5b600073ffffffffffffffffffffffffffffffffffffffff16600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160019054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146107af57600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160019054906101000a900473ffffffffffffffffffffffffffffffffffffffff1691503373ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156107aa576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f466f756e64206c6f6f7020696e2064656c65676174696f6e2e0000000000000081525060200191505060405180910390fd5b61060d565b60018160010160006101000a81548160ff021916908315150217905550818160010160016101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373
t.ok(util.compareByteCode(code1, code2), 'should match even though cbor encoded metadatas are different')
2 years ago
const storageCode1 = '0x608060405234801561001057600080fd5b50610150806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80632e64cec11461003b5780636057361d14610059575b600080fd5b610043610075565b60405161005091906100d9565b60405180910390f35b610073600480360381019061006e919061009d565b61007e565b005b60008054905090565b8060008190555050565b60008135905061009781610103565b92915050565b6000602082840312156100b3576100b26100fe565b5b60006100c184828501610088565b91505092915050565b6100d3816100f4565b82525050565b60006020820190506100ee60008301846100ca565b92915050565b6000819050919050565b600080fd5b61010c816100f4565b811461011757600080fd5b5056fea2646970667358221220c29dfcfdfd91e29bbf21ece97752e17fd3a0c07db30320f5b60b087866d2b7b164736f6c63430008070033'
const storageCode2 = '0x608060405234801561001057600080fd5b506101e8806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80632e64cec11461003b5780636057361d14610059575b600080fd5b610043610075565b60405161005091906100ec565b60405180910390f35b610073600480360381019061006e91906100b0565b61007e565b005b60008054905090565b6000602a90506001826100919190610107565b6000819055505050565b6000813590506100aa8161019b565b92915050565b6000602082840312156100c6576100c5610196565b5b60006100d48482850161009b565b91505092915050565b6100e68161015d565b82525050565b600060208201905061010160008301846100dd565b92915050565b60006101128261015d565b915061011d8361015d565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561015257610151610167565b5b828201905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b6101a48161015d565b81146101af57600080fd5b5056fea264697066735822122096fdfc54e6f9b12cf7a0e5d49289e40f3e9ed7554a1e5485cff008aebc78f27264736f6c63430008070033'
t.ok(!util.compareByteCode(storageCode1, storageCode2), 'should not match')
t.ok(!util.compareByteCode(sampleERC721, ERC721), 'should not match')
2 years ago
const immutableRef1 = '0x6080604052600436106101145760003560e01c806352d1902d116100a057806395d89b411161006457806395d89b4114610380578063a457c2d7146103ab578063a9059cbb146103e8578063dd62ed3e14610425578063f2fde38b1461046257610114565b806352d1902d146102bf57806370a08231146102ea578063715018a6146103275780638129fc1c1461033e5780638da5cb5b1461035557610114565b8063313ce567116100e7578063313ce567146101e95780633659cfe614610214578063395093511461023d57806340c10f191461027a5780634f1ef286146102a357610114565b806306fdde0314610119578063095ea7b31461014457806318160ddd1461018157806323b872dd146101ac575b600080fd5b34801561012557600080fd5b5061012e61048b565b60405161013b919061228a565b60405180910390f35b34801561015057600080fd5b5061016b60048036038101906101669190611e3d565b61051d565b6040516101789190612239565b60405180910390f35b34801561018d57600080fd5b50610196610540565b6040516101a3919061250c565b60405180910390f35b3480156101b857600080fd5b506101d360048036038101906101ce9190611d8e565b61054a565b6040516101e09190612239565b60405180910390f35b3480156101f557600080fd5b506101fe610579565b60405161020b9190612527565b60405180910390f35b34801561022057600080fd5b5061023b60048036038101906102369190611d21565b610582565b005b34801561024957600080fd5b50610264600480360381019061025f9190611e3d565b61070b565b6040516102719190612239565b60405180910390f35b34801561028657600080fd5b506102a1600480360381019061029c9190611e3d565b610742565b005b6102bd60048036038101906102b89190611de1565b610758565b005b3480156102cb57600080fd5b506102d4610895565b6040516102e19190612254565b60405180910390f35b3480156102f657600080fd5b50610311600480360381019061030c9190611d21565b61094e565b60405161031e919061250c565b60405180910390f35b34801561033357600080fd5b5061033c610997565b005b34801561034a57600080fd5b506103536109ab565b005b34801561036157600080fd5b5061036a610b65565b604051610377919061221e565b60405180910390f35b34801561038c57600080fd5b50610395610b8f565b6040516103a2919061228a565b60405180910390f35b3480156103b757600080fd5b506103d260048036038101906103cd9190611e3d565b610c21565b6040516103df9190612239565b60405180910390f35b3480156103f457600080fd5b5061040f600480360381019061040a9190611e3d565b610c98565b60405161041c9190612239565b60405180910390f35b34801561043157600080fd5b5061044c60048036038101906104479190611d4e565b610cbb565b604051610459919061250c565b60405180910390f35b34801561046e57600080fd5b5061048960048036038101906104849190611d21565b610d42565b005b60606036805461049a906126d3565b80601f01602080910402602001604051908101604052809291908181526020018280546104c6906126d3565b80156105135780601f106104e857610100808354040283529160200191610513565b820191906000526020600020905b8154815290600101906020018083116104f657829003601f168201915b5050505050905090565b600080610528610dc6565b9050610535818585610dce565b600191505092915050565b6000603554905090565b600080610555610dc6565b9050610562858285610f99565b61056d858585611025565b60019150509392505050565b60006012905090565b7f000000000000000000000000d9145cce52d386f254917e481eb44e9943f3913873ffffffffffffffffffffffffffffffffffffffff163073ffffffffffffffffffffffffffffffffffffffff161415610611576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016106089061230c565b60405180910390fd5b7f000000000000000000000000d9145cce52d386f254917e481eb44e9943f3913873ffffffffffffffffffffffffffffffffffffffff166106506112a0565b73ffffffffffffffffffffffffffffffffffffffff16146106a6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161069d9061236c565b60405180910390fd5b6106af816112f7565b61070881600067ffffffffffffffff8111156106ce576106cd612794565b5b6040519080825280601f01601f1916602001820160405280156107005781602001600182028036833780820191505090505b506000611302565b50565b600080610716610dc6565b90506107378185856107288589610cbb565b61073291906125ca565b610dce565b600191505092915050565b61074a61147f565b61075482826114fd565b5050565b7f000000000000000000000000d9145cce52d386f254917e481eb44e9943f3913873ffffffffffffffffffffffffffffffffffffffff163073ffffffffffffffffffffffffffffffffffffffff1614156107e7576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016107de9061230c565b60405180910390fd5b7f000000000000000000
const immutableRef2 = '0x6080604052600436106101145760003560e01c806352d1902d116100a057806395d89b411161006457806395d89b4114610380578063a457c2d7146103ab578063a9059cbb146103e8578063dd62ed3e14610425578063f2fde38b1461046257610114565b806352d1902d146102bf57806370a08231146102ea578063715018a6146103275780638129fc1c1461033e5780638da5cb5b1461035557610114565b8063313ce567116100e7578063313ce567146101e95780633659cfe614610214578063395093511461023d57806340c10f191461027a5780634f1ef286146102a357610114565b806306fdde0314610119578063095ea7b31461014457806318160ddd1461018157806323b872dd146101ac575b600080fd5b34801561012557600080fd5b5061012e61048b565b60405161013b919061228a565b60405180910390f35b34801561015057600080fd5b5061016b60048036038101906101669190611e3d565b61051d565b6040516101789190612239565b60405180910390f35b34801561018d57600080fd5b50610196610540565b6040516101a3919061250c565b60405180910390f35b3480156101b857600080fd5b506101d360048036038101906101ce9190611d8e565b61054a565b6040516101e09190612239565b60405180910390f35b3480156101f557600080fd5b506101fe610579565b60405161020b9190612527565b60405180910390f35b34801561022057600080fd5b5061023b60048036038101906102369190611d21565b610582565b005b34801561024957600080fd5b50610264600480360381019061025f9190611e3d565b61070b565b6040516102719190612239565b60405180910390f35b34801561028657600080fd5b506102a1600480360381019061029c9190611e3d565b610742565b005b6102bd60048036038101906102b89190611de1565b610758565b005b3480156102cb57600080fd5b506102d4610895565b6040516102e19190612254565b60405180910390f35b3480156102f657600080fd5b50610311600480360381019061030c9190611d21565b61094e565b60405161031e919061250c565b60405180910390f35b34801561033357600080fd5b5061033c610997565b005b34801561034a57600080fd5b506103536109ab565b005b34801561036157600080fd5b5061036a610b65565b604051610377919061221e565b60405180910390f35b34801561038c57600080fd5b50610395610b8f565b6040516103a2919061228a565b60405180910390f35b3480156103b757600080fd5b506103d260048036038101906103cd9190611e3d565b610c21565b6040516103df9190612239565b60405180910390f35b3480156103f457600080fd5b5061040f600480360381019061040a9190611e3d565b610c98565b60405161041c9190612239565b60405180910390f35b34801561043157600080fd5b5061044c60048036038101906104479190611d4e565b610cbb565b604051610459919061250c565b60405180910390f35b34801561046e57600080fd5b5061048960048036038101906104849190611d21565b610d42565b005b60606036805461049a906126d3565b80601f01602080910402602001604051908101604052809291908181526020018280546104c6906126d3565b80156105135780601f106104e857610100808354040283529160200191610513565b820191906000526020600020905b8154815290600101906020018083116104f657829003601f168201915b5050505050905090565b600080610528610dc6565b9050610535818585610dce565b600191505092915050565b6000603554905090565b600080610555610dc6565b9050610562858285610f99565b61056d858585611025565b60019150509392505050565b60006012905090565b7f000000000000000000000000000000000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff163073ffffffffffffffffffffffffffffffffffffffff161415610611576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016106089061230c565b60405180910390fd5b7f000000000000000000000000000000000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff166106506112a0565b73ffffffffffffffffffffffffffffffffffffffff16146106a6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161069d9061236c565b60405180910390fd5b6106af816112f7565b61070881600067ffffffffffffffff8111156106ce576106cd612794565b5b6040519080825280601f01601f1916602001820160405280156107005781602001600182028036833780820191505090505b506000611302565b50565b600080610716610dc6565b90506107378185856107288589610cbb565b61073291906125ca565b610dce565b600191505092915050565b61074a61147f565b61075482826114fd565b5050565b7f000000000000000000000000000000000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff163073ffffffffffffffffffffffffffffffffffffffff1614156107e7576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016107de9061230c565b60405180910390fd5b7f000000000000000000
t.ok(util.compareByteCode(immutableRef1, immutableRef2), 'code contains immutable refs and should match')
2 years ago
})
tape('util.getInputParameters', function (t) {
t.plan(2)
t.equal(util.getinputParameters(sampleERC721), '000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016500000000000000000000000000000000000000000000000000000000000000')
t.equal(util.getinputParameters(ERC721), '')
7 years ago
})
2 years ago