From f5b422450fc9ac04cbf3d7decdbf5553253a842f Mon Sep 17 00:00:00 2001 From: Ayoub Hammoudi Date: Wed, 4 Nov 2026 10:43:35 +0100 Subject: [PATCH] TEST: Cover edge values and every mode in the converter identifier contract --- .../test_image_prompt_style_converter.py | 11 ++ .../registry/converter_identifier_samples.py | 144 +++++++++++++++--- .../test_converter_identifier_contract.py | 59 +++++-- 3 files changed, 184 insertions(+), 30 deletions(-) diff --git a/tests/unit/converter/test_image_prompt_style_converter.py b/tests/unit/converter/test_image_prompt_style_converter.py index 023bfe1615..23117b2750 100644 --- a/tests/unit/converter/test_image_prompt_style_converter.py +++ b/tests/unit/converter/test_image_prompt_style_converter.py @@ -61,6 +61,17 @@ def test_init_no_filter_uses_configured_root_seed(mock_target) -> None: configure_random_seed(seed=None) +def test_init_no_filter_is_identified_by_the_filter_it_draws(mock_target) -> None: + try: + for seed in range(10): + configure_random_seed(seed=seed) + converter = ImagePromptStyleConverter(converter_target=mock_target) + + assert converter.get_identifier().params["filter_name"] == converter._filter_name + finally: + configure_random_seed(seed=None) + + def test_init_filter_path_custom_yaml(mock_target, tmp_path) -> None: custom_yaml = tmp_path / "custom_filter.yaml" custom_yaml.write_text("style_instructions: custom style\nvariations:\n my_variation: description of variation\n") diff --git a/tests/unit/registry/converter_identifier_samples.py b/tests/unit/registry/converter_identifier_samples.py index 4829d7906c..e3fca08c80 100644 --- a/tests/unit/registry/converter_identifier_samples.py +++ b/tests/unit/registry/converter_identifier_samples.py @@ -11,6 +11,7 @@ from pathlib import Path from typing import Any +import yaml from PIL import Image from pyrit.common.path import CONVERTER_SEED_PROMPT_PATH @@ -18,18 +19,21 @@ AllWordsSelectionStrategy, CodeAttackConverter, ContentWordSelectionStrategy, + ImagePromptStyleConverter, IndexSelectionStrategy, KeywordSelectionStrategy, PositionSelectionStrategy, ProportionSelectionStrategy, RangeSelectionStrategy, RegexSelectionStrategy, + RolePlayToolCallConverter, TokenSelectionStrategy, WordIndexSelectionStrategy, WordKeywordSelectionStrategy, WordPositionSelectionStrategy, WordProportionSelectionStrategy, WordRegexSelectionStrategy, + ZalgoConverter, ) from pyrit.datasets import TextJailBreak from pyrit.models import SeedPrompt @@ -107,11 +111,19 @@ def _repeat(times: int) -> Any: "encoding": CodeAttackConverter.Encoding.PYTHON_LIST, } +LONG_TEXT = "x" * 1000 +LONG_LIST = [f"item{index}" for index in range(100)] +_CODEWORDS = tuple(f"word{index}" for index in range(21)) + +_IMAGE_FILTER = {"filter_name": ImagePromptStyleConverter.list_available_filters()[0]} + SETTINGS_THAT_NEED_ANOTHER: dict[tuple[str, str], dict[str, Any]] = { ("DecompositionConverter", "codewords"): {"use_word_game": True}, + ("GridCompositeConverter", "payload_position"): {"random_seed": 1}, ("DecompositionConverter", "word_game_prompt"): {"use_word_game": True}, ("SelectiveTextConverter", "word_separator"): {"selection_strategy": WordIndexSelectionStrategy(indices=[0])}, - ("ImagePromptStyleConverter", "variation"): {"filter_name": "public_space_tv_broadcast"}, + ("ImagePromptStyleConverter", "filter_name"): _IMAGE_FILTER, + ("ImagePromptStyleConverter", "variation"): _IMAGE_FILTER, ("CodeChameleonConverter", "encrypt_function"): _CUSTOM_CODECHAMELEON, ("CodeChameleonConverter", "decrypt_function"): _CUSTOM_CODECHAMELEON, ("CodeAttackConverter", "encoding"): _CUSTOM_CODE_ATTACK, @@ -146,7 +158,12 @@ def _repeat(times: int) -> Any: SELECTION_STRATEGY_SETTINGS: dict[type, tuple[dict[str, Any], ...]] = { IndexSelectionStrategy: ({"end": 3}, {"end": 4}, {"start": 1, "end": 3}, {"end": 0}, {}), - RegexSelectionStrategy: ({"pattern": "a"}, {"pattern": "b"}, {"pattern": re.compile("a", re.IGNORECASE)}), + RegexSelectionStrategy: ( + {"pattern": "a"}, + {"pattern": "b"}, + {"pattern": re.compile("a", re.IGNORECASE)}, + {"pattern": re.compile("a", re.MULTILINE)}, + ), KeywordSelectionStrategy: ( {"keyword": "a"}, {"keyword": "b"}, @@ -180,7 +197,12 @@ def _repeat(times: int) -> Any: {"proportion": 0.5, "seed": 1}, {"proportion": 0.5, "seed": 0}, ), - WordRegexSelectionStrategy: ({"pattern": "a"}, {"pattern": "b"}, {"pattern": re.compile("a", re.IGNORECASE)}), + WordRegexSelectionStrategy: ( + {"pattern": "a"}, + {"pattern": "b"}, + {"pattern": re.compile("a", re.IGNORECASE)}, + {"pattern": re.compile("a", re.MULTILINE)}, + ), WordPositionSelectionStrategy: _PROPORTIONS, AllWordsSelectionStrategy: ({},), ContentWordSelectionStrategy: ( @@ -192,6 +214,7 @@ def _repeat(times: int) -> Any: {"stopwords": ["a", "c"]}, {"stopwords": ["b", "c"]}, {"stopwords": _MANY_WORDS}, + {"stopwords": []}, {"candidate_words": ["a", "b"]}, {"candidate_words": ["a", "c"]}, {"candidate_words": ["b", "c"]}, @@ -234,12 +257,28 @@ def make_samples(folder: Path) -> dict[str, tuple[Any, ...]]: other_pdf, other_docx = folder / "first_document" / "other.pdf", folder / "first_document" / "other.docx" other_pdf.write_bytes(b"other") other_docx.write_bytes(b"other") - styles = Path(CONVERTER_SEED_PROMPT_PATH) / "image_prompt_style" - filters = [] - for subfolder, source in (("first", "gritty_documentary.yaml"), ("second", "laundromat_fisheye.yaml")): + styles = ImagePromptStyleConverter.IMAGE_PROMPT_STYLE_DIR + filters, wordswaps = [], [] + for subfolder, source in zip(("first", "second"), ImagePromptStyleConverter.list_available_filters(), strict=False): (folder / subfolder).mkdir(exist_ok=True) filters.append(folder / subfolder / "custom.yaml") - filters[-1].write_bytes((styles / source).read_bytes()) + filters[-1].write_bytes((styles / f"{source}.yaml").read_bytes()) + wordswaps.append(folder / subfolder / "wordswap.yaml") + wordswaps[-1].write_text(f"word: [{subfolder}]\n", encoding="utf-8") + first_filter = yaml.safe_load(filters[0].read_text(encoding="utf-8")) + variations = first_filter["variations"] + first_variation = next(iter(variations)) + for subfolder, change in ( + ("style_instructions", {"style_instructions": "Other."}), + ("variation_text", {"variations": {**variations, first_variation: "Other."}}), + ( + "variation_name", + {"variations": {"other" if key == first_variation else key: value for key, value in variations.items()}}, + ), + ): + (folder / subfolder).mkdir(exist_ok=True) + filters.append(folder / subfolder / "custom.yaml") + filters[-1].write_text(yaml.safe_dump({**first_filter, **change}, sort_keys=False), encoding="utf-8") code_attack = _CUSTOM_CODE_ATTACK["template"].read_text(encoding="utf-8") (folder / "first_code_attack").mkdir(exist_ok=True) (folder / "second_code_attack").mkdir(exist_ok=True) @@ -258,7 +297,7 @@ def make_samples(folder: Path) -> dict[str, tuple[Any, ...]]: "base_image": (pngs[0], pngs[1], str(other_png)), "carrier_image_path": (pngs[0], pngs[1], str(other_png)), "benign_image_path": (jpgs[0], jpgs[1], str(other_jpg)), - "innocuous_images": (pngs[:19], [pngs[19], *pngs[1:19]], [*pngs[:18], pngs[19]]), + "innocuous_images": (pngs[:19], [pngs[19], *pngs[1:19]], [*pngs[:18], pngs[19]], [*pngs[:19], pngs[18]]), "payload_background": (pngs[0], pngs[1], str(other_png)), "PDFConverter.existing_pdf": (documents[0], documents[2], other_pdf), "WordDocConverter.existing_docx": (documents[1], documents[3], other_docx), @@ -271,6 +310,8 @@ def make_samples(folder: Path) -> dict[str, tuple[Any, ...]]: SeedPrompt(value="Second {{ prompt }}", parameters=["prompt"]), ), "LLMGenericTextConverter.system_prompt_template": (SeedPrompt(value="First"), SeedPrompt(value="Second")), + "PuzzledConverter.essential_words": (["value", "valuex"], ["valuex", "valuex"], ["value", "valuexy"]), + "TranslationConverter.language": ("first", "second", f"{LONG_TEXT}a", f"{LONG_TEXT}b"), "TranslationConverter.prompt_template": ( SeedPrompt(value="First {{ language }}", parameters=["language"]), SeedPrompt(value="Second {{ language }}", parameters=["language"]), @@ -291,10 +332,35 @@ def make_samples(folder: Path) -> dict[str, tuple[Any, ...]]: "CodeAttackConverter.encoding": tuple(CodeAttackConverter.Encoding), "ScientificTranslationConverter.mode": ("academic", "technical"), "RepeatTokenConverter.token_insert_mode": ("prepend", "append", "repeat"), + "DiacriticConverter.accent": ("grave", "tilde", "umlaut"), + "DiacriticConverter.target_chars": ("aei", "aeo", "aeioux", "aeiouxy", "AEIOU"), + "ZalgoConverter.intensity": (11, 12, 0, ZalgoConverter.MAX_INTENSITY - 1, ZalgoConverter.MAX_INTENSITY), + "AsciiArtConverter.font": ("block", "block2", "mix", "rnd-small"), + "CharSwapConverter.word_selection_strategy": ( + WordIndexSelectionStrategy(indices=[0]), + WordIndexSelectionStrategy(indices=[1]), + WordProportionSelectionStrategy(proportion=0.5), + WordProportionSelectionStrategy(proportion=0.6), + ), + "PDFConverter.font_type": ("Times", "Courier", "Symbol", "ZapfDingbats", "Helvetica-Bold", "Helvetica-Oblique"), + "PDFConverter.font_color": ((0, 0, 0), (1, 1, 1), (2, 1, 1), (1, 2, 1), (1, 1, 2), (255, 255, 255)), + "RolePlayToolCallConverter.message_format": ( + *RolePlayToolCallConverter.MESSAGE_FORMATS, + "xml", + f"{LONG_TEXT}a", + f"{LONG_TEXT}b", + ), + "GridCompositeConverter.payload_position": (0, 2), + "GridCompositeConverter.random_seed": (2, 0), + "codewords": ( + _CODEWORDS, + tuple(reversed(_CODEWORDS)), + *((*_CODEWORDS[:index], f"{word}x", *_CODEWORDS[index + 1 :]) for index, word in enumerate(_CODEWORDS)), + ), "SATAMaskingConverter.num_masks": (3, 4), - "SATAMaskingConverter.skip_first": (2, 3), + "SATAMaskingConverter.skip_first": (0, 2, 3), "video_path": ("first/video.mp4", "second/video.mp4", "first/other.mp4"), - "wordswap_path": ("filipino.yaml", "indian.yaml"), + "wordswap_path": ("filipino.yaml", "indian.yaml", *wordswaps), "font_name": (Path("first/font.ttf"), Path("second/font.ttf"), Path("first/other.ttf")), "filter_path": tuple(filters), "injection_items": ( @@ -304,29 +370,67 @@ def make_samples(folder: Path) -> dict[str, tuple[Any, ...]]: [{"text": "first", "page": 1}, {"text": "second"}], [{"text": "first", "x": 20}, {"text": "second"}], [{"text": "first", "font_size": 20}, {"text": "second"}], + [{"text": "first", "y": 20}, {"text": "second"}], + [{"text": "first", "font": "Times"}, {"text": "second"}], + [{"text": "first", "font_color": (255, 0, 0)}, {"text": "second"}], ), "encrypt_function": (*_SAME_QUALNAME_FUNCTIONS, _repeat(2), _repeat(3)), "decrypt_function": _SAME_QUALNAME_FUNCTIONS, - "filter_name": ("laundromat_fisheye", "polaroid_vintage_film"), - "variation": ("dive_bar_reflection", "airport_lounge"), - "replace": ("first", "second", ["first", "second"], ["first", "third"], ["second", "third"]), + "filter_name": tuple(ImagePromptStyleConverter.list_available_filters()), + "variation": tuple(ImagePromptStyleConverter.list_available_variations(**_IMAGE_FILTER)), + "replace": ( + "first", + "second", + ["first", "second"], + ["first", "third"], + ["second", "third"], + ["first", "first", "second"], + f"{LONG_TEXT}a", + f"{LONG_TEXT}b", + ), "zero_char": ("\u200b", "\u200c"), "one_char": ("\u200d", "\u2060"), "base_char_utf8": ("a", "b"), - "custom_substitutions": ({"a": ["4"]}, {"a": ["@"]}, {"b": ["4"]}), + "custom_substitutions": ( + {"a": ["4"]}, + {"a": ["@"]}, + {"b": ["4"]}, + {"a": ["4", "@"]}, + {"a": ["@", "4"]}, + {"a": ["4"], "b": ["@"]}, + {"a": ["4"], "b": ["4"]}, + ), "stopwords": (["the", "of"], ["the", "in"], ["of", "in"]), "candidate_words": (["alpha", "beta"], ["alpha", "gamma"], ["beta", "gamma"]), - "template": ("First {{ prompt }}", "Second {{ prompt }}", "First {{ prompt }} now"), + "template": ( + "First {{ prompt }}", + "Second {{ prompt }}", + "First {{ prompt }} now", + "First {{ prompt }} " + LONG_TEXT + "a", + "First {{ prompt }} " + LONG_TEXT + "b", + ), "seed": (0, 1), "random_seed": (0, 1), - "CaesarConverter.caesar_offset": (1, -1, 2), - "trap_template": ("{prompt} is {wrong_value}", "Is {prompt} {wrong_value}?"), - "encrypt_type": ("reverse", "binary_tree"), - "persuasion_technique": ("authority_endorsement", "expert_endorsement"), + "CaesarConverter.caesar_offset": (1, -1, 2, 0, 24, 25), + "trap_template": ( + "{prompt} is {wrong_value}", + "Is {prompt} {wrong_value}?", + "{prompt} is {wrong_value} " + LONG_TEXT + "a", + "{prompt} is {wrong_value} " + LONG_TEXT + "b", + ), + "encrypt_type": ("reverse", "binary_tree", "odd_even", "length"), + "persuasion_technique": tuple( + sorted(path.stem for path in (Path(CONVERTER_SEED_PROMPT_PATH) / "persuasion").glob("*.yaml")) + ), "tokenizer": (_Vocabulary("abcdefgh"), _Vocabulary("ijklmnop")), "MathObfuscationConverter.rng": (random.Random(1), random.Random(2)), "retry_exceptions": ((ValueError,), (KeyError,)), - "SearchReplaceConverter.regex_flags": (re.IGNORECASE, re.MULTILINE), + "SearchReplaceConverter.regex_flags": ( + re.IGNORECASE, + re.MULTILINE, + re.IGNORECASE | re.MULTILINE, + re.DOTALL, + ), "jailbreak_template": ( TextJailBreak(string_template="Say first {{ prompt }} now"), TextJailBreak(string_template="Say third {{ prompt }} now"), diff --git a/tests/unit/registry/test_converter_identifier_contract.py b/tests/unit/registry/test_converter_identifier_contract.py index caad99df4e..4ac9840c83 100644 --- a/tests/unit/registry/test_converter_identifier_contract.py +++ b/tests/unit/registry/test_converter_identifier_contract.py @@ -43,6 +43,8 @@ BASELINE_SETTINGS, KEYWORD_SETTINGS, KNOWN_GAPS, + LONG_LIST, + LONG_TEXT, NEUTRAL_SETTINGS, SELECTION_STRATEGY_SETTINGS, SETTINGS_A_MODE_REPLACES, @@ -196,12 +198,16 @@ def _generated_values(parameter: Parameter, default: Any) -> tuple[Any, ...] | N return first, second +def _listed_samples(owner: str, name: str, samples: dict[str, tuple[Any, ...]]) -> tuple[Any, ...] | None: + return samples.get(f"{owner}.{name}", samples.get(name)) + + def _sample_values( owner: str, parameter: Parameter, default: Any, samples: dict[str, tuple[Any, ...]] ) -> tuple[Any, ...] | None: - for key in (f"{owner}.{parameter.name}", parameter.name): - if key in samples: - return samples[key] + listed = _listed_samples(owner, parameter.name, samples) + if listed is not None: + return listed values = _generated_values(parameter, default) takes_none = type(None) in get_args(parameter.param_type) and (owner, parameter.name) not in NEUTRAL_SETTINGS return (*values, None) if values and default is not None and takes_none else values @@ -263,7 +269,7 @@ def _cases() -> list[Any]: def _setup( - converter_name: str, parameter_name: str, samples: dict[str, tuple[Any, ...]] + converter_name: str, parameter_name: str, samples: dict[str, tuple[Any, ...]], *, edges: bool = False ) -> tuple[type, dict[str, Any], tuple[Any, ...]]: registry = _registry() cls = registry.get_class(converter_name) @@ -291,6 +297,9 @@ def _setup( found = None if template is None else (_changed(template), _changed(_changed(template))) if found is None: pytest.fail(f"{converter_name}.{parameter_name}: add a sample value") + if edges and _listed_samples(converter_name, parameter_name, samples) is None: + edge_values = _edge_values(annotation, found[0]) + found = (*found, *(value for value in edge_values if _accepts(cls, {**kwargs, parameter_name: value}))) changed = _changed_from(kwargs.get(parameter_name, _MISSING), found) if not changed: pytest.fail(f"{converter_name}.{parameter_name}: add a sample value that differs from the baseline") @@ -316,6 +325,31 @@ def _changed_from(baseline: Any, values: tuple[Any, ...]) -> tuple[Any, ...]: return tuple(changed) +def _edge_values(annotation: Any, first: Any) -> tuple[Any, ...]: + if annotation is int and isinstance(first, int): + return 0, -first, first + 1000, first + 1001 + if annotation is float and isinstance(first, (int, float)): + return 0.0, -first, first + 0.001, first + 1000, first + 1001 + if annotation is str and isinstance(first, str): + return f"{first}{LONG_TEXT}a", f"{first}{LONG_TEXT}b", first.upper() + if isinstance(first, list) and first and all(isinstance(item, str) for item in first): + return ( + [*first, first[-1]], + [item.upper() for item in first], + [*first, *LONG_LIST, "zzza"], + [*first, *LONG_LIST, "zzzb"], + ) + return () + + +def _accepts(cls: type, kwargs: dict[str, Any]) -> bool: + try: + cls(**kwargs) + except (OSError, TypeError, ValueError): + return False + return True + + def _build(cls: type, kwargs: dict[str, Any], parameter_name: str | None = None) -> Any: try: return cls(**kwargs) @@ -370,14 +404,15 @@ def _hashes(cls: type, kwargs: dict[str, Any], parameter_name: str, values: tupl def test_converter_setting_reaches_identifier( converter_name: str, parameter_name: str, samples: dict[str, tuple[Any, ...]] ) -> None: - cls, kwargs, values = _setup(converter_name, parameter_name, samples) + cls, kwargs, values = _setup(converter_name, parameter_name, samples, edges=True) hashes = _hashes(cls, kwargs, parameter_name, values) picked = "" if parameter_name in kwargs else ", unless a sample is the value it picks when the setting is left out" picked = f"{picked}; if it only matters with another setting, list that in SETTINGS_THAT_NEED_ANOTHER" + picked = f"{picked}; if the converter clamps or normalizes a value, give the setting a sample that avoids it" assert len(set(hashes)) == len(hashes), ( - f"{converter_name}.{parameter_name} changes the converter but not its identifier{picked}" + f"{converter_name}.{parameter_name}: the baseline and {values} do not all get their own identifier{picked}" ) @@ -544,18 +579,22 @@ def test_settings_reach_identifier_in_every_mode( ) -> None: registry = _registry() cls = registry.get_class(converter_name) - mode_value = _setup(converter_name, mode, samples)[2][0] replaced = {mode, *SETTINGS_A_MODE_REPLACES.get((converter_name, mode), set())} + mode_values = _setup(converter_name, mode, samples)[2] for parameter in registry.get_class_metadata(cls).parameters: setting = (converter_name, parameter.name) if parameter.name in replaced or setting in {*NEUTRAL_SETTINGS, *KNOWN_GAPS, *SINGLE_VALUE_SETTINGS}: continue _, kwargs, values = _setup(converter_name, parameter.name, samples) - in_mode = {**kwargs, mode: mode_value, **SETTINGS_THAT_NEED_ANOTHER.get(setting, {})} + for mode_value in mode_values: + in_mode = {**kwargs, mode: mode_value, **SETTINGS_THAT_NEED_ANOTHER.get(setting, {})} - hashes = _hashes(cls, in_mode, parameter.name, values) + hashes = _hashes(cls, in_mode, parameter.name, values) - assert len(set(hashes)) == len(hashes), f"{converter_name}.{parameter.name} is lost when {mode} is {mode_value}" + assert len(set(hashes)) == len(hashes), ( + f"{converter_name}.{parameter.name} is lost when {mode} is {mode_value}; " + "if that mode makes the setting irrelevant, list it in SETTINGS_A_MODE_REPLACES" + ) def _setting_hashes(samples: dict[str, tuple[Any, ...]]) -> dict[tuple[str, str], list[str]]: