Performing $SAFE checks with test-unit

D

Daniel Berger

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:in `run'
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:in
`each'
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:in
`run'
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:in
`run'
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:in `start_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:in `run'
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:in
`run'
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
 
N

Nobuyoshi Nakada

Hi,

At Fri, 22 Jun 2007 00:44:37 +0900,
Daniel Berger 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
 
D

Daniel Berger

Hi,

At Fri, 22 Jun 2007 00:44:37 +0900,
Daniel Berger 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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top