| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
import re |
| 5 |
import argparse |
| 6 |
import sys |
| 7 |
import platform |
| 8 |
import itertools |
| 9 |
import multiprocessing |
| 10 |
import time |
| 11 |
from cmark import CMark |
| 12 |
|
| 13 |
TIMEOUT = 10 |
| 14 |
|
| 15 |
parser = argparse.ArgumentParser(description='Run cmark tests.') |
| 16 |
parser.add_argument('--program', dest='program', nargs='?', default=None, |
| 17 |
help='program to test') |
| 18 |
parser.add_argument('--library-dir', dest='library_dir', nargs='?', |
| 19 |
default=None, help='directory containing dynamic library') |
| 20 |
args = parser.parse_args(sys.argv[1:]) |
| 21 |
|
| 22 |
allowed_failures = {"many references": True} |
| 23 |
|
| 24 |
cmark = CMark(prog=args.program, library_dir=args.library_dir) |
| 25 |
|
| 26 |
def hash_collisions(): |
| 27 |
REFMAP_SIZE = 16 |
| 28 |
COUNT = 50000 |
| 29 |
|
| 30 |
def badhash(ref): |
| 31 |
h = 0 |
| 32 |
for c in ref: |
| 33 |
a = (h << 6) & 0xFFFFFFFF |
| 34 |
b = (h << 16) & 0xFFFFFFFF |
| 35 |
h = ord(c) + a + b - h |
| 36 |
h = h & 0xFFFFFFFF |
| 37 |
|
| 38 |
return (h % REFMAP_SIZE) == 0 |
| 39 |
|
| 40 |
keys = ("x%d" % i for i in itertools.count()) |
| 41 |
collisions = itertools.islice((k for k in keys if badhash(k)), COUNT) |
| 42 |
bad_key = next(collisions) |
| 43 |
|
| 44 |
document = ''.join("[%s]: /url\n\n[%s]\n\n" % (key, bad_key) for key in collisions) |
| 45 |
|
| 46 |
return document, re.compile("(<p>\[%s\]</p>\n){%d}" % (bad_key, COUNT-1)) |
| 47 |
|
| 48 |
|
| 49 |
# list of pairs consisting of input and a regex that must match the output. |
| 50 |
pathological = { |
| 51 |
# note - some pythons have limit of 65535 for {num-matches} in re. |
| 52 |
"nested strong emph": |
| 53 |
(("*a **a " * 65000) + "b" + (" a** a*" * 65000), |
| 54 |
re.compile("(<em>a <strong>a ){65000}b( a</strong> a</em>){65000}")), |
| 55 |
"many emph closers with no openers": |
| 56 |
(("a_ " * 65000), |
| 57 |
re.compile("(a[_] ){64999}a_")), |
| 58 |
"many emph openers with no closers": |
| 59 |
(("_a " * 65000), |
| 60 |
re.compile("(_a ){64999}_a")), |
| 61 |
"many link closers with no openers": |
| 62 |
(("a]" * 65000), |
| 63 |
re.compile("(a\]){65000}")), |
| 64 |
"many link openers with no closers": |
| 65 |
(("[a" * 65000), |
| 66 |
re.compile("(\[a){65000}")), |
| 67 |
"mismatched openers and closers": |
| 68 |
(("*a_ " * 50000), |
| 69 |
re.compile("([*]a[_] ){49999}[*]a_")), |
| 70 |
"issue #389": |
| 71 |
(("*a " * 20000 + "_a*_ " * 20000), |
| 72 |
re.compile("(<em>a ){20000}(_a<\/em>_ ?){20000}")), |
| 73 |
"openers and closers multiple of 3": |
| 74 |
(("a**b" + ("c* " * 50000)), |
| 75 |
re.compile("a[*][*]b(c[*] ){49999}c[*]")), |
| 76 |
"link openers and emph closers": |
| 77 |
(("[ a_" * 50000), |
| 78 |
re.compile("(\[ a_){50000}")), |
| 79 |
"pattern [ (]( repeated": |
| 80 |
(("[ (](" * 80000), |
| 81 |
re.compile("(\[ \(\]\(){80000}")), |
| 82 |
"hard link/emph case": |
| 83 |
("**x [a*b**c*](d)", |
| 84 |
re.compile("\\*\\*x <a href=\"d\">a<em>b\\*\\*c</em></a>")), |
| 85 |
"nested brackets": |
| 86 |
(("[" * 50000) + "a" + ("]" * 50000), |
| 87 |
re.compile("\[{50000}a\]{50000}")), |
| 88 |
"nested block quotes": |
| 89 |
((("> " * 50000) + "a"), |
| 90 |
re.compile("(<blockquote>\n){50000}")), |
| 91 |
"deeply nested lists": |
| 92 |
("".join(map(lambda x: (" " * x + "* a\n"), range(0,1000))), |
| 93 |
re.compile("<ul>\n(<li>a\n<ul>\n){999}<li>a</li>\n</ul>\n(</li>\n</ul>\n){999}")), |
| 94 |
"U+0000 in input": |
| 95 |
("abc\u0000de\u0000", |
| 96 |
re.compile("abc\ufffd?de\ufffd?")), |
| 97 |
"backticks": |
| 98 |
("".join(map(lambda x: ("e" + "`" * x), range(1,5000))), |
| 99 |
re.compile("^<p>[e`]*</p>\n$")), |
| 100 |
"unclosed links A": |
| 101 |
("[a](<b" * 30000, |
| 102 |
re.compile("(\[a\]\(<b){30000}")), |
| 103 |
"unclosed links B": |
| 104 |
("[a](b" * 30000, |
| 105 |
re.compile("(\[a\]\(b){30000}")), |
| 106 |
"reference collisions": hash_collisions() |
| 107 |
# "many references": |
| 108 |
# ("".join(map(lambda x: ("[" + str(x) + "]: u\n"), range(1,5000 * 16))) + "[0] " * 5000, |
| 109 |
# re.compile("(\[0\] ){4999}")) |
| 110 |
} |
| 111 |
|
| 112 |
whitespace_re = re.compile('/s+/') |
| 113 |
|
| 114 |
results = {'passed': [], 'errored': [], 'failed': [], 'ignored': []} |
| 115 |
|
| 116 |
def run_pathological_test(description, results): |
| 117 |
(inp, regex) = pathological[description] |
| 118 |
[rc, actual, err] = cmark.to_html(inp) |
| 119 |
extra = "" |
| 120 |
if rc != 0: |
| 121 |
print(description, '[ERRORED (return code %d)]' %rc) |
| 122 |
print(err) |
| 123 |
if allowed_failures[description]: |
| 124 |
results['ignored'].append(description) |
| 125 |
else: |
| 126 |
results['errored'].append(description) |
| 127 |
elif regex.search(actual): |
| 128 |
print(description, '[PASSED]') |
| 129 |
results['passed'].append(description) |
| 130 |
else: |
| 131 |
print(description, '[FAILED]') |
| 132 |
print(repr(actual)) |
| 133 |
if allowed_failures[description]: |
| 134 |
results['ignored'].append(description) |
| 135 |
else: |
| 136 |
results['failed'].append(description) |
| 137 |
|
| 138 |
def run_tests(): |
| 139 |
print("Testing pathological cases:") |
| 140 |
for description in pathological: |
| 141 |
p = multiprocessing.Process(target=run_pathological_test, |
| 142 |
args=(description, results,)) |
| 143 |
p.start() |
| 144 |
# wait TIMEOUT seconds or until it finishes |
| 145 |
p.join(TIMEOUT) |
| 146 |
# kill it if still active |
| 147 |
if p.is_alive(): |
| 148 |
print(description, '[TIMEOUT]') |
| 149 |
if allowed_failures[description]: |
| 150 |
results['ignored'].append(description) |
| 151 |
else: |
| 152 |
results['errored'].append(description) |
| 153 |
p.terminate() |
| 154 |
p.join() |
| 155 |
|
| 156 |
passed = len(results['passed']) |
| 157 |
failed = len(results['failed']) |
| 158 |
errored = len(results['errored']) |
| 159 |
ignored = len(results['ignored']) |
| 160 |
|
| 161 |
print("%d passed, %d failed, %d errored" % (passed, failed, errored)) |
| 162 |
if ignored > 0: |
| 163 |
print("Ignoring these allowed failures:") |
| 164 |
for x in results['ignored']: |
| 165 |
print(x) |
| 166 |
if failed == 0 and errored == 0: |
| 167 |
exit(0) |
| 168 |
else: |
| 169 |
exit(1) |
| 170 |
|
| 171 |
if __name__ == "__main__": |
| 172 |
run_tests() |