[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Performing $SAFE checks with test-unit

Daniel Berger

6/21/2007 3:45:00 PM

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


2 Answers

Nobuyoshi Nakada

6/21/2007 8:51:00 PM

0

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

--
Nobu Nakada

Daniel Berger

6/21/2007 9:01:00 PM

0



On Jun 21, 2:50 pm, Nobuyoshi Nakada <n...@ruby-lang.org> wrote:
> 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