Add MX Test

This commit is contained in:
Drew Warren 2017-02-17 15:37:07 -07:00
parent f44d8eed71
commit 27ddd5f1bf
4 changed files with 64 additions and 2 deletions

View File

@ -13,7 +13,7 @@ exports.DNS_PACKER_TYPE_CNAME = function (ab, dv, total, record) {
total += 2;
// RDATA
// i.e. 127.0.0.1 => 0x7F, 0x00, 0x00, 0x01
// a sequence of labels
record.data.split('.').forEach(function (label) {
cnameLen += 1 + label.length;

41
dns.packer.type.mx.js Normal file
View File

@ -0,0 +1,41 @@
(function (exports) {
'use strict';
// An 'MX' record is a 32-bit value representing the IP address
exports.DNS_PACKER_TYPE_MX = function (ab, dv, total, record) {
if (!record.exchange) {
throw new Error("no exchange for MX record");
}
if (!record.priority) {
// TODO: Check that number is in range 1-64k
throw new Error("no priority for MX record");
}
var mxLen = 2; // 16-bit priority plus label sequence
var rdLenIndex = total;
total += 2; // The space for RDLENGTH is reserved now and set below
dv.setUint16(total, parseInt(record.priority, 10), false);
total += 2;
// RDATA
// 16-bit priority and a sequence of labels as the exchange
record.exchange.split('.').forEach(function (label) {
mxLen += 1 + label.length;
dv.setUint8(total, label.length, false);
total += 1;
label.split('').forEach(function (ch) {
dv.setUint8(total, ch.charCodeAt(0), false);
total += 1;
});
});
// RDLENGTH
dv.setUint16(rdLenIndex, mxLen, false);
return total;
};
}('undefined' !== typeof window ? window : exports));

View File

@ -1,7 +1,7 @@
'use strict';
var packer = require('../dns.packer.type.cname.js').DNS_PACKER_TYPE_CNAME;
var ab = new ArrayBuffer(255); // max len per label is 63, max label sequence is 254 + null terminator
var ab = new ArrayBuffer(254); // max len per label is 63, max label sequence is 254
var dv = new DataView(ab);
var total = 0;

21
test/packer.type.mx.js Normal file
View File

@ -0,0 +1,21 @@
'use strict';
var packer = require('../dns.packer.type.mx.js').DNS_PACKER_TYPE_MX;
var ab = new ArrayBuffer(256); // priority + max len per label is 63, max label sequence is 254
var dv = new DataView(ab);
var total = 0;
[ [ '10:www.example.com'
, [ 0x00, 0x12, 0x00, 0x0A, 0x03, 119, 119, 119, 0x07, 101, 120, 97, 109, 112, 108, 101, 0x03, 99, 111, 109 ].join(' ') ]
].forEach(function (mx) {
total = packer(ab, dv, total, { priority: mx[0].split(':')[0], exchange: mx[0].split(':')[1] });
ab = ab.slice(0, total);
// TODO: Check that total increments appropriately
if (mx[1] !== new Uint8Array(ab).join(' ')) {
console.error("expected: ", mx[1]);
console.error("actual: ", new Uint8Array(ab).join(' '));
process.exit(1);
}
});
console.log('PASS');