|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// The default dgram lookup resolves a literal IP address of the socket's own |
| 4 | +// family to itself, without calling dns.lookup(). Each case below stubs the |
| 5 | +// process-global dns.lookup(), so they run sequentially to keep one case from |
| 6 | +// observing another's stub. |
| 7 | + |
| 8 | +const common = require('../common'); |
| 9 | +const assert = require('assert'); |
| 10 | +const dgram = require('dgram'); |
| 11 | +const dns = require('dns'); |
| 12 | + |
| 13 | +const originalLookup = dns.lookup; |
| 14 | + |
| 15 | +function ipv4SendSkipsLookup(next) { |
| 16 | + dns.lookup = common.mustNotCall('dns.lookup() ran for an IPv4 literal'); |
| 17 | + |
| 18 | + const receiver = dgram.createSocket('udp4'); |
| 19 | + const sender = dgram.createSocket('udp4'); |
| 20 | + |
| 21 | + receiver.on('message', common.mustCall((msg) => { |
| 22 | + assert.strictEqual(msg.toString(), 'payload'); |
| 23 | + dns.lookup = originalLookup; |
| 24 | + receiver.close(); |
| 25 | + sender.close(); |
| 26 | + next(); |
| 27 | + })); |
| 28 | + |
| 29 | + receiver.bind(0, '127.0.0.1', common.mustCall(() => { |
| 30 | + sender.send('payload', receiver.address().port, '127.0.0.1', common.mustCall()); |
| 31 | + })); |
| 32 | +} |
| 33 | + |
| 34 | +function ipv6BindSkipsLookup(next) { |
| 35 | + if (!common.hasIPv6) { |
| 36 | + next(); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + dns.lookup = common.mustNotCall('dns.lookup() ran for an IPv6 literal'); |
| 41 | + |
| 42 | + const socket = dgram.createSocket('udp6'); |
| 43 | + |
| 44 | + socket.bind(0, '::1', common.mustCall(() => { |
| 45 | + dns.lookup = originalLookup; |
| 46 | + socket.close(); |
| 47 | + next(); |
| 48 | + })); |
| 49 | +} |
| 50 | + |
| 51 | +function mismatchedFamilyFallsThrough(next) { |
| 52 | + // '::1' is not an IPv4 literal, so a udp4 socket still resolves it via |
| 53 | + // dns.lookup() rather than short-circuiting. |
| 54 | + dns.lookup = common.mustCall((host, family, callback) => { |
| 55 | + dns.lookup = originalLookup; |
| 56 | + assert.strictEqual(host, '::1'); |
| 57 | + assert.strictEqual(family, 4); |
| 58 | + callback(null, '127.0.0.1', 4); |
| 59 | + }); |
| 60 | + |
| 61 | + const socket = dgram.createSocket('udp4'); |
| 62 | + |
| 63 | + socket.bind(0, '::1', common.mustCall(() => { |
| 64 | + socket.close(); |
| 65 | + next(); |
| 66 | + })); |
| 67 | +} |
| 68 | + |
| 69 | +const cases = [ |
| 70 | + ipv4SendSkipsLookup, |
| 71 | + ipv6BindSkipsLookup, |
| 72 | + mismatchedFamilyFallsThrough, |
| 73 | +]; |
| 74 | + |
| 75 | +(function runNext() { |
| 76 | + const testCase = cases.shift(); |
| 77 | + if (testCase !== undefined) { |
| 78 | + testCase(runNext); |
| 79 | + } |
| 80 | +})(); |
0 commit comments