Skip to content
Browse files

fix: console.log regression, fixes #14

* Fixed console.log regression
* Switched from async_testing to should.js for tests
  • Loading branch information...
1 parent 0010521 commit 28f35f6f3cdd8401ef6b1b31e6528cba65545f51 @gf3 committed Jan 18, 2012
Showing with 69 additions and 75 deletions.
  1. +5 −0 example/example.js
  2. +1 −2 lib/sandbox.js
  3. +3 −2 lib/shovel.js
  4. +2 −2 package.json
  5. +0 −69 spec/sandbox.spec.js
  6. +58 −0 test/sandbox_test.js
View
5 example/example.js
@@ -46,3 +46,8 @@ s.run( "x=1;(function() {return this})().console.log.constructor('return this')(
console.log( "Example 9: " + output.result + "\n" )
})
+// Example 10 - Console Log
+s.run( "var x = 5; console.log(x * x); x", function( output ) {
+ console.log( "Example 10: " + output.console + "\n" )
+})
+
View
3 lib/sandbox.js
@@ -1,5 +1,4 @@
-// sandbox.js - Rudimentary JS sandbox
-// Gianni Chiappetta - gf3.ca - 2010
+// sandbox.js - Rudimentary Javascript Sandbox
/*------------------------- INIT -------------------------*/
var fs = require( 'fs' )
View
5 lib/shovel.js
@@ -1,5 +1,4 @@
// shovel.js - Do the heavy lifting in this sandbox
-// Gianni Chiappetta - gf3.ca - 2010
/* ------------------------------ INIT ------------------------------ */
var util = require( 'util' )
@@ -51,6 +50,7 @@ function getSafeRunner() {
}
}
+wat = 0
// Run code
function run() {
var context = Script.createContext();
@@ -60,9 +60,10 @@ function run() {
safeRunner({
send: function (event, value) {
"use strict";
+
switch (event) {
case 'stdout':
- console.push.apply(console, JSON.parse(value).slice(1));
+ console.push(JSON.parse(value)[0]);
break;
case 'end':
result = JSON.parse(value)[0];
View
4 package.json
@@ -6,12 +6,12 @@
[ "Bradley Meck <bradley.meck@gmail.com>"
, "Dominic Tarr (http://cyber-hobo.blogspot.com)"
]
-, "version" : "0.8.3"
+, "version" : "0.8.4"
, "main" : "./lib/sandbox"
, "directories" : { "lib" : "./lib" }
, "engines" : [ "node >=0.5.0" ]
, "devDependencies" :
- { "async_testing": ">=0.3.0"
+ { "should": ">=0.5.1"
}
, "repository" :
{ "type" : "git"
View
69 spec/sandbox.spec.js
@@ -1,69 +0,0 @@
-/* ------------------------------ INIT ------------------------------ */
-var Sandbox = require( '../lib/sandbox' )
- , sb = new Sandbox()
-
-/* ------------------------------ Tests ------------------------------ */
-exports['it should execute basic javascript'] = function( test ) {
- sb.run( '1 + 1', function( output ) {
- test.equal( output.result, '2' )
- test.finish()
- })
-}
-
-exports['it should gracefully handle syntax errors'] = function( test ) {
- sb.run( 'hi )there', function( output ) {
- test.equal( output.result, "'SyntaxError: Unexpected token )'" )
- test.finish()
- })
-}
-
-exports['it should effectively prevent code from accessing node'] = function( test ) {
- sb.run( 'process.platform', function( output ) {
- test.equal( output.result, "'ReferenceError: process is not defined'" )
- test.finish()
- })
-}
-
-exports['it should effectively prevent code from circumventing the sandbox'] = function( test ) {
- sb.run( "var sys=require('sys'); sys.puts('Up in your fridge')", function( output ) {
- test.equal( output.result, "'ReferenceError: require is not defined'" )
- test.finish()
- })
-}
-
-exports['it should timeout on infinite loops'] = function( test ) {
- sb.run( 'while ( true ) {}', function( output ) {
- test.equal( output.result, "TimeoutError" )
- test.finish()
- })
-}
-
-exports['it should allow console output via `console.log`'] = function( test ) {
- sb.run( 'console.log(7); 42', function( output ) {
- test.equal( output.result, "42" )
- test.equal( output.console[0], "7" )
- test.finish()
- })
-}
-
-exports['it should allow console output via `print`'] = function( test ) {
- sb.run( 'print(7); 42', function( output ) {
- test.equal( output.result, "42" )
- test.equal( output.console[0], "7" )
- test.finish()
- })
-}
-
-exports['it should maintain the order of sync. console output'] = function( test ) {
- sb.run( 'console.log("first"); console.log("second"); 42', function( output ) {
- test.equal( output.result, "42" )
- test.equal( output.console[0], "'first'" )
- test.equal( output.console[1], "'second'" )
- test.finish()
- })
-}
-
-/* ------------------------------ GO GO GO ------------------------------ */
-if ( module == require.main )
- require( 'async_testing' ).run( __filename, process.ARGV )
-
View
58 test/sandbox_test.js
@@ -0,0 +1,58 @@
+/* ------------------------------ INIT ------------------------------ */
+require('should')
+
+var Sandbox = require('../lib/sandbox')
+ , sb = new Sandbox()
+
+/* ------------------------------ Tests ------------------------------ */
+// it should execute basic javascript
+sb.run( '1 + 1', function( output ) {
+ output.result.should.eql("2")
+})
+
+
+// it should gracefully handle syntax errors
+sb.run( 'hi )there', function( output ) {
+ output.result.should.eql("'SyntaxError: Unexpected token )'")
+})
+
+
+// it should effectively prevent code from accessing node
+sb.run( 'process.platform', function( output ) {
+ output.result.should.eql("'ReferenceError: process is not defined'")
+})
+
+
+// it should effectively prevent code from circumventing the sandbox
+sb.run( "var sys=require('sys'); sys.puts('Up in your fridge')", function( output ) {
+ output.result.should.eql("'ReferenceError: require is not defined'")
+})
+
+
+// it should timeout on infinite loops
+sb.run( 'while ( true ) {}', function( output ) {
+ output.result.should.eql("TimeoutError")
+})
+
+
+// it should allow console output via `console.log`
+sb.run( 'console.log(7); 42', function( output ) {
+ output.result.should.eql("42")
+ output.console[0].should.eql(7)
+})
+
+
+// it should allow console output via `print`
+sb.run( 'print(7); 42', function( output ) {
+ output.result.should.eql("42")
+ output.console[0].should.eql(7)
+})
+
+
+// it should maintain the order of sync. console output
+sb.run( 'console.log("first"); console.log("second"); 42', function( output ) {
+ output.result.should.eql("42")
+ output.console[0].should.eql("first")
+ output.console[1].should.eql("second")
+})
+

0 comments on commit 28f35f6

Please sign in to comment.
Something went wrong with that request. Please try again.