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
|
#
# fuzzer.py
#
# Copyright (C) 20244 Franco Masotti (see /README.md)
#
# This file is part of md-toc.
#
# md-toc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# md-toc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with md-toc. If not, see <http://www.gnu.org/licenses/>.
#
r"""A basic fuzzer for the build_toc function."""
import atheris
with atheris.instrument_imports():
import secrets
import sys
import tempfile
from .. import api, exceptions
def TestBuildToc(data):
r"""Test the md_toc.api.build_toc function."""
with tempfile.NamedTemporaryFile() as fp:
bytez: bytes = b''.join([
bytes('#' * (secrets.randbelow(6) + 1), 'UTF-8'),
b' ',
data,
])
fp.write(bytez)
# Move pointer to the start of the file.
fp.seek(0)
try:
for parser in ['cmark', 'github', 'gitlab', 'redcarpet']:
api.build_toc(filename=fp.name,
parser=parser,
keep_header_levels=6)
except (exceptions.GithubEmptyLinkLabel,
exceptions.TocDoesNotRenderAsCoherentList,
exceptions.GithubOverflowCharsLinkLabel) as e:
# The input string cannot be guaranteed to have a non-empty label
# (GithubEmptyLinkLabel)
# or a newline with '#' sequences can be inserted
# (TocDoesNotRenderAsCoherentList)
# or header generates a link label which is too long
# (GithubOverflowCharsLinkLabel)
print(e, end='')
atheris.Setup(sys.argv, TestBuildToc)
atheris.Fuzz()
|