TestRunner.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * You may redistribute this program and/or modify it under the terms of
  3. * the GNU General Public License as published by the Free Software Foundation,
  4. * either version 3 of the License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. */
  14. var Spawn = require('child_process').spawn;
  15. var Http = require("http");
  16. var Fs = require("fs");
  17. var TIMEOUT = 15000;
  18. var parseURL = function (url)
  19. {
  20. url = url.replace(/[a-zA-Z]*:\/\//, '');
  21. var path = url.split('/')[1] || '';
  22. url = url.split('/')[0];
  23. var port = url.split(':')[1];
  24. url = url.split(':')[0];
  25. port = Number(port) || 8083;
  26. var host = url || undefined;
  27. return {
  28. host: host,
  29. port: port,
  30. path: '/' + path
  31. };
  32. };
  33. var remote = module.exports.remote = function (url, argv) {
  34. var params = parseURL(url);
  35. if (!params.host) { throw new Error("For the client, the hostname is required"); }
  36. params.headers = { args: [ JSON.stringify(argv) ] };
  37. params.method = 'POST';
  38. return function (fileName, callback) {
  39. var out = [];
  40. out.push('Testing Remotely');
  41. var req = Http.request(params, function(res) {
  42. if (res.statusCode !== 200) {
  43. out.push('STATUS: ' + res.statusCode);
  44. out.push('HEADERS: ' + JSON.stringify(res.headers));
  45. }
  46. res.setEncoding('utf8');
  47. var body = '';
  48. res.on('data', function (chunk) { body += String(chunk); });
  49. res.on('end', function () {
  50. var ret = JSON.parse(body);
  51. if (ret.returnCode !== 0) {
  52. output.push(ret.stdout);
  53. }
  54. out.push(ret.stderr);
  55. callback(out.join('\n'), (ret.returnCode !== 0));
  56. });
  57. });
  58. Fs.createReadStream(fileName).pipe(req);
  59. req.on('error', function(e) {
  60. console.log('problem with request: ' + e.message);
  61. });
  62. };
  63. };
  64. var client = function (url, fileName, argv) {
  65. var params = parseURL(url);
  66. if (!params.host) { throw new Error("For the client, the hostname is required"); }
  67. params.headers = { args: [ JSON.stringify(argv) ] };
  68. params.method = 'POST';
  69. var req = Http.request(params, function(res) {
  70. if (res.statusCode !== 200) {
  71. console.log('STATUS: ' + res.statusCode);
  72. console.log('HEADERS: ' + JSON.stringify(res.headers));
  73. }
  74. res.setEncoding('utf8');
  75. var body = '';
  76. res.on('data', function (chunk) { body += String(chunk); });
  77. res.on('end', function () {
  78. var ret = JSON.parse(body);
  79. process.stdout.write(ret.stdout);
  80. process.stderr.write(ret.stderr);
  81. process.exit(ret.returnCode);
  82. });
  83. });
  84. Fs.createReadStream(fileName).pipe(req);
  85. req.on('error', function(e) {
  86. console.log('problem with request: ' + e.message);
  87. });
  88. };
  89. /// Server
  90. var spawnProc = function(file, args, callback, timeoutMilliseconds) {
  91. var child = Spawn(file, args);
  92. var out = '', err = '';
  93. var to = setTimeout(function() {
  94. child.kill('SIGKILL');
  95. err += "TIMEOUT\n";
  96. callback(1000, out, err);
  97. }, timeoutMilliseconds);
  98. child.stdout.on('data', function (data) { out += String(data); });
  99. child.stderr.on('data', function (data) { err += String(data); });
  100. child.on('close', function (code) {
  101. clearTimeout(to);
  102. callback(code, out, err);
  103. });
  104. child.on('error', function(err) {
  105. clearTimeout(to);
  106. callback(1, '', err.stack);
  107. });
  108. };
  109. var local = module.exports.local = function (argv) {
  110. return function (fileName, callback) {
  111. var output = [];
  112. spawnProc(fileName, argv, function (code, out, err) {
  113. if (code !== 0) {
  114. output.push(out);
  115. }
  116. output.push(err);
  117. callback(output.join('\n'), (code !== 0));
  118. }, TIMEOUT);
  119. };
  120. };
  121. var send = function (response, content) {
  122. response.writeHeader(200, {"Content-Type": "text/plain"});
  123. response.write(content);
  124. response.end();
  125. };
  126. var runTest = function (fileName, args, response, timeoutMilliseconds) {
  127. Fs.chmodSync(fileName, '755');
  128. setTimeout(function () {
  129. spawnProc(fileName, args, function(code, out, err) {
  130. send(response, JSON.stringify({ returnCode: code, stdout: out, stderr: err }));
  131. //Fs.unlink(fileName);
  132. }, timeoutMilliseconds);
  133. }, 100);
  134. };
  135. var server = function (url, tempDir) {
  136. var params = parseURL(url);
  137. tempDir = tempDir || '/tmp';
  138. console.log("Serving http://" + (params.host || '<any>') + ':' + params.port + params.path);
  139. Http.createServer(function(request, response) {
  140. if (request.method === 'POST' && request.url === params.path) {
  141. var args = JSON.parse(request.headers.args);
  142. var fileName = tempDir + "/test-" + String(Math.random()).substring(2) + ".exe";
  143. request.pipe(Fs.createWriteStream(fileName));
  144. request.on("end", function() {
  145. runTest(fileName, args, response, TIMEOUT);
  146. });
  147. } else {
  148. response.end();
  149. }
  150. }).listen(params.port, params.host);
  151. };
  152. /// Main
  153. var usage = function ()
  154. {
  155. console.log(
  156. "Usage:\n" +
  157. " TestRunner server [http://][<hostname>][:<port>][/<password>] [tempDir]\n" +
  158. " bind to <port> (default port is 8083) and allow remote execution by anyone\n" +
  159. " providing <password>. <tempDir> will be used for temporary files (default is /tmp)\n" +
  160. " http:// is optional, you can put https:// or penispenispenis:// or whatever you\n" +
  161. " want but only http is supported.\n" +
  162. " To bind to all interfaces, you can omit the hostname, eg: :3333/supersecret\n" +
  163. " and to have the default port, you can provide only the password eg: /secret\n" +
  164. " or the hostname and password, eg: mycomputer/secret\n" +
  165. " The server will print a line explaining exactly what hostname/port/pass it is\n" +
  166. " serving.\n" +
  167. "\n" +
  168. " TestRunner client [http://][<hostname>][:<port>][/<password>] <executable> [argv1, " +
  169. "[argv2, ...]]\n" +
  170. " connect to the specified URL and upload <exectable> to be run with\n" +
  171. " arguments <argv1> <argv2> ...\n" +
  172. " the URL has the same semantics as that for the server except the hostname must be\n" +
  173. " specified.");
  174. };
  175. var main = function (argv)
  176. {
  177. var cli = argv.indexOf("client");
  178. if (cli !== -1) {
  179. argv.splice(0,cli+1);
  180. var cliUrl = argv.shift();
  181. var fileName = argv.shift();
  182. return client(cliUrl, fileName, argv);
  183. }
  184. var serv = argv.indexOf("server");
  185. if (serv !== -1) {
  186. argv.splice(0,serv+1);
  187. var servUrl = argv.shift();
  188. var tempDir = argv.shift();
  189. return server(servUrl, tempDir);
  190. }
  191. return usage();
  192. };
  193. if (!module.parent) {
  194. main(process.argv);
  195. }