|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Verify that exceptions from a callback don't result in |
| 4 | +// failed CHECKs when trying to print the exception message. |
| 5 | + |
| 6 | +// This test is convoluted because it needs to trigger a callback |
| 7 | +// into JS land at just the right time when an exception is pending, |
| 8 | +// and does so by exploiting a weakness in the streams infrastructure. |
| 9 | +// I won't shed any tears if this test ever becomes invalidated. |
| 10 | + |
| 11 | +const common = require('../common'); |
| 12 | + |
| 13 | +if (!common.hasCrypto) |
| 14 | + common.skip('missing crypto'); |
| 15 | + |
| 16 | +if (process.argv[2] === 'child') { |
| 17 | + const fixtures = require('../common/fixtures'); |
| 18 | + const https = require('https'); |
| 19 | + const net = require('net'); |
| 20 | + const tls = require('tls'); |
| 21 | + const { Duplex } = require('stream'); |
| 22 | + const { mustCall } = common; |
| 23 | + |
| 24 | + const cert = fixtures.readSync('test_cert.pem'); |
| 25 | + const key = fixtures.readSync('test_key.pem'); |
| 26 | + |
| 27 | + net.createServer(mustCall(onplaintext)).listen(0, mustCall(onlisten)); |
| 28 | + |
| 29 | + function onlisten() { |
| 30 | + const { port } = this.address(); |
| 31 | + https.get({ port, rejectUnauthorized: false }); |
| 32 | + } |
| 33 | + |
| 34 | + function onplaintext(c) { |
| 35 | + const d = new class extends Duplex { |
| 36 | + _read(n) { |
| 37 | + const data = c.read(n); |
| 38 | + if (data) d.push(data); |
| 39 | + } |
| 40 | + _write(...xs) { |
| 41 | + c.write(...xs); |
| 42 | + } |
| 43 | + }(); |
| 44 | + c.on('data', d.push.bind(d)); |
| 45 | + |
| 46 | + const options = { key, cert }; |
| 47 | + const fail = () => { throw new Error('eyecatcher'); }; |
| 48 | + tls.createServer(options, mustCall(fail)).emit('connection', d); |
| 49 | + } |
| 50 | +} else { |
| 51 | + const assert = require('assert'); |
| 52 | + const { spawnSync } = require('child_process'); |
| 53 | + const result = spawnSync(process.execPath, [__filename, 'child']); |
| 54 | + const stderr = result.stderr.toString(); |
| 55 | + const ok = stderr.includes('Error: eyecatcher'); |
| 56 | + assert(ok, stderr); |
| 57 | +} |
0 commit comments