@@ -8,73 +8,51 @@ const tls = require('tls');
88const net = require ( 'net' ) ;
99const fixtures = require ( '../common/fixtures' ) ;
1010
11- // Regression test for https://github.com/nodejs/node/issues/8074
12- //
13- // This test has a dependency on the order in which the TCP connection is made,
14- // and TLS server handshake completes. It assumes those server side events occur
15- // before the client side write callback, which is not guaranteed by the TLS
16- // API. It usually passes with TLS1.3, but TLS1.3 didn't exist at the time the
17- // bug existed.
18- //
19- // Pin the test to TLS1.2, since the test shouldn't be changed in a way that
20- // doesn't trigger a segfault in Node.js 7.7.3:
21- // https://github.com/nodejs/node/issues/13184#issuecomment-303700377
22- tls . DEFAULT_MAX_VERSION = 'TLSv1.2' ;
23-
2411const key = fixtures . readKey ( 'agent2-key.pem' ) ;
2512const cert = fixtures . readKey ( 'agent2-cert.pem' ) ;
2613
27- let tlsSocket ;
28- // tls server
14+ let serverTlsSocket ;
2915const tlsServer = tls . createServer ( { cert, key } , ( socket ) => {
30- tlsSocket = socket ;
31- socket . on ( 'error' , common . mustCall ( ( error ) => {
32- assert . strictEqual ( error . code , 'EINVAL' ) ;
33- tlsServer . close ( ) ;
34- netServer . close ( ) ;
35- } ) ) ;
16+ serverTlsSocket = socket ;
3617} ) ;
3718
19+ // A plain net server, that manually passes connections to the TLS
20+ // server to be upgraded
3821let netSocket ;
39- // plain tcp server
4022const netServer = net . createServer ( ( socket ) => {
41- // If client wants to use tls
4223 tlsServer . emit ( 'connection' , socket ) ;
4324
4425 netSocket = socket ;
4526} ) . listen ( 0 , common . mustCall ( function ( ) {
4627 connectClient ( netServer ) ;
4728} ) ) ;
4829
30+ // A client that connects, sends one message, and closes the raw connection:
4931function connectClient ( server ) {
50- const tlsConnection = tls . connect ( {
32+ const clientTlsSocket = tls . connect ( {
5133 host : 'localhost' ,
5234 port : server . address ( ) . port ,
5335 rejectUnauthorized : false
5436 } ) ;
5537
56- tlsConnection . write ( 'foo' , 'utf8' , common . mustCall ( ( ) => {
38+ clientTlsSocket . write ( 'foo' , 'utf8' , common . mustCall ( ( ) => {
5739 assert ( netSocket ) ;
5840 netSocket . setTimeout ( common . platformTimeout ( 10 ) , common . mustCall ( ( ) => {
59- assert ( tlsSocket ) ;
60- // This breaks if TLSSocket is already managing the socket:
41+ assert ( serverTlsSocket ) ;
42+
6143 netSocket . destroy ( ) ;
62- const interval = setInterval ( ( ) => {
63- // Checking this way allows us to do the write at a time that causes a
64- // segmentation fault (not always, but often) in Node.js 7.7.3 and
65- // earlier. If we instead, for example, wait on the `close` event, then
66- // it will not segmentation fault, which is what this test is all about.
67- if ( tlsSocket . _handle . _parent . bytesRead === 0 ) {
68- tlsSocket . write ( 'bar' ) ;
69- clearInterval ( interval ) ;
70- }
71- } , 1 ) ;
44+
45+ setImmediate ( ( ) => {
46+ assert . strictEqual ( netSocket . destroyed , true ) ;
47+ assert . strictEqual ( clientTlsSocket . destroyed , true ) ;
48+
49+ setImmediate ( ( ) => {
50+ assert . strictEqual ( serverTlsSocket . destroyed , true ) ;
51+
52+ tlsServer . close ( ) ;
53+ netServer . close ( ) ;
54+ } ) ;
55+ } ) ;
7256 } ) ) ;
7357 } ) ) ;
74- tlsConnection . on ( 'error' , ( e ) => {
75- // Tolerate the occasional ECONNRESET.
76- // Ref: https://github.com/nodejs/node/issues/13184
77- if ( e . code !== 'ECONNRESET' )
78- throw e ;
79- } ) ;
8058}
0 commit comments