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_ci// Uploading a big file via HTTPS causes node to drop out of the event loop.
241cb0ef41Sopenharmony_ci// https://github.com/joyent/node/issues/892
251cb0ef41Sopenharmony_ci// In this test we set up an HTTPS in this process and launch a subprocess
261cb0ef41Sopenharmony_ci// to POST a 32mb file to us. A bug in the pause/resume functionality of the
271cb0ef41Sopenharmony_ci// TLS server causes the child process to exit cleanly before having sent
281cb0ef41Sopenharmony_ci// the entire buffer.
291cb0ef41Sopenharmony_ciconst common = require('../common');
301cb0ef41Sopenharmony_ciif (!common.hasCrypto)
311cb0ef41Sopenharmony_ci  common.skip('missing crypto');
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst assert = require('assert');
341cb0ef41Sopenharmony_ciconst spawn = require('child_process').spawn;
351cb0ef41Sopenharmony_ciconst https = require('https');
361cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciconst bytesExpected = 1024 * 1024 * 32;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cilet started = false;
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciconst childScript = fixtures.path('GH-892-request.js');
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cifunction makeRequest() {
451cb0ef41Sopenharmony_ci  if (started) return;
461cb0ef41Sopenharmony_ci  started = true;
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  let stderrBuffer = '';
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  // Pass along --trace-deprecation/--throw-deprecation in
511cb0ef41Sopenharmony_ci  // process.execArgv to track down nextTick recursion errors
521cb0ef41Sopenharmony_ci  // more easily.  Also, this is handy when using this test to
531cb0ef41Sopenharmony_ci  // view V8 opt/deopt behavior.
541cb0ef41Sopenharmony_ci  const args = process.execArgv.concat([ childScript,
551cb0ef41Sopenharmony_ci                                         server.address().port,
561cb0ef41Sopenharmony_ci                                         bytesExpected ]);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  const child = spawn(process.execPath, args);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  child.on('exit', function(code) {
611cb0ef41Sopenharmony_ci    assert.match(stderrBuffer, /DONE/);
621cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
631cb0ef41Sopenharmony_ci  });
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  // The following two lines forward the stdio from the child
661cb0ef41Sopenharmony_ci  // to parent process for debugging.
671cb0ef41Sopenharmony_ci  child.stderr.pipe(process.stderr);
681cb0ef41Sopenharmony_ci  child.stdout.pipe(process.stdout);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  // Buffer the stderr so that we can check that it got 'DONE'
721cb0ef41Sopenharmony_ci  child.stderr.setEncoding('ascii');
731cb0ef41Sopenharmony_ci  child.stderr.on('data', function(d) {
741cb0ef41Sopenharmony_ci    stderrBuffer += d;
751cb0ef41Sopenharmony_ci  });
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciconst serverOptions = {
801cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
811cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem'),
821cb0ef41Sopenharmony_ci};
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_cilet uploadCount = 0;
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ciconst server = https.Server(serverOptions, function(req, res) {
871cb0ef41Sopenharmony_ci  // Close the server immediately. This test is only doing a single upload.
881cb0ef41Sopenharmony_ci  // We need to make sure the server isn't keeping the event loop alive
891cb0ef41Sopenharmony_ci  // while the upload is in progress.
901cb0ef41Sopenharmony_ci  server.close();
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  req.on('data', function(d) {
931cb0ef41Sopenharmony_ci    process.stderr.write('.');
941cb0ef41Sopenharmony_ci    uploadCount += d.length;
951cb0ef41Sopenharmony_ci  });
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  req.on('end', function() {
981cb0ef41Sopenharmony_ci    assert.strictEqual(uploadCount, bytesExpected);
991cb0ef41Sopenharmony_ci    res.writeHead(200, { 'content-type': 'text/plain' });
1001cb0ef41Sopenharmony_ci    res.end('successful upload\n');
1011cb0ef41Sopenharmony_ci  });
1021cb0ef41Sopenharmony_ci});
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ciserver.listen(0, function() {
1051cb0ef41Sopenharmony_ci  console.log(`expecting ${bytesExpected} bytes`);
1061cb0ef41Sopenharmony_ci  makeRequest();
1071cb0ef41Sopenharmony_ci});
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ciprocess.on('exit', function() {
1101cb0ef41Sopenharmony_ci  console.error(`got ${uploadCount} bytes`);
1111cb0ef41Sopenharmony_ci  assert.strictEqual(uploadCount, bytesExpected);
1121cb0ef41Sopenharmony_ci});
113