Skip to content

Commit 42e18a1

Browse files
Migrate tests to JUnit Jupiter
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 1532735 commit 42e18a1

30 files changed

+817
-955
lines changed

java10-shim/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
<artifactId>java8-shim</artifactId>
3838
</dependency>
3939
<dependency>
40-
<groupId>junit</groupId>
41-
<artifactId>junit</artifactId>
40+
<groupId>org.junit.jupiter</groupId>
41+
<artifactId>junit-jupiter</artifactId>
4242
<scope>test</scope>
4343
</dependency>
4444
</dependencies>

java8-shim/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
<dependencies>
2020
<dependency>
21-
<groupId>junit</groupId>
22-
<artifactId>junit</artifactId>
21+
<groupId>org.junit.jupiter</groupId>
22+
<artifactId>junit-jupiter</artifactId>
2323
<scope>test</scope>
2424
</dependency>
2525
</dependencies>

owasp-java-html-sanitizer/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@
114114
<scope>provided</scope>
115115
</dependency>
116116
<dependency>
117-
<groupId>junit</groupId>
118-
<artifactId>junit</artifactId>
117+
<groupId>org.junit.jupiter</groupId>
118+
<artifactId>junit-jupiter</artifactId>
119119
<scope>test</scope>
120120
</dependency>
121121
<dependency>

owasp-java-html-sanitizer/src/test/java/org/owasp/html/AntiSamyTest.java

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@
2424

2525
package org.owasp.html;
2626

27-
import java.io.IOException;
27+
import java.nio.charset.StandardCharsets;
2828
import java.util.regex.Pattern;
2929

3030
import org.apache.commons.codec.binary.Base64;
3131

32-
import junit.framework.AssertionFailedError;
33-
import junit.framework.Test;
34-
import junit.framework.TestCase;
35-
import junit.framework.TestSuite;
32+
import org.junit.jupiter.api.Assertions;
33+
import org.junit.jupiter.api.Test;
34+
import org.opentest4j.AssertionFailedError;
35+
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.junit.jupiter.api.Assertions.assertFalse;
38+
import static org.junit.jupiter.api.Assertions.assertNotNull;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
3640

3741

3842
/**
@@ -42,26 +46,19 @@
4246
* @author Arshan Dabirsiaghi
4347
*
4448
*/
45-
@SuppressWarnings("javadoc")
46-
public class AntiSamyTest extends TestCase {
49+
class AntiSamyTest {
4750

48-
static final boolean RUN_KNOWN_FAILURES = false;
51+
private static final boolean RUN_KNOWN_FAILURES = false;
4952

5053
private static HtmlSanitizer.Policy makePolicy(Appendable buffer) {
5154
final HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
5255
buffer,
53-
new Handler<IOException>() {
54-
public void handle(IOException ex) {
55-
AssertionFailedError failure = new AssertionFailedError();
56-
failure.initCause(ex);
57-
throw failure;
58-
}
59-
},
60-
new Handler<String>() {
61-
public void handle(String errorMessage) {
62-
fail(errorMessage);
63-
}
64-
});
56+
ex -> {
57+
AssertionFailedError failure = new AssertionFailedError();
58+
failure.initCause(ex);
59+
throw failure;
60+
},
61+
Assertions::fail);
6562

6663
return new HtmlPolicyBuilder()
6764
.allowElements(
@@ -73,12 +70,7 @@ public void handle(String errorMessage) {
7370
.allowAttributes("src").onElements("img")
7471
.allowAttributes("class", "id", "title").globally()
7572
.allowAttributes("char").matching(
76-
new AttributePolicy() {
77-
public String apply(
78-
String elementName, String attributeName, String value) {
79-
return value.length() == 1 ? value : null;
80-
}
81-
}).onElements("td")
73+
(elementName, attributeName, value) -> value.length() == 1 ? value : null).onElements("td")
8274
.allowStandardUrlProtocols()
8375
.requireRelNofollowOnLinks()
8476
.allowStyling()
@@ -113,26 +105,12 @@ static String sanitize(String html) {
113105
"C3c+d5Q9lyTafPLdelG1TKaLFinw1TOjyI6KkrQyHKkttfnO58WFvScl1TiRcB/iHxKahskoE2+VRLUIhctuDU4sUvQh/g9Arw0LAA4QTxuLFt01XYdigurz4FT15ox2oDGGGrRb3VGjDTXK1OWVJoLMW95EVqyMc9F+Fdej85LHE+8WesIfacjUQtTG1tzYVQTfubZq0+qxXws8QrxMLFtVE38tbeXo+Ok1/U5TUa6FjWflEfvKY3XVcl8RKkXua7fVz/Blj8Gh+dWe2cOxa0lpM75ZHyz9adQrB2Pb4571E4u2xI5un0R0MFJZBQuPDc1G5rPhyk+Hb4LRG3dS0m8IASQUOskv93z978L1+Abu9CLP6d6s5p+BzWxhMUqwQXC/CCpTywrkJ0RG",
114106
};
115107

116-
@Override
117-
protected void setUp() throws Exception {
118-
super.setUp();
119-
}
120-
121-
@Override
122-
protected void tearDown() throws Exception {
123-
super.tearDown();
124-
}
125-
126-
public static Test suite() {
127-
TestSuite suite = new TestSuite(AntiSamyTest.class);
128-
return suite;
129-
}
130-
131108
/*
132109
* Test basic XSS cases.
133110
*/
134111

135-
public static void testScriptAttacks() {
112+
@Test
113+
void testScriptAttacks() {
136114
assertSanitizedDoesNotContain("test<script>alert(document.cookie)</script>", "script");
137115
assertSanitizedDoesNotContain("test<script>alert(document.cookie)</script>", "script");
138116

@@ -161,7 +139,8 @@ public static void testScriptAttacks() {
161139
assertSanitizedDoesNotContain("<a onblur=\"alert(secret)\" href=\"http://www.google.com\">Google</a>", "alert");
162140
}
163141

164-
public static void testImgAttacks() {
142+
@Test
143+
void testImgAttacks() {
165144
assertSanitizedDoesContain("<img src=\"http://www.myspace.com/img.gif\"/>", "<img");
166145
assertSanitizedDoesContain("<img src=\"http://www.myspace.com/img.gif\"/>", "<img");
167146

@@ -177,11 +156,11 @@ public static void testImgAttacks() {
177156
assertSanitizedDoesNotContain("<IMG SRC=\"jav&#x0D;ascript:alert('XSS');\">", "alert");
178157

179158
String s = "<IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>";
180-
if (sanitize(s).length() != 0) {
159+
if (!sanitize(s).isEmpty()) {
181160
assertSanitizedDoesContain(s, "&amp;");
182161
}
183162
s = "<IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>";
184-
if (sanitize(s).length() != 0) {
163+
if (!sanitize(s).isEmpty()) {
185164
assertSanitizedDoesContain(s, "&amp;");
186165
}
187166

@@ -198,7 +177,8 @@ public static void testImgAttacks() {
198177
assertSanitizedDoesNotContain("<BGSOUND SRC=\"javascript:alert('XSS');\">", "javascript");
199178
}
200179

201-
public static void testHrefAttacks() {
180+
@Test
181+
void testHrefAttacks() {
202182
assertSanitizedDoesNotContain("<LINK REL=\"stylesheet\" HREF=\"javascript:alert('XSS');\">", "href");
203183
assertSanitizedDoesNotContain("<LINK REL=\"stylesheet\" HREF=\"javascript:alert('XSS');\">", "href");
204184

@@ -304,7 +284,8 @@ public static void testHrefAttacks() {
304284
* Test CSS protections.
305285
*/
306286

307-
public static void testCssAttacks() {
287+
@Test
288+
void testCssAttacks() {
308289

309290
assertSanitizedDoesNotContain("<div style=\"position:absolute\">", "position");
310291
assertSanitizedDoesNotContain("<div style=\"position:absolute\">", "position");
@@ -323,14 +304,15 @@ public static void testCssAttacks() {
323304
* Test a bunch of strings that have tweaked the XML parsing capabilities of
324305
* NekoHTML.
325306
*/
326-
public static void testIllegalXML() throws Exception {
327-
for (int i = 0; i < BASE64_BAD_XML_STRINGS.length; i++) {
328-
String testStr = new String(
329-
Base64.decodeBase64(BASE64_BAD_XML_STRINGS[i]),
330-
"UTF-8");
331-
sanitize(testStr);
332-
sanitize(testStr);
333-
}
307+
@Test
308+
void testIllegalXML() {
309+
for (String base64BadXmlString : BASE64_BAD_XML_STRINGS) {
310+
String testStr = new String(
311+
Base64.decodeBase64(base64BadXmlString),
312+
StandardCharsets.UTF_8);
313+
sanitize(testStr);
314+
sanitize(testStr);
315+
}
334316

335317
// These fail in AntiSamy due to a bug in NekoHTML
336318
assertEquals(
@@ -340,10 +322,11 @@ public static void testIllegalXML() throws Exception {
340322
"<a href=\"http://www.test.com\" rel=\"nofollow\"></a>",
341323
sanitize("<a - href=\"http://www.test.com\">"));
342324

343-
assertTrue(sanitize("<style>") != null);
325+
assertNotNull(sanitize("<style>"));
344326
}
345327

346-
public static void testPreviousBugs() {
328+
@Test
329+
void testPreviousBugs() {
347330

348331
/*
349332
* issues 12 (and 36, which was similar). empty tags cause display
@@ -533,7 +516,7 @@ public static void testPreviousBugs() {
533516
String attack = "[if lte 8]<script>";
534517
String spacer = "<![if IE]>";
535518

536-
StringBuffer sb = new StringBuffer();
519+
StringBuilder sb = new StringBuilder();
537520

538521
sb.append("<div>text<!");
539522

@@ -555,7 +538,7 @@ public static void testPreviousBugs() {
555538
*/
556539
{
557540
String s = "<iframe src='http://foo.com/'></iframe>" + "<script src=''></script>" + "<link href='/foo.css'>";
558-
assertEquals(s, "", sanitize(s));
541+
assertEquals("", sanitize(s), s);
559542
}
560543

561544
/* issue #51 - offsite urls with () are found to be invalid */
@@ -635,7 +618,8 @@ public static void testPreviousBugs() {
635618
* Tests cases dealing with nofollowAnchors directive. Assumes anchor tags
636619
* have an action set to "validate" (may be implicit) in the policy file.
637620
*/
638-
public static void testNofollowAnchors() {
621+
@Test
622+
void testNofollowAnchors() {
639623
// adds when not present
640624
assertSanitized("<a href=\"blah\">link</a>", "<a href=\"blah\" rel=\"nofollow\">link</a>");
641625

@@ -655,7 +639,8 @@ public static void testNofollowAnchors() {
655639
assertSanitizedDoesNotContain("a href=\"blah\">link</a>", "nofollow");
656640
}
657641

658-
public static void testValidateParamAsEmbed() {
642+
@Test
643+
void testValidateParamAsEmbed() {
659644
// let's start with a YouTube embed
660645
String input = "<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>";
661646
String expectedOutput = "<object height=\"340\" width=\"560\"><param name=\"movie\" value=\"http://www.youtube.com/v/IyAyd4WnvhU&amp;hl=en&amp;fs=1&amp;\" /><param name=\"allowFullScreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><embed allowfullscreen=\"true\" allowscriptaccess=\"always\" height=\"340\" src=\"http://www.youtube.com/v/IyAyd4WnvhU&amp;hl=en&amp;fs=1&amp;\" type=\"application/x-shockwave-flash\" width=\"560\" /></object>";
@@ -684,7 +669,7 @@ public static void testValidateParamAsEmbed() {
684669
}
685670

686671
if (RUN_KNOWN_FAILURES) {
687-
assertTrue(sanitize(input).equals(saxExpectedOutput));
672+
assertEquals(saxExpectedOutput, sanitize(input));
688673
} else {
689674
assertSanitized(input, "");
690675
}
@@ -715,9 +700,8 @@ private static void assertSanitizedDoesNotContain(
715700
int index = Strings.toLowerCase(sanitized).indexOf(
716701
Strings.toLowerCase(dangerousContent));
717702
assertEquals(
718-
"`" + sanitized + "` from `" + html + "` contains `" +
719-
dangerousContent + "`",
720-
-1, index);
703+
-1, index,
704+
"`" + sanitized + "` from `" + html + "` contains `" + dangerousContent + "`");
721705
}
722706

723707
private static void assertSanitizedDoesContain(
@@ -726,9 +710,9 @@ private static void assertSanitizedDoesContain(
726710
int index = Strings.toLowerCase(sanitized).indexOf(
727711
Strings.toLowerCase(dangerousContent));
728712
assertTrue(
729-
"`" + sanitized + "` from `" + html + "` does not contain `" +
730-
dangerousContent + "`",
731-
index >= 0);
713+
index >= 0,
714+
"`" + sanitized + "` from `" + html + "` does not contain `" + dangerousContent + "`"
715+
);
732716
}
733717

734718
private static void assertSanitized(String html, String sanitized) {

owasp-java-html-sanitizer/src/test/java/org/owasp/html/Benchmark.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,8 @@ private static String sanitize(String html) {
147147
StringBuilder sb = new StringBuilder(html.length());
148148

149149
final HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
150-
sb, new Handler<String>() {
151-
152-
public void handle(String x) {
153-
throw new AssertionError(x);
154-
}
150+
sb, x -> {
151+
throw new AssertionError(x);
155152
});
156153

157154
HtmlSanitizer.sanitize(html, new HtmlSanitizer.Policy() {
@@ -206,10 +203,8 @@ private static String sanitizeUsingPolicyBuilder(String html) {
206203
StringBuilder sb = new StringBuilder(html.length());
207204

208205
HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
209-
sb, new Handler<String>() {
210-
public void handle(String x) {
211-
throw new AssertionError(x);
212-
}
206+
sb, x -> {
207+
throw new AssertionError(x);
213208
});
214209

215210
HtmlSanitizer.sanitize(html, policyBuilder.build(renderer));

owasp-java-html-sanitizer/src/test/java/org/owasp/html/CssFuzzerTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333
import java.util.Random;
3434
import java.util.regex.Pattern;
3535

36-
import org.junit.Test;
36+
import org.junit.jupiter.api.Test;
3737
import org.owasp.html.CssTokens.TokenType;
3838

39-
@SuppressWarnings("javadoc")
40-
public class CssFuzzerTest extends FuzzyTestCase {
39+
import static org.junit.jupiter.api.Assertions.assertEquals;
40+
import static org.junit.jupiter.api.Assertions.assertFalse;
41+
import static org.junit.jupiter.api.Assertions.fail;
42+
43+
class CssFuzzerTest extends FuzzyTestCase {
4144

4245
private static final String[] TOKEN_PARTS = new String[] {
4346
"'", "\"", "<!--", "-->", "/*", "*/", "***", "//", "\r", "\n",
@@ -79,7 +82,7 @@ public void run() {
7982
}
8083

8184
@Test
82-
public final void testUnderStress() {
85+
void testUnderStress() {
8386
Random r = this.rnd;
8487
Watcher watcher = new Watcher();
8588
Thread watcherThread = null;
@@ -122,17 +125,17 @@ public final void testUnderStress() {
122125
System.err.println(it.token() + ":" + it.type());
123126
}
124127
assertEquals(
125-
"not idempotent, " + msg,
126128
tokens.normalizedCss,
127-
renormalized);
129+
renormalized,
130+
"not idempotent, " + msg);
128131
}
129132
}
130133

131134
// Test normalized CSS does not contain HTML/XML breaking tokens.
132135
for (String disallowed : DISALLOWED_IN_OUTPUT) {
133136
assertFalse(
134-
"contains " + disallowed + ", " + msg,
135-
tokens.normalizedCss.contains(disallowed));
137+
tokens.normalizedCss.contains(disallowed),
138+
"contains " + disallowed + ", " + msg);
136139
}
137140

138141
// Test that tokens are roughly well-formed.
@@ -158,7 +161,7 @@ public final void testUnderStress() {
158161
}
159162
for (int j = 0; j < nTokens; ++j) {
160163
if (reverse[j] != -1) {
161-
assertEquals(msg, reverse[reverse[j]], j);
164+
assertEquals(reverse[reverse[j]], j, msg);
162165
}
163166
}
164167
}

0 commit comments

Comments
 (0)