dns-suite.js/test/packer.js

72 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2017-02-03 04:19:59 +00:00
;(function () {
'use strict';
2017-02-11 22:41:01 +00:00
var fs = require('fs');
//var path = require('path');
2017-02-11 22:41:01 +00:00
var dnsjs = require('../').DNSPacket;
2017-02-16 17:26:15 +00:00
//var dirname = path.join(__dirname, 'fixtures');
//var expected; // shim
2017-02-11 22:41:01 +00:00
var onefile = process.argv[2];
2017-02-16 17:26:15 +00:00
if (!onefile) {
console.error('');
console.error('Usage:');
console.error('node test/packer.js <test/fixtures/packet.type.json>');
console.error('Example:');
console.error('node test/packer.js test/fixtures/www.linode.com.a.json');
console.error('');
process.exit(1);
}
2017-02-11 22:41:01 +00:00
var json = JSON.parse(fs.readFileSync(onefile, 'utf8'));
var ab = dnsjs.write(json).buffer;
2017-02-11 22:41:01 +00:00
//console.log(ab);
//console.log(ui8);
2017-02-18 01:52:15 +00:00
function hexdump(ab) {
var ui8 = new Uint8Array(ab);
var bytecount = 0;
2017-02-18 01:47:21 +00:00
var head = ' 0 1 2 3 4 5 6 7 8 9 A B C D E F';
var trail;
var str = [].slice.call(ui8).map(function (i) {
var h = i.toString(16);
if (h.length < 2) {
h = '0' + h;
}
return h;
}).join('').match(/.{1,2}/g).join(' ').match(/.{1,48}/g).map(function (str) {
var lead = bytecount.toString(16);
bytecount += 16;
2017-02-11 22:41:01 +00:00
2017-02-18 01:47:21 +00:00
while (lead.length < 7) {
lead = '0' + lead;
}
return lead + ' ' + str;
}).join('\n');
trail = ab.byteLength.toString(16);
while (trail.length < 7) {
trail = '0' + trail;
}
2017-02-18 01:52:15 +00:00
return head + '\n' + str + '\n' + trail;
2017-02-11 22:41:01 +00:00
}
console.log('');
console.log('DEBUG with hexdump: ');
console.log('hexdump ' + onefile.replace(/\.[^\.]*$/, '.bin'));
console.log('');
2017-02-18 01:52:15 +00:00
console.log(hexdump(ab));
2017-02-11 22:41:01 +00:00
console.log('');
2017-02-16 21:16:21 +00:00
console.error('');
console.error('!!!');
console.error('Test implementation not complete.');
console.error('!!!');
console.error('');
2017-02-16 17:26:15 +00:00
process.exit(1);
2017-02-03 04:19:59 +00:00
}());