test: fix test failure with shared openssl
authorShigeki Ohtsu <ohtsu@iij.ad.jp>
Mon, 9 Feb 2015 04:56:38 +0000 (13:56 +0900)
committerShigeki Ohtsu <ohtsu@iij.ad.jp>
Mon, 9 Feb 2015 16:24:02 +0000 (01:24 +0900)
When configured with share openssl, use external openssl command and
check if it can be executed.

Fixes: https://github.com/iojs/io.js/issues/618
PR-URL: https://github.com/iojs/io.js/pull/762
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
test/common.js
test/parallel/test-tls-no-sslv3.js

index b682a46..1599de0 100644 (file)
@@ -2,6 +2,7 @@ var path = require('path');
 var fs = require('fs');
 var assert = require('assert');
 var os = require('os');
+var child_process = require('child_process');
 
 exports.testDir = path.dirname(__filename);
 exports.fixturesDir = path.join(exports.testDir, 'fixtures');
@@ -18,10 +19,33 @@ if (process.env.TEST_THREAD_ID) {
 }
 exports.tmpDir = path.join(exports.testDir, exports.tmpDirName);
 
-exports.opensslCli = path.join(path.dirname(process.execPath), 'openssl-cli');
+var opensslCli = null;
+
+// opensslCli defined lazily to reduce overhead of spawnSync
+Object.defineProperty(exports, 'opensslCli', {get: function() {
+  if (opensslCli !== null) return opensslCli;
+
+  if (process.config.variables.node_shared_openssl) {
+    // use external command
+    opensslCli = 'openssl';
+  } else {
+    // use command built from sources included in io.js repository
+    opensslCli = path.join(path.dirname(process.execPath), 'openssl-cli');
+  }
+
+  if (process.platform === 'win32') opensslCli += '.exe';
+
+  var openssl_cmd = child_process.spawnSync(opensslCli, ['version']);
+  if (openssl_cmd.status !== 0 || openssl_cmd.error !== undefined) {
+    // openssl command cannot be executed
+    opensslCli = false;
+  }
+  return opensslCli;
+}, enumerable: true });
+
+
 if (process.platform === 'win32') {
   exports.PIPE = '\\\\.\\pipe\\libuv-test';
-  exports.opensslCli += '.exe';
 } else {
   exports.PIPE = exports.tmpDir + '/test.sock';
 }
@@ -37,12 +61,6 @@ if (process.env.NODE_COMMON_PIPE) {
   }
 }
 
-try {
-  fs.accessSync(exports.opensslCli);
-} catch (err) {
-  exports.opensslCli = false;
-}
-
 if (process.platform === 'win32') {
   exports.faketimeCli = false;
 } else {
index 8bdc4c7..47a0aee 100644 (file)
@@ -9,6 +9,11 @@ var fs = require('fs');
 var spawn = require('child_process').spawn;
 var tls = require('tls');
 
+if (common.opensslCli === false) {
+  console.error('Skipping because openssl command cannot be executed');
+  process.exit(0);
+}
+
 var cert = fs.readFileSync(common.fixturesDir + '/test_cert.pem');
 var key = fs.readFileSync(common.fixturesDir + '/test_key.pem');
 var server = tls.createServer({ cert: cert, key: key }, assert.fail);