Performing $SAFE checks with test-unit

Hi all,

Is there a way to perform $SAFE checks with test-unit? I tried this
with Ruby 1.8.6:

require ‘test/unit’

class TC_Hash_Replace < Test::Unit::TestCase
def setup
$SAFE = 0
end

def test_replace_in_safe_mode
$SAFE = 4
assert_raise(SecurityError){ {1,2,3,4}.replace({‘a’, ‘b’}) }
end

def teardown
$SAFE = 0
end
end

Running that causes this error:

/usr/local/lib/ruby/1.8/test/unit/testcase.rb:136:in add_error': Insecure: can't modify instance variable (SecurityError) from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:91:inrun’
from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:34:in
run' from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:33:ineach’
from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in
run' from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:34:inrun’
from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in
each' from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:33:inrun’
from /usr/local/lib/ruby/1.8/test/unit/ui/
testrunnermediator.rb:46:in run_suite' from /usr/local/lib/ruby/1.8/test/unit/ui/console/ testrunner.rb:67:instart_mediator’
from /usr/local/lib/ruby/1.8/test/unit/ui/console/
testrunner.rb:41:in start' from /usr/local/lib/ruby/1.8/test/unit/ui/ testrunnerutilities.rb:29:inrun’
from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:216:in
run' from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:12:inrun’
from /usr/local/lib/ruby/1.8/test/unit.rb:278

If it’s not possible, consider it a feature request. I’ve no idea how
to implement it, however. Perhaps run it in a Sandbox? Dunno.

I haven’t tried rspec yet to see what it does.

Thanks,

Dan

Hi,

At Fri, 22 Jun 2007 00:44:37 +0900,
Daniel B. wrote in [ruby-talk:256413]:

Is there a way to perform $SAFE checks with test-unit? I tried this
with Ruby 1.8.6:

def test_replace_in_safe_mode
   o = {1,2,3,4}
   assert_raise(SecurityError) do
     proc do
       $SAFE = 4
       o.replace({'a', 'b'})
     end.call
  end
end

On Jun 21, 2:50 pm, Nobuyoshi N. [email protected] wrote:

   assert_raise(SecurityError) do
     proc do
       $SAFE = 4
       o.replace({'a', 'b'})
     end.call
  end
end

Nice, thanks Nobu. Perhaps all assertions should run in anonymous
blocks? It’s probably slower, but then I’ve never cared about the
speed of my unit tests.

Or is there some other downside to doing that?

Regards,

Dan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs