From 21053392b4704c58a330f13c75690b0f9bb562a6 Mon Sep 17 00:00:00 2001 From: Lucas Hendren Date: Sat, 16 Mar 2019 23:37:49 -0400 Subject: [PATCH] tests/solidity: WIP added in first set of Op Code tests --- tests/solidity/contracts/Migrations.sol | 23 ++++ tests/solidity/contracts/OpCodes.sol | 116 ++++++++++++++++++ .../migrations/1_initial_migration.js | 5 + .../migrations/2_opCodes_migration.js | 5 + tests/solidity/test/opCodes.js | 26 ++++ tests/solidity/truffle-config.js | 108 ++++++++++++++++ 6 files changed, 283 insertions(+) create mode 100644 tests/solidity/contracts/Migrations.sol create mode 100644 tests/solidity/contracts/OpCodes.sol create mode 100644 tests/solidity/migrations/1_initial_migration.js create mode 100644 tests/solidity/migrations/2_opCodes_migration.js create mode 100644 tests/solidity/test/opCodes.js create mode 100644 tests/solidity/truffle-config.js diff --git a/tests/solidity/contracts/Migrations.sol b/tests/solidity/contracts/Migrations.sol new file mode 100644 index 0000000000..c378ffb028 --- /dev/null +++ b/tests/solidity/contracts/Migrations.sol @@ -0,0 +1,23 @@ +pragma solidity >=0.4.21 <0.6.0; + +contract Migrations { + address public owner; + uint public last_completed_migration; + + constructor() public { + owner = msg.sender; + } + + modifier restricted() { + if (msg.sender == owner) _; + } + + function setCompleted(uint completed) public restricted { + last_completed_migration = completed; + } + + function upgrade(address new_address) public restricted { + Migrations upgraded = Migrations(new_address); + upgraded.setCompleted(last_completed_migration); + } +} diff --git a/tests/solidity/contracts/OpCodes.sol b/tests/solidity/contracts/OpCodes.sol new file mode 100644 index 0000000000..7d4e465a81 --- /dev/null +++ b/tests/solidity/contracts/OpCodes.sol @@ -0,0 +1,116 @@ +pragma solidity >=0.4.21 <0.6.0; +contract OpCodes { + + modifier onlyOwner(address _owner) { + require(msg.sender == _owner); + _; + } + // Add a todo to the list + function test() public { + + //simple_instructions + /*assembly { pop(sub(dup1, mul(dup1, dup1))) }*/ + + //keywords + assembly { pop(address) return(2, byte(2,1)) } + + //label_complex + /*assembly { 7 abc: 8 eq jump(abc) jumpi(eq(7, 8), abc) pop } + assembly { pop(jumpi(eq(7, 8), abc)) jump(abc) }*/ + + //functional + /*assembly { let x := 2 add(7, mul(6, x)) mul(7, 8) add =: x }*/ + + //for_statement + assembly { for { let i := 1 } lt(i, 5) { i := add(i, 1) } {} } + assembly { for { let i := 6 } gt(i, 5) { i := add(i, 1) } {} } + + //no_opcodes_in_strict + assembly { pop(callvalue()) } + + //no_dup_swap_in_strict + /*assembly { swap1() }*/ + + //print_functional + assembly { let x := mul(sload(0x12), 7) } + + //print_if + assembly { if 2 { pop(mload(0)) }} + + //function_definitions_multiple_args + assembly { function f(a, d){ mstore(a, d) } function g(a, d) -> x, y {}} + + //for_statement + assembly { let x := calldatasize() for { let i := 0} lt(i, x) { i := add(i, 1) } { mstore(i, 2) } } + + //keccak256 + assembly { pop(keccak256(0,0)) } + + //returndatasize + assembly { let r := returndatasize } + + //returndatacopy + assembly { returndatacopy(64, 32, 0) } + //byzantium vs const Constantinople + //staticcall + assembly { pop(staticcall(10000, 0x123, 64, 0x10, 128, 0x10)) } + + /*//create2 Constantinople + assembly { pop(create2(10, 0x123, 32, 64)) }*/ + + //shift Constantinople + /*assembly { pop(shl(10, 32)) } + assembly { pop(shr(10, 32)) } + assembly { pop(sar(10, 32)) }*/ + + + //origin + assembly { pop(origin()) } + + //gas + assembly { pop(gas())} + + //msize + assembly { pop(msize())} + + //gasprice + assembly { pop(gasprice())} + + //coinbase + assembly { pop(coinbase())} + + //timestamp + assembly { pop(timestamp())} + + //number + assembly { pop(number())} + + //difficulty + assembly { pop(difficulty())} + + //gaslimit + assembly { pop(gaslimit())} + + //selfdestruct + assembly { selfdestruct(0x02) } + } + + function test_revert() public { + + //revert + assembly{ revert(0, 0) } + } + + function test_invalid() public { + + //revert + assembly{ invalid() } + } + + function test_stop() public { + + //revert + assembly{ stop() } + } + +} diff --git a/tests/solidity/migrations/1_initial_migration.js b/tests/solidity/migrations/1_initial_migration.js new file mode 100644 index 0000000000..ee2135d295 --- /dev/null +++ b/tests/solidity/migrations/1_initial_migration.js @@ -0,0 +1,5 @@ +const Migrations = artifacts.require("Migrations"); + +module.exports = function(deployer) { + deployer.deploy(Migrations); +}; diff --git a/tests/solidity/migrations/2_opCodes_migration.js b/tests/solidity/migrations/2_opCodes_migration.js new file mode 100644 index 0000000000..65c6b6dc14 --- /dev/null +++ b/tests/solidity/migrations/2_opCodes_migration.js @@ -0,0 +1,5 @@ +var OpCodes = artifacts.require("./OpCodes.sol"); + +module.exports = function(deployer) { + deployer.deploy(OpCodes); +}; diff --git a/tests/solidity/test/opCodes.js b/tests/solidity/test/opCodes.js new file mode 100644 index 0000000000..4860cd2408 --- /dev/null +++ b/tests/solidity/test/opCodes.js @@ -0,0 +1,26 @@ +const TodoList = artifacts.require('./OpCodes.sol') +const assert = require('assert') +let contractInstance +const Web3 = require('web3'); +const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); +// const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:9545')); + +contract('OpCodes', (accounts) => { + beforeEach(async () => { + contractInstance = await TodoList.deployed() + }) + it('should add a to-do note successfully with a short text of 20 letters', async () => { + await contractInstance.test() + // await contractInstance.test_revert() + // await contractInstance.test_invalid() + await contractInstance.test_stop() + + }) +// it('should mark one of your to-dos as completed', async () => { +// await contractInstance.addTodo('example') +// await contractInstance.markTodoAsCompleted(0) +// const lastTodoAdded = await contractInstance.todos(accounts[0], 0) +// const isTodoCompleted = lastTodoAdded[3] // 3 is the bool isCompleted value of the todo note +// assert(isTodoCompleted, 'The todo should be true as completed') +// }) +}) diff --git a/tests/solidity/truffle-config.js b/tests/solidity/truffle-config.js new file mode 100644 index 0000000000..c06d8316ff --- /dev/null +++ b/tests/solidity/truffle-config.js @@ -0,0 +1,108 @@ +/** + * Use this file to configure your truffle project. It's seeded with some + * common settings for different networks and features like migrations, + * compilation and testing. Uncomment the ones you need or modify + * them to suit your project as necessary. + * + * More information about configuration can be found at: + * + * truffleframework.com/docs/advanced/configuration + * + * To deploy via Infura you'll need a wallet provider (like truffle-hdwallet-provider) + * to sign your transactions before they're sent to a remote public node. Infura API + * keys are available for free at: infura.io/register + * + * You'll also need a mnemonic - the twelve word phrase the wallet uses to generate + * public/private key pairs. If you're publishing your code to GitHub make sure you load this + * phrase from a file you've .gitignored so it doesn't accidentally become public. + * + */ + +// const HDWalletProvider = require('truffle-hdwallet-provider'); +// const infuraKey = "fj4jll3k....."; +// +// const fs = require('fs'); +// const mnemonic = fs.readFileSync(".secret").toString().trim(); + +// module.exports = { +// /** +// * Networks define how you connect to your ethereum client and let you set the +// * defaults web3 uses to send transactions. If you don't specify one truffle +// * will spin up a development blockchain for you on port 9545 when you +// * run `develop` or `test`. You can ask a truffle command to use a specific +// * network from the command line, e.g +// * +// * $ truffle test --network +// */ +// +// networks: { +// // Useful for testing. The `development` name is special - truffle uses it by default +// // if it's defined here and no other network is specified at the command line. +// // You should run a client (like ganache-cli, geth or parity) in a separate terminal +// // tab if you use this network and you must also set the `host`, `port` and `network_id` +// // options below to some value. +// // +// // development: { +// // host: "127.0.0.1", // Localhost (default: none) +// // port: 8545, // Standard Ethereum port (default: none) +// // network_id: "*", // Any network (default: none) +// // }, +// +// // Another network with more advanced options... +// // advanced: { +// // port: 8777, // Custom port +// // network_id: 1342, // Custom network +// // gas: 8500000, // Gas sent with each transaction (default: ~6700000) +// // gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei) +// // from:
, // Account to send txs from (default: accounts[0]) +// // websockets: true // Enable EventEmitter interface for web3 (default: false) +// // }, +// +// // Useful for deploying to a public network. +// // NB: It's important to wrap the provider as a function. +// // ropsten: { +// // provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/${infuraKey}`), +// // network_id: 3, // Ropsten's id +// // gas: 5500000, // Ropsten has a lower block limit than mainnet +// // confirmations: 2, // # of confs to wait between deployments. (default: 0) +// // timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50) +// // skipDryRun: true // Skip dry run before migrations? (default: false for public nets ) +// // }, +// +// // Useful for private networks +// // private: { +// // provider: () => new HDWalletProvider(mnemonic, `https://network.io`), +// // network_id: 2111, // This network is yours, in the cloud. +// // production: true // Treats this network as if it was a public net. (default: false) +// // } +// }, +// +// // Set default mocha options here, use special reporters etc. +// mocha: { +// // timeout: 100000 +// }, +// +// // Configure your compilers +// compilers: { +// solc: { +// // version: "0.5.1", // Fetch exact version from solc-bin (default: truffle's version) +// // docker: true, // Use "0.5.1" you've installed locally with docker (default: false) +// // settings: { // See the solidity docs for advice about optimization and evmVersion +// // optimizer: { +// // enabled: false, +// // runs: 200 +// // }, +// // evmVersion: "byzantium" +// // } +// } +// } +// } +module.exports = { + networks: { + development: { + host: 'localhost', + port: 8545, + network_id: '*' + } + } +}