11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciif (!common.hasCrypto)
261cb0ef41Sopenharmony_ci  common.skip('missing crypto');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciconst assert = require('assert');
291cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
301cb0ef41Sopenharmony_ciconst https = require('https');
311cb0ef41Sopenharmony_ciconst http = require('http');
321cb0ef41Sopenharmony_ciconst tls = require('tls');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst tests = [];
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst serverOptions = {
371cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
381cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem')
391cb0ef41Sopenharmony_ci};
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_cifunction test(fn) {
421cb0ef41Sopenharmony_ci  if (!tests.length)
431cb0ef41Sopenharmony_ci    process.nextTick(run);
441cb0ef41Sopenharmony_ci  tests.push(common.mustCall(fn));
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cifunction run() {
481cb0ef41Sopenharmony_ci  const fn = tests.shift();
491cb0ef41Sopenharmony_ci  if (fn) {
501cb0ef41Sopenharmony_ci    fn(run);
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_citest(function serverTimeout(cb) {
551cb0ef41Sopenharmony_ci  const server = https.createServer(serverOptions);
561cb0ef41Sopenharmony_ci  server.listen(common.mustCall(() => {
571cb0ef41Sopenharmony_ci    const s = server.setTimeout(50, common.mustCall((socket) => {
581cb0ef41Sopenharmony_ci      socket.destroy();
591cb0ef41Sopenharmony_ci      server.close();
601cb0ef41Sopenharmony_ci      cb();
611cb0ef41Sopenharmony_ci    }));
621cb0ef41Sopenharmony_ci    assert.ok(s instanceof https.Server);
631cb0ef41Sopenharmony_ci    https.get({
641cb0ef41Sopenharmony_ci      port: server.address().port,
651cb0ef41Sopenharmony_ci      rejectUnauthorized: false
661cb0ef41Sopenharmony_ci    }).on('error', common.mustCall());
671cb0ef41Sopenharmony_ci  }));
681cb0ef41Sopenharmony_ci});
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_citest(function serverRequestTimeout(cb) {
711cb0ef41Sopenharmony_ci  const server = https.createServer(
721cb0ef41Sopenharmony_ci    serverOptions,
731cb0ef41Sopenharmony_ci    common.mustCall((req, res) => {
741cb0ef41Sopenharmony_ci      // Just do nothing, we should get a timeout event.
751cb0ef41Sopenharmony_ci      const s = req.setTimeout(50, common.mustCall((socket) => {
761cb0ef41Sopenharmony_ci        socket.destroy();
771cb0ef41Sopenharmony_ci        server.close();
781cb0ef41Sopenharmony_ci        cb();
791cb0ef41Sopenharmony_ci      }));
801cb0ef41Sopenharmony_ci      assert.ok(s instanceof http.IncomingMessage);
811cb0ef41Sopenharmony_ci    }));
821cb0ef41Sopenharmony_ci  server.listen(common.mustCall(() => {
831cb0ef41Sopenharmony_ci    const req = https.request({
841cb0ef41Sopenharmony_ci      port: server.address().port,
851cb0ef41Sopenharmony_ci      method: 'POST',
861cb0ef41Sopenharmony_ci      rejectUnauthorized: false
871cb0ef41Sopenharmony_ci    });
881cb0ef41Sopenharmony_ci    req.on('error', common.mustCall());
891cb0ef41Sopenharmony_ci    req.write('Hello');
901cb0ef41Sopenharmony_ci    // req is in progress
911cb0ef41Sopenharmony_ci  }));
921cb0ef41Sopenharmony_ci});
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_citest(function serverResponseTimeout(cb) {
951cb0ef41Sopenharmony_ci  const server = https.createServer(
961cb0ef41Sopenharmony_ci    serverOptions,
971cb0ef41Sopenharmony_ci    common.mustCall((req, res) => {
981cb0ef41Sopenharmony_ci      // Just do nothing, we should get a timeout event.
991cb0ef41Sopenharmony_ci      const s = res.setTimeout(50, common.mustCall((socket) => {
1001cb0ef41Sopenharmony_ci        socket.destroy();
1011cb0ef41Sopenharmony_ci        server.close();
1021cb0ef41Sopenharmony_ci        cb();
1031cb0ef41Sopenharmony_ci      }));
1041cb0ef41Sopenharmony_ci      assert.ok(s instanceof http.OutgoingMessage);
1051cb0ef41Sopenharmony_ci    }));
1061cb0ef41Sopenharmony_ci  server.listen(common.mustCall(() => {
1071cb0ef41Sopenharmony_ci    https.get({
1081cb0ef41Sopenharmony_ci      port: server.address().port,
1091cb0ef41Sopenharmony_ci      rejectUnauthorized: false
1101cb0ef41Sopenharmony_ci    }).on('error', common.mustCall());
1111cb0ef41Sopenharmony_ci  }));
1121cb0ef41Sopenharmony_ci});
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_citest(function serverRequestNotTimeoutAfterEnd(cb) {
1151cb0ef41Sopenharmony_ci  const server = https.createServer(
1161cb0ef41Sopenharmony_ci    serverOptions,
1171cb0ef41Sopenharmony_ci    common.mustCall((req, res) => {
1181cb0ef41Sopenharmony_ci      // Just do nothing, we should get a timeout event.
1191cb0ef41Sopenharmony_ci      const s = req.setTimeout(50, common.mustNotCall());
1201cb0ef41Sopenharmony_ci      assert.ok(s instanceof http.IncomingMessage);
1211cb0ef41Sopenharmony_ci      res.on('timeout', common.mustCall());
1221cb0ef41Sopenharmony_ci    }));
1231cb0ef41Sopenharmony_ci  server.on('timeout', common.mustCall((socket) => {
1241cb0ef41Sopenharmony_ci    socket.destroy();
1251cb0ef41Sopenharmony_ci    server.close();
1261cb0ef41Sopenharmony_ci    cb();
1271cb0ef41Sopenharmony_ci  }));
1281cb0ef41Sopenharmony_ci  server.listen(common.mustCall(() => {
1291cb0ef41Sopenharmony_ci    https.get({
1301cb0ef41Sopenharmony_ci      port: server.address().port,
1311cb0ef41Sopenharmony_ci      rejectUnauthorized: false
1321cb0ef41Sopenharmony_ci    }).on('error', common.mustCall());
1331cb0ef41Sopenharmony_ci  }));
1341cb0ef41Sopenharmony_ci});
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_citest(function serverResponseTimeoutWithPipeline(cb) {
1371cb0ef41Sopenharmony_ci  let caughtTimeout = '';
1381cb0ef41Sopenharmony_ci  let secReceived = false;
1391cb0ef41Sopenharmony_ci  process.on('exit', () => {
1401cb0ef41Sopenharmony_ci    assert.strictEqual(caughtTimeout, '/2');
1411cb0ef41Sopenharmony_ci  });
1421cb0ef41Sopenharmony_ci  const server = https.createServer(serverOptions, (req, res) => {
1431cb0ef41Sopenharmony_ci    if (req.url === '/2')
1441cb0ef41Sopenharmony_ci      secReceived = true;
1451cb0ef41Sopenharmony_ci    if (req.url === '/1') {
1461cb0ef41Sopenharmony_ci      res.end();
1471cb0ef41Sopenharmony_ci      return;
1481cb0ef41Sopenharmony_ci    }
1491cb0ef41Sopenharmony_ci    const s = res.setTimeout(50, () => {
1501cb0ef41Sopenharmony_ci      caughtTimeout += req.url;
1511cb0ef41Sopenharmony_ci    });
1521cb0ef41Sopenharmony_ci    assert.ok(s instanceof http.OutgoingMessage);
1531cb0ef41Sopenharmony_ci  });
1541cb0ef41Sopenharmony_ci  server.on('timeout', common.mustCall((socket) => {
1551cb0ef41Sopenharmony_ci    if (secReceived) {
1561cb0ef41Sopenharmony_ci      socket.destroy();
1571cb0ef41Sopenharmony_ci      server.close();
1581cb0ef41Sopenharmony_ci      cb();
1591cb0ef41Sopenharmony_ci    }
1601cb0ef41Sopenharmony_ci  }));
1611cb0ef41Sopenharmony_ci  server.listen(common.mustCall(() => {
1621cb0ef41Sopenharmony_ci    const options = {
1631cb0ef41Sopenharmony_ci      port: server.address().port,
1641cb0ef41Sopenharmony_ci      allowHalfOpen: true,
1651cb0ef41Sopenharmony_ci      rejectUnauthorized: false
1661cb0ef41Sopenharmony_ci    };
1671cb0ef41Sopenharmony_ci    const c = tls.connect(options, () => {
1681cb0ef41Sopenharmony_ci      c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
1691cb0ef41Sopenharmony_ci      c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
1701cb0ef41Sopenharmony_ci      c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
1711cb0ef41Sopenharmony_ci    });
1721cb0ef41Sopenharmony_ci  }));
1731cb0ef41Sopenharmony_ci});
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_citest(function idleTimeout(cb) {
1761cb0ef41Sopenharmony_ci  // Test that the an idle connection invokes the timeout callback.
1771cb0ef41Sopenharmony_ci  const server = https.createServer(serverOptions);
1781cb0ef41Sopenharmony_ci  const s = server.setTimeout(50, common.mustCall((socket) => {
1791cb0ef41Sopenharmony_ci    socket.destroy();
1801cb0ef41Sopenharmony_ci    server.close();
1811cb0ef41Sopenharmony_ci    cb();
1821cb0ef41Sopenharmony_ci  }));
1831cb0ef41Sopenharmony_ci  assert.ok(s instanceof https.Server);
1841cb0ef41Sopenharmony_ci  server.listen(common.mustCall(() => {
1851cb0ef41Sopenharmony_ci    const options = {
1861cb0ef41Sopenharmony_ci      port: server.address().port,
1871cb0ef41Sopenharmony_ci      allowHalfOpen: true,
1881cb0ef41Sopenharmony_ci      rejectUnauthorized: false
1891cb0ef41Sopenharmony_ci    };
1901cb0ef41Sopenharmony_ci    const c = tls.connect(options, () => {
1911cb0ef41Sopenharmony_ci      // ECONNRESET could happen on a heavily-loaded server.
1921cb0ef41Sopenharmony_ci      c.on('error', (e) => {
1931cb0ef41Sopenharmony_ci        if (e.message !== 'read ECONNRESET')
1941cb0ef41Sopenharmony_ci          throw e;
1951cb0ef41Sopenharmony_ci      });
1961cb0ef41Sopenharmony_ci      c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
1971cb0ef41Sopenharmony_ci      // Keep-Alive
1981cb0ef41Sopenharmony_ci    });
1991cb0ef41Sopenharmony_ci  }));
2001cb0ef41Sopenharmony_ci});
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_citest(function fastTimeout(cb) {
2031cb0ef41Sopenharmony_ci  // Test that the socket timeout fires but no timeout fires for the request.
2041cb0ef41Sopenharmony_ci  let connectionHandlerInvoked = false;
2051cb0ef41Sopenharmony_ci  let timeoutHandlerInvoked = false;
2061cb0ef41Sopenharmony_ci  let connectionSocket;
2071cb0ef41Sopenharmony_ci
2081cb0ef41Sopenharmony_ci  function invokeCallbackIfDone() {
2091cb0ef41Sopenharmony_ci    if (connectionHandlerInvoked && timeoutHandlerInvoked) {
2101cb0ef41Sopenharmony_ci      connectionSocket.destroy();
2111cb0ef41Sopenharmony_ci      server.close();
2121cb0ef41Sopenharmony_ci      cb();
2131cb0ef41Sopenharmony_ci    }
2141cb0ef41Sopenharmony_ci  }
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_ci  const server = https.createServer(serverOptions, common.mustCall(
2171cb0ef41Sopenharmony_ci    (req, res) => {
2181cb0ef41Sopenharmony_ci      req.on('timeout', common.mustNotCall());
2191cb0ef41Sopenharmony_ci      res.end();
2201cb0ef41Sopenharmony_ci      connectionHandlerInvoked = true;
2211cb0ef41Sopenharmony_ci      invokeCallbackIfDone();
2221cb0ef41Sopenharmony_ci    }
2231cb0ef41Sopenharmony_ci  ));
2241cb0ef41Sopenharmony_ci  const s = server.setTimeout(1, common.mustCall((socket) => {
2251cb0ef41Sopenharmony_ci    connectionSocket = socket;
2261cb0ef41Sopenharmony_ci    timeoutHandlerInvoked = true;
2271cb0ef41Sopenharmony_ci    invokeCallbackIfDone();
2281cb0ef41Sopenharmony_ci  }));
2291cb0ef41Sopenharmony_ci  assert.ok(s instanceof https.Server);
2301cb0ef41Sopenharmony_ci  server.listen(common.mustCall(() => {
2311cb0ef41Sopenharmony_ci    const options = {
2321cb0ef41Sopenharmony_ci      port: server.address().port,
2331cb0ef41Sopenharmony_ci      allowHalfOpen: true,
2341cb0ef41Sopenharmony_ci      rejectUnauthorized: false
2351cb0ef41Sopenharmony_ci    };
2361cb0ef41Sopenharmony_ci    const c = tls.connect(options, () => {
2371cb0ef41Sopenharmony_ci      c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
2381cb0ef41Sopenharmony_ci      // Keep-Alive
2391cb0ef41Sopenharmony_ci    });
2401cb0ef41Sopenharmony_ci  }));
2411cb0ef41Sopenharmony_ci});
242