|
23 | 23 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
24 | 24 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
25 | 25 | import tls from 'node:tls'; |
26 | | -import { strictEqual, ok, rejects } from 'node:assert'; |
| 26 | +import { strictEqual, ok, rejects, throws } from 'node:assert'; |
27 | 27 | import { once } from 'node:events'; |
| 28 | +import net from 'node:net'; |
28 | 29 |
|
29 | 30 | // Tests are taken from |
30 | 31 | // https://github.com/nodejs/node/blob/304743655d5236c2edc39094336ee2667600b684/test/parallel/test-tls-connect-abort-controller.js |
@@ -209,3 +210,75 @@ export const tlsConnectNoHost = { |
209 | 210 | await promise; |
210 | 211 | }, |
211 | 212 | }; |
| 213 | + |
| 214 | +// Tests are taken from |
| 215 | +// https://github.com/nodejs/node/blob/755e4603fd1679de72d250514ea5096b272ae8d6/test/parallel/test-tls-connect-given-socket.js |
| 216 | +export const tlsConnectGivenSocket = { |
| 217 | + async test() { |
| 218 | + const promises = []; |
| 219 | + let waiting = 2; |
| 220 | + function establish(socket, shouldNotCallCallback = false) { |
| 221 | + const { promise, resolve } = Promise.withResolvers(); |
| 222 | + promises.push(promise); |
| 223 | + const client = tls.connect( |
| 224 | + { |
| 225 | + socket: socket, |
| 226 | + }, |
| 227 | + () => { |
| 228 | + if (shouldNotCallCallback) { |
| 229 | + reject(new Error('should not have called tls.connect() callback')); |
| 230 | + return; |
| 231 | + } |
| 232 | + let data = ''; |
| 233 | + client |
| 234 | + .on('data', (chunk) => { |
| 235 | + data += chunk.toString(); |
| 236 | + }) |
| 237 | + .on('end', () => { |
| 238 | + strictEqual(data, 'Hello'); |
| 239 | + if (--waiting === 0) { |
| 240 | + resolve(); |
| 241 | + } |
| 242 | + }); |
| 243 | + } |
| 244 | + ); |
| 245 | + |
| 246 | + if (shouldNotCallCallback) { |
| 247 | + queueMicrotask(() => resolve()); |
| 248 | + } |
| 249 | + |
| 250 | + return client; |
| 251 | + } |
| 252 | + |
| 253 | + const port = 8887; |
| 254 | + // Immediate death socket |
| 255 | + const immediateDeath = net.connect(port); |
| 256 | + establish(immediateDeath, true).destroy(); |
| 257 | + |
| 258 | + // Outliving |
| 259 | + const outlivingTCPPromise = Promise.withResolvers(); |
| 260 | + const outlivingTCP = net.connect(port, () => { |
| 261 | + outlivingTLS.destroy(); |
| 262 | + next(); |
| 263 | + outlivingTCPPromise.resolve(); |
| 264 | + }); |
| 265 | + promises.push(outlivingTCPPromise.promise); |
| 266 | + const outlivingTLS = establish(outlivingTCP, true); |
| 267 | + |
| 268 | + function next() { |
| 269 | + // Already connected socket |
| 270 | + const { promise, resolve } = Promise.withResolvers(); |
| 271 | + const connected = net.connect(port, () => { |
| 272 | + establish(connected); |
| 273 | + resolve(); |
| 274 | + }); |
| 275 | + promises.push(promise); |
| 276 | + |
| 277 | + // Connecting socket |
| 278 | + const connecting = net.connect(port); |
| 279 | + establish(connecting); |
| 280 | + } |
| 281 | + |
| 282 | + await Promise.all(promises); |
| 283 | + }, |
| 284 | +}; |
0 commit comments