Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions garak/probes/badchars.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ def _select_positions(
def _select_ascii(limit: int) -> List[str]:
if limit is None or limit <= 0 or limit >= len(ASCII_PRINTABLE):
return list(ASCII_PRINTABLE)

if limit == 1:
return [ASCII_PRINTABLE[0]]

step = max(1, (len(ASCII_PRINTABLE) - 1) // (limit - 1))
selected = [ASCII_PRINTABLE[i] for i in range(0, len(ASCII_PRINTABLE), step)]
return selected[:limit]
Expand Down
21 changes: 21 additions & 0 deletions tests/probes/test_probes_badchars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Regression tests for the badchars probe ASCII variant cap."""

import pytest

from garak.probes.badchars import ASCII_PRINTABLE, BadCharacters


@pytest.mark.parametrize(
("limit", "expected_count"),
[
(1, 1),
(2, 2),
(3, 3),
],
)
def test_select_ascii_respects_small_limits(limit: int, expected_count: int) -> None:
selected = BadCharacters._select_ascii(limit)

assert len(selected) == expected_count
assert selected[0] == ASCII_PRINTABLE[0]
assert all(character in ASCII_PRINTABLE for character in selected)