Merge pull request #1 from sc-forks/truffle3

Update to Truffle3, refactor CLI, add CLI unit tests, fix misc bugs
pull/2/head
c-g-e-w-e-k-e- 8 years ago committed by GitHub
commit 4c2c9e5e99
  1. 1
      .eslintignore
  2. 2
      .gitignore
  3. 127
      README.md
  4. 31
      coverageMap.js
  5. 211
      exec.js
  6. 31
      hookIntoEvents.patch
  7. 18
      injector.js
  8. 7
      instrumentSolidity.js
  9. 18
      package.json
  10. 19
      parse.js
  11. 61
      runCoveredTests.js
  12. 212
      test/cli.js
  13. 7
      test/cli/block-gas-limit.js
  14. 22
      test/cli/command-options.js
  15. 8
      test/cli/empty.js
  16. 14
      test/cli/inheritance.js
  17. 17
      test/cli/only-call.js
  18. 16
      test/cli/simple.js
  19. 17
      test/cli/sol-parse-fail.js
  20. 24
      test/cli/testrpc-options.js
  21. 11
      test/cli/truffle-crash.js
  22. 16
      test/cli/truffle-test-fail.js
  23. 6
      test/conditional.js
  24. 4
      test/expressions.js
  25. 99
      test/function.js
  26. 1
      test/if.js
  27. 1
      test/loops.js
  28. 4
      test/sources/cli/Empty.sol
  29. 15
      test/sources/cli/Expensive.sol
  30. 20
      test/sources/cli/Migrations.sol
  31. 12
      test/sources/cli/OnlyCall.sol
  32. 6
      test/sources/cli/Owned.sol
  33. 13
      test/sources/cli/Proxy.sol
  34. 13
      test/sources/cli/Simple.sol
  35. 14
      test/sources/cli/SimpleError.sol
  36. 12
      test/sources/function/chainable-new.sol
  37. 12
      test/sources/function/chainable-value.sol
  38. 11
      test/sources/function/chainable.sol
  39. 9
      test/sources/function/function-call.sol
  40. 4
      test/sources/statements/empty-contract-body.sol
  41. 16
      test/statements.js
  42. 133
      test/util/mockTruffle.js
  43. 4
      test/util/util.js
  44. 10
      test/util/vm.js

@ -0,0 +1 @@
test/cli/truffle-crash.js

2
.gitignore vendored

@ -2,3 +2,5 @@ allFiredEvents
coverage.json coverage.json
coverage/ coverage/
node_modules/ node_modules/
.changelog
.DS_Store

@ -1,75 +1,118 @@
#SolCover # SolCover
![CircleCI Status](https://circleci.com/gh/JoinColony/solcover.svg?style=shield&circle-token=53d5360d290ef593c7bdce505b86ae8b9414e684) ![CircleCI Status](https://circleci.com/gh/JoinColony/solcover.svg?style=shield&circle-token=53d5360d290ef593c7bdce505b86ae8b9414e684)
[![codecov](https://codecov.io/gh/JoinColony/solcover/branch/master/graph/badge.svg)](https://codecov.io/gh/JoinColony/solcover) [![codecov](https://codecov.io/gh/JoinColony/solcover/branch/master/graph/badge.svg)](https://codecov.io/gh/JoinColony/solcover)
### Code coverage for Solidity testing
###Code coverage for Solidity testing
![coverage example](https://cdn-images-1.medium.com/max/800/1*uum8t-31bUaa6dTRVVhj6w.png) ![coverage example](https://cdn-images-1.medium.com/max/800/1*uum8t-31bUaa6dTRVVhj6w.png)
For more details about what this is, how it work and potential limitations, see For more details about what this is, how it works and potential limitations, see
[the accompanying article](https://blog.colony.io/code-coverage-for-solidity-eecfa88668c2). [the accompanying article](https://blog.colony.io/code-coverage-for-solidity-eecfa88668c2).
###Installation and preparation ### Install
From your truffle directory, clone this repo:
``` ```
git clone http://github.com/JoinColony/solcover.git $ npm install --save-dev https://github.com/JoinColony/solcover.git#truffle3
cd solcover
npm install
``` ```
Until [Truffle allows the `--network` flag for the `test` command](https://github.com/ConsenSys/truffle/issues/239), in `truffle.js` you have to set a large gas amount for deployment. While this is set, uninstrumented tests likely won't run correctly, so this should only be set when running the coverage tests. An appropriately modified `truffle.js` might look like ### Run
``` ```
module.exports = { $ ./node_modules/solcover/exec.js
rpc: {
host: 'localhost',
gasPrice: 20e9,
gas: 0xfffffff,
}
};
``` ```
In the future, hopefully just adding the 'coverage' network to `truffle.js` will be enough. This will look like
``` Tests run signficantly slower while coverage is being generated. A 1 to 2 minute delay
between the end of Truffle compilation and the beginning of test execution is possible if your
test suite is large. Large solidity files can also take a while to instrument.
### Configuration
By default, Solcover generates a stub `truffle.js` that accomodates its special gas needs and
connects to a modified version of testrpc on port 8555. If your tests will run on the development network
using a standard `truffle.js` and a testrpc instance with no special options, you shouldn't have to
do any configuration. If your tests depend on logic added to `truffle.js` - for example:
[zeppelin-solidity](https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/truffle.js)
uses the file to expose a babel polyfill that its suite requires - you can override the
default behavior by declaring a coverage network in `truffle.js`. Solcover will use your 'truffle.js'
instead of a dynamically generated one.
**Example coverage network config**
```javascript
module.exports = { module.exports = {
rpc: { networks: {
host: 'localhost', development: {
gasPrice: 20e9, host: "localhost",
port: 8545,
network_id: "*" // Match any network id
}, },
networks:{ coverage: {
"coverage":{ host: "localhost",
gas: 0xfffffff, network_id: "*",
port: 8555, // <-- Use port 8555
gas: 0xfffffffffff, // <-- Use this high gas value
gasPrice: 0x01 // <-- Use this low gas price
} }
} }
} };
``` ```
and will not interfere with normal `truffle test` - or other commands - being run during development.
Note that if you have hardcoded gas costs into your tests, some of them may fail when using SolCover. This is because the instrumentation process increases the gas costs for using the contracts, due to the extra events. If this is the case, then the coverage may be incomplete. To avoid this, using `estimateGas` to estimate your gas costs should be more resilient in most cases.
###Execution You can also create a `.solcover.js` config file in the root directory of your project and specify
some additional options:
+ **port**: <Number> The port you want Solcover to run testrpc on / have truffle connect to.
+ **testrpcOptions**: <String> A string of options to be appended to a command line invocation
of testrpc.
+ Example: `--account="0x89a...b1f',10000" --port 8777`".
+ Note: you should specify the port in your `testrpcOptions` string AND as a `port` option.
+ **testCommand**: <String> By default Solcover runs `truffle test` or `truffle test --network coverage`.
This option lets you run tests some other way: ex: `mocha --timeout 5000`. You
will probably also need to make sure the web3 provider for your tests explicitly connects to the port Solcover's
testrpc is set to run on, e.g:
+ `var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8555"))`
**Example .solcover.js config file**
```
module.exports = {
port: 6545,
testrpcOptions: '-p 6545 -u 0x54fd80d6ae7584d8e9a19fe1df43f04e5282cc43',
testCommand: 'mocha --timeout 5000'
};
```
Firstly, make sure that your contracts in your truffle directory are saved elsewhere too - this script moves them and modifies them to do the instrumentation and allow `truffle` to run the tests with the instrumented contracts. It returns them after the tests are complete, but if something goes wrong, then `originalContracts` in the truffle directory should contain the unmodified contracts. ### Known Issues
SolCover runs its own (modified) `testrpc` to get the coverage data, so make sure that you've not left a previous instance running on port 8545, otherwise the coverage reported will be.... sparse... **Hardcoded gas costs**: If you have hardcoded gas costs into your tests some of them may fail when using SolCover.
This is because the instrumentation process increases the gas costs for using the contracts, due to
the extra events. If this is the case, then the coverage may be incomplete. To avoid this, using
`estimateGas` to estimate your gas costs should be more resilient in most cases.
From inside the SolCover directory, run **Events testing**: Because Solcover injects events into your contracts to log which lines your tests reach,
any tests that ask how many events are fired or where the event sits in the logs array
will probably error while coverage is being generated.
```node ./runCoveredTests.js``` **Using `require` in `migrations.js` files**: Truffle overloads Node's `require` function but
implements a simplified search algorithm for node_modules packages
([see issue #383 at Truffle](https://github.com/trufflesuite/truffle/issues/383)).
Because Solcover copies an instrumented version of your project into a temporary folder, `require`
statements handled by Truffle internally won't resolve correctly.
Upon completion of the tests, open the `./coverage/lcov-report/index.html` file to browse the HTML coverage report. **Coveralls / CodeCov**: These CI services take the Istanbul reports generated by Solcover and display
line coverage. Istanbul's own html report publishes significantly more detail and can show whether
your tests actually reach all the conditional branches in your code. It can be found inside the
`coverage` folder at `index.html` after you run the tool.
###A few, uh, provisos, a, a couple of quid pro quos... ### Examples
It is very likely that there are valid Solidity statements that this tool won't instrument correctly, as it's only been developed against a small number of contracts. If (and when) you find such cases, please raise an issue.
+ **Metacoin**: The default truffle project
+ [HTML reports](https://sc-forks.github.io/metacoin/)
+ [Metacoin with Solcover installed](https://github.com/sc-forks/metacoin) (simple, without configuration)
+ **zeppelin-solidity** at commit 453a19825013a586751b87c67bebd551a252fb50
+ [HTML reports]( https://sc-forks.github.io/zeppelin-solidity/)
+ [Zeppelin with Solcover installed](https://github.com/sc-forks/zeppelin-solidity) (declares own coverage network in truffle.js)
+ **numeraire** at commit 5ac3fa432c6b4192468c95a66e52ca086c804c95
+ [HTML reports](https://sc-forks.github.io/contract/)
+ [Numeraire with Solcover installed](https://github.com/sc-forks/contract) (uses .solcover.js)
###TODO ### TODO
- [ ] **TESTS**
- [ ] Turn into a true command line tool, rather than just a hacked-together script - [ ] Turn into a true command line tool, rather than just a hacked-together script
- [ ] Release on NPM - [ ] Release on NPM
- [ ] Do not modify the `../contract/` directory at all during operation (might need changes to truffle)
- [ ] Support for arbitrary testing commands - [ ] Support for arbitrary testing commands
- [ ] [You tell me](http://github.com/JoinColony/solcover/issues) - [ ] [You tell me](http://github.com/JoinColony/solcover/issues)

@ -5,11 +5,7 @@
*/ */
const SolidityCoder = require('web3/lib/solidity/coder.js'); const SolidityCoder = require('web3/lib/solidity/coder.js');
const path = require('path'); const path = require('path');
const keccak = require('keccakjs');
const lineTopic = 'b8995a65f405d9756b41a334f38d8ff0c93c4934e170d3c1429c3e7ca101014d';
const functionTopic = 'd4ce765fd23c5cc3660249353d61ecd18ca60549dd62cb9ca350a4244de7b87f';
const branchTopic = 'd4cf56ed5ba572684f02f889f12ac42d9583c8e3097802060e949bfbb3c1bff5';
const statementTopic = 'b51abbff580b3a34bbc725f2dc6f736e9d4b45a41293fd0084ad865a31fde0c8';
/** /**
* Converts solcover event data into an object that can be * Converts solcover event data into an object that can be
@ -20,6 +16,10 @@ module.exports = class CoverageMap {
constructor() { constructor() {
this.coverage = {}; this.coverage = {};
this.lineTopics = [];
this.functionTopics = [];
this.branchTopics = [];
this.statementTopics = [];
} }
/** /**
@ -29,6 +29,7 @@ module.exports = class CoverageMap {
* @param {String} canonicalContractPath target file location * @param {String} canonicalContractPath target file location
* @return {Object} coverage map with all values set to zero * @return {Object} coverage map with all values set to zero
*/ */
addContract(info, canonicalContractPath) { addContract(info, canonicalContractPath) {
this.coverage[canonicalContractPath] = { this.coverage[canonicalContractPath] = {
l: {}, l: {},
@ -56,6 +57,17 @@ module.exports = class CoverageMap {
for (let x = 1; x <= Object.keys(info.statementMap).length; x++) { for (let x = 1; x <= Object.keys(info.statementMap).length; x++) {
this.coverage[canonicalContractPath].s[x] = 0; this.coverage[canonicalContractPath].s[x] = 0;
} }
const keccakhex = (x => {
const hash = new keccak(256); // eslint-disable-line new-cap
hash.update(x);
return hash.digest('hex');
});
this.lineTopics.push(keccakhex('__Coverage' + info.contractName + '(string,uint256)'));
this.functionTopics.push(keccakhex('__FunctionCoverage' + info.contractName + '(string,uint256)'));
this.branchTopics.push(keccakhex('__BranchCoverage' + info.contractName + '(string,uint256,uint256)'));
this.statementTopics.push(keccakhex('__StatementCoverage' + info.contractName + '(string,uint256)'));
} }
/** /**
@ -68,19 +80,20 @@ module.exports = class CoverageMap {
generate(events, pathPrefix) { generate(events, pathPrefix) {
for (let idx = 0; idx < events.length; idx++) { for (let idx = 0; idx < events.length; idx++) {
const event = JSON.parse(events[idx]); const event = JSON.parse(events[idx]);
if (event.topics.indexOf(lineTopic) >= 0) {
if (event.topics.filter(t => this.lineTopics.indexOf(t) >= 0).length > 0) {
const data = SolidityCoder.decodeParams(['string', 'uint256'], event.data.replace('0x', '')); const data = SolidityCoder.decodeParams(['string', 'uint256'], event.data.replace('0x', ''));
const canonicalContractPath = data[0]; const canonicalContractPath = data[0];
this.coverage[canonicalContractPath].l[data[1].toNumber()] += 1; this.coverage[canonicalContractPath].l[data[1].toNumber()] += 1;
} else if (event.topics.indexOf(functionTopic) >= 0) { } else if (event.topics.filter(t => this.functionTopics.indexOf(t) >= 0).length > 0) {
const data = SolidityCoder.decodeParams(['string', 'uint256'], event.data.replace('0x', '')); const data = SolidityCoder.decodeParams(['string', 'uint256'], event.data.replace('0x', ''));
const canonicalContractPath = data[0]; const canonicalContractPath = data[0];
this.coverage[canonicalContractPath].f[data[1].toNumber()] += 1; this.coverage[canonicalContractPath].f[data[1].toNumber()] += 1;
} else if (event.topics.indexOf(branchTopic) >= 0) { } else if (event.topics.filter(t => this.branchTopics.indexOf(t) >= 0).length > 0) {
const data = SolidityCoder.decodeParams(['string', 'uint256', 'uint256'], event.data.replace('0x', '')); const data = SolidityCoder.decodeParams(['string', 'uint256', 'uint256'], event.data.replace('0x', ''));
const canonicalContractPath = data[0]; const canonicalContractPath = data[0];
this.coverage[canonicalContractPath].b[data[1].toNumber()][data[2].toNumber()] += 1; this.coverage[canonicalContractPath].b[data[1].toNumber()][data[2].toNumber()] += 1;
} else if (event.topics.indexOf(statementTopic) >= 0) { } else if (event.topics.filter(t => this.statementTopics.indexOf(t) >= 0).length > 0) {
const data = SolidityCoder.decodeParams(['string', 'uint256'], event.data.replace('0x', '')); const data = SolidityCoder.decodeParams(['string', 'uint256'], event.data.replace('0x', ''));
const canonicalContractPath = data[0]; const canonicalContractPath = data[0];
this.coverage[canonicalContractPath].s[data[1].toNumber()] += 1; this.coverage[canonicalContractPath].s[data[1].toNumber()] += 1;

@ -0,0 +1,211 @@
#!/usr/bin/env node
const shell = require('shelljs');
const fs = require('fs');
const reqCwd = require('req-cwd');
const path = require('path');
const childprocess = require('child_process');
const getInstrumentedVersion = require('./instrumentSolidity.js');
const CoverageMap = require('./coverageMap.js');
const istanbul = require('istanbul');
const istanbulCollector = new istanbul.Collector();
const istanbulReporter = new istanbul.Reporter();
// Very high gas block limits / contract deployment limits
const gasLimitString = '0xfffffffffff';
const gasLimitHex = 0xfffffffffff;
const gasPriceHex = 0x01;
const coverage = new CoverageMap();
// Paths
const coverageDir = './coverageEnv'; // Env that instrumented .sols are tested in
// Options
let coverageOption = '--network coverage'; // Default truffle network execution flag
let silence = ''; // Default log level: configurable by --silence
let log = console.log; // Default log level: configurable by --silence
let testrpcProcess; // ref to testrpc server we need to close on exit
let events; // ref to string loaded from 'allFiredEvents'
// --------------------------------------- Utilities -----------------------------------------------
/**
* Removes coverage build artifacts, kills testrpc.
* Exits (1) and prints msg on error, exits (0) otherwise.
* @param {String} err error message
*/
function cleanUp(err) {
log('Cleaning up...');
shell.config.silent = true;
shell.rm('-Rf', `${coverageDir}`);
shell.rm('./allFiredEvents');
if (testrpcProcess) { testrpcProcess.kill(); }
if (err) {
log(`${err}\nExiting without generating coverage...`);
process.exit(1);
} else {
process.exit(0);
}
}
// --------------------------------------- Script --------------------------------------------------
const config = reqCwd.silent('./.solcover.js') || {};
const workingDir = config.dir || '.'; // Relative path to contracts folder
const port = config.port || 8555; // Port testrpc listens on
const accounts = config.accounts || 35; // Number of accounts to testrpc launches with
// Set testrpc options
const defaultRpcOptions = `--gasLimit ${gasLimitString} --accounts ${accounts} --port ${port}`;
const testrpcOptions = config.testrpcOptions || defaultRpcOptions;
// Silence shell and script logging (for solcover's unit tests / CI)
if (config.silent) {
silence = '> /dev/null 2>&1';
log = () => {};
}
// Run modified testrpc with large block limit, on (hopefully) unused port.
// (Changes here should be also be added to the before() block of test/run.js).
if (!config.norpc) {
try {
// NPM installs the testrpc-sc fork at different locations in node_modules based on
// whether testrpc is a pre-existing dependency of the project solcover is being added to
// using (--save-dev).
let command;
if (shell.test('-e', './node_modules/ethereumjs-testrpc-sc')) {
command = './node_modules/ethereumjs-testrpc-sc/bin/testrpc ';
} else {
command = './node_modules/solcover/node_modules/ethereumjs-testrpc-sc/bin/testrpc ';
}
testrpcProcess = childprocess.exec(command + testrpcOptions);
log(`Testrpc launched on port ${port}`);
} catch (err) {
const msg = `There was a problem launching testrpc: ${err}`;
cleanUp(msg);
}
}
// Generate a copy of the target truffle project configured for solcover and save to the coverage
// environment folder.
log('Generating coverage environment');
try {
shell.mkdir(`${coverageDir}`);
shell.cp('-R', `${workingDir}/contracts`, `${coverageDir}`);
shell.cp('-R', `${workingDir}/migrations`, `${coverageDir}`);
shell.cp('-R', `${workingDir}/test`, `${coverageDir}`);
const truffleConfig = reqCwd(`${workingDir}/truffle.js`);
// Coverage network opts specified: copy truffle.js whole to coverage environment
if (truffleConfig.networks.coverage) {
shell.cp(`${workingDir}/truffle.js`, `${coverageDir}/truffle.js`);
// Coverage network opts NOT specified: default to the development network w/ modified
// port, gasLimit, gasPrice. Export the config object only.
} else {
const trufflejs = `
module.exports = {
networks: {
development: {
host: "localhost",
network_id: "*",
port: ${port},
gas: ${gasLimitHex},
gasPrice: ${gasPriceHex}
}
}
};`;
coverageOption = '';
fs.writeFileSync(`${coverageDir}/truffle.js`, trufflejs);
}
} catch (err) {
const msg = ('There was a problem generating the coverage environment: ');
cleanUp(msg + err);
}
// For each contract except migrations.sol:
// 1. Generate file path reference for coverage report
// 2. Load contract as string
// 3. Instrument contract
// 4. Save instrumented contract in the coverage environment folder where covered tests will run
// 5. Add instrumentation info to the coverage map
let currentFile;
try {
shell.ls(`${coverageDir}/contracts/**/*.sol`).forEach(file => {
const migrations = `${coverageDir}/contracts/Migrations.sol`;
if (file !== migrations) {
log('Instrumenting ', file);
currentFile = file;
const contractPath = path.resolve(file);
const canonicalPath = contractPath.split('/coverageEnv').join('');
const contract = fs.readFileSync(contractPath).toString();
const instrumentedContractInfo = getInstrumentedVersion(contract, canonicalPath);
fs.writeFileSync(contractPath, instrumentedContractInfo.contract);
coverage.addContract(instrumentedContractInfo, canonicalPath);
}
});
} catch (err) {
const msg = (`There was a problem instrumenting ${currentFile}: `);
cleanUp(msg + err);
}
// Run solcover's fork of truffle over instrumented contracts in the
// coverage environment folder. Shell cd command needs to be invoked
// as its own statement for command line options to work, apparently.
try {
log('Launching test command (this can take a few seconds)...');
const defaultCommand = `truffle test ${coverageOption} ${silence}`;
const command = config.testCommand || defaultCommand;
shell.cd('./coverageEnv');
shell.exec(command);
shell.cd('./..');
} catch (err) {
cleanUp(err);
}
// Get events fired during instrumented contracts execution.
try {
events = fs.readFileSync('./allFiredEvents').toString().split('\n');
events.pop();
} catch (err) {
const msg =
`
There was an error generating coverage. Possible reasons include:
1. Another application is using port ${port}
2. Truffle crashed because your tests errored
`;
cleanUp(msg + err);
}
// Generate coverage / write coverage report / run istanbul
try {
coverage.generate(events, './contracts');
const json = JSON.stringify(coverage.coverage);
fs.writeFileSync('./coverage.json', json);
istanbulCollector.add(coverage.coverage);
istanbulReporter.add('html');
istanbulReporter.add('lcov');
istanbulReporter.add('text');
istanbulReporter.write(istanbulCollector, true, () => {
log('Istanbul coverage reports generated');
});
} catch (err) {
if (config.testing) {
cleanUp();
} else {
const msg = 'There was a problem generating producing the coverage map / running Istanbul.\n';
cleanUp(msg + err);
}
}
// Finish
cleanUp();

@ -1,31 +0,0 @@
--- opFns.js.orig 2016-12-19 15:10:12.000000000 -0800
+++ opFns.js 2016-12-19 15:15:51.000000000 -0800
@@ -6,6 +6,7 @@
const logTable = require('./logTable.js')
const ERROR = constants.ERROR
const MAX_INT = 9007199254740991
+const fs = require('fs');
// the opcode functions
module.exports = {
@@ -489,7 +490,7 @@
memLength = utils.bufferToInt(memLength)
const numOfTopics = runState.opCode - 0xa0
const mem = memLoad(runState, memOffset, memLength)
- subGas(runState, new BN(numOfTopics * fees.logTopicGas.v + memLength * fees.logDataGas.v))
+ //subGas(runState, new BN(numOfTopics * fees.logTopicGas.v + memLength * fees.logDataGas.v))
// add address
var log = [runState.address]
@@ -497,6 +498,11 @@
// add data
log.push(mem)
+ var toWrite = {};
+ toWrite.address= log[0].toString('hex')
+ toWrite.topics = log[1].map(function(x){return x.toString('hex')})
+ toWrite.data = log[2].toString('hex')
+ fs.appendFileSync('./allFiredEvents', JSON.stringify(toWrite) + '\n')
runState.logs.push(log)
},

@ -6,27 +6,27 @@ injector.callEvent = function injectCallEvent(contract, fileName, injectionPoint
const linecount = (contract.instrumented.slice(0, injectionPoint).match(/\n/g) || []).length + 1; const linecount = (contract.instrumented.slice(0, injectionPoint).match(/\n/g) || []).length + 1;
contract.runnableLines.push(linecount); contract.runnableLines.push(linecount);
contract.instrumented = contract.instrumented.slice(0, injectionPoint) + contract.instrumented = contract.instrumented.slice(0, injectionPoint) +
'Coverage(\'' + fileName + '\',' + linecount + ');\n' + '__Coverage' + contract.contractName + '(\'' + fileName + '\',' + linecount + ');\n' +
contract.instrumented.slice(injectionPoint); contract.instrumented.slice(injectionPoint);
}; };
injector.callFunctionEvent = function injectCallFunctionEvent(contract, fileName, injectionPoint, injection) { injector.callFunctionEvent = function injectCallFunctionEvent(contract, fileName, injectionPoint, injection) {
contract.instrumented = contract.instrumented.slice(0, injectionPoint) + contract.instrumented = contract.instrumented.slice(0, injectionPoint) +
'FunctionCoverage(\'' + fileName + '\',' + injection.fnId + ');\n' + '__FunctionCoverage' + contract.contractName + '(\'' + fileName + '\',' + injection.fnId + ');\n' +
contract.instrumented.slice(injectionPoint); contract.instrumented.slice(injectionPoint);
}; };
injector.callBranchEvent = function injectCallFunctionEvent(contract, fileName, injectionPoint, injection) { injector.callBranchEvent = function injectCallFunctionEvent(contract, fileName, injectionPoint, injection) {
contract.instrumented = contract.instrumented.slice(0, injectionPoint) + contract.instrumented = contract.instrumented.slice(0, injectionPoint) +
(injection.openBracket ? '{' : '') + (injection.openBracket ? '{' : '') +
'BranchCoverage(\'' + fileName + '\',' + injection.branchId + ',' + injection.locationIdx + ')' + '__BranchCoverage' + contract.contractName + '(\'' + fileName + '\',' + injection.branchId + ',' + injection.locationIdx + ')' +
(injection.comma ? ',' : ';') + (injection.comma ? ',' : ';') +
contract.instrumented.slice(injectionPoint); contract.instrumented.slice(injectionPoint);
}; };
injector.callEmptyBranchEvent = function injectCallEmptyBranchEvent(contract, fileName, injectionPoint, injection) { injector.callEmptyBranchEvent = function injectCallEmptyBranchEvent(contract, fileName, injectionPoint, injection) {
contract.instrumented = contract.instrumented.slice(0, injectionPoint) + contract.instrumented = contract.instrumented.slice(0, injectionPoint) +
'else { BranchCoverage(\'' + fileName + '\',' + injection.branchId + ',' + injection.locationIdx + ');}\n' + 'else { __BranchCoverage' + contract.contractName + '(\'' + fileName + '\',' + injection.branchId + ',' + injection.locationIdx + ');}\n' +
contract.instrumented.slice(injectionPoint); contract.instrumented.slice(injectionPoint);
}; };
@ -44,16 +44,16 @@ injector.literal = function injectLiteral(contract, fileName, injectionPoint, in
injector.statement = function injectStatement(contract, fileName, injectionPoint, injection) { injector.statement = function injectStatement(contract, fileName, injectionPoint, injection) {
contract.instrumented = contract.instrumented.slice(0, injectionPoint) + contract.instrumented = contract.instrumented.slice(0, injectionPoint) +
' StatementCoverage(\'' + fileName + '\',' + injection.statementId + ');\n' + ' __StatementCoverage' + contract.contractName + '(\'' + fileName + '\',' + injection.statementId + ');\n' +
contract.instrumented.slice(injectionPoint); contract.instrumented.slice(injectionPoint);
}; };
injector.eventDefinition = function injectEventDefinition(contract, fileName, injectionPoint, injection) { injector.eventDefinition = function injectEventDefinition(contract, fileName, injectionPoint, injection) {
contract.instrumented = contract.instrumented.slice(0, injectionPoint) + contract.instrumented = contract.instrumented.slice(0, injectionPoint) +
'event Coverage(string fileName, uint256 lineNumber);\n' + 'event __Coverage' + contract.contractName + '(string fileName, uint256 lineNumber);\n' +
'event FunctionCoverage(string fileName, uint256 fnId);\n' + 'event __FunctionCoverage' + contract.contractName + '(string fileName, uint256 fnId);\n' +
'event StatementCoverage(string fileName, uint256 statementId);\n' + 'event __StatementCoverage' + contract.contractName + '(string fileName, uint256 statementId);\n' +
'event BranchCoverage(string fileName, uint256 branchId, uint256 locationIdx);\n' + 'event __BranchCoverage' + contract.contractName + '(string fileName, uint256 branchId, uint256 locationIdx);\n' +
contract.instrumented.slice(injectionPoint); contract.instrumented.slice(injectionPoint);
}; };

@ -38,8 +38,13 @@ module.exports = function instrumentSolidity(contractSource, fileName) {
contract.preprocessed = preprocessor.run(contract.source); contract.preprocessed = preprocessor.run(contract.source);
contract.instrumented = contract.preprocessed; contract.instrumented = contract.preprocessed;
ast = SolidityParser.parse(contract.preprocessed); ast = SolidityParser.parse(contract.preprocessed);
const contractStatement = ast.body.filter(node => (node.type === 'ContractStatement' ||
node.type === 'LibraryStatement'));
contract.contractName = contractStatement[0].name;
parse[ast.type](contract, ast); parse[ast.type](contract, ast);
// var result = solparse.parse(contract); // var result = solparse.parse(contract);
@ -59,5 +64,7 @@ module.exports = function instrumentSolidity(contractSource, fileName) {
}); });
retValue.runnableLines = contract.runnableLines; retValue.runnableLines = contract.runnableLines;
retValue.contract = contract.instrumented; retValue.contract = contract.instrumented;
retValue.contractName = contractStatement[0].name;
return retValue; return retValue;
}; };

@ -2,27 +2,33 @@
"name": "solcover", "name": "solcover",
"version": "0.0.1", "version": "0.0.1",
"description": "", "description": "",
"main": "runCoveredTests.js", "bin": {
"solcover": "./exec.js"
},
"directories": { "directories": {
"test": "test" "test": "test"
}, },
"scripts": { "scripts": {
"test": "mocha --timeout 30000", "test": "mocha --timeout 60000",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --timeout 30000" "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --timeout 60000",
"lint": "./node_modules/.bin/eslint --fix ./"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"ethereumjs-testrpc": "https://github.com/ethereumjs/testrpc.git#5ba3c45b2ca306ab589f4a4c649d9afc39042680", "commander": "^2.9.0",
"ethereumjs-testrpc-sc": "https://github.com/sc-forks/testrpc-sc.git",
"istanbul": "^0.4.5", "istanbul": "^0.4.5",
"keccakjs": "^0.2.1",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"req-cwd": "^1.0.1",
"shelljs": "^0.7.4", "shelljs": "^0.7.4",
"sol-explore": "^1.6.2", "sol-explore": "^1.6.2",
"solidity-parser": "0.3.0" "solidity-parser": "0.3.0"
}, },
"devDependencies": { "devDependencies": {
"crypto-js": "^3.1.9-1", "crypto-js": "^3.1.9-1",
"eslint": "^3.13.1", "eslint": "^3.19.0",
"eslint-config-airbnb-base": "^11.0.1", "eslint-config-airbnb-base": "^11.0.1",
"eslint-plugin-import": "^2.2.0", "eslint-plugin-import": "^2.2.0",
"eslint-plugin-mocha": "^4.8.0", "eslint-plugin-mocha": "^4.8.0",
@ -32,6 +38,6 @@
"istanbul": "^0.4.5", "istanbul": "^0.4.5",
"merkle-patricia-tree": "~2.1.2", "merkle-patricia-tree": "~2.1.2",
"mocha": "^3.1.0", "mocha": "^3.1.0",
"solc": "0.4.6" "solc": "0.4.8"
} }
} }

@ -5,7 +5,10 @@ const instrumenter = require('./instrumenter');
// functions where appropriate, which determine where to inject events. // functions where appropriate, which determine where to inject events.
// Assign a dummy function to everything we might see in the AST // Assign a dummy function to everything we might see in the AST
['AssignmentExpression', ['AssemblyAssignment',
'AssemblyItem',
'AssemblyLocalBinding',
'AssignmentExpression',
'BinaryExpression', 'BinaryExpression',
'BlockStatement', 'BlockStatement',
'BreakStatement', 'BreakStatement',
@ -24,12 +27,15 @@ const instrumenter = require('./instrumenter');
'ExpressionStatement', 'ExpressionStatement',
'ForInStatement', 'ForInStatement',
'ForStatement', 'ForStatement',
'FunctionalAssemblyExpression',
'FunctionDeclaration', 'FunctionDeclaration',
'FunctionName', 'FunctionName',
'Identifier', 'Identifier',
'IfStatement', 'IfStatement',
'ImportStatement', 'ImportStatement',
'InformalParameter', 'InformalParameter',
'InlineAssemblyBlock',
'InlineAssemblyStatement',
'IsStatement', 'IsStatement',
'LibraryStatement', 'LibraryStatement',
'Literal', 'Literal',
@ -106,11 +112,14 @@ parse.MemberExpression = function parseMemberExpression(contract, expression) {
}; };
parse.CallExpression = function parseCallExpression(contract, expression) { parse.CallExpression = function parseCallExpression(contract, expression) {
// It looks like in any given chain of call expressions, only the head callee is an Identifier
// node ... and we only want to instrument the statement once.
if (expression.callee.type === 'Identifier') {
instrumenter.instrumentStatement(contract, expression); instrumenter.instrumentStatement(contract, expression);
parse[expression.callee.type](contract, expression.callee); parse[expression.callee.type](contract, expression.callee);
// for (x in expression.arguments){ } else {
// parse[expression.arguments[x].type](contract, expression.arguments[x]) parse[expression.callee.type](contract, expression.callee);
// } }
}; };
parse.UnaryExpression = function parseUnaryExpression(contract, expression) { parse.UnaryExpression = function parseUnaryExpression(contract, expression) {
@ -188,9 +197,11 @@ parse.ContractOrLibraryStatement = function parseContractOrLibraryStatement(cont
}]; }];
} }
if (expression.body) {
expression.body.forEach(construct => { expression.body.forEach(construct => {
parse[construct.type](contract, construct); parse[construct.type](contract, construct);
}); });
}
}; };
parse.ContractStatement = function ParseContractStatement(contract, expression) { parse.ContractStatement = function ParseContractStatement(contract, expression) {

@ -1,61 +0,0 @@
const Web3 = require('web3');
const shell = require('shelljs');
const SolidityCoder = require('web3/lib/solidity/coder.js');
const getInstrumentedVersion = require('./instrumentSolidity.js');
const fs = require('fs');
const path = require('path');
const CoverageMap = require('./coverageMap.js');
const mkdirp = require('mkdirp');
const childprocess = require('child_process');
const coverage = new CoverageMap();
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
// Patch our local testrpc if necessary
if (!shell.test('-e', './node_modules/ethereumjs-vm/lib/opFns.js.orig')) {
console.log('patch local testrpc...');
shell.exec('patch -b ./node_modules/ethereumjs-vm/lib/opFns.js ./hookIntoEvents.patch');
}
// Run the modified testrpc with large block limit
const testrpcProcess = childprocess.exec('./node_modules/ethereumjs-testrpc/bin/testrpc --gasLimit 0xfffffffffffff --gasPrice 0x1');
if (shell.test('-d', '../originalContracts')) {
console.log('There is already an "originalContracts" directory in your truffle directory.\n' +
'This is probably due to a previous solcover failure.\n' +
'Please make sure the ./contracts/ directory contains your contracts (perhaps by copying them from originalContracts), ' +
'and then delete the originalContracts directory.');
process.exit(1);
}
shell.mv('./../contracts/', './../originalContracts/');
shell.mkdir('./../contracts/');
// For each contract in originalContracts, get the instrumented version
shell.ls('./../originalContracts/**/*.sol').forEach(file => {
if (file !== 'originalContracts/Migrations.sol') {
const canonicalContractPath = path.resolve(file);
console.log('instrumenting ', canonicalContractPath);
const contract = fs.readFileSync(canonicalContractPath).toString();
const instrumentedContractInfo = getInstrumentedVersion(contract, canonicalContractPath);
mkdirp.sync(path.dirname(canonicalContractPath.replace('originalContracts', 'contracts')));
fs.writeFileSync(canonicalContractPath.replace('originalContracts', 'contracts'), instrumentedContractInfo.contract);
coverage.addContract(instrumentedContractInfo, canonicalContractPath);
}
});
shell.cp('./../originalContracts/Migrations.sol', './../contracts/Migrations.sol');
shell.rm('./allFiredEvents'); // Delete previous results
shell.exec('truffle test --network test');
const events = fs.readFileSync('./allFiredEvents').toString().split('\n');
events.pop();
// The pop here isn't a bug - there is an empty line at the end of this file, so we
// don't want to include it as an event.
coverage.generate(events, './../originalContracts/');
fs.writeFileSync('./coverage.json', JSON.stringify(coverage.coverage));
shell.exec('./node_modules/istanbul/lib/cli.js report lcov');
testrpcProcess.kill();
shell.rm('-rf', './../contracts');
shell.mv('./../originalContracts', './../contracts');

@ -0,0 +1,212 @@
/* eslint-env node, mocha */
const assert = require('assert');
const shell = require('shelljs');
const fs = require('fs');
const childprocess = require('child_process');
const mock = require('./util/mockTruffle.js');
// shell.test alias for legibility
function pathExists(path) { return shell.test('-e', path); }
// tests run out of memory in CI without this
function collectGarbage() {
if (global.gc) { global.gc(); }
}
describe('cli', () => {
let testrpcProcess = null;
const script = 'node ./exec.js';
const port = 8555;
const config = {
dir: './mock',
port,
testing: true,
silent: true, // <-- Set to false to debug tests
norpc: true,
};
before(() => {
const command = `./node_modules/ethereumjs-testrpc-sc/bin/testrpc --gasLimit 0xfffffffffff --port ${port}`;
testrpcProcess = childprocess.exec(command);
});
afterEach(() => {
mock.remove();
});
after(() => {
testrpcProcess.kill();
});
// #1: The 'config' tests ask exec.js to run testrpc on special ports, the subsequent tests use
// the testrpc launched in the before() block. For some reason config tests fail randomly
// unless they are at the top of the suite. Hard to debug since they pass if logging is turned
// on - there might be a timing issue around resource cleanup or something.
//
// #2: Creating repeated instances of testrpc hits the container memory limit on
// CI so these tests are disabled for that context
it('config with testrpc options string: should generate coverage, cleanup & exit(0)', () => {
if (!process.env.CI) {
const privateKey = '0x3af46c9ac38ee1f01b05f9915080133f644bf57443f504d339082cb5285ccae4';
const balance = '0xfffffffffffffff';
const testConfig = Object.assign({}, config);
testConfig.testrpcOptions = `--account="${privateKey},${balance}" --port 8777`;
testConfig.norpc = false;
testConfig.port = 8777;
// Installed test will process.exit(1) and crash truffle if the test isn't
// loaded with the account specified above
mock.install('Simple.sol', 'testrpc-options.js', testConfig);
shell.exec(script);
assert(shell.error() === null, 'script should not error');
collectGarbage();
}
});
it('config with test command options string: should run test', () => {
if (!process.env.CI) {
assert(pathExists('./allFiredEvents') === false, 'should start without: events log');
const testConfig = Object.assign({}, config);
testConfig.testCommand = 'mocha --timeout 5000 > /dev/null 2>&1';
testConfig.norpc = false;
testConfig.port = 8888;
// Installed test will write a fake allFiredEvents to ./ after 4000ms
// allowing test to pass
mock.install('Simple.sol', 'command-options.js', testConfig);
shell.exec(script);
assert(shell.error() === null, 'script should not error');
collectGarbage();
}
});
it('simple contract: should generate coverage, cleanup & exit(0)', () => {
// Directory should be clean
assert(pathExists('./coverage') === false, 'should start without: coverage');
assert(pathExists('./coverage.json') === false, 'should start without: coverage.json');
// Run script (exits 0);
mock.install('Simple.sol', 'simple.js', config);
shell.exec(script);
assert(shell.error() === null, 'script should not error');
// Directory should have coverage report
assert(pathExists('./coverage') === true, 'script should gen coverage folder');
assert(pathExists('./coverage.json') === true, 'script should gen coverage.json');
// Coverage should be real.
// This test is tightly bound to the function names in Simple.sol
const produced = JSON.parse(fs.readFileSync('./coverage.json', 'utf8'));
const path = Object.keys(produced)[0];
assert(produced[path].fnMap['1'].name === 'test', 'coverage.json should map "test"');
assert(produced[path].fnMap['2'].name === 'getX', 'coverage.json should map "getX"');
collectGarbage();
});
it('contract only uses .call: should generate coverage, cleanup & exit(0)', () => {
// Run against contract that only uses method.call.
assert(pathExists('./coverage') === false, 'should start without: coverage');
assert(pathExists('./coverage.json') === false, 'should start without: coverage.json');
mock.install('OnlyCall.sol', 'only-call.js', config);
shell.exec(script);
assert(shell.error() === null, 'script should not error');
assert(pathExists('./coverage') === true, 'script should gen coverage folder');
assert(pathExists('./coverage.json') === true, 'script should gen coverage.json');
const produced = JSON.parse(fs.readFileSync('./coverage.json', 'utf8'));
const path = Object.keys(produced)[0];
assert(produced[path].fnMap['1'].name === 'addTwo', 'coverage.json should map "addTwo"');
collectGarbage();
});
it('contract uses inheritance: should generate coverage, cleanup & exit(0)', () => {
// Run against a contract that 'is' another contract
assert(pathExists('./coverage') === false, 'should start without: coverage');
assert(pathExists('./coverage.json') === false, 'should start without: coverage.json');
mock.installInheritanceTest(config);
shell.exec(script);
assert(shell.error() === null, 'script should not error');
assert(pathExists('./coverage') === true, 'script should gen coverage folder');
assert(pathExists('./coverage.json') === true, 'script should gen coverage.json');
const produced = JSON.parse(fs.readFileSync('./coverage.json', 'utf8'));
const ownedPath = Object.keys(produced)[0];
const proxyPath = Object.keys(produced)[1];
assert(produced[ownedPath].fnMap['1'].name === 'Owned', 'coverage.json should map "Owned"');
assert(produced[proxyPath].fnMap['1'].name === 'isOwner', 'coverage.json should map "isOwner"');
collectGarbage();
});
it('truffle tests failing: should generate coverage, cleanup & exit(0)', () => {
assert(pathExists('./coverage') === false, 'should start without: coverage');
assert(pathExists('./coverage.json') === false, 'should start without: coverage.json');
// Run with Simple.sol and a failing assertion in a truffle test
mock.install('Simple.sol', 'truffle-test-fail.js', config);
shell.exec(script);
assert(shell.error() === null, 'script should not error');
assert(pathExists('./coverage') === true, 'script should gen coverage folder');
assert(pathExists('./coverage.json') === true, 'script should gen coverage.json');
const produced = JSON.parse(fs.readFileSync('./coverage.json', 'utf8'));
const path = Object.keys(produced)[0];
assert(produced[path].fnMap['1'].name === 'test', 'coverage.json should map "test"');
assert(produced[path].fnMap['2'].name === 'getX', 'coverage.json should map "getX"');
collectGarbage();
});
it('deployment cost > block gasLimit: should generate coverage, cleanup & exit(0)', () => {
// Just making sure Expensive.sol compiles and deploys here.
mock.install('Expensive.sol', 'block-gas-limit.js', config);
shell.exec(script);
assert(shell.error() === null, 'script should not error');
collectGarbage();
});
it('truffle crashes: should generate NO coverage, cleanup and exit(1)', () => {
assert(pathExists('./coverage') === false, 'should start without: coverage');
assert(pathExists('./coverage.json') === false, 'should start without: coverage.json');
// Run with Simple.sol and a syntax error in the truffle test
mock.install('Simple.sol', 'truffle-crash.js', config);
shell.exec(script);
assert(shell.error() !== null, 'script should error');
assert(pathExists('./coverage') !== true, 'script should NOT gen coverage folder');
assert(pathExists('./coverage.json') !== true, 'script should NOT gen coverage.json');
collectGarbage();
});
it('instrumentation errors: should generate NO coverage, cleanup and exit(1)', () => {
assert(pathExists('./coverage') === false, 'should start without: coverage');
assert(pathExists('./coverage.json') === false, 'should start without: coverage.json');
// Run with SimpleError.sol (has syntax error) and working truffle test
mock.install('SimpleError.sol', 'simple.js', config);
shell.exec(script);
assert(shell.error() !== null, 'script should error');
assert(pathExists('./coverage') !== true, 'script should NOT gen coverage folder');
assert(pathExists('./coverage.json') !== true, 'script should NOT gen coverage.json');
collectGarbage();
});
it('no events log produced: should generate NO coverage, cleanup and exit(1)', () => {
// Run contract and test that pass but fire no events
assert(pathExists('./coverage') === false, 'should start without: coverage');
assert(pathExists('./coverage.json') === false, 'should start without: coverage.json');
mock.install('Empty.sol', 'empty.js', config);
shell.exec(script);
assert(shell.error() !== null, 'script should error');
assert(pathExists('./coverage') !== true, 'script should NOT gen coverage folder');
assert(pathExists('./coverage.json') !== true, 'script should NOT gen coverage.json');
collectGarbage();
});
});

@ -0,0 +1,7 @@
/* eslint-env node, mocha */
/* global artifacts, contract */
const Expensive = artifacts.require('./Expensive.sol');
contract('Expensive', () => {
it('should deploy', () => Expensive.deployed());
});

@ -0,0 +1,22 @@
/* eslint-env node, mocha */
const assert = require('assert');
const fs = require('fs');
// Fake event for Simple.sol
const fakeEvent = {
address: '7c548f8a5ba3a37774440587743bb50f58c7e91c',
topics: ['1accf53d733f86cbefdf38d52682bc905cf6715eb3d860be0b5b052e58b0741d'],
data: '0',
};
// Tests whether or not the testCommand option is invoked by exec.js
// Mocha's default timeout is 2000 - here we fake the creation of
// allFiredEvents at 4000.
describe('Test uses mocha', () => {
it('should run "mocha --timeout 5000" successfully', done => {
setTimeout(() => {
fs.writeFileSync('./../allFiredEvents', fakeEvent);
done();
}, 4000);
});
});

@ -0,0 +1,8 @@
/* eslint-env node, mocha */
/* global artifacts, contract */
const Empty = artifacts.require('./Empty.sol');
contract('Empty', () => {
it('should deploy', () => Empty.deployed());
});

@ -0,0 +1,14 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const Owned = artifacts.require('./Owned.sol');
const Proxy = artifacts.require('./Proxy.sol');
contract('Proxy', accounts => {
it('Should compile and run when one contract inherits from another', () => Owned.deployed()
.then(() => Proxy.deployed())
.then(instance => instance.isOwner.call({
from: accounts[0],
}))
.then(val => assert.equal(val, true)));
});

@ -0,0 +1,17 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const OnlyCall = artifacts.require('./OnlyCall.sol');
contract('OnlyCall', accounts => {
it('should return val + 2', done => {
OnlyCall.deployed().then(instance => {
instance.addTwo.call(5, {
from: accounts[0],
}).then(val => {
assert.equal(val, 7);
done();
});
});
});
});

@ -0,0 +1,16 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const Simple = artifacts.require('./Simple.sol');
contract('Simple', () => {
it('should set x to 5', () => {
let simple;
return Simple.deployed().then(instance => {
simple = instance;
return simple.test(5);
})
.then(() => simple.getX.call())
.then(val => assert.equal(val.toNumber(), 5));
});
});

@ -0,0 +1,17 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const Simple = artifacts.require('./Simple.sol');
// This test is constructed correctly but the SimpleError.sol has a syntax error
contract('SimpleError', () => {
it('should set x to 5', () => {
let simple;
return Simple.deployed().then(instance => {
simple = instance;
return simple.test(5);
})
.then(() => simple.getX.call())
.then(val => assert.equal(val, 5));
});
});

@ -0,0 +1,24 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const Simple = artifacts.require('./Simple.sol');
contract('Simple', accounts => {
// Crash truffle if the account loaded in the options string isn't found here.
it('should load with expected account', () => {
if (accounts[0] !== '0xa4860cedd5143bd63f347cab453bf91425f8404f') {
process.exit(1);
}
});
// Generate some coverage so the script doesn't exit(1) because there are no events
it('should set x to 5', () => {
let simple;
return Simple.deployed().then(instance => {
simple = instance;
return simple.test(5);
})
.then(() => simple.getX.call())
.then(val => assert.equal(val.toNumber(), 5));
});
});

@ -0,0 +1,11 @@
/* eslint-env node, mocha */
/* global artifacts, contract */
var Simple = artifacts.require('./Simple.sol');
// This test should break truffle because it has a syntax error.
contract('Simple', () => {
it('should crash', function(){
return Simple.deployed().then.why.
})
})

@ -0,0 +1,16 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const Simple = artifacts.require('./Simple.sol');
contract('Simple', () => {
it('should set x to 5', () => {
let simple;
return Simple.deployed().then(instance => {
simple = instance;
return simple.test(5);
})
.then(() => simple.getX.call())
.then(val => assert.equal(val.toNumber(), 4)); // <-- Wrong result: test fails
});
});

@ -1,6 +1,5 @@
/* eslint-env node, mocha */ /* eslint-env node, mocha */
const solc = require('solc');
const path = require('path'); const path = require('path');
const getInstrumentedVersion = require('./../instrumentSolidity.js'); const getInstrumentedVersion = require('./../instrumentSolidity.js');
const util = require('./util/util.js'); const util = require('./util/util.js');
@ -159,9 +158,10 @@ describe('conditional statements', () => {
}); });
// Solcover has trouble with this case. The conditional coverage strategy relies on being able to // Solcover has trouble with this case. The conditional coverage strategy relies on being able to
// reference the left-hand variable before its value is assigned. Solidity doesn't allow this for 'var'. // reference the left-hand variable before its value is assigned. Solidity doesn't allow this
// for 'var'.
/* it('should cover a variable delcaration assignment by conditional that reaches the alternate', (done) => { /* it('should cover a var decl assignment by conditional that reaches the alternate', (done) => {
const contract = util.getCode('conditional/variable-decl-assignment-alternate.sol'); const contract = util.getCode('conditional/variable-decl-assignment-alternate.sol');
const info = getInstrumentedVersion(contract, filePath); const info = getInstrumentedVersion(contract, filePath);
const coverage = new CoverageMap(); const coverage = new CoverageMap();

@ -3,10 +3,7 @@
const solc = require('solc'); const solc = require('solc');
const getInstrumentedVersion = require('./../instrumentSolidity.js'); const getInstrumentedVersion = require('./../instrumentSolidity.js');
const util = require('./util/util.js'); const util = require('./util/util.js');
const CoverageMap = require('./../coverageMap');
const path = require('path'); const path = require('path');
const vm = require('./util/vm');
const assert = require('assert');
/** /**
* NB: passing '1' to solc as an option activates the optimiser * NB: passing '1' to solc as an option activates the optimiser
@ -15,7 +12,6 @@ const assert = require('assert');
*/ */
describe('generic expressions', () => { describe('generic expressions', () => {
const filePath = path.resolve('./test.sol'); const filePath = path.resolve('./test.sol');
const pathPrefix = './';
it('should compile after instrumenting a single binary expression', () => { it('should compile after instrumenting a single binary expression', () => {
const contract = util.getCode('expressions/single-binary-expression.sol'); const contract = util.getCode('expressions/single-binary-expression.sol');

@ -3,6 +3,10 @@
const solc = require('solc'); const solc = require('solc');
const getInstrumentedVersion = require('./../instrumentSolidity.js'); const getInstrumentedVersion = require('./../instrumentSolidity.js');
const util = require('./util/util.js'); const util = require('./util/util.js');
const path = require('path');
const CoverageMap = require('./../coverageMap');
const vm = require('./util/vm');
const assert = require('assert');
/** /**
* NB: passing '1' to solc as an option activates the optimiser * NB: passing '1' to solc as an option activates the optimiser
@ -10,6 +14,9 @@ const util = require('./util/util.js');
* and passing the error to mocha. * and passing the error to mocha.
*/ */
describe('function declarations', () => { describe('function declarations', () => {
const filePath = path.resolve('./test.sol');
const pathPrefix = './';
it('should compile after instrumenting an ordinary function declaration', () => { it('should compile after instrumenting an ordinary function declaration', () => {
const contract = util.getCode('function/function.sol'); const contract = util.getCode('function/function.sol');
const info = getInstrumentedVersion(contract, 'test.sol'); const info = getInstrumentedVersion(contract, 'test.sol');
@ -37,4 +44,96 @@ describe('function declarations', () => {
const output = solc.compile(info.contract, 1); const output = solc.compile(info.contract, 1);
util.report(output.errors); util.report(output.errors);
}); });
it('should compile after instrumenting a new->constructor-->method chain', () => {
const contract = util.getCode('function/chainable-new.sol');
const info = getInstrumentedVersion(contract, 'test.sol');
const output = solc.compile(info.contract, 1);
util.report(output.errors);
});
it('should compile after instrumenting a constructor call that chains to a method call', () => {
const contract = util.getCode('function/chainable.sol');
const info = getInstrumentedVersion(contract, 'test.sol');
const output = solc.compile(info.contract, 1);
util.report(output.errors);
});
it('should compile after instrumenting a constructor-->method-->value chain', () => {
const contract = util.getCode('function/chainable-value.sol');
const info = getInstrumentedVersion(contract, 'test.sol');
const output = solc.compile(info.contract, 1);
util.report(output.errors);
});
it('should cover a simple invoked function call', done => {
const contract = util.getCode('function/function-call.sol');
const info = getInstrumentedVersion(contract, filePath);
const coverage = new CoverageMap();
coverage.addContract(info, filePath);
vm.execute(info.contract, 'a', []).then(events => {
const mapping = coverage.generate(events, pathPrefix);
assert.deepEqual(mapping[filePath].l, {
7: 1,
});
assert.deepEqual(mapping[filePath].b, {});
assert.deepEqual(mapping[filePath].s, {
1: 1,
});
assert.deepEqual(mapping[filePath].f, {
1: 1,
2: 1,
});
done();
}).catch(done);
});
it('should cover a constructor call that chains to a method call', done => {
const contract = util.getCode('function/chainable.sol');
const info = getInstrumentedVersion(contract, filePath);
const coverage = new CoverageMap();
coverage.addContract(info, filePath);
// The vm runs out of gas here - but we can verify line / statement / fn
// coverage is getting mapped.
vm.execute(info.contract, 'a', []).then(events => {
const mapping = coverage.generate(events, pathPrefix);
assert.deepEqual(mapping[filePath].l, {
9: 0,
});
assert.deepEqual(mapping[filePath].b, {});
assert.deepEqual(mapping[filePath].s, {
1: 0,
});
assert.deepEqual(mapping[filePath].f, {
1: 0,
2: 0,
});
done();
}).catch(done);
});
it('should cover a constructor call that chains to a method call', done => {
const contract = util.getCode('function/chainable-value.sol');
const info = getInstrumentedVersion(contract, filePath);
const coverage = new CoverageMap();
coverage.addContract(info, filePath);
// The vm runs out of gas here - but we can verify line / statement / fn
// coverage is getting mapped.
vm.execute(info.contract, 'a', []).then(events => {
const mapping = coverage.generate(events, pathPrefix);
assert.deepEqual(mapping[filePath].l, {
10: 0,
});
assert.deepEqual(mapping[filePath].b, {});
assert.deepEqual(mapping[filePath].s, {
1: 0,
});
assert.deepEqual(mapping[filePath].f, {
1: 0,
2: 0,
});
done();
}).catch(done);
});
}); });

@ -1,6 +1,5 @@
/* eslint-env node, mocha */ /* eslint-env node, mocha */
const solc = require('solc');
const path = require('path'); const path = require('path');
const getInstrumentedVersion = require('./../instrumentSolidity.js'); const getInstrumentedVersion = require('./../instrumentSolidity.js');
const util = require('./util/util.js'); const util = require('./util/util.js');

@ -1,6 +1,5 @@
/* eslint-env node, mocha */ /* eslint-env node, mocha */
const solc = require('solc');
const path = require('path'); const path = require('path');
const getInstrumentedVersion = require('./../instrumentSolidity.js'); const getInstrumentedVersion = require('./../instrumentSolidity.js');
const util = require('./util/util.js'); const util = require('./util/util.js');

@ -0,0 +1,4 @@
pragma solidity ^0.4.3;
contract Empty {
}

@ -0,0 +1,15 @@
// Cost to deploy Expensive: 0x4e042f
// Block gas limit is: 0x47e7c4
// Should throw out of gas on unmodified truffle
// Should pass solcover truffle
pragma solidity ^0.4.2;
contract Expensive {
mapping (uint => address) map;
function Expensive(){
for(uint i = 0; i < 250; i++ ){
map[i] = address(this);
}
}
}

@ -0,0 +1,20 @@
pragma solidity ^0.4.4;
contract Migrations {
address public owner;
uint public last_completed_migration;
modifier restricted() {if (msg.sender == owner) _;}
function Migrations() {
owner = msg.sender;
}
function setCompleted(uint completed) restricted {
last_completed_migration = completed;
}
function upgrade(address new_address) restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}

@ -0,0 +1,12 @@
/**
* This contract contains a single function that is accessed using method.call
* With an unpatched testrpc it should not generate any events.
*/
pragma solidity ^0.4.3;
contract OnlyCall {
function addTwo(uint val) returns (uint){
val = val + 2;
return val;
}
}

@ -0,0 +1,6 @@
pragma solidity ^0.4.4;
contract Owned {
function Owned() { owner = msg.sender; }
address owner;
}

@ -0,0 +1,13 @@
pragma solidity ^0.4.4;
import "./Owned.sol";
contract Proxy is Owned {
function isOwner() returns (bool) {
if (msg.sender == owner) {
return true;
} else {
return false;
}
}
}

@ -0,0 +1,13 @@
pragma solidity ^0.4.3;
contract Simple {
uint x = 0;
function test(uint val) {
x = x + val;
}
function getX() returns (uint){
return x;
}
}

@ -0,0 +1,14 @@
// This contract should throw a parse error in instrumentSolidity.js
pragma solidity ^0.4.3;
contract SimpleError {
uint x = 0;
function test(uint val) {
x = x + val // <-- no semi-colon
}
function getX() returns (uint){
return x;
}
}

@ -0,0 +1,12 @@
pragma solidity ^0.4.3;
// This is for a test that verifies solcover can instrument a
// chained constructor/method call invoked by the new operator.
contract Chainable {
function chainWith(uint y, uint z) {}
}
contract Test {
function a(){
new Chainable().chainWith(3, 4);
}
}

@ -0,0 +1,12 @@
pragma solidity ^0.4.3;
// This is for a test that verifies solcover can instrument a
// another kind of long CallExpression chain
contract Test {
function paySomeone(address x, address y) payable {
}
function a() payable {
Test(0x00).paySomeone.value(msg.value)(0x00, 0x00);
}
}

@ -0,0 +1,11 @@
pragma solidity ^0.4.3;
// This is for a test that verifies solcover can instrument a
// chained constructor/method call.
contract Test {
function chainWith(uint y, uint z) {}
function a(){
Test(0x00).chainWith(3, 4);
}
}

@ -0,0 +1,9 @@
pragma solidity ^0.4.3;
// This test verifies that an invoked function gets logged as a statement
contract Test {
function loggedAsStatement(uint x) {}
function a(){
loggedAsStatement(5);
}
}

@ -0,0 +1,4 @@
pragma solidity ^0.4.3;
contract Test {
}

@ -110,4 +110,20 @@ describe('generic statements', () => {
done(); done();
}).catch(done); }).catch(done);
}); });
it('should cover an empty bodied contract statement', done => {
const contract = util.getCode('statements/empty-contract-body.sol');
const info = getInstrumentedVersion(contract, filePath);
const coverage = new CoverageMap();
coverage.addContract(info, filePath);
vm.execute(info.contract, null, []).then(events => {
const mapping = coverage.generate(events, pathPrefix);
assert.deepEqual(mapping[filePath].l, {});
assert.deepEqual(mapping[filePath].b, {});
assert.deepEqual(mapping[filePath].s, {});
assert.deepEqual(mapping[filePath].f, {});
done();
}).catch(done);
});
}); });

@ -0,0 +1,133 @@
/*
This file contains utilities for generating a mock truffle project to test solcover's
run script against.
*/
const fs = require('fs');
const shell = require('shelljs');
/**
* Installs mock truffle project at ./mock with a single contract
* and test specified by the params.
* @param {String} contract <contractName.sol> located in /test/sources/cli/
* @param {[type]} test <testName.js> located in /test/cli/
*/
module.exports.install = function install(contract, test, config) {
shell.mkdir('./mock');
shell.mkdir('./mock/contracts');
shell.mkdir('./mock/migrations');
shell.mkdir('./mock/test');
// Mock contracts
if (Array.isArray(contract)) {
contract.forEach(item => {
shell.cp(`./test/sources/cli/${item}`, `./mock/contracts/${item}`);
});
} else {
shell.cp(`./test/sources/cli/${contract}`, `./mock/contracts/${contract}`);
}
shell.cp('./test/sources/cli/Migrations.sol', './mock/contracts/Migrations.sol');
// Mock migrations
const initialMigration = `
let Migrations = artifacts.require('Migrations.sol');
module.exports = function(deployer) {
deployer.deploy(Migrations);
};`;
const contractLocation = `./${contract}`;
const deployContracts = `
var contract = artifacts.require('${contractLocation}');
module.exports = function(deployer) {
deployer.deploy(contract);
};`;
fs.writeFileSync('./mock/migrations/1_initial_migration.js', initialMigration);
fs.writeFileSync('./mock/migrations/2_deploy_contracts.js', deployContracts);
// Mock test
shell.cp(`./test/cli/${test}`, `./mock/test/${test}`);
// Mock truffle.js
const trufflejs = `module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*"
}}};`
;
const configjs = `module.exports = ${JSON.stringify(config)}`;
fs.writeFileSync('./mock/truffle.js', trufflejs);
fs.writeFileSync('./.solcover.js', configjs);
};
/**
* Installs mock truffle project at ./mock with a single contract
* and test specified by the params.
* @param {String} contract <contractName.sol> located in /test/sources/cli/
* @param {[type]} test <testName.js> located in /test/cli/
*/
module.exports.installInheritanceTest = function installInheritanceTest(config) {
shell.mkdir('./mock');
shell.mkdir('./mock/contracts');
shell.mkdir('./mock/migrations');
shell.mkdir('./mock/test');
// Mock contracts
shell.cp('./test/sources/cli/Proxy.sol', './mock/contracts/Proxy.sol');
shell.cp('./test/sources/cli/Owned.sol', './mock/contracts/Owned.sol');
shell.cp('./test/sources/cli/Migrations.sol', './mock/contracts/Migrations.sol');
// Mock migrations
const initialMigration = `
let Migrations = artifacts.require('Migrations.sol');
module.exports = function(deployer) {
deployer.deploy(Migrations);
};`;
const deployContracts = `
var Owned = artifacts.require('./Owned.sol');
var Proxy = artifacts.require('./Proxy.sol');
module.exports = function(deployer) {
deployer.deploy(Owned);
deployer.link(Owned, Proxy);
deployer.deploy(Proxy);
};`;
fs.writeFileSync('./mock/migrations/1_initial_migration.js', initialMigration);
fs.writeFileSync('./mock/migrations/2_deploy_contracts.js', deployContracts);
// Mock test
shell.cp('./test/cli/inheritance.js', './mock/test/inheritance.js');
// Mock truffle.js
const trufflejs = `module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*"
}}};`
;
const configjs = `module.exports = ${JSON.stringify(config)}`;
fs.writeFileSync('./mock/truffle.js', trufflejs);
fs.writeFileSync('./.solcover.js', configjs);
};
/**
* Removes mock truffle project and coverage reports generated by exec.js
*/
module.exports.remove = function remove() {
shell.config.silent = true;
shell.rm('./.solcover.js');
shell.rm('-Rf', 'mock');
shell.rm('-Rf', 'coverage');
shell.rm('coverage.json');
shell.config.silent = false;
};

@ -7,11 +7,11 @@ const path = require('path');
* @return {String} contents of a .sol file * @return {String} contents of a .sol file
*/ */
module.exports.getCode = function getCode(_path) { module.exports.getCode = function getCode(_path) {
return fs.readFileSync(path.join(__dirname, './../sources/' + _path), 'utf8'); return fs.readFileSync(path.join(__dirname, `./../sources/${_path}`), 'utf8');
}; };
module.exports.report = function report(errors) { module.exports.report = function report(errors) {
if (errors) { if (errors) {
throw new Error('Instrumented solidity invalid: ' + errors); throw new Error(`Instrumented solidity invalid: ${errors}`);
} }
}; };

@ -1,5 +1,4 @@
const solc = require('solc'); const solc = require('solc');
const path = require('path');
const VM = require('ethereumjs-vm'); const VM = require('ethereumjs-vm');
const Account = require('ethereumjs-account'); const Account = require('ethereumjs-account');
const Transaction = require('ethereumjs-tx'); const Transaction = require('ethereumjs-tx');
@ -17,12 +16,12 @@ const accountAddress = new Buffer('7caf6f9bc8b3ba5c7824f934c826bd6dc38c8467', 'h
* Source: consensys/eth-lightwallet/lib/txutils.js (line 18) * Source: consensys/eth-lightwallet/lib/txutils.js (line 18)
*/ */
function encodeFunctionTxData(functionName, types, args) { function encodeFunctionTxData(functionName, types, args) {
const fullName = functionName + '(' + types.join() + ')'; const fullName = `${functionName}(${types.join()})`;
const signature = CryptoJS.SHA3(fullName, { const signature = CryptoJS.SHA3(fullName, {
outputLength: 256, outputLength: 256,
}).toString(CryptoJS.enc.Hex).slice(0, 8); }).toString(CryptoJS.enc.Hex).slice(0, 8);
const dataHex = signature + coder.encodeParams(types, args); const dataHex = signature + coder.encodeParams(types, args);
return '0x' + dataHex; return `0x${dataHex}`;
} }
/** /**
@ -37,7 +36,8 @@ function getTypesFromAbi(abi, functionName) {
return json.type; return json.type;
} }
const funcJson = abi.filter(matchesFunctionName)[0]; const funcJson = abi.filter(matchesFunctionName)[0];
return (funcJson.inputs).map(getTypes);
return funcJson ? (funcJson.inputs).map(getTypes) : [];
} }
/** /**
@ -112,7 +112,7 @@ function callMethod(vm, abi, address, functionName, args) {
const tx = new Transaction(options); const tx = new Transaction(options);
tx.sign(new Buffer(secretKey, 'hex')); tx.sign(new Buffer(secretKey, 'hex'));
return new Promise((resolve, reject) => { return new Promise(resolve => {
vm.runTx({ vm.runTx({
tx, tx,
}, (err, results) => { }, (err, results) => {

Loading…
Cancel
Save