-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathXSSTest.java
More file actions
84 lines (67 loc) · 2.46 KB
/
XSSTest.java
File metadata and controls
84 lines (67 loc) · 2.46 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
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Constructor;
import org.junit.jupiter.api.Test;
public class XSSTest {
@Test
public void testPrivateConstructor() throws Exception {
// Access the private constructor to achieve 100% line coverage
Constructor<XSS> constructor = XSS.class.getDeclaredConstructor();
constructor.setAccessible(true);
XSS instance = constructor.newInstance();
assertNotNull(instance, "Instance should be created successfully via reflection");
}
@Test
public void testUri() {
// Branch: value == null
assertEquals("", XSS.uri(null));
// Branch: value.isEmpty()
assertEquals("", XSS.uri(""));
// Branch: Safe characters (should return the same string)
String safe = "abc-._~123";
assertEquals(safe, XSS.uri(safe));
// Branch: Requires escaping (spaces to %20)
String escaped = XSS.uri("space here");
assertNotNull(escaped);
assertTrue(escaped.contains("%20"), "Space should be encoded as %20");
}
@Test
public void testHtml() {
// Branch: value == null
assertEquals("", XSS.html(null));
// Branch: value.isEmpty()
assertEquals("", XSS.html(""));
// Branch: Safe characters
String safe = "safeText";
assertEquals(safe, XSS.html(safe));
// Branch: Requires HTML level 2 escaping (<, >, ', ")
String escaped = XSS.html("<script>alert('xss')</script>");
assertNotNull(escaped);
assertTrue(
escaped.contains("<script>"), "HTML tags should be escaped to named references");
assertTrue(
escaped.contains("'") || escaped.contains("'"), "Single quotes should be escaped");
}
@Test
public void testJson() {
// Branch: value == null
assertEquals("\"\"", XSS.json(null));
// Branch: value.isEmpty()
assertEquals("\"\"", XSS.json(""));
// Branch: Safe characters
String safe = "safeString123";
assertEquals(safe, XSS.json(safe));
// Branch: Requires JSON level 2 escaping (Quotes, newlines, control characters)
String escaped = XSS.json("quote\"newline\n");
assertNotNull(escaped);
assertTrue(
escaped.contains("\\\"") || escaped.contains("\\u0022"), "Double quotes should be escaped");
assertTrue(
escaped.contains("\\n") || escaped.contains("\\u000A"), "Newlines should be escaped");
}
}