deno.land / std@0.166.0 / node / _tools / test / parallel / test-dns.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 16.13.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
// Copyright Joyent, Inc. and other Node contributors.//// Permission is hereby granted, free of charge, to any person obtaining a// copy of this software and associated documentation files (the// "Software"), to deal in the Software without restriction, including// without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit// persons to whom the Software is furnished to do so, subject to the// following conditions://// The above copyright notice and this permission notice shall be included// in all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
// TODO: enable remaining tests once functionality is implemented.
const common = require('../common');const dnstools = require('../common/dns');const assert = require('assert');
const dns = require('dns');const dnsPromises = dns.promises;const dgram = require('dgram');
// TODO(cmorten): currently don't expose defaults// const existing = dns.getServers();// assert(existing.length > 0);
// Verify that setServers() handles arrays with holes and other oddities{ const servers = [];
servers[0] = '127.0.0.1'; servers[2] = '0.0.0.0'; dns.setServers(servers);
assert.deepStrictEqual(dns.getServers(), ['127.0.0.1', '0.0.0.0']);}
{ const servers = ['127.0.0.1', '192.168.1.1'];
servers[3] = '127.1.0.1'; servers[4] = '127.1.0.1'; servers[5] = '127.1.1.1';
Object.defineProperty(servers, 2, { enumerable: true, get: () => { servers.length = 3; return '0.0.0.0'; } });
dns.setServers(servers); assert.deepStrictEqual(dns.getServers(), [ '127.0.0.1', '192.168.1.1', '0.0.0.0', ]);}
{ // Various invalidities, all of which should throw a clean error. const invalidServers = [ ' ', '\n', '\0', '1'.repeat(3 * 4), // Check for REDOS issues. ':'.repeat(100000), '['.repeat(100000), '['.repeat(100000) + ']'.repeat(100000) + 'a', ]; invalidServers.forEach((serv) => { assert.throws( () => { dns.setServers([serv]); }, { name: 'TypeError', code: 'ERR_INVALID_IP_ADDRESS' } ); });}
const goog = [ '8.8.8.8', '8.8.4.4',];dns.setServers(goog);assert.deepStrictEqual(dns.getServers(), goog);assert.throws(() => dns.setServers(['foobar']), { code: 'ERR_INVALID_IP_ADDRESS', name: 'TypeError', message: 'Invalid IP address: foobar'});assert.throws(() => dns.setServers(['127.0.0.1:va']), { code: 'ERR_INVALID_IP_ADDRESS', name: 'TypeError', message: 'Invalid IP address: 127.0.0.1:va'});assert.deepStrictEqual(dns.getServers(), goog);
const goog6 = [ '2001:4860:4860::8888', '2001:4860:4860::8844',];dns.setServers(goog6);assert.deepStrictEqual(dns.getServers(), goog6);
goog6.push('4.4.4.4');dns.setServers(goog6);assert.deepStrictEqual(dns.getServers(), goog6);
const ports = [ '4.4.4.4:53', '[2001:4860:4860::8888]:53', '103.238.225.181:666', '[fe80::483a:5aff:fee6:1f04]:666', '[fe80::483a:5aff:fee6:1f04]',];const portsExpected = [ '4.4.4.4', '2001:4860:4860::8888', '103.238.225.181:666', '[fe80::483a:5aff:fee6:1f04]:666', 'fe80::483a:5aff:fee6:1f04',];dns.setServers(ports);assert.deepStrictEqual(dns.getServers(), portsExpected);
dns.setServers([]);assert.deepStrictEqual(dns.getServers(), []);
{ const errObj = { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "rrtype" argument must be of type string. ' + 'Received an instance of Array' }; assert.throws(() => { dns.resolve('example.com', [], common.mustNotCall()); }, errObj); assert.throws(() => { dnsPromises.resolve('example.com', []); }, errObj);}{ const errObj = { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "name" argument must be of type string. ' + 'Received undefined' }; assert.throws(() => { dnsPromises.resolve(); }, errObj);}
// dns.lookup should accept only falsey and string values{ const errorReg = { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: /^The "hostname" argument must be of type string\. Received .*/ };
assert.throws(() => dns.lookup({}, common.mustNotCall()), errorReg);
assert.throws(() => dns.lookup([], common.mustNotCall()), errorReg);
assert.throws(() => dns.lookup(true, common.mustNotCall()), errorReg);
assert.throws(() => dns.lookup(1, common.mustNotCall()), errorReg);
assert.throws(() => dns.lookup(common.mustNotCall(), common.mustNotCall()), errorReg);
assert.throws(() => dnsPromises.lookup({}), errorReg); assert.throws(() => dnsPromises.lookup([]), errorReg); assert.throws(() => dnsPromises.lookup(true), errorReg); assert.throws(() => dnsPromises.lookup(1), errorReg); assert.throws(() => dnsPromises.lookup(common.mustNotCall()), errorReg);}
// dns.lookup should accept falsey values{ const checkCallback = (err, address, family) => { assert.ifError(err); assert.strictEqual(address, null); assert.strictEqual(family, 4); };
['', null, undefined, 0, NaN].forEach(async (value) => { const res = await dnsPromises.lookup(value); assert.deepStrictEqual(res, { address: null, family: 4 }); dns.lookup(value, common.mustCall(checkCallback)); });}
{ // Make sure that dns.lookup throws if hints does not represent a valid flag. // (dns.V4MAPPED | dns.ADDRCONFIG | dns.ALL) + 1 is invalid because: // - it's different from dns.V4MAPPED and dns.ADDRCONFIG and dns.ALL. // - it's different from any subset of them bitwise ored. // - it's different from 0. // - it's an odd number different than 1, and thus is invalid, because // flags are either === 1 or even. const hints = (dns.V4MAPPED | dns.ADDRCONFIG | dns.ALL) + 1; const err = { code: 'ERR_INVALID_ARG_VALUE', name: 'TypeError', message: /The argument 'hints' is invalid\. Received \d+/ };
assert.throws(() => { dnsPromises.lookup('nodejs.org', { hints }); }, err); assert.throws(() => { dns.lookup('nodejs.org', { hints }, common.mustNotCall()); }, err);}
assert.throws(() => dns.lookup("nodejs.org"), { code: "ERR_INVALID_ARG_TYPE", name: "TypeError",});
assert.throws(() => dns.lookup("nodejs.org", 4), { code: "ERR_INVALID_ARG_TYPE", name: "TypeError",});
dns.lookup('', { family: 4, hints: 0 }, common.mustCall());
dns.lookup('', { family: 6, hints: dns.ADDRCONFIG}, common.mustCall());
dns.lookup('', { hints: dns.V4MAPPED }, common.mustCall());
dns.lookup('', { hints: dns.ADDRCONFIG | dns.V4MAPPED}, common.mustCall());
dns.lookup('', { hints: dns.ALL}, common.mustCall());
dns.lookup('', { hints: dns.V4MAPPED | dns.ALL}, common.mustCall());
dns.lookup('', { hints: dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL}, common.mustCall());
(async function() { await dnsPromises.lookup('', { family: 4, hints: 0 }); await dnsPromises.lookup('', { family: 6, hints: dns.ADDRCONFIG }); await dnsPromises.lookup('', { hints: dns.V4MAPPED }); await dnsPromises.lookup('', { hints: dns.ADDRCONFIG | dns.V4MAPPED }); await dnsPromises.lookup('', { hints: dns.ALL }); await dnsPromises.lookup('', { hints: dns.V4MAPPED | dns.ALL }); await dnsPromises.lookup('', { hints: dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL });})().then(common.mustCall());
// {// const err = {// code: 'ERR_MISSING_ARGS',// name: 'TypeError',// message: 'The "address", "port", and "callback" arguments must be ' +// 'specified'// };
// assert.throws(() => dns.lookupService('0.0.0.0'), err);// err.message = 'The "address" and "port" arguments must be specified';// assert.throws(() => dnsPromises.lookupService('0.0.0.0'), err);// }
// {// const invalidAddress = 'fasdfdsaf';// const err = {// code: 'ERR_INVALID_ARG_VALUE',// name: 'TypeError',// message: `The argument 'address' is invalid. Received '${invalidAddress}'`// };
// assert.throws(() => {// dnsPromises.lookupService(invalidAddress, 0);// }, err);
// assert.throws(() => {// dns.lookupService(invalidAddress, 0, common.mustNotCall());// }, err);// }
// const portErr = (port) => {// const err = {// code: 'ERR_SOCKET_BAD_PORT',// message:// `Port should be >= 0 and < 65536. Received ${port}.`,// name: 'RangeError'// };
// assert.throws(() => {// dnsPromises.lookupService('0.0.0.0', port);// }, err);
// assert.throws(() => {// dns.lookupService('0.0.0.0', port, common.mustNotCall());// }, err);// };// portErr(null);// portErr(undefined);// portErr(65538);// portErr('test');
// assert.throws(() => {// dns.lookupService('0.0.0.0', 80, null);// }, {// code: 'ERR_INVALID_ARG_TYPE',// name: 'TypeError'// });
{ dns.resolveMx('foo.onion', function(err) { assert.deepStrictEqual(err.code, 'ENOTFOUND'); assert.deepStrictEqual(err.syscall, 'queryMx'); assert.deepStrictEqual(err.hostname, 'foo.onion'); assert.deepStrictEqual(err.message, 'queryMx ENOTFOUND foo.onion'); });}
{ const cases = [ { method: "resolveAny", answers: [ { type: "A", address: "1.2.3.4" /*ttl: 3333333333*/ }, { type: "AAAA", address: "::42" /*ttl: 3333333333*/ }, { type: "MX", priority: 42, exchange: "foobar.com", ttl: 3333333333 }, { type: "NS", value: "foobar.org", ttl: 3333333333 }, { type: "PTR", value: "baz.org", ttl: 3333333333 }, { type: "SOA", nsname: "ns1.example.com", hostmaster: "admin.example.com", serial: 3210987654, refresh: 900, retry: 900, expire: 1800, minttl: 3333333333, }, ], },
// TODO(cmorten): support ttl option // { // method: "resolve4", // options: { ttl: true }, // answers: [{ type: "A", address: "1.2.3.4", ttl: 3333333333 }], // },
// { // method: "resolve6", // options: { ttl: true }, // answers: [{ type: "AAAA", address: "::42", ttl: 3333333333 }], // },
{ method: "resolveSoa", answers: [ { type: "SOA", nsname: "ns1.example.com", hostmaster: "admin.example.com", serial: 3210987654, refresh: 900, retry: 900, expire: 1800, minttl: 3333333333, }, ], }, ];
const server = dgram.createSocket('udp4');
server.on('message', common.mustCall((msg, { address, port }) => { const parsed = dnstools.parseDNSPacket(msg); const domain = parsed.questions[0].domain; assert.strictEqual(domain, 'example.org');
server.send(dnstools.writeDNSPacket({ id: parsed.id, questions: parsed.questions, answers: cases[0].answers.map( (answer) => Object.assign({ domain }, answer) ), }), port, address); // Don't have "ANY" query type available so calls greatly increased with // polyfill method. }, /*cases.length * 2*/ 32));
server.bind(0, common.mustCall(() => { const address = server.address(); dns.setServers([`127.0.0.1:${address.port}`]);
function validateResults(res) { if (!Array.isArray(res)) res = [res];
assert.deepStrictEqual(res.map(tweakEntry), cases[0].answers.map(tweakEntry)); }
function tweakEntry(r) { const ret = { ...r };
const { method } = cases[0];
// TTL values are only provided for A and AAAA entries. if (!['A', 'AAAA'].includes(ret.type) && !/^resolve(4|6)?$/.test(method)) delete ret.ttl;
if (method !== 'resolveAny') delete ret.type;
return ret; }
(async function nextCase() { if (cases.length === 0) return server.close();
const { method, options } = cases[0];
validateResults(await dnsPromises[method]('example.org', options));
dns[method]('example.org', options, common.mustSucceed((res) => { validateResults(res); cases.shift(); nextCase(); })); })().then(common.mustCall());
}));}
Version Info