DeepTeam is a framework to red team LLMs and AI agents.
tests updated
A-Vamshif837598378b2
diff --git a/tests/test_core/test_attacks/test_context_flooding.py b/tests/test_core/test_attacks/test_context_flooding.pyindex 7427d327..e8785143 100644--- a/tests/test_core/test_attacks/test_context_flooding.py+++ b/tests/test_core/test_attacks/test_context_flooding.py@@ -14,27 +14,49 @@ class TestContextFlooding:assert attack.weight == 5assert attack.get_name() == "ContextFlooding"- def test_context_flooding_enhance(self):- attack = ContextFlooding()- original_text = "Hello, world!"+ def test_context_flooding_enhance_basic(self):+ attack = ContextFlooding(target_chars=500)+ original_text = "Test attack payload"enhanced = attack.enhance(original_text)- assert len(enhanced) > (4000 + len(original_text) // 4)- def test_context_flooding_enhance_special_characters(self):- attack = ContextFlooding()- original_text = "!@#$%^&*()_+"- enhanced = attack.enhance(original_text)- assert len(enhanced) > (4000 + len(original_text) // 4)+ assert original_text in enhanced+ assert len(enhanced) >= len(original_text)- def test_context_flooding_enhance_unicode(self):- attack = ContextFlooding()- original_text = "Hello 世界 🌍"- enhanced = attack.enhance(original_text)- assert len(enhanced) > (4000 + len(original_text) // 4)+ def test_context_flooding_reaches_target_chars(self):+ target_chars = 800+ attack = ContextFlooding(target_chars=target_chars)+ enhanced = attack.enhance("attack")++ assert len(enhanced) >= target_chars++ def test_context_flooding_custom_prefix(self):+ custom_prefix = "Custom prefix content."+ attack = ContextFlooding(prefix=custom_prefix, target_chars=300)+ enhanced = attack.enhance("payload")- def test_context_flooding_enhance_with_prefix(self):- attack = ContextFlooding(prefix="This is a test" * 10)- original_text = "Hello 世界 🌍"+ assert custom_prefix in enhanced+ assert enhanced.endswith("payload")++ def test_context_flooding_invalid_empty_prefix(self):+ with pytest.raises(ValueError):+ ContextFlooding(prefix="")++ def test_context_flooding_invalid_prefix_type(self):+ with pytest.raises(ValueError):+ ContextFlooding(prefix_type="invalid")++ def test_context_flooding_deterministic_output(self):+ attack = ContextFlooding(target_chars=600)+ original_text = "attack payload"++ enhanced_1 = attack.enhance(original_text)+ enhanced_2 = attack.enhance(original_text)++ assert enhanced_1 == enhanced_2++ def test_context_flooding_unicode_payload(self):+ attack = ContextFlooding(target_chars=500)+ original_text = "こんにちは 世界 🌏"enhanced = attack.enhance(original_text)- assert len(enhanced) > (4000 + len(original_text) // 4)- assert "This is a test" in enhanced++ assert original_text in enhanceddiff --git a/tests/test_core/test_attacks/test_embedded_instruction_json.py b/tests/test_core/test_attacks/test_embedded_instruction_json.pyindex 4b7e4bd0..e949c651 100644--- a/tests/test_core/test_attacks/test_embedded_instruction_json.py+++ b/tests/test_core/test_attacks/test_embedded_instruction_json.py@@ -1,7 +1,15 @@+import jsonimport pytestfrom deepteam.attacks.single_turn import EmbeddedInstructionJSON+def extract_json(text: str) -> dict:+ start = text.find("{")+ if start == -1:+ raise ValueError("No JSON object found in text")+ return json.loads(text[start:])++class TestEmbeddedInstructionJSON:def test_embedded_instruction_json_initialization(self):@@ -10,24 +18,112 @@ class TestEmbeddedInstructionJSON:assert attack.get_name() == "EmbeddedInstructionJSON"def test_embedded_instruction_json_initialization_with_weight(self):- attack = EmbeddedInstructionJSON(weight=5)- assert attack.weight == 5- assert attack.get_name() == "EmbeddedInstructionJSON"+ attack = EmbeddedInstructionJSON(weight=3)+ assert attack.weight == 3- def test_embedded_instruction_json_enhance(self):- attack = EmbeddedInstructionJSON()- original_text = "Hello, world!"- enhanced = attack.enhance(original_text)- assert len(enhanced) > (2000 + len(original_text) // 4)+ def test_embedded_instruction_json_enhance_basic(self):+ attack = EmbeddedInstructionJSON(target_chars=500)+ payload = "test payload"+ enhanced = attack.enhance(payload)- def test_embedded_instruction_json_enhance_special_characters(self):- attack = EmbeddedInstructionJSON()- original_text = "!@#$%^&*()_+"- enhanced = attack.enhance(original_text)- assert len(enhanced) > (2000 + len(original_text) // 4)+ assert payload in enhanced+ assert len(enhanced) >= 500- def test_embedded_instruction_json_enhance_unicode(self):- attack = EmbeddedInstructionJSON()- original_text = "Hello 世界 🌍"- enhanced = attack.enhance(original_text)- assert len(enhanced) > (2000 + len(original_text) // 4)+ def test_embedded_instruction_json_reaches_target_chars(self):+ target_chars = 1000+ attack = EmbeddedInstructionJSON(target_chars=target_chars)+ enhanced = attack.enhance("attack")++ assert len(enhanced) >= target_chars++ def test_embedded_instruction_json_contains_main_note(self):+ attack = EmbeddedInstructionJSON(target_chars=500)+ payload = "important instruction"+ enhanced = attack.enhance(payload)++ parsed = extract_json(enhanced)++ def find_main_note(obj):+ if isinstance(obj, dict):+ if "main_note" in obj:+ return obj["main_note"]+ for v in obj.values():+ found = find_main_note(v)+ if found is not None:+ return found+ elif isinstance(obj, list):+ for item in obj:+ found = find_main_note(item)+ if found is not None:+ return found+ return None++ assert find_main_note(parsed) == payload++ def test_embedded_instruction_json_attack_position_start(self):+ attack = EmbeddedInstructionJSON(+ target_chars=500, attack_position="start"+ )+ enhanced = attack.enhance("payload")+ parsed = extract_json(enhanced)++ first_key = next(iter(parsed.keys()))+ assert first_key == "deep_context"++ def test_embedded_instruction_json_attack_position_middle(self):+ attack = EmbeddedInstructionJSON(+ target_chars=500, attack_position="middle"+ )+ enhanced = attack.enhance("payload")+ parsed = extract_json(enhanced)++ keys = list(parsed.keys())+ assert "deep_context" in keys+ assert keys.index("deep_context") != 0+ assert keys.index("deep_context") != len(keys) - 1++ def test_embedded_instruction_json_attack_position_end(self):+ attack = EmbeddedInstructionJSON(+ target_chars=500, attack_position="end"+ )+ enhanced = attack.enhance("payload")+ parsed = extract_json(enhanced)++ last_key = list(parsed.keys())[-1]+ assert last_key == "deep_context"++ def test_embedded_instruction_json_custom_template(self):+ custom_json = {+ "tool": "search",+ "results": [{"title": "doc", "snippet": "text"}],+ }+ attack = EmbeddedInstructionJSON(+ json_template=custom_json,+ target_chars=500,+ )+ enhanced = attack.enhance("payload")+ parsed = extract_json(enhanced)++ assert "tool" in parsed+ assert "results" in parsed++ def test_embedded_instruction_json_deterministic(self):+ attack = EmbeddedInstructionJSON(target_chars=700)+ payload = "deterministic payload"++ enhanced_1 = attack.enhance(payload)+ enhanced_2 = attack.enhance(payload)++ assert enhanced_1 == enhanced_2++ def test_embedded_instruction_json_invalid_target_chars(self):+ with pytest.raises(ValueError):+ EmbeddedInstructionJSON(target_chars=10)++ def test_embedded_instruction_json_invalid_target_depth(self):+ with pytest.raises(ValueError):+ EmbeddedInstructionJSON(target_depth=0)++ def test_embedded_instruction_json_invalid_json_template(self):+ with pytest.raises(ValueError):+ EmbeddedInstructionJSON(json_template="not a dict")