b2g/simulator/packages/subprocess/tests/test-subprocess.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4 */
michael@0 5
michael@0 6 const { Cc, Ci } = require("chrome");
michael@0 7 const subprocess = require("subprocess");
michael@0 8 const env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
michael@0 9
michael@0 10 // For now, only test on windows
michael@0 11 if (env.get('OS') && env.get('OS').match(/Windows/)) {
michael@0 12
michael@0 13 exports.testWindows = function (test) {
michael@0 14 test.waitUntilDone();
michael@0 15 let envTestValue = "OK";
michael@0 16 let gotStdout = false;
michael@0 17
michael@0 18 var p = subprocess.call({
michael@0 19 // Retrieve windows cmd.exe path from env
michael@0 20 command: env.get('ComSpec'),
michael@0 21 // In order to execute a simple "echo" function
michael@0 22 arguments: ['/C', 'echo %ENV_TEST%'], // ' & type CON' should display stdin, but doesn't work
michael@0 23 // Printing an environnement variable set here by the parent process
michael@0 24 environment: ['ENV_TEST='+envTestValue],
michael@0 25
michael@0 26 stdin: function(stdin) {
michael@0 27 // Win32 command line is not really made for stdin
michael@0 28 // So it doesn't seems to work as it's hard to retrieve stdin
michael@0 29 stdin.write("stdin");
michael@0 30 stdin.close();
michael@0 31 },
michael@0 32 stdout: function(data) {
michael@0 33 test.assert(!gotStdout,"don't get stdout twice");
michael@0 34 test.assertEqual(data,envTestValue+"\r\n","stdout contains the environment variable");
michael@0 35 gotStdout = true;
michael@0 36 },
michael@0 37 stderr: function(data) {
michael@0 38 test.fail("shouldn't get stderr");
michael@0 39 },
michael@0 40 done: function() {
michael@0 41 test.assert(gotStdout, "got stdout before finished");
michael@0 42 test.done();
michael@0 43 },
michael@0 44 mergeStderr: false
michael@0 45 });
michael@0 46
michael@0 47 }
michael@0 48
michael@0 49 exports.testWindowsStderr = function (test) {
michael@0 50 test.waitUntilDone();
michael@0 51 let gotStderr = false;
michael@0 52
michael@0 53 var p = subprocess.call({
michael@0 54 command: env.get('ComSpec'),
michael@0 55 arguments: ['/C', 'nonexistent'],
michael@0 56
michael@0 57 stdout: function(data) {
michael@0 58 test.fail("shouldn't get stdout");
michael@0 59 },
michael@0 60 stderr: function(data) {
michael@0 61 test.assert(!gotStderr,"don't get stderr twice");
michael@0 62 test.assertEqual(
michael@0 63 data,
michael@0 64 "'nonexistent' is not recognized as an internal or external command,\r\n" +
michael@0 65 "operable program or batch file.\r\n",
michael@0 66 "stderr contains the error message"
michael@0 67 );
michael@0 68 gotStderr = true;
michael@0 69 },
michael@0 70 done: function() {
michael@0 71 test.assert(gotStderr, "got stderr before finished");
michael@0 72 test.done();
michael@0 73 },
michael@0 74 mergeStderr: false
michael@0 75 });
michael@0 76
michael@0 77 }
michael@0 78
michael@0 79 }
michael@0 80
michael@0 81 if (env.get('USER') && env.get('SHELL')) {
michael@0 82
michael@0 83 exports.testUnix = function (test) {
michael@0 84 test.waitUntilDone();
michael@0 85 let envTestValue = "OK";
michael@0 86 let gotStdout = false;
michael@0 87
michael@0 88 var p = subprocess.call({
michael@0 89 command: '/bin/sh',
michael@0 90 // Print stdin and our env variable
michael@0 91 //arguments: ['-c', 'echo $@ $ENV_TEST'],
michael@0 92 environment: ['ENV_TEST='+envTestValue],
michael@0 93
michael@0 94 stdin: function(stdin) {
michael@0 95 stdin.write("echo $ENV_TEST");
michael@0 96 stdin.close();
michael@0 97 },
michael@0 98 stdout: function(data) {
michael@0 99 test.assert(!gotStdout,"don't get stdout twice");
michael@0 100 test.assertEqual(data,envTestValue+"\n","stdout contains the environment variable");
michael@0 101 gotStdout = true;
michael@0 102 },
michael@0 103 stderr: function(data) {
michael@0 104 test.fail("shouldn't get stderr");
michael@0 105 },
michael@0 106 done: function() {
michael@0 107 test.assert(gotStdout, "got stdout before finished");
michael@0 108 test.done();
michael@0 109 },
michael@0 110 mergeStderr: false
michael@0 111 });
michael@0 112 }
michael@0 113
michael@0 114 exports.testUnixStderr = function (test) {
michael@0 115 test.waitUntilDone();
michael@0 116 let gotStderr = false;
michael@0 117
michael@0 118 var p = subprocess.call({
michael@0 119 // Hope that we don't have to give absolute path on linux ...
michael@0 120 command: '/bin/sh',
michael@0 121 arguments: ['nonexistent'],
michael@0 122
michael@0 123 stdout: function(data) {
michael@0 124 test.fail("shouldn't get stdout");
michael@0 125 },
michael@0 126 stderr: function(data) {
michael@0 127 test.assert(!gotStderr,"don't get stderr twice");
michael@0 128 // There is two variant of error message
michael@0 129 if (data == "/bin/sh: 0: Can't open nonexistent\n")
michael@0 130 test.pass("stderr containes the expected error message");
michael@0 131 else
michael@0 132 test.assertEqual(data, "/bin/sh: nonexistent: No such file or directory\n",
michael@0 133 "stderr contains the error message");
michael@0 134 gotStderr = true;
michael@0 135 },
michael@0 136 done: function() {
michael@0 137 test.assert(gotStderr, "got stderr before finished");
michael@0 138 test.done();
michael@0 139 },
michael@0 140 mergeStderr: false
michael@0 141 });
michael@0 142 }
michael@0 143
michael@0 144 }

mercurial