| 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 |
from random import Random |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import HealthCheck, settings |
| 16 |
from hypothesis.internal.conjecture.engine import ConjectureData, ConjectureRunner |
| 17 |
from hypothesis.strategies._internal import SearchStrategy |
| 18 |
|
| 19 |
from tests.conjecture.common import interesting_origin |
| 20 |
|
| 21 |
POISON = "POISON" |
| 22 |
MAX_INT = 2**32 - 1 |
| 23 |
|
| 24 |
|
| 25 |
class PoisonedTree(SearchStrategy): |
| 26 |
"""Generates variable sized tuples with an implicit tree structure. |
| 27 |
|
| 28 |
The actual result is flattened out, but the hierarchy is implicit in |
| 29 |
the data. |
| 30 |
""" |
| 31 |
|
| 32 |
def __init__(self, p): |
| 33 |
super().__init__() |
| 34 |
self.__p = p |
| 35 |
|
| 36 |
def do_draw(self, data): |
| 37 |
if data.draw_boolean(self.__p): |
| 38 |
return data.draw(self) + data.draw(self) |
| 39 |
else: |
| 40 |
# We draw n as two separate calls so that it doesn't show up as a |
| 41 |
# single choice. If it did, the heuristics that allow us to move |
| 42 |
# choices around would fire and it would move right, which would |
| 43 |
# then allow us to shrink it more easily. |
| 44 |
n1 = data.draw_integer(0, 2**16 - 1) << 16 |
| 45 |
n2 = data.draw_integer(0, 2**16 - 1) |
| 46 |
n = n1 | n2 |
| 47 |
if n == MAX_INT: |
| 48 |
return (POISON,) |
| 49 |
else: |
| 50 |
return (None,) |
| 51 |
|
| 52 |
|
| 53 |
TEST_SETTINGS = settings( |
| 54 |
database=None, |
| 55 |
suppress_health_check=list(HealthCheck), |
| 56 |
max_examples=10**6, |
| 57 |
deadline=None, |
| 58 |
) |
| 59 |
|
| 60 |
|
| 61 |
@pytest.mark.parametrize("size", [2, 5, 10]) |
| 62 |
@pytest.mark.parametrize("seed", [0, 15993493061449915028]) |
| 63 |
def test_can_reduce_poison_from_any_subtree(size, seed): |
| 64 |
"""This test validates that we can minimize to any leaf node of a binary |
| 65 |
tree, regardless of where in the tree the leaf is.""" |
| 66 |
random = Random(seed) |
| 67 |
|
| 68 |
# Initially we create the minimal tree of size n, regardless of whether it |
| 69 |
# is poisoned (which it won't be - the poison event essentially never |
| 70 |
# happens when drawing uniformly at random). |
| 71 |
|
| 72 |
# Choose p so that the expected size of the tree is equal to the desired |
| 73 |
# size. |
| 74 |
p = 1.0 / (2.0 - 1.0 / size) |
| 75 |
strat = PoisonedTree(p) |
| 76 |
|
| 77 |
def test_function(data): |
| 78 |
v = data.draw(strat) |
| 79 |
if len(v) >= size: |
| 80 |
data.mark_interesting(interesting_origin()) |
| 81 |
|
| 82 |
runner = ConjectureRunner(test_function, random=random, settings=TEST_SETTINGS) |
| 83 |
runner.generate_new_examples() |
| 84 |
runner.shrink_interesting_examples() |
| 85 |
(data,) = runner.interesting_examples.values() |
| 86 |
assert len(ConjectureData.for_choices(data.choices).draw(strat)) == size |
| 87 |
|
| 88 |
# find the nodes corresponding to n1 and n2 |
| 89 |
nodes = [ |
| 90 |
node |
| 91 |
for node in data.nodes |
| 92 |
if node.type == "integer" and node.constraints["max_value"] == 2**16 - 1 |
| 93 |
] |
| 94 |
assert len(nodes) % 2 == 0 |
| 95 |
|
| 96 |
marker = bytes([1, 2, 3, 4]) |
| 97 |
for i in range(0, len(nodes), 2): |
| 98 |
# Now for each leaf position in the tree we try inserting a poison |
| 99 |
# value artificially. Additionally, we add a marker to the end that |
| 100 |
# must be preserved. The marker means that we are not allow to rely on |
| 101 |
# discarding the end of the choice sequence to get the desired shrink. |
| 102 |
node = nodes[i] |
| 103 |
|
| 104 |
def test_function_with_poison(data): |
| 105 |
v = data.draw(strat) |
| 106 |
m = data.draw_bytes(len(marker), len(marker)) |
| 107 |
if POISON in v and m == marker: |
| 108 |
data.mark_interesting(interesting_origin()) |
| 109 |
|
| 110 |
runner = ConjectureRunner( |
| 111 |
test_function_with_poison, random=random, settings=TEST_SETTINGS |
| 112 |
) |
| 113 |
# replace n1 and n2 with 2**16 - 1 to insert a poison value here |
| 114 |
runner.cached_test_function( |
| 115 |
data.choices[: node.index] |
| 116 |
+ (2**16 - 1, 2**16 - 1) |
| 117 |
+ (data.choices[node.index + 2 :]) |
| 118 |
+ (marker,) |
| 119 |
) |
| 120 |
assert runner.interesting_examples |
| 121 |
|
| 122 |
runner.shrink_interesting_examples() |
| 123 |
(shrunk,) = runner.interesting_examples.values() |
| 124 |
assert ConjectureData.for_choices(shrunk.choices).draw(strat) == (POISON,) |