pull/1/head
Adria Massanet 8 years ago
parent 128dd31c6f
commit fbfd049f06
  1. 38
      coverageMap.js
  2. 18
      injector.js
  3. 6
      instrumentSolidity.js

@ -5,7 +5,7 @@
*/
const SolidityCoder = require('web3/lib/solidity/coder.js');
const path = require('path');
const keccak = require('keccakjs')
const keccak = require('keccakjs');
/**
* Converts solcover event data into an object that can be
@ -16,10 +16,10 @@ module.exports = class CoverageMap {
constructor() {
this.coverage = {};
this.lineTopics = []
this.functionTopics = []
this.branchTopics = []
this.statementTopics = []
this.lineTopics = [];
this.functionTopics = [];
this.branchTopics = [];
this.statementTopics = [];
}
/**
@ -31,7 +31,6 @@ module.exports = class CoverageMap {
*/
addContract(info, canonicalContractPath) {
this.coverage[canonicalContractPath] = {
l: {},
path: canonicalContractPath,
@ -60,16 +59,15 @@ module.exports = class CoverageMap {
}
const keccakhex = (x => {
var hash = new keccak(256)
hash.update(x)
return hash.digest('hex')
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)"));
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)'));
}
/**
@ -83,26 +81,22 @@ module.exports = class CoverageMap {
for (let idx = 0; idx < events.length; idx++) {
const event = JSON.parse(events[idx]);
if (event.topics.filter( t => this.lineTopics.indexOf(t) >= 0 ).length > 0) {
if (event.topics.filter(t => this.lineTopics.indexOf(t) >= 0).length > 0) {
const data = SolidityCoder.decodeParams(['string', 'uint256'], event.data.replace('0x', ''));
const canonicalContractPath = data[0];
this.coverage[canonicalContractPath].l[data[1].toNumber()] += 1;
} else if (event.topics.filter( t => this.functionTopics.indexOf(t) >= 0 ).length > 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 canonicalContractPath = data[0];
this.coverage[canonicalContractPath].f[data[1].toNumber()] += 1;
} else if (event.topics.filter( t => this.branchTopics.indexOf(t) >= 0 ).length > 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 canonicalContractPath = data[0];
this.coverage[canonicalContractPath].b[data[1].toNumber()][data[2].toNumber()] += 1;
} else if (event.topics.filter( t => this.statementTopics.indexOf(t) >= 0 ).length > 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 canonicalContractPath = data[0];
this.coverage[canonicalContractPath].s[data[1].toNumber()] += 1;
}
}
return Object.assign({}, this.coverage);

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

@ -41,8 +41,8 @@ module.exports = function instrumentSolidity(contractSource, fileName) {
ast = SolidityParser.parse(contract.preprocessed);
const contractStatement = ast['body'].filter( node => { return node.type == 'ContractStatement' });
contract.contractName = contractStatement[0].name
const contractStatement = ast.body.filter(node => node.type === 'ContractStatement');
contract.contractName = contractStatement[0].name;
parse[ast.type](contract, ast);
@ -63,7 +63,7 @@ module.exports = function instrumentSolidity(contractSource, fileName) {
});
retValue.runnableLines = contract.runnableLines;
retValue.contract = contract.instrumented;
retValue.contractName = contractStatement[0].name
retValue.contractName = contractStatement[0].name;
return retValue;
};

Loading…
Cancel
Save