-
Notifications
You must be signed in to change notification settings - Fork 5
Add fuzzing #928
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
Add fuzzing #928
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c3e0dea
Add fuzzing
ben-edna d6b0cd9
Only accept safe strings (no strange chars)
ben-edna 49cda94
Use hardcoded schema
ben-edna c128ed5
Update test_fuzzing.py
spoorcc 3915afe
Update test_fuzzing.py
spoorcc 6ccc317
Review comments
spoorcc 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 |
|---|---|---|
|
|
@@ -14,3 +14,4 @@ example/Tests/ | |
| venv* | ||
| *.cdx.json | ||
| release_notes.txt | ||
| .hypothesis | ||
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
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
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
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
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,194 @@ | ||
| """Fuzz test the manifest.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import tempfile | ||
| from contextlib import suppress | ||
| from typing import Any | ||
|
|
||
| import yaml | ||
| from hypothesis import given, settings | ||
| from hypothesis import strategies as st | ||
| from strictyaml import as_document, load | ||
|
|
||
| from dfetch.__main__ import DfetchFatalException, run | ||
| from dfetch.manifest.manifest import Manifest | ||
| from dfetch.manifest.schema import MANIFEST_SCHEMA as schema | ||
| from dfetch.util.util import in_directory | ||
|
|
||
| settings.register_profile( | ||
| "ci", | ||
| max_examples=30, | ||
| deadline=None, | ||
| print_blob=True, | ||
| ) | ||
| settings.register_profile( | ||
| "dev", | ||
| max_examples=100, | ||
| deadline=None, | ||
| ) | ||
| settings.register_profile( | ||
| "manual", | ||
| max_examples=300, | ||
| deadline=None, | ||
| ) | ||
| if os.getenv("CI"): | ||
| settings.load_profile("ci") | ||
| else: | ||
| settings.load_profile("dev") | ||
|
|
||
| # Avoid control chars and NUL to prevent OS/path/subprocess issues in tests | ||
| SAFE_TEXT = st.text( | ||
| alphabet=st.characters( | ||
| min_codepoint=32, blacklist_categories=("Cs",) | ||
| ), # no controls/surrogates | ||
| min_size=0, | ||
| max_size=64, | ||
| ) | ||
|
|
||
| # NUMBER = Int() | Float() with finite floats | ||
| SAFE_NUMBER = st.one_of( | ||
| st.integers(), | ||
| st.floats(allow_nan=False, allow_infinity=False), | ||
| ) | ||
|
|
||
|
|
||
| def opt_str(): | ||
| """Small helper for optional text fields.""" | ||
| return st.none() | SAFE_TEXT | ||
|
|
||
|
|
||
| remote_entry = st.builds( | ||
| lambda name, url_base, default: { | ||
| k: v | ||
| for k, v in { | ||
| "name": name, | ||
| "url-base": url_base, | ||
| "default": default, | ||
| }.items() | ||
| if v is not None | ||
| }, | ||
| name=SAFE_TEXT.filter(lambda s: len(s) > 0), | ||
| url_base=SAFE_TEXT.filter(lambda s: len(s) > 0), | ||
| default=st.none() | st.booleans(), | ||
| ) | ||
|
|
||
| vcs_enum = st.sampled_from(["git", "svn"]) | ||
|
|
||
| ignore_list = st.lists(SAFE_TEXT, min_size=1, max_size=5) | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| project_entry = st.builds( | ||
| lambda name, dst, branch, tag, revision, url, repo_path, remote, patch, vcs, src, ignore: { | ||
| k: v | ||
| for k, v in { | ||
| "name": name, | ||
| "dst": dst, | ||
| "branch": branch, | ||
| "tag": tag, | ||
| "revision": revision, | ||
| "url": url, | ||
| "repo-path": repo_path, | ||
| "remote": remote, | ||
| "patch": patch, | ||
| "vcs": vcs, | ||
| "src": src, | ||
| "ignore": ignore, | ||
| }.items() | ||
| if v is not None | ||
| }, | ||
| name=SAFE_TEXT.filter(lambda s: len(s) > 0), | ||
| dst=opt_str(), | ||
| branch=opt_str(), | ||
| tag=opt_str(), | ||
| revision=opt_str(), | ||
| url=opt_str(), | ||
| repo_path=opt_str(), | ||
| remote=opt_str(), | ||
| patch=opt_str(), | ||
| vcs=st.none() | vcs_enum, | ||
| src=opt_str(), | ||
| ignore=ignore_list, | ||
| ) | ||
|
|
||
| remotes_seq = st.none() | st.lists(remote_entry, min_size=1, max_size=4) | ||
| projects_seq = st.lists(project_entry, min_size=1, max_size=6) | ||
|
|
||
| manifest_strategy = st.builds( | ||
| lambda version, remotes, projects: { | ||
| "manifest": { | ||
| "version": version, | ||
| **({"remotes": remotes} if remotes is not None else {}), | ||
| "projects": projects, | ||
| } | ||
| }, | ||
| version=SAFE_NUMBER, | ||
| remotes=remotes_seq, | ||
| projects=projects_seq, | ||
| ) | ||
|
|
||
|
|
||
| def validate_with_strictyaml(data: Any, yaml_schema: Any) -> None: | ||
| """ | ||
| Ensure that 'data' is serializable with the given StrictYAML schema. | ||
| If it doesn't conform, as_document will raise. | ||
| """ | ||
| as_document(data, yaml_schema) # will raise YAMLSerializationError on mismatch | ||
|
|
||
|
|
||
| @given(manifest_strategy) | ||
| def test_data_conforms_to_schema(data): | ||
| """Validate by attempting to serialize via StrictYAML.""" | ||
| # If data violates the schema, this raises and Hypothesis will shrink to a minimal counterexample. | ||
| validate_with_strictyaml(data, schema) | ||
|
|
||
|
|
||
| @given(manifest_strategy) | ||
| def test_manifest_can_be_created(data): | ||
| """Validate by attempting to construct a Manifest.""" | ||
| try: | ||
| Manifest(data) | ||
| except KeyError: | ||
| pass | ||
|
|
||
|
|
||
| @given(manifest_strategy) | ||
| def test_check(data): | ||
| """Validate check command.""" | ||
| with suppress(DfetchFatalException): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| with in_directory(tmpdir): | ||
| with open("dfetch.yaml", "w", encoding="UTF-8") as manifest_file: | ||
| yaml.dump(data, manifest_file) | ||
| run(["check"]) | ||
|
|
||
|
|
||
| @given(manifest_strategy) | ||
| def test_update(data): | ||
| """Validate update command.""" | ||
| with suppress(DfetchFatalException): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| with in_directory(tmpdir): | ||
| with open("dfetch.yaml", "w", encoding="UTF-8") as manifest_file: | ||
| yaml.dump(data, manifest_file) | ||
| run(["update"]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
| settings.load_profile("manual") | ||
|
|
||
| example = manifest_strategy.example() | ||
| print("One generated example:\n", example) | ||
|
|
||
| # Show the YAML StrictYAML would emit for the example: | ||
| print("\nYAML output:\n", as_document(example, schema).as_yaml()) | ||
|
|
||
| # And ensure parse+validate round-trip works: | ||
| parsed = load(as_document(example, schema).as_yaml(), schema) | ||
| print("\nRound-trip parsed .data:\n", parsed.data) | ||
|
|
||
| test_data_conforms_to_schema() | ||
| test_manifest_can_be_created() | ||
| test_check() | ||
| test_update() | ||
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.