Skip to content

Commit

Permalink
Add Diff Fuzz Target
Browse files Browse the repository at this point in the history
Adds a new `fuzz_diff.py` fuzz target that covers `Diff` class
initialization using fuzzed data.
  • Loading branch information
DaveLak committed May 8, 2024
1 parent a5815b6 commit a915adf
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions fuzzing/fuzz-targets/fuzz_diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys
import os
import tempfile
from binascii import Error as BinasciiError

import atheris

if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
path_to_bundled_git_binary = os.path.abspath(os.path.join(os.path.dirname(__file__), "git"))
os.environ["GIT_PYTHON_GIT_EXECUTABLE"] = path_to_bundled_git_binary

with atheris.instrument_imports():
from git import Repo, Diff


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

with tempfile.TemporaryDirectory() as temp_dir:
repo = Repo.init(path=temp_dir)
try:
Diff(
repo,
a_rawpath=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())),
b_rawpath=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())),
a_blob_id=fdp.ConsumeBytes(20),
b_blob_id=fdp.ConsumeBytes(20),
a_mode=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())),
b_mode=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())),
new_file=fdp.ConsumeBool(),
deleted_file=fdp.ConsumeBool(),
copied_file=fdp.ConsumeBool(),
raw_rename_from=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())),
raw_rename_to=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())),
diff=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())),
change_type=fdp.PickValueInList(["A", "D", "C", "M", "R", "T", "U"]),
score=fdp.ConsumeIntInRange(0, fdp.remaining_bytes()),
)
except BinasciiError:
return -1
except AssertionError as e:
if "Require 20 byte binary sha, got" in str(e):
return -1
else:
raise e


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


if __name__ == "__main__":
main()

0 comments on commit a915adf

Please sign in to comment.