22
33import eu .europa .ec .itb .xml .XmlSchemaVersion ;
44import org .junit .jupiter .api .Test ;
5+ import org .junit .jupiter .api .io .TempDir ;
56import org .xml .sax .ErrorHandler ;
67import org .xml .sax .SAXException ;
78import org .xml .sax .SAXParseException ;
89
10+ import java .nio .file .Files ;
11+ import java .nio .file .Path ;
12+ import java .util .Objects ;
13+ import java .util .UUID ;
14+
915import static eu .europa .ec .itb .xml .util .Utils .secureSchemaValidation ;
1016import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
1117import static org .junit .jupiter .api .Assertions .assertThrows ;
1218import static org .mockito .Mockito .*;
1319
1420class UtilsTest {
1521
22+ @ TempDir
23+ Path tempDirectory ;
24+
1625 @ Test
1726 void testSchemaValidationValid () {
1827 assertDoesNotThrow (() -> {
1928 try (
2029 var inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/testFiles/valid.xml" );
2130 var schemaStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/PurchaseOrder.xsd" )
2231 ) {
23- secureSchemaValidation (inputStream , schemaStream , null , null , null , XmlSchemaVersion .VERSION_1_0 );
32+ Path schemaPath = tempDirectory .resolve (UUID .randomUUID () +".xsd" );
33+ Files .copy (Objects .requireNonNull (schemaStream ), schemaPath );
34+ secureSchemaValidation (inputStream , schemaPath , null , null , null , XmlSchemaVersion .VERSION_1_0 );
2435 }
2536 });
2637 }
@@ -33,7 +44,9 @@ void testSchemaValidationInvalidXSD() throws SAXException {
3344 var inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/testFiles/invalid_xsd.xml" );
3445 var schemaStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/PurchaseOrder.xsd" )
3546 ) {
36- secureSchemaValidation (inputStream , schemaStream , null , null , null , XmlSchemaVersion .VERSION_1_0 );
47+ Path schemaPath = tempDirectory .resolve (UUID .randomUUID () +".xsd" );
48+ Files .copy (Objects .requireNonNull (schemaStream ), schemaPath );
49+ secureSchemaValidation (inputStream , schemaPath , null , null , null , XmlSchemaVersion .VERSION_1_0 );
3750 }
3851 });
3952 // With error handler.
@@ -43,7 +56,9 @@ void testSchemaValidationInvalidXSD() throws SAXException {
4356 var inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/testFiles/invalid_xsd.xml" );
4457 var schemaStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/PurchaseOrder.xsd" )
4558 ) {
46- secureSchemaValidation (inputStream , schemaStream , errorHandler , null , null , XmlSchemaVersion .VERSION_1_0 );
59+ Path schemaPath = tempDirectory .resolve (UUID .randomUUID () +".xsd" );
60+ Files .copy (Objects .requireNonNull (schemaStream ), schemaPath );
61+ secureSchemaValidation (inputStream , schemaPath , errorHandler , null , null , XmlSchemaVersion .VERSION_1_0 );
4762 }
4863 });
4964 verify (errorHandler , atLeastOnce ()).error (any (SAXParseException .class ));
@@ -57,7 +72,9 @@ void testSchemaValidationInvalidXML() throws SAXException {
5772 var inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/testFiles/invalid_xml.xml" );
5873 var schemaStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/PurchaseOrder.xsd" )
5974 ) {
60- secureSchemaValidation (inputStream , schemaStream , null , null , null , XmlSchemaVersion .VERSION_1_0 );
75+ Path schemaPath = tempDirectory .resolve (UUID .randomUUID () +".xsd" );
76+ Files .copy (Objects .requireNonNull (schemaStream ), schemaPath );
77+ secureSchemaValidation (inputStream , schemaPath , null , null , null , XmlSchemaVersion .VERSION_1_0 );
6178 }
6279 });
6380 // With error handler.
@@ -67,7 +84,9 @@ void testSchemaValidationInvalidXML() throws SAXException {
6784 var inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/testFiles/invalid_xml.txt" );
6885 var schemaStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/PurchaseOrder.xsd" )
6986 ) {
70- secureSchemaValidation (inputStream , schemaStream , errorHandler , null , null , XmlSchemaVersion .VERSION_1_0 );
87+ Path schemaPath = tempDirectory .resolve (UUID .randomUUID () +".xsd" );
88+ Files .copy (Objects .requireNonNull (schemaStream ), schemaPath );
89+ secureSchemaValidation (inputStream , schemaPath , errorHandler , null , null , XmlSchemaVersion .VERSION_1_0 );
7190 }
7291 });
7392 verify (errorHandler , atLeastOnce ()).error (any (SAXParseException .class ));
@@ -81,7 +100,9 @@ void testSchemaValidationMissingXML() throws SAXException {
81100 var inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/testFiles/missing.xml" );
82101 var schemaStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/PurchaseOrder.xsd" )
83102 ) {
84- secureSchemaValidation (inputStream , schemaStream , null , null , null , XmlSchemaVersion .VERSION_1_0 );
103+ Path schemaPath = tempDirectory .resolve (UUID .randomUUID () +".xsd" );
104+ Files .copy (Objects .requireNonNull (schemaStream ), schemaPath );
105+ secureSchemaValidation (inputStream , schemaPath , null , null , null , XmlSchemaVersion .VERSION_1_0 );
85106 }
86107 });
87108 // With error handler.
@@ -91,7 +112,9 @@ void testSchemaValidationMissingXML() throws SAXException {
91112 var inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/testFiles/missing.txt" );
92113 var schemaStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/PurchaseOrder.xsd" )
93114 ) {
94- secureSchemaValidation (inputStream , schemaStream , errorHandler , null , null , XmlSchemaVersion .VERSION_1_0 );
115+ Path schemaPath = tempDirectory .resolve (UUID .randomUUID () +".xsd" );
116+ Files .copy (Objects .requireNonNull (schemaStream ), schemaPath );
117+ secureSchemaValidation (inputStream , schemaPath , errorHandler , null , null , XmlSchemaVersion .VERSION_1_0 );
95118 }
96119 });
97120 verify (errorHandler , never ()).error (any ());
@@ -105,7 +128,9 @@ void testSchemaValidationInvalidXXE() throws SAXException {
105128 var inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/testFiles/invalid_xxe.xml" );
106129 var schemaStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/PurchaseOrder.xsd" )
107130 ) {
108- secureSchemaValidation (inputStream , schemaStream , null , null , null , XmlSchemaVersion .VERSION_1_0 );
131+ Path schemaPath = tempDirectory .resolve (UUID .randomUUID () +".xsd" );
132+ Files .copy (Objects .requireNonNull (schemaStream ), schemaPath );
133+ secureSchemaValidation (inputStream , schemaPath , null , null , null , XmlSchemaVersion .VERSION_1_0 );
109134 }
110135 });
111136 // With error handler.
@@ -115,10 +140,25 @@ void testSchemaValidationInvalidXXE() throws SAXException {
115140 var inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/testFiles/invalid_xxe.xml" );
116141 var schemaStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/PurchaseOrder.xsd" )
117142 ) {
118- secureSchemaValidation (inputStream , schemaStream , errorHandler , null , null , XmlSchemaVersion .VERSION_1_0 );
143+ Path schemaPath = tempDirectory .resolve (UUID .randomUUID () +".xsd" );
144+ Files .copy (Objects .requireNonNull (schemaStream ), schemaPath );
145+ secureSchemaValidation (inputStream , schemaPath , errorHandler , null , null , XmlSchemaVersion .VERSION_1_0 );
119146 }
120147 });
121148 verify (errorHandler , never ()).error (any ());
122149 }
123150
151+ @ Test
152+ void testSchemaValidationWithVersionDetection () {
153+ assertDoesNotThrow (() -> {
154+ try (
155+ var inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/testFiles/valid.xml" );
156+ var schemaStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("utils/PurchaseOrder.xsd" )
157+ ) {
158+ Path schemaPath = tempDirectory .resolve (UUID .randomUUID () +".xsd" );
159+ Files .copy (Objects .requireNonNull (schemaStream ), schemaPath );
160+ secureSchemaValidation (inputStream , schemaPath , null , null , null , null );
161+ }
162+ });
163+ }
124164}
0 commit comments