|
2 | 2 | const common = require('../common'); |
3 | 3 | const assert = require('assert'); |
4 | 4 | const net = require('net'); |
| 5 | +const { describe, it } = require('node:test'); |
5 | 6 |
|
6 | 7 | function check(addressType, cb) { |
7 | 8 | const server = net.createServer(function(client) { |
@@ -65,3 +66,44 @@ check(4, function() { |
65 | 66 | } |
66 | 67 | }).on('error', common.expectsError({ code: 'ERR_INVALID_IP_ADDRESS' })); |
67 | 68 | } |
| 69 | + |
| 70 | +const brokenCustomLookup = (_hostname, options, callback) => { |
| 71 | + // Incorrectly return an array of IPs instead of a string. |
| 72 | + callback(null, ['127.0.0.1'], options.family); |
| 73 | +}; |
| 74 | + |
| 75 | +describe('when family is ipv4', () => { |
| 76 | + it('net.connect() throws when lookup does not return a string', (t, done) => { |
| 77 | + const options = { |
| 78 | + host: 'example.com', |
| 79 | + port: 80, |
| 80 | + lookup: brokenCustomLookup, |
| 81 | + family: 4 |
| 82 | + }; |
| 83 | + |
| 84 | + const socket = net.connect(options, common.mustNotCall()); |
| 85 | + socket.on('error', (err) => { |
| 86 | + t.assert.strictEqual(err.code, 'ERR_INVALID_IP_ADDRESS'); |
| 87 | + |
| 88 | + done(); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('when family is ipv6', () => { |
| 94 | + it('net.connect() throws when lookup does not return a string', (t, done) => { |
| 95 | + const options = { |
| 96 | + host: 'example.com', |
| 97 | + port: 80, |
| 98 | + lookup: brokenCustomLookup, |
| 99 | + family: 6 |
| 100 | + }; |
| 101 | + |
| 102 | + const socket = net.connect(options, common.mustNotCall()); |
| 103 | + socket.on('error', (err) => { |
| 104 | + t.assert.strictEqual(err.code, 'ERR_INVALID_IP_ADDRESS'); |
| 105 | + |
| 106 | + done(); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments