add tests

This commit is contained in:
AJ ONeal 2017-02-02 21:19:59 -07:00
parent 228c1d096f
commit 99e4dca8a6
3 changed files with 78 additions and 1 deletions

View File

@ -4,7 +4,7 @@
"description": "testing dns",
"main": "dns.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node test/parser.js && node test/packer.js"
},
"repository": {
"type": "git",

6
test/packer.js Normal file
View File

@ -0,0 +1,6 @@
;(function () {
'use strict';
throw new Error("packer tests not written");
}());

71
test/parser.js Normal file
View File

@ -0,0 +1,71 @@
;(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);
});
});
}());