-
Notifications
You must be signed in to change notification settings - Fork 22
Add Hypothesis property tests (Scorecard Fuzzing check) #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| bandit | ||
| hypothesis | ||
| pycodestyle | ||
| pylint | ||
| pytest | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # SPDX-License-Identifier: MIT | ||
| # The MIT License (MIT) | ||
| # | ||
| # SPDX-FileCopyrightText: 2014-2026 Tim Case <bitmath@lnx.cx> | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person | ||
| # obtaining a copy of this software and associated documentation files | ||
| # (the "Software"), to deal in the Software without restriction, | ||
| # including without limitation the rights to use, copy, modify, merge, | ||
| # publish, distribute, sublicense, and/or sell copies of the Software, | ||
| # and to permit persons to whom the Software is furnished to do so, | ||
| # subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be | ||
| # included in all copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||
| # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
| # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| # SOFTWARE. | ||
|
|
||
| """ | ||
| Hypothesis-driven property tests for the bitmath parser and unit | ||
| conversions. These are fuzz-style tests: hypothesis generates inputs | ||
| across the value/unit space and shrinks any counterexample it finds. | ||
|
|
||
| Three invariants are checked: | ||
|
|
||
| 1. Roundtrip: an instance formatted as "value unit_singular" and then | ||
| re-parsed produces a Bitmath with the same underlying bit count. | ||
| 2. Lossless conversion: x.to_DST().to_SRC() preserves x.bits across | ||
| every (SRC, DST) pair in ALL_UNIT_TYPES. | ||
| 3. Parser exception contract: parse_string raises only ValueError on | ||
| bad input. A leaked IndexError, AttributeError, KeyError, or | ||
| TypeError would be a bug. | ||
| """ | ||
|
|
||
| import math | ||
|
|
||
| import pytest | ||
|
|
||
| # Downstream packagers (Fedora/EPEL RPM builds) run %check without | ||
| # installing test-only dependencies. Skip the whole module when | ||
| # hypothesis isn't on the path rather than failing collection. | ||
| pytest.importorskip("hypothesis") | ||
|
|
||
| from hypothesis import given, settings, strategies as st # noqa: E402 | ||
|
|
||
| import bitmath # noqa: E402 | ||
|
|
||
|
|
||
| # Bracketed to avoid values that Python's str() renders in scientific | ||
| # notation. The parser splits a value/unit string on the first | ||
| # alphabetic character, which mis-handles the 'e' in '1e+16' or | ||
| # '1e-05'. Scientific-notation file sizes aren't a realistic user | ||
| # input, so the test space stays in the regular-notation range | ||
| # (approximately [1e-4, 1e16)), plus exactly zero. | ||
| nonneg_finite = st.one_of( | ||
| st.just(0.0), | ||
| st.floats( | ||
| min_value=1e-4, | ||
| max_value=1e15, | ||
| allow_nan=False, | ||
| allow_infinity=False, | ||
| ), | ||
| ) | ||
|
|
||
| unit_names = list(bitmath.ALL_UNIT_TYPES) | ||
|
|
||
|
|
||
| @given(value=nonneg_finite, unit=st.sampled_from(unit_names)) | ||
| @settings(max_examples=200) | ||
| def test_parse_string_roundtrip(value, unit): | ||
| cls = getattr(bitmath, unit) | ||
| original = cls(value) | ||
| formatted = original.format("{value} {unit_singular}") | ||
| parsed = bitmath.parse_string(formatted) | ||
| assert math.isclose(parsed.bits, original.bits, rel_tol=1e-9, abs_tol=1e-9) | ||
|
|
||
|
|
||
| @given( | ||
| value=nonneg_finite, | ||
| src=st.sampled_from(unit_names), | ||
| dst=st.sampled_from(unit_names), | ||
| ) | ||
| @settings(max_examples=300) | ||
| def test_unit_conversion_lossless(value, src, dst): | ||
| src_cls = getattr(bitmath, src) | ||
| original = src_cls(value) | ||
| via = getattr(original, f"to_{dst}")() | ||
| back = getattr(via, f"to_{src}")() | ||
| assert math.isclose(back.bits, original.bits, rel_tol=1e-9, abs_tol=1e-9) | ||
|
timlnx marked this conversation as resolved.
Dismissed
|
||
|
|
||
|
|
||
| @given(garbage=st.text()) | ||
| @settings(max_examples=500) | ||
| def test_parse_string_strict_only_raises_value_error(garbage): | ||
| try: | ||
| result = bitmath.parse_string(garbage) | ||
| except ValueError: | ||
| return | ||
| assert isinstance(result, bitmath.Bitmath) | ||
|
timlnx marked this conversation as resolved.
Dismissed
|
||
|
|
||
|
|
||
| @given(garbage=st.text()) | ||
| @settings(max_examples=500) | ||
| def test_parse_string_unsafe_only_raises_value_error(garbage): | ||
| try: | ||
| result = bitmath.parse_string(garbage, strict=False) | ||
| except ValueError: | ||
| return | ||
| assert isinstance(result, bitmath.Bitmath) | ||
|
timlnx marked this conversation as resolved.
Dismissed
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.