WIP self-test on a bin or json file
This commit is contained in:
		
							parent
							
								
									e9169d7ef1
								
							
						
					
					
						commit
						e0af1007c7
					
				
							
								
								
									
										86
									
								
								bin/dns-test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								bin/dns-test.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,86 @@ | ||||
| 'use strict'; | ||||
| 
 | ||||
| // EXAMPLE:
 | ||||
| // node bin/dns-parse.js samples/a-0.mdns.bin
 | ||||
| 
 | ||||
| var path = require('path'); | ||||
| // pass a terminal arg
 | ||||
| var filename = process.argv[2]; | ||||
| var outname = process.argv[3]; | ||||
| 
 | ||||
| if (!filename || /(-h)|(--help)/.test(process.argv.join(' '))) { | ||||
|   console.error(""); | ||||
|   console.error("Accepts a DNS packet (binary or json), converts it, and then converts it back to verify the function of the parser and packer"); | ||||
|   console.error(""); | ||||
|   console.error("Usage: node bin/dns-test.js <path/to/sample.json|.bin>"); | ||||
|   console.error("Example: node bin/dns-test.js ./samples/services-0.mdns.json"); | ||||
|   process.exit(1); | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| if (!outname) { | ||||
|   console.warn(''); | ||||
|   console.warn( | ||||
|     "Usage: node bin/dns-pack.js '" + filename + "' './" + path.basename(filename).replace(path.extname(filename), '') + ".bin'" | ||||
|   ); | ||||
|   console.warn(''); | ||||
| } | ||||
| 
 | ||||
| var PromiseA = require('bluebird'); | ||||
| var fs = PromiseA.promisifyAll(require('fs')); | ||||
| var dnsjs = require('../').DNSPacket; | ||||
| var extname = path.extname(filename).substr(1).toLowerCase(); | ||||
| 
 | ||||
| if ('json' !== extname && 'bin' !== extname) { | ||||
|   console.error("The file extension must end in .json or .bin (raw DNS packet)"); | ||||
|   process.exit(3); | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| var hexdump = require('hexdump.js').hexdump; | ||||
| 
 | ||||
| function testJsonAsync(nb) { | ||||
|   var packet = dnsjs.write(JSON.parse(nb.toString('ascii'))); | ||||
|   var str = hexdump(packet); | ||||
|   console.log(str); | ||||
|   var json = dnsjs.parse(packet); | ||||
|   if (json.error) { | ||||
|     console.error(json); | ||||
|     process.exit(37); | ||||
|     return; | ||||
|   } | ||||
| 
 | ||||
|   console.log("[OK] JSON -> binary -> JSON"); | ||||
| } | ||||
| 
 | ||||
| function testBinAsync(nb) { | ||||
|   var bin = nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength); | ||||
|   var json = dnsjs.parse(bin); | ||||
|   if (json.error) { | ||||
|     console.error(json); | ||||
|     process.exit(38); | ||||
|     return; | ||||
|   } | ||||
|   var bin = dnsjs.write(json); | ||||
|   var json2 = dnsjs.parse(bin); | ||||
|   if (json2.error) { | ||||
|     console.error(json2); | ||||
|     process.exit(37); | ||||
|     return; | ||||
|   } | ||||
| 
 | ||||
|   var assert = require('assert'); | ||||
|   json = JSON.parse(JSON.stringify()); | ||||
|   assert.deepEqual(json, json2); | ||||
| 
 | ||||
|   console.log("[OK] binary -> JSON -> binary -> JSON"); | ||||
|   console.log("(compression pointer support needs to be improved in order to support direct bin -> JSON -> bin testing)"); | ||||
| } | ||||
| 
 | ||||
| if ('json' === extname) { | ||||
|   return fs.readFileAsync(filename, null).then(testJsonAsync); | ||||
| } | ||||
| 
 | ||||
| if ('bin' === extname) { | ||||
|   return fs.readFileAsync(filename, null).then(testBinAsync); | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user