dns-suite.js/test/parser.js

72 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-02-03 04:19:59 +00:00
;(function () {
//'use strict';
require('./convert-fixtures-to-json.js');
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var dnsjs = require('../').DNSPacket;
var expected; // shim
var dirname = path.join(__dirname, 'fixtures');
function deepLike(expected, actual, prevKey) {
prevKey = prevKey || '';
return Object.keys(expected).every(function (key) {
var val = expected[key];
if (val && 'object' === typeof val && !Array.isArray(val)) {
if ('undefined' === typeof actual[key]) {
throw new Error("[" + prevKey + key + "] is missing from actual, but seen in expected");
}
return deepLike(expected[key], actual[key], prevKey + '.');
}
if (Array.isArray(val)) {
// assumes that arrays do not contain non-primitive values (no nested objects or arrays)
return assert.deepEqual(val, actual[key]);
}
if (val !== actual[key]) {
// value should always be a primitive by now (we do not expect functions)
throw new Error("[" + prevKey + key + "] actual is not equal to expected: " + expected[key] + ' : ' + actual[key]);
}
return true;
});
}
fs.readdirSync(dirname).forEach(function (name) {
if (!/\.bin$/.test(name)) {
return;
}
var filename = path.join(dirname, name);
var expected = require(path.join(dirname, name.replace(/\.bin$/, '.json')));
fs.readFile(filename, null, function (err, nb) {
console.log('Testing ' + filename);
var ab = nb.buffer;
var result;
try {
result = dnsjs.parse(ab);
} catch(e) {
console.log(ab);
console.error('[Error] ' + e.message);
console.error('');
//console.error(e.stack);
return;
}
// TODO deepHas
// compare two objects and make sure that the second has all of what the first has (but perhaps more)
deepLike(expected, result);
});
});
}());