forked from NVIDIA/garak
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_detectors_base.py
More file actions
198 lines (159 loc) · 6.45 KB
/
Copy pathtest_detectors_base.py
File metadata and controls
198 lines (159 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import pytest
from garak.attempt import Attempt
import garak.detectors.base
def test_filedetector_nonexist():
d = garak.detectors.base.FileDetector()
a = Attempt(prompt="")
a.outputs = [None, "", "/non/existing/file"]
a.notes["format"] = d.valid_format
assert (
len(list(d.detect(a))) == 0
), "FileDetector should skip filenames for non-existing files"
TEST_STRINGS = [
"test", # 0: lower
"UPPERCASE", # 1: upper
"MixedCase", # 2: mixed (camel)
]
@pytest.fixture
def string_detector():
return garak.detectors.base.StringDetector(TEST_STRINGS)
def test_case_sensitive_exact_match(string_detector):
"""Test that exact matches are detected when case_sensitive=True"""
for test_str in TEST_STRINGS:
attempt = Attempt(prompt="Hello")
attempt.outputs = [test_str]
results = string_detector.detect(attempt, case_sensitive=True)
assert results == [1.0], f"Failed to detect exact match for '{test_str}'"
def test_case_sensitive_no_match(string_detector):
"""Test that case variations are not detected when case_sensitive=True"""
test_cases = [
TEST_STRINGS[0].upper(), # uppercase variation
TEST_STRINGS[0].title(), # capitalized variation
"tEsT", # mixed case variation
TEST_STRINGS[1].lower(), # lowercase variation
TEST_STRINGS[2].lower(), # lowercase variation
TEST_STRINGS[2].title(), # title but not camelcase variation
]
for test_str in test_cases:
attempt = Attempt(prompt="Hello")
attempt.outputs = [test_str]
results = string_detector.detect(attempt, case_sensitive=True)
assert results == [
0.0
], f"Should not detect absent case variation '{test_str}' in case sensitive mode"
def test_case_insensitive_match(string_detector):
"""Test that case variations are detected when case_sensitive=False"""
test_cases = [
(TEST_STRINGS[0], TEST_STRINGS[0].upper()),
(TEST_STRINGS[0], TEST_STRINGS[0].title()),
(TEST_STRINGS[1], TEST_STRINGS[1].lower()),
(TEST_STRINGS[1], TEST_STRINGS[1].title()),
(TEST_STRINGS[2], TEST_STRINGS[2].lower()),
(TEST_STRINGS[2], TEST_STRINGS[2].upper()),
]
for original, variant in test_cases:
attempt = Attempt(prompt="Hello")
attempt.outputs = [variant]
results = string_detector.detect(attempt, case_sensitive=False)
assert results == [
1.0
], f"Failed to detect case insensitive match: '{original}' vs '{variant}'"
def test_multiple_outputs(string_detector):
"""Test detection across multiple outputs with mixed case sensitivity"""
attempt = Attempt(prompt="Hello")
attempt.outputs = [TEST_STRINGS[0], TEST_STRINGS[0].upper(), "no match"]
# Case sensitive - should only match exact "test"
results = string_detector.detect(attempt, case_sensitive=True)
assert results == [
1.0,
0.0,
0.0,
], "Case sensitive matching failed for multiple outputs"
# Case insensitive - should match both "test" and "TEST"
results = string_detector.detect(attempt, case_sensitive=False)
assert results == [
1.0,
1.0,
0.0,
], "Case insensitive matching failed for multiple outputs"
def test_none_outputs(string_detector):
"""Test handling of None outputs"""
attempt = Attempt(prompt="Hello")
attempt.outputs = [None, TEST_STRINGS[0], None]
results = string_detector.detect(attempt, case_sensitive=True)
assert len(results) == 1, "Detector expected to skip None outputs"
assert results == [1.0], "Failed to handle None outputs correctly"
MATCHTYPE_AND_CASING_CASES = {
"str": [
(f"begin {TEST_STRINGS[0]}ing", True), # should match
(
f"begin {TEST_STRINGS[0].upper()}ING",
False,
), # shouldn't match case sensitive
],
"word": [
(f"{TEST_STRINGS[0]} word", True), # should match
(
f"{TEST_STRINGS[0].upper()} word",
False,
), # shouldn't match case sensitive
],
"startswith": [
(f"{TEST_STRINGS[0]} start", True), # should match
(
f"{TEST_STRINGS[0].upper()} start",
False,
), # shouldn't match case sensitive
],
}
@pytest.mark.parametrize("matchtype", MATCHTYPE_AND_CASING_CASES.keys())
def test_matchtype_with_case_sensitivity(matchtype):
"""Test case sensitivity with different matchtypes"""
detector = garak.detectors.base.StringDetector([TEST_STRINGS[0]])
detector.matchtype = matchtype
for text, should_match_sensitive in MATCHTYPE_AND_CASING_CASES[matchtype]:
attempt = Attempt(prompt="Hello")
attempt.outputs = [text]
# Test case sensitive
results = detector.detect(attempt, case_sensitive=True)
assert results == [
1.0 if should_match_sensitive else 0.0
], f"Case sensitive {matchtype} match failed for '{text}'"
# Test case insensitive
results = detector.detect(attempt, case_sensitive=False)
assert results == [
1.0
], f"Case insensitive {matchtype} match failed for '{text}'"
def test_stringdetector_word_boundary_matching():
detector = garak.detectors.base.StringDetector(TEST_STRINGS)
detector.matchtype = "word"
test_cases = [
("begin testing", False),
("begin the test", True),
("THIS IS UPPERCASE", True),
("THIS IS UPPER CASE", False),
("THISISUPPERCASE", False),
]
for text, match in test_cases:
attempt = Attempt(prompt="")
attempt.outputs = [text]
results = detector.detect(attempt, case_sensitive=True)
assert results == [
1.0 if match else 0.0
], f"Word boundary match failed for '{text}', expected {match}"
def test_stringdetector_startswith():
detector = garak.detectors.base.StringDetector(TEST_STRINGS)
detector.matchtype = "startswith"
test_cases = [
("this is a test", False),
("tests always succeed", True),
]
for text, match in test_cases:
attempt = Attempt(prompt="")
attempt.outputs = [text]
results = detector.detect(attempt, case_sensitive=True)
assert results == [
1.0 if match else 0.0
], f"startswith match failed for '{text}', expected {match}"