From 121d104d46b2f640e1c1410c1f507c0587f0a9c9 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Thu, 19 Oct 2017 02:51:20 -0400 Subject: [PATCH] Removed old nodeunit test cases. --- tests/run-contract-interface.js | 110 ------------------ tests/run-hdnode.js | 45 -------- tests/run-utils.js | 120 ------------------- tests/run-wallet.js | 197 -------------------------------- 4 files changed, 472 deletions(-) delete mode 100644 tests/run-contract-interface.js delete mode 100644 tests/run-hdnode.js delete mode 100644 tests/run-utils.js delete mode 100644 tests/run-wallet.js diff --git a/tests/run-contract-interface.js b/tests/run-contract-interface.js deleted file mode 100644 index eaddbdeeb..000000000 --- a/tests/run-contract-interface.js +++ /dev/null @@ -1,110 +0,0 @@ -'use strict'; - -var utils = (function() { - var bigNumber = require('../utils/bignumber'); - var convert = require('../utils/convert'); - - return { - arrayify: convert.arrayify, - bigNumberify: bigNumber.bigNumberify, - - isHexString: convert.isHexString, - }; -})(); - - -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 (!a.eq(b)) { return false; } - return true; - } - - // Uint8Array - if (a.buffer) { - if (!utils.isHexString(b)) { return false; } - b = utils.arrayify(b); - - 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 - ' + testcase.name); - - } catch (error) { - test.ok(false, 'Failed Encode (' + testcase.name + ') - ' + error.message); - } - - try { - var decoded = Interface.decodeParams(types, result); - - var decodedArray = Array.prototype.slice.call(decoded);; - - test.ok(equals(values, decodedArray), 'failed to decode parameters - ' + testcase.name); - - } catch (error) { - test.ok(false, 'Failed Decode (' + testcase.name + ') - ' + error.message); - } - }); - - test.done(); -} - -module.exports = { - "contract-interface": testContractInterface, -} diff --git a/tests/run-hdnode.js b/tests/run-hdnode.js deleted file mode 100644 index a19a4b96f..000000000 --- a/tests/run-hdnode.js +++ /dev/null @@ -1,45 +0,0 @@ -'use strict'; - -var HDNode = require('../wallet/hdnode'); - -function testHDNode(test) { - var Wallet = require('../wallet/wallet'); - var c = 0; - var testcases = require('./tests/hdnode.json'); - testcases.forEach(function(testcase) { - //if (c++ > 10) { return; } - var rootNode = new 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, -} diff --git a/tests/run-utils.js b/tests/run-utils.js deleted file mode 100644 index a66a2debd..000000000 --- a/tests/run-utils.js +++ /dev/null @@ -1,120 +0,0 @@ -'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 testContractAddress(test) { - - // @TODO: Mine a large collection of these from the blockchain - - var getContractAddress = require('../utils/contract-address.js').getContractAddress; - - // Transaction: 0x939aa17985bc2a52a0c1cba9497ef09e092355a805a8150e30e24b753bac6864 - var transaction = { - from: '0xb2682160c482eb985ec9f3e364eec0a904c44c23', - nonce: 10, - } - - test.equal( - getContractAddress(transaction), - "0x3474627D4F63A678266BC17171D87f8570936622", - 'Failed to match contract address' - ) - - // Ropsten: 0x5bdfd14fcc917abc2f02a30721d152a6f147f09e8cbaad4e0d5405d646c5c3e1 - transaction = { - from: '0xc6af6e1a78a6752c7f8cd63877eb789a2adb776c', - nonce: 0 - }; - - test.equal( - getContractAddress(transaction), - '0x0CcCC7507aEDf9FEaF8C8D731421746e16b4d39D', - 'zero nonce' - ); - - 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(); -} - -function testNamehash(test) { - var namehash = require('../utils/namehash'); - - var testcases = require('./tests/namehash.json'); - testcases.forEach(function(testcase) { - test.equal(namehash(testcase.name), testcase.expected, 'namehash(' + testcase.name + ')'); - }); - test.done(); -} - -function testId(test) { - var id = require('../utils/id'); - - var sig = id('setAddr(bytes32,address)').substring(0, 10); - test.equal(sig, '0xd5fa2b00', 'id("setAddr(bytes32,address)")') - - test.done(); -} - -module.exports = { - "address": testAddress, - "contract-address": testContractAddress, - 'namehash': testNamehash, - 'id': testId, - "rlp-coder": testRLPCoder, - "units": testUnits, -} - diff --git a/tests/run-wallet.js b/tests/run-wallet.js deleted file mode 100644 index 4083df3cc..000000000 --- a/tests/run-wallet.js +++ /dev/null @@ -1,197 +0,0 @@ -'use strict'; - -var Wallet = require('../wallet/wallet.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.fromBrainWallet(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) { - // Currently removed support for crowdsale wallets; will add this back soon. - if (testcase.type === 'crowdsale') { return; } - - test.ok(Wallet.isEncryptedWallet(testcase.json), 'failed to detect secret storage wallet'); - var promise = Wallet.fromEncryptedWallet(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.fromEncryptedWallet(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, 'parse into a big number - ' + key + ' - ' + testcase.name); - value = value.toHexString(); - - if (!expected || expected === '0x') { - expected = '0x00'; - } - - } else { - test.ok(false, 'parse into a big number - ' + key + ' - ' + testcase.name); - return; - } - - } else if (key === 'nonce') { - if (typeof(value) === 'number') { - test.ok(true, 'parse into a number - nonce - ' + testcase.name); - value = utils.hexlify(value); - - if (!expected || expected === '0x') { - expected = '0x00'; - } - - } else { - test.ok(false, '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, 'create checksum address - to - ' + testcase.name); - } - value = value.toLowerCase(); - } - } - - test.equal(value, expected, 'parse - ' + key + ' - ' + testcase.name); - - transaction[key] = testcase[key]; - }); - - test.equal(parsedTransaction.from, utils.getAddress(testcase.accountAddress), 'compute - from - ' + testcase.name); - - test.equal(parsedTransaction.chainId, 0, 'parse - chainId - ' + testcase.name); - - var signedTransaction = wallet.sign(transaction); - test.equal(signedTransaction, testcase.signedTransaction, 'sign transaction - ' + testcase.name); - - // EIP155 - - var parsedTransactionChainId5 = Wallet.parseTransaction(testcase.signedTransactionChainId5); - ['data', 'from', 'nonce', 'to'].forEach(function (key) { - test.equal(parsedTransaction[key], parsedTransactionChainId5[key], 'eip155 parse - ' + key + ' - ' + testcase.name); - }); - ['gasLimit', 'gasPrice', 'value'].forEach(function (key) { - test.ok(parsedTransaction[key].eq(parsedTransactionChainId5[key]), 'eip155 parse - ' + key + ' - ' + testcase.name); - }); - test.equal(parsedTransactionChainId5.chainId, 5, 'eip155 parse - chainId - ' + testcase.name); - - transaction.chainId = 5; - var signedTransactionChainId5 = wallet.sign(transaction); - test.equal(signedTransactionChainId5, testcase.signedTransactionChainId5, 'eip155 sign transaction - ' + testcase.name); - }); - - test.done(); -} - -module.exports = { - "accounts": testAccounts, - "brainwallet": testBrainWallet, - "transactions": testTransactions, - "wallets": testWallets, -} -