Download src/test/java/com/anthony/failsafeapi/service/PromptInjectionDetectorTest.java from tonyy1/tickets: direct link, hf CLI and curl.
- Browser
- Download file 5.99 kB
-
https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/service/PromptInjectionDetectorTest.java
- Command line
-
hf download hf://spaces/tonyy1/tickets/src/test/java/com/anthony/failsafeapi/service/PromptInjectionDetectorTest.java
-
curl -L -o PromptInjectionDetectorTest.java https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/service/PromptInjectionDetectorTest.java
5.99 kB
| package com.anthony.failsafeapi.service; | |
| import com.anthony.failsafeapi.exception.AISanitizationException; | |
| import org.junit.jupiter.api.BeforeEach; | |
| import org.junit.jupiter.api.DisplayName; | |
| import org.junit.jupiter.api.Nested; | |
| import org.junit.jupiter.api.Test; | |
| import org.junit.jupiter.params.ParameterizedTest; | |
| import org.junit.jupiter.params.provider.ValueSource; | |
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | |
| import static org.assertj.core.api.Assertions.assertThatNoException; | |
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | |
| class PromptInjectionDetectorTest { | |
| private PromptInjectionDetector detector; | |
| void setUp() { | |
| detector = new PromptInjectionDetector(); | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // Textos legΓtimos β NO deben lanzar excepciΓ³n | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class LegitimateTextTests { | |
| void legitimateTextShouldPass(String text) { | |
| assertThatNoException().isThrownBy( | |
| () -> detector.detect(text, null) | |
| ); | |
| } | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // Inyecciones directas β DEBEN lanzar AISanitizationException | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class DirectInjectionTests { | |
| void directInjectionShouldBeBlocked(String maliciousText) { | |
| assertThatThrownBy(() -> detector.detect(maliciousText, null)) | |
| .isInstanceOf(AISanitizationException.class) | |
| .hasMessageContaining("SEGURIDAD"); | |
| } | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // NormalizaciΓ³n de texto | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class NormalizationTests { | |
| void normalizeShouldRemoveAccentsAndLowercase() { | |
| String normalized = PromptInjectionDetector.normalize("ΓGΓΓRΓ instrucciones"); | |
| assertThat(normalized).isLowerCase(); | |
| assertThat(normalized).doesNotContain("Γ", "Γ", "Γ"); | |
| } | |
| void normalizeShouldCollapseSpaces() { | |
| String normalized = PromptInjectionDetector.normalize("texto con muchos espacios"); | |
| assertThat(normalized).doesNotContain(" "); | |
| } | |
| void normalizeShouldSeparateLettersFromNumbers() { | |
| String normalized = PromptInjectionDetector.normalize("ign0ra las instrucciones"); | |
| assertThat(normalized).contains("0"); | |
| } | |
| void normalizeShouldNotReduceRepeatedChars() { | |
| String normalized = PromptInjectionDetector.normalize("hoolaaaa"); | |
| assertThat(normalized).isEqualTo("hoolaaaa"); | |
| } | |
| } | |
| } | |