Sonar fixes

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885219 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2021-01-07 00:27:13 +00:00
parent 2125c37d31
commit dde88b1102
3 changed files with 78 additions and 109 deletions

View File

@ -16,21 +16,24 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.ooxml; package org.apache.poi.ooxml;
import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleFileStream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.DocumentFactoryHelper; import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
import org.apache.poi.poifs.filesystem.FileMagic; import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
/** /**
* Class to test that HXF correctly detects OOXML * Class to test that HXF correctly detects OOXML
@ -40,27 +43,23 @@ import org.junit.jupiter.api.Test;
public class TestDetectAsOOXML { public class TestDetectAsOOXML {
@Test @Test
public void testOpensProperly() throws IOException, InvalidFormatException { public void testOpensProperly() throws IOException, InvalidFormatException {
OPCPackage.open(HSSFTestDataSamples.openSampleFileStream("sample.xlsx")).close(); try (InputStream is = openSampleFileStream("sample.xlsx");
OPCPackage pkg = OPCPackage.open(is)) {
assertNotNull(pkg);
}
} }
@Test @ParameterizedTest
public void testDetectAsPOIFS() throws IOException { @CsvSource({"SampleSS.xlsx, OOXML", "SampleSS.xls, OLE2", "SampleSS.txt, UNKNOWN"})
Object[][] fileAndMagic = { public void testDetectAsPOIFS(String file, FileMagic fm) throws IOException {
{"SampleSS.xlsx", FileMagic.OOXML}, try (InputStream is = FileMagic.prepareToCheckMagic(openSampleFileStream(file))) {
{"SampleSS.xls", FileMagic.OLE2}, FileMagic act = FileMagic.valueOf(is);
{"SampleSS.txt", FileMagic.UNKNOWN}
};
for (Object[] fm : fileAndMagic) { assertEquals(act == FileMagic.OOXML, DocumentFactoryHelper.hasOOXMLHeader(is),
try (InputStream is = FileMagic.prepareToCheckMagic(HSSFTestDataSamples.openSampleFileStream((String)fm[0]))) { "OOXML files should be detected, others not");
FileMagic act = FileMagic.valueOf(is);
assertEquals(act == FileMagic.OOXML, DocumentFactoryHelper.hasOOXMLHeader(is), assertEquals(fm, act, "file magic failed for " + file);
"OOXML files should be detected, others not"); }
assertEquals(fm[1], act, "file magic failed for " + fm[0]);
}
}
} }
@Test @Test

View File

@ -17,6 +17,7 @@
package org.apache.poi.openxml4j.opc; package org.apache.poi.openxml4j.opc;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
@ -31,6 +32,8 @@ import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.internal.ContentType; import org.apache.poi.openxml4j.opc.internal.ContentType;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
/** /**
* Tests for content type (ContentType class). * Tests for content type (ContentType class).
@ -46,14 +49,10 @@ public final class TestContentType {
* package parts shall fit the definition and syntax for media types as * package parts shall fit the definition and syntax for media types as
* specified in RFC 2616, \u00A73.7. * specified in RFC 2616, \u00A73.7.
*/ */
@Test @ParameterizedTest
public void testContentTypeValidation() throws InvalidFormatException { @ValueSource(strings = {"text/xml", "application/pgp-key", "application/vnd.hp-PCLXL", "application/vnd.lotus-1-2-3"})
String[] contentTypesToTest = new String[]{"text/xml", public void testContentTypeValidation(String contentType) throws InvalidFormatException {
"application/pgp-key", "application/vnd.hp-PCLXL", assertDoesNotThrow(() -> new ContentType(contentType));
"application/vnd.lotus-1-2-3"};
for (String contentType : contentTypesToTest) {
new ContentType(contentType);
}
} }
/** /**
@ -71,19 +70,17 @@ public final class TestContentType {
* designers shall specify only such content types for inclusion in the * designers shall specify only such content types for inclusion in the
* format. * format.
*/ */
@Test @ParameterizedTest
public void testContentTypeValidationFailure() { @ValueSource(strings = {"text/xml/app", "",
String[] contentTypesToTest = new String[]{"text/xml/app", "", "test", "text(xml/xml", "text)xml/xml", "text<xml/xml",
"test", "text(xml/xml", "text)xml/xml", "text<xml/xml", "text>/xml", "text@/xml", "text,/xml", "text;/xml",
"text>/xml", "text@/xml", "text,/xml", "text;/xml", "text:/xml", "text\\/xml", "t/ext/xml", "t\"ext/xml",
"text:/xml", "text\\/xml", "t/ext/xml", "t\"ext/xml", "text[/xml", "text]/xml", "text?/xml", "tex=t/xml",
"text[/xml", "text]/xml", "text?/xml", "tex=t/xml", "te{xt/xml", "tex}t/xml", "te xt/xml",
"te{xt/xml", "tex}t/xml", "te xt/xml", "text\u0009/xml", "text xml", " text/xml "})
"text" + (char) 9 + "/xml", "text xml", " text/xml "}; public void testContentTypeValidationFailure(String contentType) {
for (String contentType : contentTypesToTest) { assertThrows(InvalidFormatException.class, () -> new ContentType(contentType),
assertThrows(InvalidFormatException.class, () -> new ContentType(contentType), "Must have fail for content type: '" + contentType + "' !");
"Must have fail for content type: '" + contentType + "' !");
}
} }
/** /**
@ -92,33 +89,28 @@ public final class TestContentType {
* Invalid parameters are verified as incorrect in * Invalid parameters are verified as incorrect in
* {@link #testContentTypeParameterFailure()} * {@link #testContentTypeParameterFailure()}
*/ */
@Test @ParameterizedTest
public void testContentTypeParam() throws InvalidFormatException { @ValueSource(strings = {"mail/toto;titi=tata",
String[] contentTypesToTest = new String[]{"mail/toto;titi=tata", "text/xml;a=b;c=d", "text/xml;key1=param1;key2=param2",
"text/xml;a=b;c=d", "text/xml;key1=param1;key2=param2", "application/pgp-key;version=\"2\"",
"application/pgp-key;version=\"2\"", "application/x-resqml+xml;version=2.0;type=obj_global2dCrs"})
"application/x-resqml+xml;version=2.0;type=obj_global2dCrs" public void testContentTypeParam(String contentType) {
}; assertDoesNotThrow(() -> new ContentType(contentType));
for (String contentType : contentTypesToTest) {
new ContentType(contentType);
}
} }
/** /**
* Check rule [O1.2]: Format designers might restrict the usage of * Check rule [O1.2]: Format designers might restrict the usage of
* parameters for content types. * parameters for content types.
*/ */
@Test @ParameterizedTest
public void testContentTypeParameterFailure() { @ValueSource(strings = {
String[] contentTypesToTest = new String[]{ "mail/toto;\"titi=tata\"", // quotes not allowed like that
"mail/toto;\"titi=tata\"", // quotes not allowed like that "mail/toto;titi = tata", // spaces not allowed
"mail/toto;titi = tata", // spaces not allowed "text/\u0080" // characters above ASCII are not allowed
"text/\u0080" // characters above ASCII are not allowed })
}; public void testContentTypeParameterFailure(String contentType) {
for (String contentType : contentTypesToTest) { assertThrows(InvalidFormatException.class, () -> new ContentType(contentType),
assertThrows(InvalidFormatException.class, () -> new ContentType(contentType), "Must have fail for content type: '" + contentType + "' !");
"Must have fail for content type: '" + contentType + "' !");
}
} }
/** /**
@ -126,13 +118,11 @@ public final class TestContentType {
* that does not include comments and the format designer shall specify such * that does not include comments and the format designer shall specify such
* a content type. * a content type.
*/ */
@Test @ParameterizedTest
public void testContentTypeCommentFailure() { @ValueSource(strings = {"text/xml(comment)"})
String[] contentTypesToTest = new String[]{"text/xml(comment)"}; public void testContentTypeCommentFailure(String contentType) {
for (String contentType : contentTypesToTest) { assertThrows(InvalidFormatException.class, () -> new ContentType(contentType),
assertThrows(InvalidFormatException.class, () -> new ContentType(contentType), "Must have fail for content type: '" + contentType + "' !");
"Must have fail for content type: '" + contentType + "' !");
}
} }
/** /**

View File

@ -61,7 +61,6 @@ import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.poi.EncryptedDocumentException; import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.POITestCase; import org.apache.poi.POITestCase;
import org.apache.poi.UnsupportedFileFormatException;
import org.apache.poi.extractor.ExtractorFactory; import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.extractor.POITextExtractor; import org.apache.poi.extractor.POITextExtractor;
import org.apache.poi.ooxml.POIXMLException; import org.apache.poi.ooxml.POIXMLException;
@ -69,8 +68,6 @@ import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.InvalidOperationException; import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException;
import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException;
import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
import org.apache.poi.openxml4j.opc.internal.ContentTypeManager; import org.apache.poi.openxml4j.opc.internal.ContentTypeManager;
import org.apache.poi.openxml4j.opc.internal.FileHelper; import org.apache.poi.openxml4j.opc.internal.FileHelper;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
@ -92,6 +89,8 @@ import org.apache.poi.xwpf.usermodel.XWPFRelation;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
@ -631,47 +630,29 @@ public final class TestPackage {
} }
} }
@Test @SuppressWarnings("unchecked")
public void NonOOXML_File() throws Exception { @ParameterizedTest
handleNonOOXML( @CsvSource({
"SampleSS.xls", OLE2NotOfficeXmlFileException.class, "SampleSS.xls, org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException, The supplied data appears to be in the OLE2 Format, You are calling the part of POI that deals with OOXML",
"The supplied data appears to be in the OLE2 Format", "SampleSS.xml, org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException, The supplied data appears to be a raw XML file, Formats such as Office 2003 XML",
"You are calling the part of POI that deals with OOXML" "SampleSS.ods, org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException, The supplied data appears to be in ODF, Formats like these (eg ODS",
); "SampleSS.txt, org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException, No valid entries or contents found, not a valid OOXML"
})
public void NonOOXML_File(String file, String exClazzStr, String msg1, String msg2) throws Exception {
Class<? extends Exception> exClazz = (Class<? extends Exception>)Class.forName(exClazzStr);
handleNonOOXML(
"SampleSS.xml", NotOfficeXmlFileException.class,
"The supplied data appears to be a raw XML file",
"Formats such as Office 2003 XML"
);
handleNonOOXML(
"SampleSS.ods", ODFNotOfficeXmlFileException.class,
"The supplied data appears to be in ODF",
"Formats like these (eg ODS"
);
handleNonOOXML(
"SampleSS.txt", NotOfficeXmlFileException.class,
"No valid entries or contents found",
"not a valid OOXML"
);
}
private void handleNonOOXML(String file, Class<? extends UnsupportedFileFormatException> exception, String... messageParts) throws IOException {
try (InputStream stream = xlsSamples.openResourceAsStream(file)) { try (InputStream stream = xlsSamples.openResourceAsStream(file)) {
Executable[] trs = { Executable[] trs = {
() -> OPCPackage.open(stream), () -> OPCPackage.open(stream),
() -> OPCPackage.open(xlsSamples.getFile(file)) () -> OPCPackage.open(xlsSamples.getFile(file))
}; };
for (Executable tr : trs) { for (Executable tr : trs) {
Exception ex = assertThrows(exception, tr, "Shouldn't be able to open "+file); Exception ex = assertThrows(exClazz, tr, "Shouldn't be able to open "+file);
Stream.of(messageParts).forEach(mp -> assertTrue(ex.getMessage().contains(mp))); Stream.of(msg1, msg2).forEach(mp -> assertTrue(ex.getMessage().contains(mp)));
} }
} }
} }
/** /**
* Zip bomb handling test * Zip bomb handling test
* *
@ -1009,8 +990,8 @@ public final class TestPackage {
//make absolutely certain that sequential calls don't throw InvalidFormatExceptions //make absolutely certain that sequential calls don't throw InvalidFormatExceptions
String originalFile = getSampleFileName("TestPackageCommon.docx"); String originalFile = getSampleFileName("TestPackageCommon.docx");
try (OPCPackage p2 = OPCPackage.open(originalFile, PackageAccess.READ)) { try (OPCPackage p2 = OPCPackage.open(originalFile, PackageAccess.READ)) {
p2.getParts(); assertDoesNotThrow(p2::getParts);
p2.getParts(); assertDoesNotThrow(p2::getParts);
} }
} }
@ -1026,7 +1007,7 @@ public final class TestPackage {
@Override @Override
public void close() { public void close() {
throw new IllegalStateException("close should not be called here"); fail("close should not be called here");
} }
}; };
@ -1074,7 +1055,6 @@ public final class TestPackage {
int numPartsBefore = 0; int numPartsBefore = 0;
String md5Before = Files.asByteSource(tmpFile).hash(Hashing.sha256()).toString(); String md5Before = Files.asByteSource(tmpFile).hash(Hashing.sha256()).toString();
RuntimeException ex = null;
try(OPCPackage pkg = OPCPackage.open(tmpFile, PackageAccess.READ_WRITE)) try(OPCPackage pkg = OPCPackage.open(tmpFile, PackageAccess.READ_WRITE))
{ {
numPartsBefore = pkg.getParts().size(); numPartsBefore = pkg.getParts().size();
@ -1083,13 +1063,13 @@ public final class TestPackage {
pkg.addMarshaller("poi/junit", (part, out) -> { pkg.addMarshaller("poi/junit", (part, out) -> {
throw new RuntimeException("Bugzilla 63029"); throw new RuntimeException("Bugzilla 63029");
}); });
pkg.createPart(createPartName("/poi/test.xml"), "poi/junit"); pkg.createPart(createPartName("/poi/test.xml"), "poi/junit");
} catch (RuntimeException e){
ex = e; RuntimeException ex = assertThrows(RuntimeException.class, pkg::close);
// verify there was an exception while closing the file
assertEquals("Fail to save: an error occurs while saving the package : Bugzilla 63029", ex.getMessage());
} }
// verify there was an exception while closing the file
assertNotNull(ex, "Fail to save: an error occurs while saving the package : Bugzilla 63029");
assertEquals("Fail to save: an error occurs while saving the package : Bugzilla 63029", ex.getMessage());
// assert that md5 after closing is the same, i.e. the source is left intact // assert that md5 after closing is the same, i.e. the source is left intact
String md5After = Files.asByteSource(tmpFile).hash(Hashing.sha256()).toString(); String md5After = Files.asByteSource(tmpFile).hash(Hashing.sha256()).toString();