2018-08-06 21:40:30 +00:00
|
|
|
// Flags: --expose-internals
|
2017-01-17 01:37:14 +00:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
2018-08-23 13:28:41 +00:00
|
|
|
const { internalBinding } = require('internal/test/binding');
|
|
|
|
const cares = internalBinding('cares_wrap');
|
2019-04-18 21:28:46 +00:00
|
|
|
|
|
|
|
// Stub `getaddrinfo` to *always* error. This has to be done before we load the
|
|
|
|
// `dns` module to guarantee that the `dns` module uses the stub.
|
|
|
|
cares.getaddrinfo = () => internalBinding('uv').UV_ENOMEM;
|
|
|
|
|
2017-01-17 01:37:14 +00:00
|
|
|
const dns = require('dns');
|
2018-06-11 18:56:33 +00:00
|
|
|
const dnsPromises = dns.promises;
|
|
|
|
|
|
|
|
{
|
|
|
|
const err = {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError',
|
2018-09-30 10:17:36 +00:00
|
|
|
message: /^The "hostname" argument must be of type string\. Received type number/
|
2018-06-11 18:56:33 +00:00
|
|
|
};
|
|
|
|
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(() => dns.lookup(1, {}), err);
|
|
|
|
assert.throws(() => dnsPromises.lookup(1, {}), err);
|
2018-06-11 18:56:33 +00:00
|
|
|
}
|
2017-01-17 01:37:14 +00:00
|
|
|
|
2018-12-28 13:33:33 +00:00
|
|
|
// This also verifies different expectWarning notations.
|
2018-09-30 10:17:36 +00:00
|
|
|
common.expectWarning({
|
|
|
|
// For 'internal/test/binding' module.
|
|
|
|
'internal/test/binding': [
|
2021-03-26 15:51:08 +00:00
|
|
|
'These APIs are for internal testing only. Do not use them.',
|
2018-09-30 10:17:36 +00:00
|
|
|
],
|
|
|
|
// For calling `dns.lookup` with falsy `hostname`.
|
2018-12-28 13:33:33 +00:00
|
|
|
'DeprecationWarning': {
|
|
|
|
DEP0118: 'The provided hostname "false" is not a valid ' +
|
|
|
|
'hostname, and is supported in the dns module solely for compatibility.'
|
|
|
|
}
|
2018-09-30 10:17:36 +00:00
|
|
|
});
|
|
|
|
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(() => {
|
2017-01-17 01:37:14 +00:00
|
|
|
dns.lookup(false, 'cb');
|
2017-12-06 14:32:42 +00:00
|
|
|
}, {
|
2022-01-24 16:09:16 +00:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError'
|
2017-12-06 14:32:42 +00:00
|
|
|
});
|
2017-01-17 01:37:14 +00:00
|
|
|
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(() => {
|
2017-01-17 01:37:14 +00:00
|
|
|
dns.lookup(false, 'options', 'cb');
|
2017-12-06 14:32:42 +00:00
|
|
|
}, {
|
2022-01-24 16:09:16 +00:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError'
|
2017-12-06 14:32:42 +00:00
|
|
|
});
|
2017-01-17 01:37:14 +00:00
|
|
|
|
2018-06-11 18:56:33 +00:00
|
|
|
{
|
|
|
|
const err = {
|
2020-08-08 16:01:59 +00:00
|
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError',
|
2020-08-08 16:01:59 +00:00
|
|
|
message: "The argument 'hints' is invalid. Received 100"
|
2018-06-11 18:56:33 +00:00
|
|
|
};
|
|
|
|
const options = {
|
2017-01-17 01:37:14 +00:00
|
|
|
hints: 100,
|
|
|
|
family: 0,
|
|
|
|
all: false
|
2018-06-11 18:56:33 +00:00
|
|
|
};
|
2017-01-17 01:37:14 +00:00
|
|
|
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(() => { dnsPromises.lookup(false, options); }, err);
|
|
|
|
assert.throws(() => {
|
2018-06-11 18:56:33 +00:00
|
|
|
dns.lookup(false, options, common.mustNotCall());
|
|
|
|
}, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const err = {
|
2020-08-08 16:01:59 +00:00
|
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError',
|
2020-08-08 16:01:59 +00:00
|
|
|
message: "The argument 'family' must be one of: 0, 4, 6. Received 20"
|
2018-06-11 18:56:33 +00:00
|
|
|
};
|
|
|
|
const options = {
|
2017-01-17 01:37:14 +00:00
|
|
|
hints: 0,
|
|
|
|
family: 20,
|
|
|
|
all: false
|
2018-06-11 18:56:33 +00:00
|
|
|
};
|
|
|
|
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(() => { dnsPromises.lookup(false, options); }, err);
|
|
|
|
assert.throws(() => {
|
2018-06-11 18:56:33 +00:00
|
|
|
dns.lookup(false, options, common.mustNotCall());
|
|
|
|
}, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
(async function() {
|
|
|
|
let res;
|
|
|
|
|
|
|
|
res = await dnsPromises.lookup(false, {
|
|
|
|
hints: 0,
|
|
|
|
family: 0,
|
|
|
|
all: true
|
|
|
|
});
|
|
|
|
assert.deepStrictEqual(res, []);
|
|
|
|
|
|
|
|
res = await dnsPromises.lookup('127.0.0.1', {
|
|
|
|
hints: 0,
|
|
|
|
family: 4,
|
|
|
|
all: true
|
|
|
|
});
|
|
|
|
assert.deepStrictEqual(res, [{ address: '127.0.0.1', family: 4 }]);
|
|
|
|
|
|
|
|
res = await dnsPromises.lookup('127.0.0.1', {
|
|
|
|
hints: 0,
|
|
|
|
family: 4,
|
|
|
|
all: false
|
|
|
|
});
|
|
|
|
assert.deepStrictEqual(res, { address: '127.0.0.1', family: 4 });
|
2020-07-14 15:45:39 +00:00
|
|
|
})().then(common.mustCall());
|
2017-01-17 01:37:14 +00:00
|
|
|
|
2018-02-09 01:32:04 +00:00
|
|
|
dns.lookup(false, {
|
|
|
|
hints: 0,
|
|
|
|
family: 0,
|
|
|
|
all: true
|
2020-09-06 20:27:07 +00:00
|
|
|
}, common.mustSucceed((result, addressType) => {
|
2018-02-09 01:32:04 +00:00
|
|
|
assert.deepStrictEqual(result, []);
|
|
|
|
assert.strictEqual(addressType, undefined);
|
|
|
|
}));
|
2017-01-17 01:37:14 +00:00
|
|
|
|
2018-02-09 01:32:04 +00:00
|
|
|
dns.lookup('127.0.0.1', {
|
|
|
|
hints: 0,
|
|
|
|
family: 4,
|
|
|
|
all: true
|
2020-09-06 20:27:07 +00:00
|
|
|
}, common.mustSucceed((result, addressType) => {
|
2018-02-09 01:32:04 +00:00
|
|
|
assert.deepStrictEqual(result, [{
|
|
|
|
address: '127.0.0.1',
|
|
|
|
family: 4
|
|
|
|
}]);
|
|
|
|
assert.strictEqual(addressType, undefined);
|
|
|
|
}));
|
2017-01-17 01:37:14 +00:00
|
|
|
|
2018-02-09 01:32:04 +00:00
|
|
|
dns.lookup('127.0.0.1', {
|
|
|
|
hints: 0,
|
|
|
|
family: 4,
|
|
|
|
all: false
|
2020-09-06 20:27:07 +00:00
|
|
|
}, common.mustSucceed((result, addressType) => {
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(result, '127.0.0.1');
|
2018-02-09 01:32:04 +00:00
|
|
|
assert.strictEqual(addressType, 4);
|
|
|
|
}));
|
2017-01-17 01:37:14 +00:00
|
|
|
|
2018-02-09 01:32:04 +00:00
|
|
|
let tickValue = 0;
|
2017-01-17 01:37:14 +00:00
|
|
|
|
2019-04-18 21:28:46 +00:00
|
|
|
// Should fail due to stub.
|
2018-02-09 01:32:04 +00:00
|
|
|
dns.lookup('example.com', common.mustCall((error, result, addressType) => {
|
|
|
|
assert(error);
|
|
|
|
assert.strictEqual(tickValue, 1);
|
2019-04-18 21:28:46 +00:00
|
|
|
assert.strictEqual(error.code, 'ENOMEM');
|
2018-04-01 05:38:47 +00:00
|
|
|
const descriptor = Object.getOwnPropertyDescriptor(error, 'message');
|
2018-09-13 20:40:11 +00:00
|
|
|
// The error message should be non-enumerable.
|
|
|
|
assert.strictEqual(descriptor.enumerable, false);
|
2018-02-09 01:32:04 +00:00
|
|
|
}));
|
2017-01-17 01:37:14 +00:00
|
|
|
|
2019-04-18 21:28:46 +00:00
|
|
|
// Make sure that the error callback is called on next tick.
|
2018-02-09 01:32:04 +00:00
|
|
|
tickValue = 1;
|
2019-04-18 21:28:46 +00:00
|
|
|
|
|
|
|
// Should fail due to stub.
|
|
|
|
assert.rejects(dnsPromises.lookup('example.com'),
|
|
|
|
{ code: 'ENOMEM', hostname: 'example.com' });
|