| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import io |
| 12 |
import unittest |
| 13 |
from operator import attrgetter |
| 14 |
from random import randbytes |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
from hypothesis import Phase, given, settings, strategies as st |
| 19 |
from hypothesis.database import InMemoryExampleDatabase |
| 20 |
from hypothesis.errors import InvalidArgument |
| 21 |
from hypothesis.internal.conjecture.engine import shortlex |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.parametrize( |
| 25 |
"buffer_type", |
| 26 |
[bytes, bytearray, memoryview, io.BytesIO], |
| 27 |
ids=attrgetter("__name__"), |
| 28 |
) |
| 29 |
def test_fuzz_one_input(buffer_type): |
| 30 |
db = InMemoryExampleDatabase() |
| 31 |
seen = [] |
| 32 |
seeds = [] |
| 33 |
|
| 34 |
# This is a standard `@given` test, which we can also use as a fuzz target. |
| 35 |
# Note that we specify the DB so we can make more precise assertions, |
| 36 |
# and tighten the phases so we can be sure the failing examples come from fuzzing. |
| 37 |
@given(st.text()) |
| 38 |
@settings(database=db, phases=[Phase.reuse, Phase.shrink]) |
| 39 |
def test(s): |
| 40 |
seen.append(s) |
| 41 |
assert len(s) < 5, repr(s) |
| 42 |
|
| 43 |
# Before running fuzz_one_input, there's nothing in `db`, and so the test passes |
| 44 |
# (because example generation is disabled by the custom settings) |
| 45 |
with pytest.raises(unittest.SkipTest): # because this generates no examples |
| 46 |
test() |
| 47 |
assert len(seen) == 0 |
| 48 |
|
| 49 |
# If we run a lot of random bytestrings through fuzz_one_input, we'll eventually |
| 50 |
# find a failing example. |
| 51 |
with pytest.raises(AssertionError): |
| 52 |
for _ in range(1000): |
| 53 |
buf = randbytes(1000) |
| 54 |
seeds.append(buf) |
| 55 |
test.hypothesis.fuzz_one_input(buffer_type(buf)) |
| 56 |
|
| 57 |
# fuzz_one_input returns False for invalid bytestrings, due to e.g. assume(False) |
| 58 |
assert len(seen) <= len(seeds) |
| 59 |
|
| 60 |
# `db` contains exactly one failing example, which is either the most |
| 61 |
# recent seed that we tried or the pruned-and-canonicalised form of it. |
| 62 |
(saved_examples,) = db.data.values() |
| 63 |
assert len(saved_examples) == 1 |
| 64 |
assert shortlex(seeds[-1]) >= shortlex(next(iter(saved_examples))) |
| 65 |
|
| 66 |
# Now that we have a failure in `db`, re-running our test is sufficient to |
| 67 |
# reproduce it, *and shrink to a minimal example*. |
| 68 |
with pytest.raises(AssertionError): |
| 69 |
test() |
| 70 |
assert seen[-1] == "0" * 5 |
| 71 |
|
| 72 |
|
| 73 |
def test_can_fuzz_with_database_eq_None(): |
| 74 |
# This test exists to cover the can't-record-failure branch. |
| 75 |
|
| 76 |
@given(st.none()) |
| 77 |
@settings(database=None) |
| 78 |
def test(s): |
| 79 |
raise AssertionError |
| 80 |
|
| 81 |
with pytest.raises(AssertionError): |
| 82 |
test.hypothesis.fuzz_one_input(b"\x00\x00") |
| 83 |
|
| 84 |
|
| 85 |
def test_fuzzing_unsatisfiable_test_always_returns_None(): |
| 86 |
# There are no examples of `st.none().filter(bool)`, but while the Hypothesis |
| 87 |
# engine would give up, fuzz_one_input will just return None each time. |
| 88 |
|
| 89 |
@given(st.none().filter(bool)) |
| 90 |
@settings(database=None) |
| 91 |
def test(s): |
| 92 |
raise AssertionError("Unreachable because there are no valid examples") |
| 93 |
|
| 94 |
for _ in range(100): |
| 95 |
buf = randbytes(3) |
| 96 |
ret = test.hypothesis.fuzz_one_input(buf) |
| 97 |
assert ret is None |
| 98 |
|
| 99 |
|
| 100 |
def test_autopruning_of_returned_buffer(): |
| 101 |
@given(st.binary(min_size=4, max_size=4)) |
| 102 |
@settings(database=None) |
| 103 |
def test(s): |
| 104 |
pass |
| 105 |
|
| 106 |
# Unused portions of the input buffer are discarded from output. |
| 107 |
# (and canonicalised, but that's a no-op for fixed-length `binary()`) |
| 108 |
assert test.hypothesis.fuzz_one_input(b"deadbeef") == b"dead" |
| 109 |
|
| 110 |
|
| 111 |
def test_can_access_strategy_for_wrapped_test(): |
| 112 |
strategy = st.builds(object) |
| 113 |
|
| 114 |
@given(x=strategy) |
| 115 |
def addx(x, y): |
| 116 |
pass |
| 117 |
|
| 118 |
@given(strategy) |
| 119 |
def addy(x, y): |
| 120 |
pass |
| 121 |
|
| 122 |
assert addx.hypothesis._given_kwargs == {"x": strategy} |
| 123 |
assert addy.hypothesis._given_kwargs == {"y": strategy} |
| 124 |
|
| 125 |
|
| 126 |
@pytest.mark.parametrize( |
| 127 |
"buffers,db_size", |
| 128 |
[ |
| 129 |
([b"aa", b"bb", b"cc", b"dd"], 1), # ascending -> only saves first |
| 130 |
([b"dd", b"cc", b"bb", b"aa"], 4), # descending -> saves all |
| 131 |
([b"cc", b"dd", b"aa", b"bb"], 2), # sawtooth -> saves cc then aa |
| 132 |
([b"aa", b"bb", b"cc", b"XX"], 2), # two distinct errors -> saves both |
| 133 |
], |
| 134 |
) |
| 135 |
def test_fuzz_one_input_does_not_add_redundant_entries_to_database(buffers, db_size): |
| 136 |
db = InMemoryExampleDatabase() |
| 137 |
seen = [] |
| 138 |
|
| 139 |
@given(st.binary(min_size=2, max_size=2)) |
| 140 |
@settings(database=db) |
| 141 |
def test(s): |
| 142 |
seen.append(s) |
| 143 |
assert s != b"XX" |
| 144 |
raise AssertionError |
| 145 |
|
| 146 |
for buf in buffers: |
| 147 |
with pytest.raises(AssertionError): |
| 148 |
test.hypothesis.fuzz_one_input(buf) |
| 149 |
|
| 150 |
(saved_examples,) = db.data.values() |
| 151 |
assert seen == buffers |
| 152 |
assert len(saved_examples) == db_size |
| 153 |
|
| 154 |
|
| 155 |
def test_fuzzing_invalid_test_raises_error(): |
| 156 |
# Invalid: @given with too many positional arguments |
| 157 |
@given(st.integers(), st.integers()) |
| 158 |
def invalid_test(s): |
| 159 |
pass |
| 160 |
|
| 161 |
with pytest.raises(InvalidArgument, match="Too many positional arguments"): |
| 162 |
# access the property to check error happens during setup |
| 163 |
invalid_test.hypothesis.fuzz_one_input |