summaryrefslogtreecommitdiff
path: root/tests/test_java.py
diff options
context:
space:
mode:
authorKurt McKee <contactme@kurtmckee.org>2020-11-09 00:26:21 -0600
committerGitHub <noreply@github.com>2020-11-09 07:26:21 +0100
commitfb40b7113e6c71f29c110cc9c32077a0883b732b (patch)
tree0747d9f1df61997fa3247353eeb38566ddecb478 /tests/test_java.py
parentafa9e3be5a05a27ed2c8ac515a10e146a766ad15 (diff)
downloadpygments-git-fb40b7113e6c71f29c110cc9c32077a0883b732b.tar.gz
Fix a catastrophic backtracking bug in JavaLexer (#1594)
* JavaLexer: Demonstrate a catastrophic backtracking bug * JavaLexer: Fix a catastrophic backtracking bug Closes #1586
Diffstat (limited to 'tests/test_java.py')
-rw-r--r--tests/test_java.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/tests/test_java.py b/tests/test_java.py
index 3baec0ad..f7b16bd7 100644
--- a/tests/test_java.py
+++ b/tests/test_java.py
@@ -7,9 +7,11 @@
:license: BSD, see LICENSE for details.
"""
+import time
+
import pytest
-from pygments.token import Text, Name, Punctuation, Keyword, Number
+from pygments.token import Keyword, Name, Number, Punctuation, String, Text
from pygments.lexers import JavaLexer
@@ -76,3 +78,24 @@ def test_numeric_literals(lexer):
(Text, '\n')
]
assert list(lexer.get_tokens(fragment)) == tokens
+
+
+@pytest.mark.parametrize(
+ 'text',
+ (
+ '""', '"abc"', '"ひらがな"', '"123"',
+ '"\\\\"', '"\\t"' '"\\""',
+ ),
+)
+def test_string_literals_positive_match(lexer, text):
+ """Test positive matches for string literals."""
+ tokens = list(lexer.get_tokens_unprocessed(text))
+ assert all([token is String for _, token, _ in tokens])
+ assert ''.join([value for _, _, value in tokens]) == text
+
+
+def test_string_literals_backtracking(lexer):
+ """Test catastrophic backtracking for string literals."""
+ start_time = time.time()
+ list(lexer.get_tokens_unprocessed('"' + '\\' * 100))
+ assert time.time() - start_time < 1, 'possible backtracking bug'