Skip to content

Commit

Permalink
Read class properties & call methods to cover more features
Browse files Browse the repository at this point in the history
Property access and private methods on the `Diff` class are complex and
involve encoding and decoding operations that warrant being tested.

This test borrows its design from the `test_diff.py` unit test file.
  • Loading branch information
DaveLak committed May 8, 2024
1 parent a915adf commit 989ae1a
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion fuzzing/fuzz-targets/fuzz_diff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import os
import io
import tempfile
from binascii import Error as BinasciiError

Expand All @@ -13,13 +14,26 @@
from git import Repo, Diff


class BytesProcessAdapter:
"""Allows bytes to be used as process objects returned by subprocess.Popen."""

def __init__(self, input_string):
self.stdout = io.BytesIO(input_string)
self.stderr = io.BytesIO()

def wait(self):
return 0

poll = wait


def TestOneInput(data):
fdp = atheris.FuzzedDataProvider(data)

with tempfile.TemporaryDirectory() as temp_dir:
repo = Repo.init(path=temp_dir)
try:
Diff(
diff = Diff(
repo,
a_rawpath=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())),
b_rawpath=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())),
Expand All @@ -44,6 +58,21 @@ def TestOneInput(data):
else:
raise e

_ = diff.__str__()
_ = diff.a_path
_ = diff.b_path
_ = diff.rename_from
_ = diff.rename_to
_ = diff.renamed_file

diff_index = diff._index_from_patch_format(
repo, proc=BytesProcessAdapter(fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())))
)

diff._handle_diff_line(
lines_bytes=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())), repo=repo, index=diff_index
)


def main():
atheris.Setup(sys.argv, TestOneInput)
Expand Down

0 comments on commit 989ae1a

Please sign in to comment.