Skip to content

Commit dab735a

Browse files
committed
added framework for formatting tests, refactored XMLUtilsTest
* added FormattingTest annotation (JUnit tagging) * added FormattingChecker interface, various implementations for different formatting configurations and a factory to get appropriate implementation * added formatting options properties sets and multiple executions for Surefire plugin * refactored XMLUtilsTest: made it a @FormattingTest, removed hacks with classloader
1 parent 408fa31 commit dab735a

14 files changed

Lines changed: 454 additions & 243 deletions

pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,80 @@
551551
<LANGUAGE>en_US:us</LANGUAGE>
552552
</environmentVariables>
553553
</configuration>
554+
<executions>
555+
<execution>
556+
<id>formatting-ignore-line-breaks</id>
557+
<goals>
558+
<goal>test</goal>
559+
</goals>
560+
<configuration>
561+
<groups>formattingTest</groups>
562+
<systemPropertiesFile>
563+
${project.build.testOutputDirectory}/formatting/ignore-line-breaks.properties
564+
</systemPropertiesFile>
565+
</configuration>
566+
</execution>
567+
<execution>
568+
<id>formatting-ignore-line-breaks-override</id>
569+
<goals>
570+
<goal>test</goal>
571+
</goals>
572+
<configuration>
573+
<groups>formattingTest</groups>
574+
<systemPropertiesFile>
575+
${project.build.testOutputDirectory}/formatting/ignore-line-breaks-override.properties
576+
</systemPropertiesFile>
577+
</configuration>
578+
</execution>
579+
<execution>
580+
<id>formatting-ignore-base64-line-breaks</id>
581+
<goals>
582+
<goal>test</goal>
583+
</goals>
584+
<configuration>
585+
<groups>formattingTest</groups>
586+
<systemPropertiesFile>
587+
${project.build.testOutputDirectory}/formatting/ignore-base64-line-breaks.properties
588+
</systemPropertiesFile>
589+
</configuration>
590+
</execution>
591+
<execution>
592+
<id>formatting-ignore-base64-line-breaks-override</id>
593+
<goals>
594+
<goal>test</goal>
595+
</goals>
596+
<configuration>
597+
<groups>formattingTest</groups>
598+
<systemPropertiesFile>
599+
${project.build.testOutputDirectory}/formatting/ignore-base64-line-breaks-override.properties
600+
</systemPropertiesFile>
601+
</configuration>
602+
</execution>
603+
<execution>
604+
<id>formatting-base64-custom-formatting</id>
605+
<goals>
606+
<goal>test</goal>
607+
</goals>
608+
<configuration>
609+
<groups>formattingTest</groups>
610+
<systemPropertiesFile>
611+
${project.build.testOutputDirectory}/formatting/base64-custom-formatting.properties
612+
</systemPropertiesFile>
613+
</configuration>
614+
</execution>
615+
<execution>
616+
<id>formatting-illegal</id>
617+
<goals>
618+
<goal>test</goal>
619+
</goals>
620+
<configuration>
621+
<groups>formattingTest</groups>
622+
<systemPropertiesFile>
623+
${project.build.testOutputDirectory}/formatting/illegal.properties
624+
</systemPropertiesFile>
625+
</configuration>
626+
</execution>
627+
</executions>
554628
</plugin>
555629
<plugin>
556630
<artifactId>maven-failsafe-plugin</artifactId>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.xml.security.formatting;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.Matchers.*;
24+
25+
/**
26+
* Checks that XML document is 'pretty-printed', including Base64 values.
27+
*/
28+
public class CustomBase64FormattingChecker implements FormattingChecker {
29+
private int lineLength;
30+
private String lineSeparatorRegex;
31+
32+
/**
33+
* Creates new checker.
34+
* @param lineLength Expected base64 maximum line length
35+
* @param lineSeparatorRegex Regex matching line separator used in Base64 values
36+
*/
37+
public CustomBase64FormattingChecker(int lineLength, String lineSeparatorRegex) {
38+
this.lineLength = lineLength;
39+
this.lineSeparatorRegex = lineSeparatorRegex;
40+
}
41+
42+
@Override
43+
public void checkDocument(String document) {
44+
assertThat(document, containsString("\n"));
45+
}
46+
47+
@Override
48+
public void checkBase64Value(String value) {
49+
String[] lines = value.split(lineSeparatorRegex);
50+
if (lines.length == 0) return;
51+
52+
for (int i = 0; i < lines.length - 1; ++i) {
53+
assertThat(lines[i], matchesPattern(BASE64_PATTERN));
54+
assertEquals(lineLength, lines[i].length());
55+
}
56+
57+
assertThat(lines[lines.length - 1], matchesPattern(BASE64_PATTERN));
58+
assertThat(lines[lines.length - 1].length(), lessThanOrEqualTo(lineLength));
59+
}
60+
61+
@Override
62+
public void checkBase64ValueWithSpacing(String value) {
63+
/* spacing is added only if the value has multiple lines */
64+
if (value.length() <= lineLength) {
65+
assertThat(value, matchesRegex(BASE64_PATTERN));
66+
return;
67+
}
68+
69+
assertThat(value.length(), greaterThanOrEqualTo(2));
70+
assertThat(value, startsWith("\n"));
71+
assertThat(value, endsWith("\n"));
72+
checkBase64Value(value.substring(1, value.length() - 1));
73+
}
74+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.xml.security.formatting;
20+
21+
import java.util.regex.Pattern;
22+
23+
/**
24+
* Checks document formatting where output depends on formatting options.
25+
* Base64 values can be treated in two ways: relatively long values can have additional line breaks
26+
* to separate them from element tags.
27+
*/
28+
public interface FormattingChecker {
29+
/**
30+
* This pattern checks if a string contains only characters from the Base64 alphabet, including padding.
31+
*/
32+
Pattern BASE64_PATTERN = Pattern.compile("^[A-Za-z0-9+/=]*$");
33+
34+
/**
35+
* Checks the formatting of the whole document.
36+
* @param document XML document as string
37+
*
38+
* @implSpec It is assumed that the document contains at least one nested element.
39+
*/
40+
void checkDocument(String document);
41+
42+
/**
43+
* Checks encoded base64 element/attribute value.
44+
* @param value Element value
45+
*/
46+
void checkBase64Value(String value);
47+
48+
/**
49+
* Checks encoded base64 element value with additional spacing.
50+
* @param value Element value
51+
*/
52+
void checkBase64ValueWithSpacing(String value);
53+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.xml.security.formatting;
20+
21+
/**
22+
* Creates formatting checker depending on system properties which define document formatting.
23+
*/
24+
public class FormattingCheckerFactory {
25+
private static final int DEFAULT_BASE64_LINE_LENGTH = 76;
26+
27+
/**
28+
* Gets formatting checker according to system properties.
29+
* @return Formatting checker implementation
30+
*/
31+
public static FormattingChecker getFormattingChecker() {
32+
if (Boolean.getBoolean("org.apache.xml.security.ignoreLineBreaks")) {
33+
/* overrides all Base64 formatting options */
34+
return new NoLineBreaksChecker();
35+
} else if (Boolean.getBoolean("org.apache.xml.security.base64.ignoreLineBreaks")) {
36+
return new NoBase64LineBreaksChecker();
37+
} else {
38+
int lineLength = Integer.getInteger("org.apache.xml.security.base64.lineLength",
39+
DEFAULT_BASE64_LINE_LENGTH);
40+
String lineSeparator = System.getProperty("org.apache.xml.security.base64.lineSeparator");
41+
String lineSeparatorRegex = "lf".equalsIgnoreCase(lineSeparator) ? "\\n" : "\\r\\n";
42+
return new CustomBase64FormattingChecker(lineLength, lineSeparatorRegex);
43+
}
44+
}
45+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.xml.security.formatting;
20+
21+
import org.junit.jupiter.api.Tag;
22+
23+
import java.lang.annotation.ElementType;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.RetentionPolicy;
26+
import java.lang.annotation.Target;
27+
28+
/**
29+
* Marks formatting tests which are typically run in different configurations set by system properties.
30+
* See {@link org.apache.xml.security.utils.XMLUtils} for formatting options.
31+
* Please use {@link FormattingCheckerFactory} to get an appropriate checker for the test.
32+
*/
33+
@Target({ElementType.TYPE, ElementType.METHOD})
34+
@Retention(RetentionPolicy.RUNTIME)
35+
@Tag("formattingTest")
36+
public @interface FormattingTest {
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.xml.security.formatting;
20+
21+
import static org.hamcrest.MatcherAssert.assertThat;
22+
import static org.hamcrest.Matchers.containsString;
23+
import static org.hamcrest.Matchers.matchesPattern;
24+
25+
/**
26+
* Checks the document is 'pretty-printed', but base64 values are without line breaks.
27+
*/
28+
public class NoBase64LineBreaksChecker implements FormattingChecker {
29+
@Override
30+
public void checkDocument(String document) {
31+
assertThat(document, containsString("\n"));
32+
}
33+
34+
@Override
35+
public void checkBase64Value(String value) {
36+
assertThat(value, matchesPattern(BASE64_PATTERN));
37+
}
38+
39+
@Override
40+
public void checkBase64ValueWithSpacing(String value) {
41+
assertThat(value, matchesPattern(BASE64_PATTERN));
42+
}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.xml.security.formatting;
20+
21+
import static org.hamcrest.MatcherAssert.assertThat;
22+
import static org.hamcrest.Matchers.containsString;
23+
import static org.hamcrest.Matchers.not;
24+
import static org.hamcrest.Matchers.matchesPattern;
25+
26+
/**
27+
* Checks there are no line breaks in the document.
28+
*/
29+
public class NoLineBreaksChecker implements FormattingChecker {
30+
@Override
31+
public void checkDocument(String document) {
32+
assertThat(document, not(containsString("\n")));
33+
assertThat(document, not(containsString("\r")));
34+
}
35+
36+
@Override
37+
public void checkBase64Value(String value) {
38+
assertThat(value, matchesPattern(BASE64_PATTERN));
39+
}
40+
41+
@Override
42+
public void checkBase64ValueWithSpacing(String value) {
43+
assertThat(value, matchesPattern(BASE64_PATTERN));
44+
}
45+
}

0 commit comments

Comments
 (0)