775ac7b58c
you must login with an BTP account in order to see the app
58 lines
2.5 KiB
JavaScript
58 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
var assert = require('assert');
|
|
var constants = require('./constants');
|
|
var util = require('util');
|
|
var bufferUtil = require('./buffer-utils');
|
|
|
|
|
|
module.exports = {
|
|
validateString: function (value, name) {
|
|
assert(typeof value === 'string' || (value instanceof String), util.format('%s should be a string', name));
|
|
assert(value.length !== 0, util.format('%s should not be empty', name));
|
|
},
|
|
|
|
validateHex: function (value, name) {
|
|
module.exports.validateString(value, name);
|
|
assert(/^[0-9A-F]+$/i.test(value), util.format('%s does not represent a valid hex', name));
|
|
assert(value.length % 2 === 0, 'Hex string must contain even number of characters');
|
|
},
|
|
|
|
validateASCII: function (value, name) {
|
|
var buffer = bufferUtil.create(value, 'utf8');
|
|
assert(value.length === buffer.length, util.format('%s should contain only ASCII characters', name));
|
|
},
|
|
|
|
validateObject: function (value, name) {
|
|
var isObject = value && typeof value === 'object' && !Array.isArray(value);
|
|
assert(isObject, util.format('%s should be an object', name));
|
|
},
|
|
|
|
validateBufferLength: function (buffer, fieldDisplayName, start, length) {
|
|
assert(buffer.length >= start + length,
|
|
util.format('Cannot read/write to field %s starting at byte %d with length %d byte(s). Passport length is %d byte(s).',
|
|
fieldDisplayName, start, length, buffer.length));
|
|
},
|
|
|
|
validatePreviousComponent: function (value) {
|
|
var descriptor = require('./field-descriptors').fields.PREVIOUS_COMPONENT;
|
|
var name = descriptor.name;
|
|
module.exports.validateString(value, name);
|
|
module.exports.validateASCII(value, name);
|
|
var maxLength = descriptor.length;
|
|
assert(value.length <= maxLength, util.format('%s should be not more than %d characters', name, maxLength));
|
|
},
|
|
|
|
validateConnectionID: function (value) {
|
|
var name = require('./field-descriptors').fields.CONNECTION_ID.name;
|
|
module.exports.validateHex(value, name);
|
|
assert(value.length === 32, util.format('%s should contain 32 hexadecimal characters (16 bytes)', name));
|
|
},
|
|
|
|
validateConnectionCounter: function (value) {
|
|
var name = require('./field-descriptors').fields.CONNECTION_COUNTER.name;
|
|
assert(typeof value === 'number' || (value instanceof Number), util.format('%s should be a number', name));
|
|
assert(value % 1 === 0, util.format('%s should be an integer', name));
|
|
assert(value <= constants.UINT_32_MAX, util.format('%s should not be greater than a 4-byte integer', name));
|
|
}
|
|
};
|