|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const net = require('net'); |
| 5 | +const dns = require('dns'); |
| 6 | +const { mock } = require('node:test'); |
| 7 | + |
| 8 | +if (!common.hasIPv6) { |
| 9 | + common.printSkipMessage('IPv6 support is required for this test'); |
| 10 | + return; |
| 11 | +} |
| 12 | + |
| 13 | +// Test on IPv6 Server, dns.lookup throws an error |
| 14 | +{ |
| 15 | + mock.method(dns, 'lookup', (hostname, options, callback) => { |
| 16 | + callback(new Error('Mocked error')); |
| 17 | + }); |
| 18 | + const host = 'ipv6_link_local'; |
| 19 | + |
| 20 | + const server = net.createServer(); |
| 21 | + |
| 22 | + server.on('error', common.mustCall((e) => { |
| 23 | + assert.strictEqual(e.message, 'Mocked error'); |
| 24 | + })); |
| 25 | + |
| 26 | + server.listen(common.PORT + 2, host); |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +// Test on IPv6 Server, server.listen throws an error |
| 31 | +{ |
| 32 | + mock.method(dns, 'lookup', (hostname, options, callback) => { |
| 33 | + if (hostname === 'ipv6_link_local') { |
| 34 | + callback(null, [{ address: 'fe80::1', family: 6 }]); |
| 35 | + } else { |
| 36 | + dns.lookup.wrappedMethod(hostname, options, callback); |
| 37 | + } |
| 38 | + }); |
| 39 | + const host = 'ipv6_link_local'; |
| 40 | + |
| 41 | + const server = net.createServer(); |
| 42 | + |
| 43 | + server.on('error', common.mustCall((e) => { |
| 44 | + assert.strictEqual(e.address, 'fe80::1'); |
| 45 | + assert.strictEqual(e.syscall, 'listen'); |
| 46 | + })); |
| 47 | + |
| 48 | + server.listen(common.PORT + 2, host); |
| 49 | +} |
| 50 | + |
| 51 | +// Test on IPv6 Server, picks 127.0.0.1 between that and a bunch of link-local addresses |
| 52 | +{ |
| 53 | + |
| 54 | + mock.method(dns, 'lookup', (hostname, options, callback) => { |
| 55 | + if (hostname === 'ipv6_link_local_with_many_entries') { |
| 56 | + callback(null, [ |
| 57 | + { address: 'fe80::1', family: 6 }, |
| 58 | + { address: 'fe80::abcd:1234', family: 6 }, |
| 59 | + { address: 'fe80::1ff:fe23:4567:890a', family: 6 }, |
| 60 | + { address: 'fe80::200:5aee:feaa:20a2', family: 6 }, |
| 61 | + { address: 'fe80::f2de:f1ff:fe2b:3c4b', family: 6 }, |
| 62 | + { address: 'fe81::1', family: 6 }, |
| 63 | + { address: 'fe82::abcd:1234', family: 6 }, |
| 64 | + { address: 'fe83::1ff:fe23:4567:890a', family: 6 }, |
| 65 | + { address: 'fe84::200:5aee:feaa:20a2', family: 6 }, |
| 66 | + { address: 'fe85::f2de:f1ff:fe2b:3c4b', family: 6 }, |
| 67 | + { address: 'fe86::1', family: 6 }, |
| 68 | + { address: 'fe87::abcd:1234', family: 6 }, |
| 69 | + { address: 'fe88::1ff:fe23:4567:890a', family: 6 }, |
| 70 | + { address: 'fe89::200:5aee:feaa:20a2', family: 6 }, |
| 71 | + { address: 'fe8a::f2de:f1ff:fe2b:3c4b', family: 6 }, |
| 72 | + { address: 'fe8b::1', family: 6 }, |
| 73 | + { address: 'fe8c::abcd:1234', family: 6 }, |
| 74 | + { address: 'fe8d::1ff:fe23:4567:890a', family: 6 }, |
| 75 | + { address: 'fe8e::200:5aee:feaa:20a2', family: 6 }, |
| 76 | + { address: 'fe8f::f2de:f1ff:fe2b:3c4b', family: 6 }, |
| 77 | + { address: 'fea0::1', family: 6 }, |
| 78 | + { address: 'febf::abcd:1234', family: 6 }, |
| 79 | + { address: 'febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff', family: 6 }, |
| 80 | + { address: '127.0.0.1', family: 4 }, |
| 81 | + ]); |
| 82 | + } else { |
| 83 | + dns.lookup.wrappedMethod(hostname, options, callback); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + const host = 'ipv6_link_local_with_many_entries'; |
| 88 | + |
| 89 | + const server = net.createServer(); |
| 90 | + |
| 91 | + server.on('error', common.mustNotCall()); |
| 92 | + |
| 93 | + server.listen(common.PORT + 3, host, common.mustCall(() => { |
| 94 | + const address = server.address(); |
| 95 | + assert.strictEqual(address.address, '127.0.0.1'); |
| 96 | + assert.strictEqual(address.port, common.PORT + 3); |
| 97 | + assert.strictEqual(address.family, 'IPv4'); |
| 98 | + server.close(); |
| 99 | + })); |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +// Test on IPv6 Server, picks ::1 because the other address is a link-local address |
| 104 | +{ |
| 105 | + |
| 106 | + const host = 'ipv6_link_local_with_double_entry'; |
| 107 | + const validIpv6Address = '::1'; |
| 108 | + |
| 109 | + mock.method(dns, 'lookup', (hostname, options, callback) => { |
| 110 | + if (hostname === 'ipv6_link_local_with_double_entry') { |
| 111 | + callback(null, [ |
| 112 | + { address: 'fe80::1', family: 6 }, |
| 113 | + { address: validIpv6Address, family: 6 }, |
| 114 | + ]); |
| 115 | + } else { |
| 116 | + dns.lookup.wrappedMethod(hostname, options, callback); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + const server = net.createServer(); |
| 121 | + |
| 122 | + server.on('error', common.mustNotCall()); |
| 123 | + |
| 124 | + server.listen(common.PORT + 4, host, common.mustCall(() => { |
| 125 | + const address = server.address(); |
| 126 | + assert.strictEqual(address.address, validIpv6Address); |
| 127 | + assert.strictEqual(address.port, common.PORT + 4); |
| 128 | + assert.strictEqual(address.family, 'IPv6'); |
| 129 | + server.close(); |
| 130 | + })); |
| 131 | +} |
0 commit comments