FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

test: address fuzzing review feedback · python-zeroconf/python-zeroconf@2f17ae8 · GitHub

Commit 2f17ae8

Browse files
committed
test: address fuzzing review feedback
1 parent a17aaf1 commit 2f17ae8

3 files changed

Lines changed: 30 additions & 13 deletions

File tree

‎poetry.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/conftest.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
from __future__ import annotations
44

5-
import sys
5+
import importlib.util
6+
import os
67
import threading
78
from collections.abc import AsyncGenerator, Generator, Iterator
89
from unittest.mock import patch
@@ -16,10 +17,19 @@
1617
from zeroconf._services import info as service_info
1718
from zeroconf.asyncio import AsyncZeroconf
1819

19-
# hypothesis is not installed on PyPy (no wheels for its native module)
20+
# The dev dependency group only installs hypothesis on CPython (the CI
21+
# matrix pins pypy-3.10 and hypothesis ships no pp310 wheels), and a
22+
# non-dev install has no hypothesis at all.
2023
collect_ignore: list[str] = []
21-
if sys.implementation.name != "cpython":
24+
if importlib.util.find_spec("hypothesis") is None:
2225
collect_ignore.append("test_fuzz_incoming.py")
26+
else:
27+
from hypothesis import settings as _hypothesis_settings
28+
29+
# Deterministic in CI; set HYPOTHESIS_PROFILE=long for a deep local run.
30+
_hypothesis_settings.register_profile("ci", derandomize=True, max_examples=200, deadline=None)
31+
_hypothesis_settings.register_profile("long", max_examples=50000, deadline=None)
32+
_hypothesis_settings.load_profile(os.environ.get("HYPOTHESIS_PROFILE", "ci"))
2333

2434
try:
2535
from blockbuster import BlockBuster, blockbuster_ctx

‎tests/test_fuzz_incoming.py‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,18 @@
66

77
from __future__ import annotations
88

9-
import os
109
import socket
1110

12-
from hypothesis import given, settings
11+
from hypothesis import given
1312
from hypothesis import strategies as st
1413

1514
from zeroconf import DNSIncoming, DNSNsec, DNSOutgoing, const
15+
from zeroconf._dns import DNSRecord
1616

1717
from .benchmarks.test_incoming import generate_packets
1818

19-
# Deterministic in CI; set HYPOTHESIS_PROFILE=long for a deep local run.
20-
settings.register_profile("ci", derandomize=True, max_examples=200, deadline=None)
21-
settings.register_profile("long", max_examples=50000, deadline=None)
22-
settings.load_profile(os.environ.get("HYPOTHESIS_PROFILE", "ci"))
23-
24-
MAX_PACKET_SIZE = 9194
19+
# Slack past the codec's cap so the fuzzer also probes oversized input
20+
MAX_PACKET_SIZE = const._MAX_MSG_ABSOLUTE + 256
2521

2622

2723
def _nsec_packet() -> bytes:
@@ -43,7 +39,7 @@ def _nsec_packet() -> bytes:
4339
CORPUS = [*generate_packets(), _nsec_packet()]
4440

4541

46-
def _parse(data: bytes) -> list:
42+
def _parse(data: bytes) -> list[DNSRecord]:
4743
"""Parse and touch every public surface; any exception is a bug."""
4844
incoming = DNSIncoming(data)
4945
assert isinstance(incoming.valid, bool)
@@ -53,9 +49,20 @@ def _parse(data: bytes) -> list:
5349
assert incoming.questions is not None
5450
answers = incoming.answers()
5551
assert isinstance(answers, list)
52+
for record in answers:
53+
repr(record)
54+
hash(record)
5655
return answers
5756

5857

58+
def test_corpus_parses_cleanly() -> None:
59+
"""Positive control: the fuzz corpus itself must parse fully."""
60+
for packet in CORPUS:
61+
incoming = DNSIncoming(packet)
62+
assert incoming.valid
63+
assert incoming.answers()
64+
65+
5966
@given(data=st.binary(max_size=MAX_PACKET_SIZE))
6067
def test_arbitrary_bytes_never_raise(data: bytes) -> None:
6168
"""Random garbage must parse or fail quietly, and parsing is deterministic."""

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL