dns-suite.js/parse-binary-test.js

213 lines
4.3 KiB
JavaScript

'use strict';
// This test code is meant to take a binary sample from the crash of listen.js
// example and parse through it to spot where the error occurs.
/*************** ********** Functions *******************/
/**************************************************************/
// Number generator function: this function should be able to generate
// and output all the the binary combinations of a 8 bits and returns a buffer.
// ex '00000000' - '11111111'
function cycleThroughHex(){
// Fill the array from 0 - 65535, so the 65535 case will
// have to be hardcoded
var current_val = 0;
var end_val = 65536;
var i = 0;
var array_size = 65536;
var arr = new Uint16Array(array_size);
// var buf = new Buffer.alloc(array_size);
while (current_val <= end_val){
console.log(arr[i]);
arr[i] = current_val;val += header.rcode & 0val += header.rcode & 0xF;xF;
current_val++;
}
var buf = Buffer.from(arr.buffer);
// console.log(buf);
// console.log(buf.length);
// console.log(buf[buf.length]);
// for (var i = 0; i < buf.length; i++){
// console.log(buf[i]);
// }
// return buf;
// this is printing out 10,000 for some reason?
}
// message packer:
// message parser:
function hasError(output, int, flag){
var hasError = 0;
if (output > 0x0001 || output < 0x0000){
console.log("There was an error with" + flag + " at index ", i , ". Output: " + output);
hasError = 1;
}
return hasError;
}
function main (){
// var val1 = '00000000';
// var val2 = '11111111';
// var val3 = '10101010';
// var val4 = '01010101';
var output = 0;
var buf = cycleThroughHex();
var val = 0;
for (var i = 0; i < buf.length; i++){
val = buf[i];
output = (val & 0x8000) >> 15; // test for qr
hasError(output, i, 'qr')
output = (val & 0x400) >> 10; // test for aa
hasError(output, i, 'aa');
output = (val & 0x200) >> 9; // test for tc
hasError(output,i, 'tc');
output = (val & 0x100) >> 8; // test for rd
hasError(output, i , 'rd');
output = (val & 0x80) >> 7; // test for ra
hasError(output, i, 'ra');
}
console.log("test successful");
}
// ex: pad('11111', 8, '0')
function pad(str, len, ch) {
while (str.length < len) {
str = ch + str;
}
// What is the hex value of the str?
var hex_val = ParseInt(Number(str));
return str;
}
// ex: bin2hex('10010101')
function bin2hex(str){
var hex = parseInt(str, 2).toString(16);
return hex;
}
function binaryAgent(str) {
var binString = '';
str.split(' ').map(function(bin) {
binString += String.fromCharCode(parseInt(bin, 2));
});
return binString;
}
// binary Agent test
// var str = binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100');
// console.log(str);
// cycleThroughHex();
// var hex = (0x8000 & 0x8000) >> 15;
// console.log(hex.toString(16));
// main();
'use strict';
// order one http://www.binarytides.com/dns-query-code-in-c-with-linux-sockets/
// order two http://www.zytrax.com/books/dns/ch15/
function parse(i) {
var header = {
// first byte appears to be reverse
qr: (i & 0x8000) >> 15
, opcode: (i & 0x7800) >> 11
, aa: (i & 0x400) >> 10
, tc: (i & 0x200) >> 9
, rd: (i & 0x100) >> 8
// second byte appears to be second byte, but also in reverse
, ra: (i & 0x80) >> 7
, res1: (i & 0x40) >> 6 // z
, res2: (i & 0x20) >> 5 // ad
, res3: (i & 0x10) >> 4 // cd
, rcode: (i & 0x1F)
};
return header;
}
function pack(header) {
var val = 0;
val += (header.qr << 15) & 0x8000;
val += (header.opcode << 11) & 0x7800;
val += (header.aa << 10) & 0x400;
val += (header.tc << 9) & 0x200;
val += (header.rd << 8) & 0x100;
val += (header.ra << 7) & 0x80;
val += (header.res1 << 6) & 0x40;
val += (header.res2 << 5) & 0x20;
val += (header.res3 << 4) & 0x10;
val += header.rcode & 0x1F;
return val;
}
var start = {
qr: 1
, opcode: 12
, aa: 0
, tc: 0
, rd: 1
, ra: 0
, res1: 0
, res2: 0
, res3: 0
, rcode: 0
};
var i = pack(start);
var obj = parse(i);
console.log(i);
console.log(start);
console.log(obj);
// does a recursive check to see if start and obj are equal
require('assert').deepEqual(start, obj);