2017-10-18 21:28:45 -04:00
|
|
|
'use strict';
|
|
|
|
|
2016-07-26 17:58:17 -04:00
|
|
|
var crypto = require('crypto');
|
2017-10-18 21:28:45 -04:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var zlib = require('zlib');
|
2016-07-26 17:58:17 -04:00
|
|
|
|
2018-07-23 03:02:39 -04:00
|
|
|
var getEthers = require('./utils-ethers');
|
|
|
|
|
2018-06-21 20:24:30 -04:00
|
|
|
var bigNumber = require('../utils/bignumber');
|
|
|
|
var convert = require('../utils/bytes');
|
|
|
|
var keccak256 = require('../utils/keccak256').keccak256;
|
|
|
|
var utf8 = require('../utils/utf8');
|
2016-07-26 17:58:17 -04:00
|
|
|
|
2018-07-23 03:02:39 -04:00
|
|
|
var readFileSync = fs.readFileSync;
|
|
|
|
if (!readFileSync) {
|
|
|
|
var data = require('./dist/tests.json');
|
|
|
|
|
|
|
|
readFileSync = function(filename) {
|
2018-09-04 10:20:31 -04:00
|
|
|
return Buffer.from(data[filename], 'base64');
|
2018-07-23 03:02:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 21:28:45 -04:00
|
|
|
/*
|
2016-07-26 17:58:17 -04:00
|
|
|
function random(lowerRandomInterval, upperOpenInterval) {
|
|
|
|
return lowerRandomInterval + parseInt((upperOpenInterval - lowerRandomInterval) * Math.random());
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomBuffer(length) {
|
|
|
|
var buffer = crypto.randomBytes(length);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomHexString(length) {
|
|
|
|
return '0x' + randomBuffer(length).toString('hex');
|
|
|
|
}
|
2017-10-18 21:28:45 -04:00
|
|
|
*/
|
|
|
|
function randomBytes(seed, lower, upper) {
|
|
|
|
if (!upper) { upper = lower; }
|
|
|
|
|
|
|
|
if (upper === 0 && upper === lower) { return new Uint8Array(0); }
|
|
|
|
|
|
|
|
seed = utf8.toUtf8Bytes(seed);
|
|
|
|
|
|
|
|
var result = convert.arrayify(keccak256(seed));
|
|
|
|
while (result.length < upper) {
|
|
|
|
result = convert.concat([result, keccak256(convert.concat([seed, result]))]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var top = convert.arrayify(keccak256(result));
|
2018-08-21 13:14:21 +02:00
|
|
|
var percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x01000000;
|
2017-10-18 21:28:45 -04:00
|
|
|
|
|
|
|
return result.slice(0, lower + parseInt((upper - lower) * percent));
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomHexString(seed, lower, upper) {
|
|
|
|
return convert.hexlify(randomBytes(seed, lower, upper));
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomNumber(seed, lower, upper) {
|
|
|
|
var top = randomBytes(seed, 3);
|
2018-08-21 13:14:21 +02:00
|
|
|
var percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x01000000;
|
2017-10-18 21:28:45 -04:00
|
|
|
return lower + parseInt((upper - lower) * percent);
|
|
|
|
}
|
|
|
|
|
2016-07-26 17:58:17 -04:00
|
|
|
|
2017-02-24 14:41:24 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-18 21:28:45 -04:00
|
|
|
function saveTests(tag, data) {
|
|
|
|
var filename = path.resolve(__dirname, 'tests', tag + '.json.gz');
|
|
|
|
|
|
|
|
var data = JSON.stringify(data, undefined, ' ') + '\n';
|
|
|
|
fs.writeFileSync(filename, zlib.gzipSync(data));
|
|
|
|
|
|
|
|
console.log('Save testcase: ' + filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadTests(tag) {
|
|
|
|
var filename = path.resolve(__dirname, 'tests', tag + '.json.gz');
|
2018-07-23 03:02:39 -04:00
|
|
|
return JSON.parse(zlib.gunzipSync(readFileSync(filename)));
|
2017-10-18 21:28:45 -04:00
|
|
|
}
|
|
|
|
|
2018-06-20 20:29:54 -04:00
|
|
|
function loadJson(filename) {
|
|
|
|
var filename = path.resolve(__dirname, 'tests', filename);
|
2018-07-23 03:02:39 -04:00
|
|
|
return JSON.parse(readFileSync(filename).toString());
|
2018-06-20 20:29:54 -04:00
|
|
|
}
|
|
|
|
|
2018-07-16 00:19:50 -04:00
|
|
|
function loadText(filename) {
|
|
|
|
var filename = path.resolve(__dirname, filename);
|
2018-07-23 03:02:39 -04:00
|
|
|
return readFileSync(filename).toString();
|
2018-07-16 00:19:50 -04:00
|
|
|
}
|
|
|
|
|
2018-06-20 20:29:54 -04:00
|
|
|
|
2016-07-26 17:58:17 -04:00
|
|
|
module.exports = {
|
2018-07-23 03:02:39 -04:00
|
|
|
getEthers: getEthers,
|
|
|
|
|
2017-10-18 21:28:45 -04:00
|
|
|
randomBytes: randomBytes,
|
2016-07-26 17:58:17 -04:00
|
|
|
randomHexString: randomHexString,
|
2017-10-18 21:28:45 -04:00
|
|
|
randomNumber:randomNumber,
|
|
|
|
|
|
|
|
bigNumberify: bigNumber.bigNumberify,
|
|
|
|
|
2017-02-24 14:41:24 -05:00
|
|
|
equals: equals,
|
2017-10-18 21:28:45 -04:00
|
|
|
isHexString: convert.isHexString,
|
|
|
|
hexlify: convert.hexlify,
|
|
|
|
arrayify: convert.arrayify,
|
2016-07-26 17:58:17 -04:00
|
|
|
|
2017-10-18 21:28:45 -04:00
|
|
|
loadTests: loadTests,
|
|
|
|
saveTests: saveTests,
|
2018-06-20 20:29:54 -04:00
|
|
|
|
|
|
|
loadJson: loadJson,
|
2018-07-16 00:19:50 -04:00
|
|
|
loadText: loadText,
|
2016-07-26 17:58:17 -04:00
|
|
|
}
|