@@ -8,73 +8,51 @@ const tls = require('tls');
8
8
const net = require ( 'net' ) ;
9
9
const fixtures = require ( '../common/fixtures' ) ;
10
10
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
-
24
11
const key = fixtures . readKey ( 'agent2-key.pem' ) ;
25
12
const cert = fixtures . readKey ( 'agent2-cert.pem' ) ;
26
13
27
- let tlsSocket ;
28
- // tls server
14
+ let serverTlsSocket ;
29
15
const 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 ;
36
17
} ) ;
37
18
19
+ // A plain net server, that manually passes connections to the TLS
20
+ // server to be upgraded
38
21
let netSocket ;
39
- // plain tcp server
40
22
const netServer = net . createServer ( ( socket ) => {
41
- // If client wants to use tls
42
23
tlsServer . emit ( 'connection' , socket ) ;
43
24
44
25
netSocket = socket ;
45
26
} ) . listen ( 0 , common . mustCall ( function ( ) {
46
27
connectClient ( netServer ) ;
47
28
} ) ) ;
48
29
30
+ // A client that connects, sends one message, and closes the raw connection:
49
31
function connectClient ( server ) {
50
- const tlsConnection = tls . connect ( {
32
+ const clientTlsSocket = tls . connect ( {
51
33
host : 'localhost' ,
52
34
port : server . address ( ) . port ,
53
35
rejectUnauthorized : false
54
36
} ) ;
55
37
56
- tlsConnection . write ( 'foo' , 'utf8' , common . mustCall ( ( ) => {
38
+ clientTlsSocket . write ( 'foo' , 'utf8' , common . mustCall ( ( ) => {
57
39
assert ( netSocket ) ;
58
40
netSocket . setTimeout ( common . platformTimeout ( 10 ) , common . mustCall ( ( ) => {
59
- assert ( tlsSocket ) ;
60
- // This breaks if TLSSocket is already managing the socket:
41
+ assert ( serverTlsSocket ) ;
42
+
61
43
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
+ } ) ;
72
56
} ) ) ;
73
57
} ) ) ;
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
- } ) ;
80
58
}
0 commit comments