Skip to content

Commit

Permalink
odfdom: add some zip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mistmist authored and svanteschubert committed Sep 18, 2024
1 parent 4d66887 commit 6e985d3
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ private static void addFilesFromFolder(final File folder, Collection<Object[]> t
&& !filePath.contains("textTestTemplate.ott")
&& !filePath.contains("indentTest.odt")
&& !filePath.contains("testInvalidPkg")
&& !filePath.contains("duplicate-files.odt")
&& !filePath.contains("unicode-path.odt")
&& !filePath.contains("slash.odt")
&& !filePath.contains("two-zips.odt")
&& !filePath.contains("BigTable.odt") // too slow 4 now
&& !filePath.endsWith("PasswordProtected.odt")) {
// !filePath.contains("Text1.odt")
Expand Down
106 changes: 106 additions & 0 deletions odfdom/src/test/java/org/odftoolkit/odfdom/pkg/PackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,112 @@ public void validationTest() {
handler4.validate();
}

@Test
public void validationTest5() {
// TESTDOC5: duplicate ZIP entries
Map<ValidationConstraint, Integer> expectedFatalErrors =
new HashMap<ValidationConstraint, Integer>();
expectedFatalErrors.put(OdfPackageConstraint.PACKAGE_ENTRY_DUPLICATE, 1);
ErrorHandlerStub handler = new ErrorHandlerStub(null, null, expectedFatalErrors);
handler.setTestFilePath("duplicate-files.odt");

try {
try {
OdfPackage.loadPackage(
new File(ResourceUtilities.getAbsoluteInputPath(handler.getTestFilePath())),
null,
handler);
Assert.fail();
} catch (Exception e) {
String errorMsg = OdfPackageConstraint.PACKAGE_ENTRY_DUPLICATE.getMessage();
if (e.getMessage().indexOf(errorMsg.substring(0, errorMsg.indexOf("%2$s"))) == -1) {
Assert.fail();
}
}
} catch (Exception ex) {
LOG.log(Level.SEVERE, null, ex);
Assert.fail(ex.toString());
}
handler.validate();
}

@Test
public void validationTest6() {
// TESTDOC6: Info-ZIP Unicode Path Extra Field with different name
Map<ValidationConstraint, Integer> expectedErrors =
new HashMap<ValidationConstraint, Integer>();
// depending on setUseUnicodeExtraFields this would be reported as
// MANIFEST_DOES_NOT_LIST_FILE or as PACKAGE_ENTRY_DUPLICATE
expectedErrors.put(OdfPackageConstraint.MANIFEST_DOES_NOT_LIST_FILE, 1);
ErrorHandlerStub handler = new ErrorHandlerStub(null, expectedErrors, null);
handler.setTestFilePath("unicode-path.odt");

try {
OdfPackage pkg =
OdfPackage.loadPackage(
new File(ResourceUtilities.getAbsoluteInputPath(handler.getTestFilePath())),
null,
handler);
Assert.assertNotNull(pkg);
} catch (Exception ex) {
LOG.log(Level.SEVERE, null, ex);
Assert.fail(ex.toString());
}
handler.validate();
}

@Test
public void validationTest7() {
// TESTDOC7: invalid file name
Map<ValidationConstraint, Integer> expectedFatalErrors =
new HashMap<ValidationConstraint, Integer>();
expectedFatalErrors.put(OdfPackageConstraint.PACKAGE_ENTRY_INVALID_FILE_NAME, 1);
ErrorHandlerStub handler = new ErrorHandlerStub(null, null, expectedFatalErrors);
handler.setTestFilePath("slash.odt");

try {
try {
OdfPackage.loadPackage(
new File(ResourceUtilities.getAbsoluteInputPath(handler.getTestFilePath())),
null,
handler);
Assert.fail();
} catch (Exception e) {
String errorMsg = OdfPackageConstraint.PACKAGE_ENTRY_INVALID_FILE_NAME.getMessage();
if (e.getMessage().indexOf(errorMsg.substring(0, errorMsg.indexOf("%2$s"))) == -1) {
Assert.fail();
}
}
} catch (Exception ex) {
LOG.log(Level.SEVERE, null, ex);
Assert.fail(ex.toString());
}
handler.validate();
}

@Test
public void validationTest8() {
// TESTDOC8: 2 concatenated zips
Map<ValidationConstraint, Integer> expectedErrors =
new HashMap<ValidationConstraint, Integer>();
expectedErrors.put(OdfPackageConstraint.MIMETYPE_NOT_FIRST_IN_PACKAGE, 1);
ErrorHandlerStub handler = new ErrorHandlerStub(null, expectedErrors, null);
handler.setTestFilePath("two-zips.odt");

try {
OdfPackage pkg =
OdfPackage.loadPackage(
new File(ResourceUtilities.getAbsoluteInputPath(handler.getTestFilePath())),
null,
handler);
Assert.assertNotNull(pkg);
} catch (Exception ex) {
LOG.log(Level.SEVERE, null, ex);
Assert.fail(ex.toString());
}
handler.validate();
}

@Test
public void testPackagePassword() {
File tmpFile = ResourceUtilities.getTestOutputFile("PackagePassword.ods");
Expand Down
Binary file not shown.
Binary file added odfdom/src/test/resources/test-input/slash.odt
Binary file not shown.
Binary file added odfdom/src/test/resources/test-input/two-zips.odt
Binary file not shown.
Binary file not shown.

0 comments on commit 6e985d3

Please sign in to comment.