mirror of
https://github.com/ruby/ruby.git
synced 2025-09-15 08:33:58 +02:00
* ext/readline/readline.c: check $SAFE.
* test/readline/test_readline.rb: added tests for readline. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7309 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ac5eac4066
commit
1e6cdc00e7
3 changed files with 113 additions and 15 deletions
57
test/readline/test_readline.rb
Normal file
57
test/readline/test_readline.rb
Normal file
|
@ -0,0 +1,57 @@
|
|||
begin
|
||||
require "readline"
|
||||
rescue LoadError
|
||||
end
|
||||
|
||||
if defined?(Readline)
|
||||
|
||||
require "test/unit"
|
||||
require "tempfile"
|
||||
|
||||
class TestReadline < Test::Unit::TestCase
|
||||
def test_readline
|
||||
stdin = Tempfile.new("test_readline_stdin")
|
||||
stdout = Tempfile.new("test_readline_stdout")
|
||||
begin
|
||||
stdin.write("hello\n")
|
||||
stdin.rewind
|
||||
line = replace_stdio(stdin, stdout) { Readline.readline("> ") }
|
||||
assert_equal("hello", line)
|
||||
assert_equal(true, line.tainted?)
|
||||
assert_raises(SecurityError) do
|
||||
Thread.start {
|
||||
$SAFE = 1
|
||||
replace_stdio(stdin, stdout) { Readline.readline("> ".taint) }
|
||||
}.join
|
||||
end
|
||||
assert_raises(SecurityError) do
|
||||
Thread.start {
|
||||
$SAFE = 4
|
||||
replace_stdio(stdin, stdout) { Readline.readline("> ") }
|
||||
}.join
|
||||
end
|
||||
stdout.rewind
|
||||
assert_equal("> ", stdout.read(2))
|
||||
ensure
|
||||
stdin.close(true)
|
||||
stdout.close(true)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def replace_stdio(stdin, stdout)
|
||||
orig_stdin = STDIN.dup
|
||||
orig_stdout = STDOUT.dup
|
||||
STDIN.reopen(stdin)
|
||||
STDOUT.reopen(stdout)
|
||||
begin
|
||||
yield
|
||||
ensure
|
||||
STDIN.reopen(orig_stdin)
|
||||
STDOUT.reopen(orig_stdout)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue