You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestZipPackage.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.openxml4j.opc;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertTrue;
  20. import static org.junit.Assert.fail;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;
  27. import java.io.OutputStreamWriter;
  28. import java.io.PrintWriter;
  29. import java.io.UnsupportedEncodingException;
  30. import org.apache.poi.POIDataSamples;
  31. import org.apache.poi.POITextExtractor;
  32. import org.apache.poi.POIXMLException;
  33. import org.apache.poi.extractor.ExtractorFactory;
  34. import org.apache.poi.hssf.HSSFTestDataSamples;
  35. import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
  36. import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException;
  37. import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException;
  38. import org.apache.poi.sl.usermodel.SlideShow;
  39. import org.apache.poi.sl.usermodel.SlideShowFactory;
  40. import org.apache.poi.ss.usermodel.Workbook;
  41. import org.apache.poi.ss.usermodel.WorkbookFactory;
  42. import org.apache.poi.xssf.XSSFTestDataSamples;
  43. import org.apache.poi.xwpf.usermodel.XWPFRelation;
  44. import org.apache.xmlbeans.XmlException;
  45. import org.junit.Test;
  46. public class TestZipPackage {
  47. @Test
  48. public void testBug56479() throws Exception {
  49. InputStream is = OpenXML4JTestDataSamples.openSampleStream("dcterms_bug_56479.zip");
  50. OPCPackage p = OPCPackage.open(is);
  51. // Check we found the contents of it
  52. boolean foundCoreProps = false, foundDocument = false, foundTheme1 = false;
  53. for (PackagePart part : p.getParts()) {
  54. if (part.getPartName().toString().equals("/docProps/core.xml")) {
  55. assertEquals(ContentTypes.CORE_PROPERTIES_PART, part.getContentType());
  56. foundCoreProps = true;
  57. }
  58. if (part.getPartName().toString().equals("/word/document.xml")) {
  59. assertEquals(XWPFRelation.DOCUMENT.getContentType(), part.getContentType());
  60. foundDocument = true;
  61. }
  62. if (part.getPartName().toString().equals("/word/theme/theme1.xml")) {
  63. assertEquals(XWPFRelation.THEME.getContentType(), part.getContentType());
  64. foundTheme1 = true;
  65. }
  66. }
  67. assertTrue("Core not found in " + p.getParts(), foundCoreProps);
  68. assertFalse("Document should not be found in " + p.getParts(), foundDocument);
  69. assertFalse("Theme1 should not found in " + p.getParts(), foundTheme1);
  70. p.close();
  71. is.close();
  72. }
  73. @Test
  74. public void testZipEntityExpansionTerminates() throws IOException {
  75. try {
  76. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("poc-xmlbomb.xlsx");
  77. wb.close();
  78. fail("Should catch exception due to entity expansion limitations");
  79. } catch (POIXMLException e) {
  80. assertEntityLimitReached(e);
  81. }
  82. }
  83. private void assertEntityLimitReached(Exception e) throws UnsupportedEncodingException {
  84. ByteArrayOutputStream str = new ByteArrayOutputStream();
  85. PrintWriter writer = new PrintWriter(new OutputStreamWriter(str, "UTF-8"));
  86. try {
  87. e.printStackTrace(writer);
  88. } finally {
  89. writer.close();
  90. }
  91. String string = new String(str.toByteArray(), "UTF-8");
  92. assertTrue("Had: " + string, string.contains("The parser has encountered more than"));
  93. }
  94. @Test
  95. public void testZipEntityExpansionExceedsMemory() throws Exception {
  96. try {
  97. Workbook wb = WorkbookFactory.create(XSSFTestDataSamples.openSamplePackage("poc-xmlbomb.xlsx"));
  98. wb.close();
  99. fail("Should catch exception due to entity expansion limitations");
  100. } catch (POIXMLException e) {
  101. assertEntityLimitReached(e);
  102. }
  103. try {
  104. POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-xmlbomb.xlsx"));
  105. try {
  106. assertNotNull(extractor);
  107. try {
  108. extractor.getText();
  109. } catch (IllegalStateException e) {
  110. // expected due to shared strings expansion
  111. }
  112. } finally {
  113. extractor.close();
  114. }
  115. } catch (POIXMLException e) {
  116. assertEntityLimitReached(e);
  117. }
  118. }
  119. @Test
  120. public void testZipEntityExpansionSharedStringTable() throws Exception {
  121. Workbook wb = WorkbookFactory.create(XSSFTestDataSamples.openSamplePackage("poc-shared-strings.xlsx"));
  122. wb.close();
  123. POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"));
  124. try {
  125. assertNotNull(extractor);
  126. try {
  127. extractor.getText();
  128. } catch (IllegalStateException e) {
  129. // expected due to shared strings expansion
  130. }
  131. } finally {
  132. extractor.close();
  133. }
  134. }
  135. @Test
  136. public void testZipEntityExpansionSharedStringTableEvents() throws Exception {
  137. boolean before = ExtractorFactory.getThreadPrefersEventExtractors();
  138. ExtractorFactory.setThreadPrefersEventExtractors(true);
  139. try {
  140. POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"));
  141. try {
  142. assertNotNull(extractor);
  143. try {
  144. extractor.getText();
  145. } catch (IllegalStateException e) {
  146. // expected due to shared strings expansion
  147. }
  148. } finally {
  149. extractor.close();
  150. }
  151. } catch (XmlException e) {
  152. assertEntityLimitReached(e);
  153. } finally {
  154. ExtractorFactory.setThreadPrefersEventExtractors(before);
  155. }
  156. }
  157. @Test
  158. public void unparseableCentralDirectory() throws IOException {
  159. File f = OpenXML4JTestDataSamples.getSampleFile("at.pzp.www_uploads_media_PP_Scheinecker-jdk6error.pptx");
  160. SlideShow<?,?> ppt = SlideShowFactory.create(f, null, true);
  161. ppt.close();
  162. }
  163. @Test
  164. public void testClosingStreamOnException() throws IOException {
  165. InputStream is = OpenXML4JTestDataSamples.openSampleStream("dcterms_bug_56479.zip");
  166. File tmp = File.createTempFile("poi-test-truncated-zip", "");
  167. // create a corrupted zip file by truncating a valid zip file to the first 100 bytes
  168. OutputStream os = new FileOutputStream(tmp);
  169. for (int i = 0; i < 100; i++) {
  170. os.write(is.read());
  171. }
  172. os.flush();
  173. os.close();
  174. is.close();
  175. // feed the corrupted zip file to OPCPackage
  176. try {
  177. OPCPackage.open(tmp, PackageAccess.READ);
  178. } catch (Exception e) {
  179. // expected: the zip file is invalid
  180. // this test does not care if open() throws an exception or not.
  181. }
  182. // If the stream is not closed on exception, it will keep a file descriptor to tmp,
  183. // and requests to the OS to delete the file will fail.
  184. assertTrue("Can't delete tmp file", tmp.delete());
  185. }
  186. /**
  187. * If ZipPackage is passed an invalid file, a call to close
  188. * (eg from the OPCPackage open method) should tidy up the
  189. * stream / file the broken file is being read from.
  190. * See bug #60128 for more
  191. */
  192. @Test
  193. public void testTidyStreamOnInvalidFile() throws Exception {
  194. // Spreadsheet has a good mix of alternate file types
  195. POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
  196. File[] notValidF = new File[] {
  197. files.getFile("SampleSS.ods"), files.getFile("SampleSS.txt")
  198. };
  199. InputStream[] notValidS = new InputStream[] {
  200. files.openResourceAsStream("SampleSS.ods"), files.openResourceAsStream("SampleSS.txt")
  201. };
  202. for (File notValid : notValidF) {
  203. ZipPackage pkg = new ZipPackage(notValid, PackageAccess.READ);
  204. assertNotNull(pkg.getZipArchive());
  205. assertFalse(pkg.getZipArchive().isClosed());
  206. try {
  207. pkg.getParts();
  208. fail("Shouldn't work");
  209. } catch (ODFNotOfficeXmlFileException e) {
  210. } catch (NotOfficeXmlFileException ne) {}
  211. pkg.close();
  212. assertNotNull(pkg.getZipArchive());
  213. assertTrue(pkg.getZipArchive().isClosed());
  214. }
  215. for (InputStream notValid : notValidS) {
  216. ZipPackage pkg = new ZipPackage(notValid, PackageAccess.READ);
  217. assertNotNull(pkg.getZipArchive());
  218. assertFalse(pkg.getZipArchive().isClosed());
  219. try {
  220. pkg.getParts();
  221. fail("Shouldn't work");
  222. } catch (ODFNotOfficeXmlFileException e) {
  223. } catch (NotOfficeXmlFileException ne) {}
  224. pkg.close();
  225. assertNotNull(pkg.getZipArchive());
  226. assertTrue(pkg.getZipArchive().isClosed());
  227. }
  228. }
  229. }