ethers.js/tests/utils.js

143 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
2016-07-26 17:58:17 -04:00
var crypto = require('crypto');
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');
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) {
return Buffer.from(data[filename], 'base64');
2018-07-23 03:02:39 -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');
}
*/
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;
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;
return lower + parseInt((upper - lower) * percent);
}
2016-07-26 17:58:17 -04: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;
}
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)));
}
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-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
}
2016-07-26 17:58:17 -04:00
module.exports = {
2018-07-23 03:02:39 -04:00
getEthers: getEthers,
randomBytes: randomBytes,
2016-07-26 17:58:17 -04:00
randomHexString: randomHexString,
randomNumber:randomNumber,
bigNumberify: bigNumber.bigNumberify,
equals: equals,
isHexString: convert.isHexString,
hexlify: convert.hexlify,
arrayify: convert.arrayify,
2016-07-26 17:58:17 -04:00
loadTests: loadTests,
saveTests: saveTests,
loadJson: loadJson,
2018-07-16 00:19:50 -04:00
loadText: loadText,
2016-07-26 17:58:17 -04:00
}