'use strict';
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');
{
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',
]);
}
{
const invalidServers = [
' ',
'\n',
'\0',
'1'.repeat(3 * 4),
':'.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);
}
{
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);
}
{
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));
});
}
{
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());
{
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" },
{ type: "AAAA", address: "::42" },
{ 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,
},
],
},
{
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);
}, 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];
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());
}));
}