mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Fix and add regexp tests
This commit is contained in:
parent
ff2998a86c
commit
80d145fa52
1 changed files with 22 additions and 2 deletions
|
@ -1589,7 +1589,7 @@ class TestRegexp < Test::Unit::TestCase
|
||||||
t = Time.now
|
t = Time.now
|
||||||
assert_raise_with_message(Regexp::TimeoutError, "regexp match timeout") do
|
assert_raise_with_message(Regexp::TimeoutError, "regexp match timeout") do
|
||||||
# A typical ReDoS case
|
# A typical ReDoS case
|
||||||
/^(a*)*$/ =~ "a" * 1000000 + "x"
|
/^(a*)*\1$/ =~ "a" * 1000000 + "x"
|
||||||
end
|
end
|
||||||
t = Time.now - t
|
t = Time.now - t
|
||||||
|
|
||||||
|
@ -1631,7 +1631,7 @@ class TestRegexp < Test::Unit::TestCase
|
||||||
|
|
||||||
Regexp.timeout = global_timeout
|
Regexp.timeout = global_timeout
|
||||||
|
|
||||||
re = Regexp.new("^a*b?a*$", timeout: per_instance_timeout)
|
re = Regexp.new("^(a*)\\1b?a*$", timeout: per_instance_timeout)
|
||||||
assert_equal(per_instance_timeout, re.timeout)
|
assert_equal(per_instance_timeout, re.timeout)
|
||||||
|
|
||||||
t = Time.now
|
t = Time.now
|
||||||
|
@ -1673,4 +1673,24 @@ class TestRegexp < Test::Unit::TestCase
|
||||||
assert_raise(ArgumentError) { Regexp.new("foo", timeout: -1) }
|
assert_raise(ArgumentError) { Regexp.new("foo", timeout: -1) }
|
||||||
end;
|
end;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_cache_optimization_exponential
|
||||||
|
assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}")
|
||||||
|
begin;
|
||||||
|
timeout = EnvUtil.apply_timeout_scale(0.2)
|
||||||
|
Regexp.timeout = timeout
|
||||||
|
|
||||||
|
assert_nil(/^(a*)*$/ =~ "a" * 1000000 + "x")
|
||||||
|
end;
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_cache_optimization_square
|
||||||
|
assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}")
|
||||||
|
begin;
|
||||||
|
timeout = EnvUtil.apply_timeout_scale(0.2)
|
||||||
|
Regexp.timeout = timeout
|
||||||
|
|
||||||
|
assert_nil(/^a*b?a*$/ =~ "a" * 1000000 + "x")
|
||||||
|
end;
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue