1'use strict'; 2 3const common = require('../common'); 4 5const assert = require('assert'); 6const dns = require('dns'); 7const net = require('net'); 8 9let running = false; 10const queue = []; 11 12const dnsPromises = dns.promises; 13const isIPv4 = net.isIPv4; 14const isIPv6 = net.isIPv6; 15 16dns.setServers([ '8.8.8.8', '8.8.4.4' ]); 17 18function checkWrap(req) { 19 assert.ok(typeof req === 'object'); 20} 21 22const checkers = { 23 checkA(r) { 24 assert.ok(isIPv4(r.address)); 25 assert.strictEqual(typeof r.ttl, 'number'); 26 assert.strictEqual(r.type, 'A'); 27 }, 28 checkAAAA(r) { 29 assert.ok(isIPv6(r.address)); 30 assert.strictEqual(typeof r.ttl, 'number'); 31 assert.strictEqual(r.type, 'AAAA'); 32 }, 33 checkCNAME(r) { 34 assert.ok(r.value); 35 assert.strictEqual(typeof r.value, 'string'); 36 assert.strictEqual(r.type, 'CNAME'); 37 }, 38 checkMX(r) { 39 assert.strictEqual(typeof r.exchange, 'string'); 40 assert.strictEqual(typeof r.priority, 'number'); 41 assert.strictEqual(r.type, 'MX'); 42 }, 43 checkNAPTR(r) { 44 assert.strictEqual(typeof r.flags, 'string'); 45 assert.strictEqual(typeof r.service, 'string'); 46 assert.strictEqual(typeof r.regexp, 'string'); 47 assert.strictEqual(typeof r.replacement, 'string'); 48 assert.strictEqual(typeof r.order, 'number'); 49 assert.strictEqual(typeof r.preference, 'number'); 50 assert.strictEqual(r.type, 'NAPTR'); 51 }, 52 checkNS(r) { 53 assert.strictEqual(typeof r.value, 'string'); 54 assert.strictEqual(r.type, 'NS'); 55 }, 56 checkPTR(r) { 57 assert.strictEqual(typeof r.value, 'string'); 58 assert.strictEqual(r.type, 'PTR'); 59 }, 60 checkTXT(r) { 61 assert.ok(Array.isArray(r.entries)); 62 assert.ok(r.entries.length > 0); 63 assert.strictEqual(r.type, 'TXT'); 64 }, 65 checkSOA(r) { 66 assert.strictEqual(typeof r.nsname, 'string'); 67 assert.strictEqual(typeof r.hostmaster, 'string'); 68 assert.strictEqual(typeof r.serial, 'number'); 69 assert.strictEqual(typeof r.refresh, 'number'); 70 assert.strictEqual(typeof r.retry, 'number'); 71 assert.strictEqual(typeof r.expire, 'number'); 72 assert.strictEqual(typeof r.minttl, 'number'); 73 assert.strictEqual(r.type, 'SOA'); 74 }, 75 checkSRV(r) { 76 assert.strictEqual(typeof r.name, 'string'); 77 assert.strictEqual(typeof r.port, 'number'); 78 assert.strictEqual(typeof r.priority, 'number'); 79 assert.strictEqual(typeof r.weight, 'number'); 80 assert.strictEqual(r.type, 'SRV'); 81 }, 82}; 83 84function TEST(f) { 85 function next() { 86 const f = queue.shift(); 87 if (f) { 88 running = true; 89 f(done); 90 } 91 } 92 93 function done() { 94 running = false; 95 process.nextTick(next); 96 } 97 98 queue.push(f); 99 100 if (!running) { 101 next(); 102 } 103} 104 105function processResult(res) { 106 assert.ok(Array.isArray(res)); 107 assert.ok(res.length > 0); 108 109 const types = {}; 110 res.forEach((obj) => { 111 types[obj.type] = true; 112 checkers[`check${obj.type}`](obj); 113 }); 114 115 return types; 116} 117 118TEST(async function test_sip2sip_for_naptr(done) { 119 function validateResult(res) { 120 const types = processResult(res); 121 assert.ok(types.A && types.NS && types.NAPTR && types.SOA, 122 `Missing record type, found ${Object.keys(types)}`); 123 } 124 125 validateResult(await dnsPromises.resolve('sip2sip.info', 'ANY')); 126 127 const req = dns.resolve( 128 'sip2sip.info', 129 'ANY', 130 common.mustSucceed((ret) => { 131 validateResult(ret); 132 done(); 133 })); 134 135 checkWrap(req); 136}); 137 138TEST(async function test_google_for_cname_and_srv(done) { 139 function validateResult(res) { 140 const types = processResult(res); 141 assert.ok(types.SRV); 142 } 143 144 validateResult(await dnsPromises.resolve('_caldav._tcp.google.com', 'ANY')); 145 146 const req = dns.resolve( 147 '_caldav._tcp.google.com', 148 'ANY', 149 common.mustSucceed((ret) => { 150 validateResult(ret); 151 done(); 152 })); 153 154 checkWrap(req); 155}); 156 157TEST(async function test_ptr(done) { 158 function validateResult(res) { 159 const types = processResult(res); 160 assert.ok(types.PTR); 161 } 162 163 validateResult(await dnsPromises.resolve('8.8.8.8.in-addr.arpa', 'ANY')); 164 165 const req = dns.resolve( 166 '8.8.8.8.in-addr.arpa', 167 'ANY', 168 common.mustSucceed((ret) => { 169 validateResult(ret); 170 done(); 171 })); 172 173 checkWrap(req); 174}); 175