Class SafeEvalTest
In: lib/safe_eval.rb
Parent: Test::Unit::TestCase

Methods

Public Instance methods

[Source]

# File lib/safe_eval.rb, line 125
    def getBinding; a = 42; return binding; end

[Source]

# File lib/safe_eval.rb, line 138
    def getBinding2; a = 51; return binding; end

[Source]

# File lib/safe_eval.rb, line 186
  def test_File_chmod_forbidden
    se = SafeEval.new
    assert_raises(SecurityError) do
      se.run("File.chmod(0755, '/tmp')")
    end
    assert_equal([], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 112
  def test_almost_empty_code
    se = SafeEval.new
    assert_equal(nil, se.run('            '))
    assert_equal([], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 240
  def test_bad_identifier
    se = SafeEval.new
    assert_raises(NameError) do
      se.run('bad_identifier.directory?')
    end
    assert_equal([], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 124
  def test_binding_argument
    def getBinding; a = 42; return binding; end
    se = SafeEval.new
    assert_equal(nil, se.run('puts a', getBinding))
    assert_equal(['42'], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 131
  def test_binding_block
    se = SafeEval.new
    assert_equal(true, se.run('puts a; true') { a = 42; return binding })
    assert_equal(['42'], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 137
  def test_binding_both
    def getBinding2; a = 51; return binding; end
    se = SafeEval.new
    ret = se.run('puts a; true', getBinding2) { a = 42; return binding }
    assert_equal(true, ret)
    assert_equal(['42'], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 145
  def test_exception
    mypid = $$
    se = SafeEval.new
    $tmp = 42
    begin
      $tmp = se.run('raise NoMethodError')
    rescue NoMethodError
      assert_equal(mypid, $$, "bad pid")
      assert_equal(42, $tmp, "result must be nil")
      assert_equal([], se.output)
    ensure
      assert_equal(mypid, $$, "bad pid")
    end
  end

[Source]

# File lib/safe_eval.rb, line 160
  def test_exception_collect
    se = SafeEval.new
    assert_raises(Exception) do
      se.run('puts "toto"; raise Exception')
    end
    assert_equal(['toto'], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 275
  def test_exit_forbidden
    se = SafeEval.new(3, false)
    assert_raises(SystemExit) do
      se.run('exit(42)')
    end
    assert_equal(nil, se.output)
  end

[Source]

# File lib/safe_eval.rb, line 283
  def test_exit_strong_forbidden
    se = SafeEval.new
    assert_raises(EOFError) do
      se.run('exit!(42)')
    end
    assert_equal([], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 100
  def test_false
    se = SafeEval.new
    assert_equal(false, se.run('false'))
    assert_equal([], se.output)
  end

Tests

[Source]

# File lib/safe_eval.rb, line 78
  def test_interface
    se = SafeEval.new
    assert_respond_to(se, :run)
    assert_respond_to(se, :output)
    assert_respond_to(se, :safe)
    assert_respond_to(se, :collect)
    assert(se.protected_methods.include?('set_environment'),
           "no protected method 'set_environment'")
  end

[Source]

# File lib/safe_eval.rb, line 88
  def test_nil
    se = SafeEval.new
    assert_equal(nil, se.run)
    assert_equal([], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 106
  def test_nil_no_collect
    se = SafeEval.new(3, false)
    assert_equal(nil, se.run)
    assert_equal(nil, se.output)
  end

[Source]

# File lib/safe_eval.rb, line 208
  def test_print
    se = SafeEval.new
    assert_equal(nil, se.run('print "hello"'))
    assert_equal(['hello'], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 220
  def test_print_no_collect
    se = SafeEval.new(3, false)
    assert_equal(nil, se.run('print "hello"'))
    assert_equal(nil, se.output)
  end

[Source]

# File lib/safe_eval.rb, line 214
  def test_puts
    se = SafeEval.new
    assert_equal(nil, se.run('puts "hello"'))
    assert_equal(['hello'], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 226
  def test_puts_several_lines
    se = SafeEval.new
    assert_equal(nil, se.run('puts "hello\nworld"'))
    assert_equal(['hello', 'world'], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 298
  def test_read_my_var
    se = SafeEval.new
    str = 'puts code'
    se.run(str)
    assert_equal(str, se.output[-1])
  end

FIXME: this test block if DRb::SessionManager test suite is executed before

       this test suite in check-ruby_ex.
  def test_exception_no_collect
    se = SafeEval.new(3, false)
    assert_raises(Exception) do
      se.run('puts "toto"; raise Exception')
    end
    assert_equal(nil, se.output)
  end

[Source]

# File lib/safe_eval.rb, line 178
  def test_require_forbidden
    se = SafeEval.new
    assert_raises(SecurityError) do
      se.run("require 'foo'")
    end
    assert_equal([], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 328
  def test_sleep
    se = SafeEval.new
    assert(se.run('sleep(1)') >= 1)
    assert_equal([], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 232
  def test_syntax_error
    se = SafeEval.new
    assert_raises(SyntaxError) do
      se.run('4syntaxerror')
    end
    assert_equal([], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 248
  def test_syntax_error_location
    se = SafeEval.new
    exc = assert_raises(SyntaxError) do
      se.run('4syntaxerror', nil, 'toto.yml', 1)
    end
    # expected_message = "compile error\ntoto.yml:1: syntax error\n4syntaxerror\n            ^"
    expected_message = "compile error\ntoto.yml:1: parse error, unexpected tIDENTIFIER, expecting $\n4syntaxerror\n            ^"
    assert_equal(expected_message, exc.message)
  end

[Source]

# File lib/safe_eval.rb, line 258
  def test_syntax_error_location_several_line
    code = "puts \"hello\"\nputs \"world\"\n4syntaxerror;\n"
    se = SafeEval.new
    exc = assert_raises(SyntaxError) do
      se.run(code, nil, 'toto.yml', 1)
    end
    # expected_message = "compile error\ntoto.yml:3: syntax error\n4syntaxerror;\n            ^"
    expected_message = "compile error\ntoto.yml:3: parse error, unexpected tIDENTIFIER, expecting $\n4syntaxerror;\n            ^"
    expected_message.chomp!
    assert_equal(expected_message, exc.message)
  end

[Source]

# File lib/safe_eval.rb, line 202
  def test_system_allowed
    se = SafeEval.new(2)
    se.run("system(\"echo 'toto'\")")
    assert_equal(['toto'], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 194
  def test_system_forbidden
    se = SafeEval.new
    assert_raises(SecurityError) do
      se.run("system('rm /tmp/x')")
    end
    assert_equal([], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 94
  def test_true
    se = SafeEval.new
    assert_equal(true, se.run('true'))
    assert_equal([], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 118
  def test_true_no_collect
    se = SafeEval.new(3, false)
    assert_equal(true, se.run('true'))
    assert_equal(nil, se.output)
  end

[Source]

# File lib/safe_eval.rb, line 291
  def test_undef_me
    se = SafeEval.new
    assert_raises(NoMethodError) do
      se.run('undef_method :run')
    end
  end

[Source]

# File lib/safe_eval.rb, line 316
  def test_write_output
    se = SafeEval.new
    assert_equal(['foo'], se.run('@output << "foo"'))
    assert_equal([], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 322
  def test_write_output_and_print
    se = SafeEval.new
    assert_equal(nil, se.run('@output << "foo"; p @output'))
    assert_equal(["[\"foo\"]"], se.output)
  end

[Source]

# File lib/safe_eval.rb, line 310
  def test_write_safe
    se = SafeEval.new
    assert_equal(1, se.run('@safe = 1'))
    assert_equal(true, se.run('@safe == 3'))
  end

[Source]

# File lib/safe_eval.rb, line 305
  def test_write_status
    se = SafeEval.new
    assert_equal(1, se.run('status = 1'))
  end

[Validate]