mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-28 08:05:51 +00:00
Merge pull request #355 from ripple/validate-uint-hex-input
Validate UInt* hex input
This commit is contained in:
1675
test/fixtures/uint.json
vendored
Normal file
1675
test/fixtures/uint.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,23 +1,104 @@
|
||||
var assert = require('assert');
|
||||
var UInt128 = require('ripple-lib').UInt128;
|
||||
'use strict';
|
||||
|
||||
describe('UInt', function() {
|
||||
describe('128', function() {
|
||||
describe('#parse_number', function () {
|
||||
it('should create 00000000000000000000000000000000 when called with 0', function () {
|
||||
var val = UInt128.from_number(0);
|
||||
assert.strictEqual(val.to_hex(), '00000000000000000000000000000000');
|
||||
/* eslint-disable max-len */
|
||||
|
||||
const assert = require('assert-diff');
|
||||
const lodash = require('lodash');
|
||||
const ripple = require('ripple-lib');
|
||||
const fixtures = require('./fixtures/uint');
|
||||
|
||||
function resultError(test, result) {
|
||||
function type(e) {
|
||||
return Object.prototype.toString.call(e);
|
||||
}
|
||||
return `Expected ${type(test.input)}: ${test.input} to yield ${type(test.expected)}: ${test.expected === 'null' ? NaN : test.expected}. Actual: ${type(result)}: ${result}`;
|
||||
}
|
||||
|
||||
function makeTests(uIntType) {
|
||||
describe(uIntType, function() {
|
||||
const rippleType = ripple[uIntType];
|
||||
const tests = fixtures[uIntType];
|
||||
|
||||
it('from_json().to_json()', function() {
|
||||
tests['from_json().to_json()'].forEach(function(test) {
|
||||
let result = rippleType.from_json(test.input);
|
||||
assert.strictEqual(result.is_valid(), String(test.expected) !== 'null', `Validity check failed: ${test.input}`);
|
||||
result = result.to_json();
|
||||
|
||||
if (test.expected === 'null') {
|
||||
// XXX
|
||||
// UInt160.to_json() returns NaN rather than null if input is invalid
|
||||
assert.strictEqual(lodash.isNaN(result), true, resultError(test, result));
|
||||
} else {
|
||||
assert.strictEqual(result, test.expected, resultError(test, result));
|
||||
}
|
||||
});
|
||||
it('should create 00000000000000000000000000000001 when called with 1', function () {
|
||||
var val = UInt128.from_number(1);
|
||||
assert.strictEqual(val.to_hex(), '00000000000000000000000000000001');
|
||||
});
|
||||
it('from_json().to_bytes()', function() {
|
||||
tests['from_json().to_bytes()'].forEach(function(test) {
|
||||
const result = rippleType.from_json(test.input);
|
||||
assert.strictEqual(result.is_valid(), String(test.expected) !== 'null', `Validity check failed: ${test.input}`);
|
||||
assert.deepEqual(result.to_bytes(), test.expected, resultError(test, result));
|
||||
});
|
||||
it('should create 000000000000000000000000FFFFFFFF when called with 0xFFFFFFFF', function () {
|
||||
var val = UInt128.from_number(0xFFFFFFFF);
|
||||
assert.strictEqual(val.to_hex(), '000000000000000000000000FFFFFFFF');
|
||||
});
|
||||
it('from_number().to_json()', function() {
|
||||
tests['from_number().to_json()'].forEach(function(test) {
|
||||
let result = rippleType.from_number(test.input);
|
||||
assert.strictEqual(result.is_valid(), String(test.expected) !== 'null', `Validity check failed: ${test.input}`);
|
||||
result = result.to_json();
|
||||
|
||||
if (test.expected === 'null') {
|
||||
// XXX
|
||||
// UInt160.to_json() returns NaN rather than null if input is invalid
|
||||
assert.strictEqual(lodash.isNaN(result), true, resultError(test, result));
|
||||
} else {
|
||||
assert.strictEqual(result, test.expected, resultError(test, result));
|
||||
}
|
||||
});
|
||||
});
|
||||
it('from_number().to_hex()', function() {
|
||||
tests['from_number().to_hex()'].forEach(function(test) {
|
||||
const result = rippleType.from_number(test.input);
|
||||
assert.strictEqual(result.is_valid(), String(test.expected) !== 'null', `Validity check failed: ${test.input}`);
|
||||
assert.strictEqual(result.to_hex(), test.expected, resultError(test, result));
|
||||
});
|
||||
});
|
||||
it('from_generic().to_*()', function() {
|
||||
tests['from_generic().to_*()'].forEach(function(test) {
|
||||
let result = rippleType.from_generic(test.input);
|
||||
|
||||
switch (test.input) {
|
||||
// XXX
|
||||
// from_generic() accepts these as "zero"
|
||||
case 0:
|
||||
case '0':
|
||||
case undefined:
|
||||
switch (test.outputMethod) {
|
||||
case 'to_bytes':
|
||||
test.expected = Array(rippleType.width).fill(0);
|
||||
break;
|
||||
case 'to_json':
|
||||
case 'to_hex':
|
||||
test.expected = Array(rippleType.width * 2).fill(0).join('');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert.strictEqual(result.is_valid(), String(test.expected) !== 'null',
|
||||
`Validity check failed: ${test.input} > ${test.expected}`);
|
||||
|
||||
result = result[test.outputMethod]();
|
||||
|
||||
if (test.expected === 'null') {
|
||||
// XXX
|
||||
// UInt160.to_json() returns NaN rather than null if input is invalid
|
||||
assert.strictEqual(lodash.isNaN(result), true, resultError(test, result));
|
||||
} else {
|
||||
assert.deepEqual(result, test.expected, resultError(test, result));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// vim:sw=2:sts=2:ts=8:et
|
||||
['UInt128', 'UInt160', 'UInt256'].forEach(makeTests);
|
||||
|
||||
Reference in New Issue
Block a user