1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
begin
require "readline"
rescue LoadError
end
if defined?(Readline) && !/EditLine/n.match(Readline::VERSION)
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.close
stdout.close
line = replace_stdio(stdin.path, stdout.path) {
Readline.readline("> ", true)
}
assert_equal("hello", line)
assert_equal(true, line.tainted?)
stdout.open
assert_equal("> ", stdout.read(2))
assert_equal(1, Readline::HISTORY.length)
assert_equal("hello", Readline::HISTORY[0])
assert_raises(SecurityError) do
Thread.start {
$SAFE = 1
replace_stdio(stdin.path, stdout.path) do
Readline.readline("> ".taint)
end
}.join
end
assert_raises(SecurityError) do
Thread.start {
$SAFE = 4
replace_stdio(stdin.path, stdout.path) { Readline.readline("> ") }
}.join
end
ensure
stdin.close(true)
stdout.close(true)
end
end
def test_completion_append_character
begin
Readline.completion_append_character = "x"
assert_equal("x", Readline.completion_append_character)
Readline.completion_append_character = "xyz"
assert_equal("x", Readline.completion_append_character)
Readline.completion_append_character = nil
assert_equal(nil, Readline.completion_append_character)
Readline.completion_append_character = ""
assert_equal(nil, Readline.completion_append_character)
rescue NotImplementedError
end
end
private
def replace_stdio(stdin_path, stdout_path)
open(stdin_path, "r"){|stdin|
open(stdout_path, "w"){|stdout|
orig_stdin = STDIN.dup
orig_stdout = STDOUT.dup
STDIN.reopen(stdin)
STDOUT.reopen(stdout)
begin
Readline.input = STDIN
Readline.output = STDOUT
yield
ensure
STDIN.reopen(orig_stdin)
STDOUT.reopen(orig_stdout)
orig_stdin.close
orig_stdout.close
end
}
}
end
end
end
|