From dfca03ec0313b57ee359456b1b06540962682012 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Wed, 24 Jun 2015 08:53:25 +0000 Subject: [PATCH] Fix test to find the exception-text in all cases git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1687212 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/openxml4j/opc/TestPackage.java | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java index 2383455fce..2f857933e7 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java @@ -721,15 +721,9 @@ public final class TestPackage { // depending if this executed via "ant test" or within eclipse // maybe a difference in JDK ... } catch (InvalidFormatException e) { - if(!e.getMessage().equals("Zip bomb detected! Exiting.")) { - throw new IllegalStateException(e); - } + checkForZipBombException(e); } catch (POIXMLException e) { - InvocationTargetException t = (InvocationTargetException)e.getCause(); - IOException t2 = (IOException)t.getTargetException(); - if(!t2.getMessage().equals("Zip bomb detected! Exiting.")) { - throw new IllegalStateException(e); - } + checkForZipBombException(e); } // check max entry size ouf of bounds @@ -739,15 +733,9 @@ public final class TestPackage { wb = WorkbookFactory.create(file, null, true); wb.close(); } catch (InvalidFormatException e) { - if(!e.getMessage().equals("Zip bomb detected! Exiting.")) { - throw new IllegalStateException(e); - } + checkForZipBombException(e); } catch (POIXMLException e) { - InvocationTargetException t = (InvocationTargetException)e.getCause(); - IOException t2 = (IOException)t.getTargetException(); - if(!t2.getMessage().equals("Zip bomb detected! Exiting.")) { - throw new IllegalStateException(e); - } + checkForZipBombException(e); } } finally { // reset otherwise a lot of ooxml tests will fail @@ -755,4 +743,26 @@ public final class TestPackage { ZipSecureFile.setMaxEntrySize(0xFFFFFFFFl); } } + + private void checkForZipBombException(Throwable e) { + if(e instanceof InvocationTargetException) { + InvocationTargetException t = (InvocationTargetException)e; + IOException t2 = (IOException)t.getTargetException(); + if("Zip bomb detected! Exiting.".equals(t2.getMessage())) { + return; + } + } + + if ("Zip bomb detected! Exiting.".equals(e.getMessage())) { + return; + } + + // recursively check the causes for the message as it can be nested further down in the exception-tree + if(e.getCause() != null && e.getCause() != e) { + checkForZipBombException(e.getCause()); + return; + } + + throw new IllegalStateException("Expected to catch an Exception because of a detected Zip Bomb, but did not find the related error message in the exception", e); + } } -- 2.39.5