• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const assert = require('assert');
25
26const dns = require('dns');
27const dnsPromises = dns.promises;
28
29(async function() {
30  let res;
31
32  res = await dnsPromises.lookup(null);
33  assert.strictEqual(res.address, null);
34  assert.strictEqual(res.family, 4);
35
36  res = await dnsPromises.lookup('127.0.0.1');
37  assert.strictEqual(res.address, '127.0.0.1');
38  assert.strictEqual(res.family, 4);
39
40  res = await dnsPromises.lookup('::1');
41  assert.strictEqual(res.address, '::1');
42  assert.strictEqual(res.family, 6);
43})();
44
45// Try resolution without hostname.
46dns.lookup(null, common.mustCall((error, result, addressType) => {
47  assert.ifError(error);
48  assert.strictEqual(result, null);
49  assert.strictEqual(addressType, 4);
50}));
51
52dns.lookup('127.0.0.1', common.mustCall((error, result, addressType) => {
53  assert.ifError(error);
54  assert.strictEqual(result, '127.0.0.1');
55  assert.strictEqual(addressType, 4);
56}));
57
58dns.lookup('::1', common.mustCall((error, result, addressType) => {
59  assert.ifError(error);
60  assert.strictEqual(result, '::1');
61  assert.strictEqual(addressType, 6);
62}));
63
64[
65  // Try calling resolve with an unsupported type.
66  'HI',
67  // Try calling resolve with an unsupported type that's an object key
68  'toString'
69].forEach((val) => {
70  const err = {
71    code: 'ERR_INVALID_OPT_VALUE',
72    name: 'TypeError',
73    message: `The value "${val}" is invalid for option "rrtype"`
74  };
75
76  assert.throws(
77    () => dns.resolve('www.google.com', val),
78    err
79  );
80
81  assert.throws(() => dnsPromises.resolve('www.google.com', val), err);
82});
83
84// Windows doesn't usually have an entry for localhost 127.0.0.1 in
85// C:\Windows\System32\drivers\etc\hosts
86// so we disable this test on Windows.
87// IBMi reports `ENOTFOUND` when get hostname by address 127.0.0.1
88if (!common.isWindows && !common.isIBMi) {
89  dns.reverse('127.0.0.1', common.mustCall(function(error, domains) {
90    assert.ifError(error);
91    assert.ok(Array.isArray(domains));
92  }));
93
94  (async function() {
95    assert.ok(Array.isArray(await dnsPromises.reverse('127.0.0.1')));
96  })();
97}
98