Scripts to run test cases.
This commit is contained in:
parent
6aceee8486
commit
89c4c75e3c
108
tests/run-contract-iterface.js
Normal file
108
tests/run-contract-iterface.js
Normal file
@ -0,0 +1,108 @@
|
||||
'use strict';
|
||||
|
||||
var utils = (function() {
|
||||
var bigNumber = require('../utils/bignumber.js');
|
||||
var convert = require('../utils/convert.js');
|
||||
|
||||
return {
|
||||
arrayify: convert.arrayify,
|
||||
bigNumberify: bigNumber.bigNumberify,
|
||||
};
|
||||
})();
|
||||
|
||||
function equals(a, b) {
|
||||
|
||||
// Array (treat recursively)
|
||||
if (Array.isArray(a)) {
|
||||
if (!Array.isArray(b) || a.length !== b.length) { return false; }
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
if (!equals(a[i], b[i])) { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// BigNumber
|
||||
if (a.eq) {
|
||||
if (!b.eq || !a.eq(b)) { return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
// Uint8Array
|
||||
if (a.buffer) {
|
||||
if (!b.buffer || a.length !== b.length) { return false; }
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
if (a[i] !== b[i]) { return false; }
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Something else
|
||||
return a === b;
|
||||
}
|
||||
|
||||
function getValues(object) {
|
||||
if (Array.isArray(object)) {
|
||||
var result = [];
|
||||
object.forEach(function(object) {
|
||||
result.push(getValues(object));
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
switch (object.type) {
|
||||
case 'number':
|
||||
return utils.bigNumberify(object.value);
|
||||
|
||||
case 'boolean':
|
||||
case 'string':
|
||||
return object.value;
|
||||
|
||||
case 'buffer':
|
||||
return utils.arrayify(object.value);
|
||||
|
||||
default:
|
||||
throw new Error('invalid type - ' + object.type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function testContractInterface(test) {
|
||||
var Interface = require('../contracts/index.js').Interface;
|
||||
|
||||
var testcases = require('./tests/contract-interface.json');
|
||||
testcases.forEach(function(testcase) {
|
||||
var values = getValues(JSON.parse(testcase.normalizedValues));
|
||||
var types = JSON.parse(testcase.types);
|
||||
|
||||
var result = testcase.result;
|
||||
|
||||
try {
|
||||
var encoded = Interface.encodeParams(types, values);
|
||||
test.equal(result, encoded, 'failed to encode data');
|
||||
|
||||
} catch (error) {
|
||||
test.ok(false, 'Failed Encode (' + testcase.name + ') - ' + error.message);
|
||||
}
|
||||
|
||||
try {
|
||||
var decoded = Interface.decodeParams(types, result);
|
||||
|
||||
var decodedArray = [];
|
||||
for (var i = 0; decoded[i] != null; i++) {
|
||||
decodedArray.push(decoded[i]);
|
||||
}
|
||||
|
||||
test.ok(equals(values, decodedArray), 'failed to decode parameters');
|
||||
|
||||
} catch (error) {
|
||||
test.ok(false, 'Failed Decode (' + testcase.name + ') - ' + error.message);
|
||||
}
|
||||
});
|
||||
|
||||
test.done();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
"contract-interface": testContractInterface,
|
||||
}
|
45
tests/run-hdnode.js
Normal file
45
tests/run-hdnode.js
Normal file
@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
var hdnode = require('../hdnode/index.js');
|
||||
|
||||
function testHDNode(test) {
|
||||
var Wallet = require('../wallet/index.js');
|
||||
var c = 0;
|
||||
var testcases = require('./tests/hdnode.json');
|
||||
testcases.forEach(function(testcase) {
|
||||
//if (c++ > 10) { return; }
|
||||
var rootNode = new hdnode.HDNode.fromSeed(testcase.seed);
|
||||
testcase.hdnodes.forEach(function(hdTestcase) {
|
||||
|
||||
var node = rootNode.derivePath(hdTestcase.path);
|
||||
test.equal(node.privateKey, hdTestcase.privateKey,
|
||||
'Failed to generate privateKey - ' + testcase.name);
|
||||
|
||||
var wallet = new Wallet(node.privateKey);
|
||||
test.equal(wallet.address.toLowerCase(), hdTestcase.address,
|
||||
'Failed to generate address - ' + testcase.name);
|
||||
});
|
||||
});
|
||||
|
||||
test.done();
|
||||
}
|
||||
|
||||
function testMnemonic(test) {
|
||||
var c = 0;
|
||||
var testcases = require('./tests/hdnode.json');
|
||||
testcases.forEach(function(testcase) {
|
||||
//if (c++ > 10) { return; }
|
||||
test.equal(hdnode.entropyToMnemonic(testcase.entropy), testcase.mnemonic,
|
||||
'Failed to convert mnemonic - ' + testcase.name);
|
||||
test.equal(hdnode.mnemonicToEntropy(testcase.mnemonic), testcase.entropy,
|
||||
'Failed to convert entropy - ' + testcase.name);
|
||||
test.equal(hdnode.mnemonicToSeed(testcase.mnemonic, testcase.password), testcase.seed,
|
||||
'Failed to convert seed - ' + testcase.name);
|
||||
});
|
||||
test.done();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
"hdnode": testHDNode,
|
||||
"mnemonic": testMnemonic,
|
||||
}
|
65
tests/run-utils.js
Normal file
65
tests/run-utils.js
Normal file
@ -0,0 +1,65 @@
|
||||
'use strict';
|
||||
|
||||
function equals(a, b) {
|
||||
if (Array.isArray(a)) {
|
||||
if (!Array.isArray(b) || a.length !== b.length) {
|
||||
return false;
|
||||
}
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
if (!equals(a[i], b[i])) { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return a === b;
|
||||
}
|
||||
|
||||
function testAddress(test) {
|
||||
var getAddress = require('../utils/address.js').getAddress;
|
||||
|
||||
var testcases = require('./tests/addresses.json');
|
||||
testcases.forEach(function(testcase) {
|
||||
test.equal(getAddress(testcase.address), testcase.checksumAddress, 'getAddress failed to match checsum address');
|
||||
test.equal(getAddress(testcase.address, true), testcase.icapAddress, 'getAddress failed to match ICAP address');
|
||||
});
|
||||
|
||||
test.done();
|
||||
}
|
||||
|
||||
function testRLPCoder(test) {
|
||||
var rlp = require('../utils/rlp.js');
|
||||
|
||||
var testcases = require('./tests/rlp-coder.json');
|
||||
testcases.forEach(function(testcase) {
|
||||
test.equal(rlp.encode(testcase.decoded), testcase.encoded, 'RLP encoding failed - ' + testcase.name);
|
||||
test.ok(equals(rlp.decode(testcase.encoded), testcase.decoded), 'RLP decoding failed - ' + testcase.name);
|
||||
});
|
||||
|
||||
test.done();
|
||||
}
|
||||
|
||||
function testUnits(test) {
|
||||
var units = require('../utils/units.js');
|
||||
var bigNumberify = require('../utils/bignumber.js').bigNumberify;
|
||||
|
||||
var testcases = require('./tests/units.json');
|
||||
testcases.forEach(function(testcase) {
|
||||
var wei = bigNumberify(testcase.wei);
|
||||
var formatting = testcase.format || {};
|
||||
|
||||
test.ok(units.parseEther(testcase.ether).eq(wei),
|
||||
'parsing ether failed - ' + testcase.name);
|
||||
|
||||
test.equal(units.formatEther(wei, formatting), testcase.etherFormat,
|
||||
'formatting wei failed - ' + testcase.name);
|
||||
});
|
||||
|
||||
test.done();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
"address": testAddress,
|
||||
"rlp-coder": testRLPCoder,
|
||||
"units": testUnits,
|
||||
}
|
||||
|
176
tests/run-wallet.js
Normal file
176
tests/run-wallet.js
Normal file
@ -0,0 +1,176 @@
|
||||
'use strict';
|
||||
|
||||
var Wallet = require('../wallet/index.js');
|
||||
|
||||
function testAccounts(test) {
|
||||
|
||||
var testcases = require('./tests/private-keys.json');
|
||||
testcases.forEach(function(testcase) {
|
||||
var wallet = new Wallet(testcase.privateKey);
|
||||
test.equal(wallet.address, testcase.checksumAddress, 'Wallet failed converting private key to an address');
|
||||
});
|
||||
|
||||
test.done();
|
||||
}
|
||||
|
||||
function testBrainWallet(test) {
|
||||
var username = 'ricmoo';
|
||||
var password = 'password';
|
||||
|
||||
Wallet.summonBrainWallet(username, password).then(function(wallet) {
|
||||
test.equal(wallet.address, '0xbed9d2E41BdD066f702C4bDB86eB3A3740101acC', 'wrong wallet generated');
|
||||
test.done();
|
||||
}, function(error) {
|
||||
test.ok(false, 'Failed to generarte brain wallet');
|
||||
test.done();
|
||||
});
|
||||
}
|
||||
|
||||
function testWallets(test) {
|
||||
var utils = require('./make-tests/utils.js');
|
||||
|
||||
var promises = [];
|
||||
|
||||
var testcases = require('./tests/wallets.json');
|
||||
testcases.forEach(function(testcase) {
|
||||
|
||||
test.ok(Wallet.isValidWallet(testcase.json), 'failed to detect secret storage wallet');
|
||||
var promise = Wallet.decrypt(testcase.json, testcase.password).then(function(wallet) {
|
||||
test.equal(wallet.privateKey, testcase.privateKey, 'failed to generate correct private key');
|
||||
test.equal(wallet.address.toLowerCase(), testcase.address, 'failed to generate correct address');
|
||||
}, function(error) {
|
||||
test.ok(false, 'failed to decrypt wallet - ' + testcase.address);
|
||||
});
|
||||
|
||||
promises.push(promise);
|
||||
});
|
||||
|
||||
// A few extra test cases to test encrypting/decrypting
|
||||
['one', 'two', 'three'].forEach(function(i) {
|
||||
var password = 'foobar' + i;
|
||||
var wallet = new Wallet(utils.randomHexString('test-' + i, 32));
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
wallet.encrypt(password).then(function(json) {
|
||||
Wallet.decrypt(json, password).then(function(decryptedWallet) {
|
||||
test.equal(decryptedWallet.address, wallet.address, 'failed to decrypt encrypted wallet - ' + wallet.address);
|
||||
decryptedWallet.encrypt(password).then(function(encryptedWallet) {
|
||||
var parsedWallet = JSON.parse(encryptedWallet);
|
||||
test.equal(decryptedWallet.address.toLowerCase().substring(2), parsedWallet.address, 'failed to re-encrypt wallet - ' + wallet.address);
|
||||
resolve();
|
||||
}, function(error) {
|
||||
console.log(error);
|
||||
reject(new Error('failed to re-encrypt - ' + wallet.address));
|
||||
});
|
||||
}, function(error) {
|
||||
console.log(error);
|
||||
reject('failed to decrypt - ' + wallet.address);
|
||||
});
|
||||
}, function(error) {
|
||||
console.log(error);
|
||||
reject('failed to encrypt - ' + wallet.address);
|
||||
});
|
||||
});
|
||||
|
||||
promises.push(promise);
|
||||
});
|
||||
|
||||
Promise.all(promises).then(function() {
|
||||
test.done();
|
||||
}, function(error) {
|
||||
console.log(error);
|
||||
test.ok(false, 'error occurred');
|
||||
test.done();
|
||||
});
|
||||
}
|
||||
|
||||
function testTransactions(test) {
|
||||
|
||||
var utils = (function() {
|
||||
var bigNumber = require('../utils/bignumber.js');
|
||||
var convert = require('../utils/convert.js');
|
||||
return {
|
||||
isBigNumber: bigNumber.isBigNumber,
|
||||
bigNumberify: bigNumber.bigNumberify,
|
||||
|
||||
getAddress: require('../utils/address.js').getAddress,
|
||||
|
||||
hexlify: convert.hexlify,
|
||||
}
|
||||
})();
|
||||
|
||||
var testcases = require('./tests/transactions.json');
|
||||
testcases.forEach(function(testcase) {
|
||||
var wallet = new Wallet(testcase.privateKey);
|
||||
|
||||
var transaction = {};
|
||||
|
||||
var parsedTransaction = Wallet.parseTransaction(testcase.signedTransaction);
|
||||
|
||||
['nonce', 'gasLimit', 'gasPrice', 'to', 'value', 'data'].forEach(function(key) {
|
||||
var expected = testcase[key];
|
||||
|
||||
var value = parsedTransaction[key];
|
||||
|
||||
if ({gasLimit: 1, gasPrice: 1, value: 1}[key]) {
|
||||
if (utils.isBigNumber(value)) {
|
||||
test.ok(true, 'failed to parse into a big number - ' + key + ' - ' + testcase.name);
|
||||
value = value.toHexString();
|
||||
|
||||
if (!expected || expected === '0x') {
|
||||
expected = '0x00';
|
||||
}
|
||||
|
||||
} else {
|
||||
test.ok(false, 'failed to parse into a big number - ' + key + ' - ' + testcase.name);
|
||||
return;
|
||||
}
|
||||
|
||||
} else if (key === 'nonce') {
|
||||
if (typeof(value) === 'number') {
|
||||
test.ok(true, 'failed to parse into a number - nonce - ' + testcase.name);
|
||||
value = utils.hexlify(value);
|
||||
|
||||
if (!expected || expected === '0x') {
|
||||
expected = '0x00';
|
||||
}
|
||||
|
||||
} else {
|
||||
test.ok(false, 'failed to parse into a number - nonce - ' + testcase.name);
|
||||
return;
|
||||
}
|
||||
|
||||
} else if (key === 'data') {
|
||||
if (!expected) {
|
||||
expected = '0x';
|
||||
}
|
||||
|
||||
} else if (key === 'to') {
|
||||
if (value) {
|
||||
try {
|
||||
utils.getAddress(value);
|
||||
} catch (error) {
|
||||
test.ok(false, 'failed to create checksum address - to - ' + testcase.name);
|
||||
}
|
||||
value = value.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
test.equal(value, expected, 'failed to parse - ' + key + ' - ' + testcase.name);
|
||||
|
||||
transaction[key] = testcase[key];
|
||||
});
|
||||
|
||||
var signedTransaction = wallet.sign(transaction);
|
||||
test.equal(signedTransaction, testcase.signedTransaction,
|
||||
'failed to sign transaction - ' + testcase.name);
|
||||
});
|
||||
|
||||
test.done();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
"accounts": testAccounts,
|
||||
"transactions": testTransactions,
|
||||
"wallets": testWallets,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user