• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3const common = require('../common');
4const assert = require('assert');
5const { internalBinding } = require('internal/test/binding');
6const cares = internalBinding('cares_wrap');
7
8// Stub `getaddrinfo` to *always* error. This has to be done before we load the
9// `dns` module to guarantee that the `dns` module uses the stub.
10cares.getaddrinfo = () => internalBinding('uv').UV_ENOMEM;
11
12const dns = require('dns');
13const dnsPromises = dns.promises;
14
15{
16  const err = {
17    code: 'ERR_INVALID_ARG_TYPE',
18    name: 'TypeError',
19    message: /^The "hostname" argument must be of type string\. Received type number/
20  };
21
22  assert.throws(() => dns.lookup(1, {}), err);
23  assert.throws(() => dnsPromises.lookup(1, {}), err);
24}
25
26// This also verifies different expectWarning notations.
27common.expectWarning({
28  // For 'internal/test/binding' module.
29  'internal/test/binding': [
30    'These APIs are for internal testing only. Do not use them.',
31  ],
32  // For calling `dns.lookup` with falsy `hostname`.
33  'DeprecationWarning': {
34    DEP0118: 'The provided hostname "false" is not a valid ' +
35      'hostname, and is supported in the dns module solely for compatibility.'
36  }
37});
38
39assert.throws(() => {
40  dns.lookup(false, 'cb');
41}, {
42  code: 'ERR_INVALID_CALLBACK',
43  name: 'TypeError'
44});
45
46assert.throws(() => {
47  dns.lookup(false, 'options', 'cb');
48}, {
49  code: 'ERR_INVALID_CALLBACK',
50  name: 'TypeError'
51});
52
53{
54  const err = {
55    code: 'ERR_INVALID_OPT_VALUE',
56    name: 'TypeError',
57    message: 'The value "100" is invalid for option "hints"'
58  };
59  const options = {
60    hints: 100,
61    family: 0,
62    all: false
63  };
64
65  assert.throws(() => { dnsPromises.lookup(false, options); }, err);
66  assert.throws(() => {
67    dns.lookup(false, options, common.mustNotCall());
68  }, err);
69}
70
71{
72  const err = {
73    code: 'ERR_INVALID_OPT_VALUE',
74    name: 'TypeError',
75    message: 'The value "20" is invalid for option "family". ' +
76    'Must be one of: 0, 4, 6'
77  };
78  const options = {
79    hints: 0,
80    family: 20,
81    all: false
82  };
83
84  assert.throws(() => { dnsPromises.lookup(false, options); }, err);
85  assert.throws(() => {
86    dns.lookup(false, options, common.mustNotCall());
87  }, err);
88}
89
90(async function() {
91  let res;
92
93  res = await dnsPromises.lookup(false, {
94    hints: 0,
95    family: 0,
96    all: true
97  });
98  assert.deepStrictEqual(res, []);
99
100  res = await dnsPromises.lookup('127.0.0.1', {
101    hints: 0,
102    family: 4,
103    all: true
104  });
105  assert.deepStrictEqual(res, [{ address: '127.0.0.1', family: 4 }]);
106
107  res = await dnsPromises.lookup('127.0.0.1', {
108    hints: 0,
109    family: 4,
110    all: false
111  });
112  assert.deepStrictEqual(res, { address: '127.0.0.1', family: 4 });
113})().then(common.mustCall());
114
115dns.lookup(false, {
116  hints: 0,
117  family: 0,
118  all: true
119}, common.mustSucceed((result, addressType) => {
120  assert.deepStrictEqual(result, []);
121  assert.strictEqual(addressType, undefined);
122}));
123
124dns.lookup('127.0.0.1', {
125  hints: 0,
126  family: 4,
127  all: true
128}, common.mustSucceed((result, addressType) => {
129  assert.deepStrictEqual(result, [{
130    address: '127.0.0.1',
131    family: 4
132  }]);
133  assert.strictEqual(addressType, undefined);
134}));
135
136dns.lookup('127.0.0.1', {
137  hints: 0,
138  family: 4,
139  all: false
140}, common.mustSucceed((result, addressType) => {
141  assert.deepStrictEqual(result, '127.0.0.1');
142  assert.strictEqual(addressType, 4);
143}));
144
145let tickValue = 0;
146
147// Should fail due to stub.
148dns.lookup('example.com', common.mustCall((error, result, addressType) => {
149  assert(error);
150  assert.strictEqual(tickValue, 1);
151  assert.strictEqual(error.code, 'ENOMEM');
152  const descriptor = Object.getOwnPropertyDescriptor(error, 'message');
153  // The error message should be non-enumerable.
154  assert.strictEqual(descriptor.enumerable, false);
155}));
156
157// Make sure that the error callback is called on next tick.
158tickValue = 1;
159
160// Should fail due to stub.
161assert.rejects(dnsPromises.lookup('example.com'),
162               { code: 'ENOMEM', hostname: 'example.com' });
163