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 }; 77 const options = { 78 hints: 0, 79 family: 20, 80 all: false 81 }; 82 83 assert.throws(() => { dnsPromises.lookup(false, options); }, err); 84 assert.throws(() => { 85 dns.lookup(false, options, common.mustNotCall()); 86 }, err); 87} 88 89(async function() { 90 let res; 91 92 res = await dnsPromises.lookup(false, { 93 hints: 0, 94 family: 0, 95 all: true 96 }); 97 assert.deepStrictEqual(res, []); 98 99 res = await dnsPromises.lookup('127.0.0.1', { 100 hints: 0, 101 family: 4, 102 all: true 103 }); 104 assert.deepStrictEqual(res, [{ address: '127.0.0.1', family: 4 }]); 105 106 res = await dnsPromises.lookup('127.0.0.1', { 107 hints: 0, 108 family: 4, 109 all: false 110 }); 111 assert.deepStrictEqual(res, { address: '127.0.0.1', family: 4 }); 112})(); 113 114dns.lookup(false, { 115 hints: 0, 116 family: 0, 117 all: true 118}, common.mustCall((error, result, addressType) => { 119 assert.ifError(error); 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.mustCall((error, result, addressType) => { 129 assert.ifError(error); 130 assert.deepStrictEqual(result, [{ 131 address: '127.0.0.1', 132 family: 4 133 }]); 134 assert.strictEqual(addressType, undefined); 135})); 136 137dns.lookup('127.0.0.1', { 138 hints: 0, 139 family: 4, 140 all: false 141}, common.mustCall((error, result, addressType) => { 142 assert.ifError(error); 143 assert.deepStrictEqual(result, '127.0.0.1'); 144 assert.strictEqual(addressType, 4); 145})); 146 147let tickValue = 0; 148 149// Should fail due to stub. 150dns.lookup('example.com', common.mustCall((error, result, addressType) => { 151 assert(error); 152 assert.strictEqual(tickValue, 1); 153 assert.strictEqual(error.code, 'ENOMEM'); 154 const descriptor = Object.getOwnPropertyDescriptor(error, 'message'); 155 // The error message should be non-enumerable. 156 assert.strictEqual(descriptor.enumerable, false); 157})); 158 159// Make sure that the error callback is called on next tick. 160tickValue = 1; 161 162// Should fail due to stub. 163assert.rejects(dnsPromises.lookup('example.com'), 164 { code: 'ENOMEM', hostname: 'example.com' }); 165