|
| 1 | +import platform |
1 | 2 | import time |
2 | 3 | from unittest import TestCase |
3 | 4 |
|
4 | 5 | from mistune import create_markdown |
5 | 6 |
|
6 | 7 |
|
| 8 | +IS_PYPY = platform.python_implementation() == "PyPy" |
| 9 | + |
| 10 | + |
7 | 11 | class TestFormattingSecurity(TestCase): |
| 12 | + def test_repeated_formatting_pairs_render_as_adjacent_spans(self): |
| 13 | + md = create_markdown(plugins=["strikethrough"]) |
| 14 | + result = md("~~x~~" * 3) |
| 15 | + self.assertEqual(result.strip(), "<p><del>x</del><del>x</del><del>x</del></p>") |
| 16 | + |
8 | 17 | def test_repeated_formatting_pairs_return_quickly(self): |
9 | 18 | cases = [ |
10 | 19 | ("strikethrough", "~~x~~"), |
11 | 20 | ("mark", "==x=="), |
12 | 21 | ("insert", "^^x^^"), |
13 | 22 | ] |
14 | 23 | for plugin, unit in cases: |
15 | | - md = create_markdown(plugins=[plugin]) |
16 | | - text = unit * 3000 |
| 24 | + with self.subTest(plugin=plugin): |
| 25 | + md = create_markdown(plugins=[plugin]) |
| 26 | + md(unit) |
| 27 | + |
| 28 | + repetitions = 500 if IS_PYPY else 3000 |
| 29 | + deadline = 1.0 if IS_PYPY else 0.5 |
| 30 | + text = unit * repetitions |
17 | 31 |
|
18 | | - start = time.monotonic() |
19 | | - md(text) |
20 | | - elapsed = time.monotonic() - start |
| 32 | + start = time.monotonic() |
| 33 | + md(text) |
| 34 | + elapsed = time.monotonic() - start |
21 | 35 |
|
22 | | - self.assertLess(elapsed, 0.5, plugin) |
| 36 | + self.assertLess(elapsed, deadline) |
0 commit comments