Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TestPackage.java 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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.assertNull;
  20. import static org.junit.Assert.assertTrue;
  21. import static org.junit.Assert.fail;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.OutputStream;
  30. import java.lang.reflect.InvocationTargetException;
  31. import java.net.URI;
  32. import java.net.URISyntaxException;
  33. import java.util.Enumeration;
  34. import java.util.HashMap;
  35. import java.util.List;
  36. import java.util.TreeMap;
  37. import java.util.regex.Pattern;
  38. import java.util.zip.ZipEntry;
  39. import java.util.zip.ZipFile;
  40. import java.util.zip.ZipOutputStream;
  41. import org.apache.poi.EncryptedDocumentException;
  42. import org.apache.poi.POIDataSamples;
  43. import org.apache.poi.POITestCase;
  44. import org.apache.poi.POIXMLException;
  45. import org.apache.poi.UnsupportedFileFormatException;
  46. import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
  47. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  48. import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
  49. import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException;
  50. import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException;
  51. import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
  52. import org.apache.poi.openxml4j.opc.internal.ContentTypeManager;
  53. import org.apache.poi.openxml4j.opc.internal.FileHelper;
  54. import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
  55. import org.apache.poi.openxml4j.opc.internal.ZipHelper;
  56. import org.apache.poi.openxml4j.util.ZipSecureFile;
  57. import org.apache.poi.ss.usermodel.Workbook;
  58. import org.apache.poi.ss.usermodel.WorkbookFactory;
  59. import org.apache.poi.util.DocumentHelper;
  60. import org.apache.poi.util.IOUtils;
  61. import org.apache.poi.util.POILogFactory;
  62. import org.apache.poi.util.POILogger;
  63. import org.apache.poi.util.TempFile;
  64. import org.junit.Ignore;
  65. import org.junit.Test;
  66. import org.w3c.dom.Document;
  67. import org.w3c.dom.Element;
  68. import org.w3c.dom.NodeList;
  69. import org.xml.sax.SAXException;
  70. public final class TestPackage {
  71. private static final POILogger logger = POILogFactory.getLogger(TestPackage.class);
  72. /**
  73. * Test that just opening and closing the file doesn't alter the document.
  74. */
  75. @Test
  76. public void openSave() throws IOException, InvalidFormatException {
  77. String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx");
  78. File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageOpenSaveTMP.docx");
  79. @SuppressWarnings("resource")
  80. OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE);
  81. try {
  82. p.save(targetFile.getAbsoluteFile());
  83. // Compare the original and newly saved document
  84. assertTrue(targetFile.exists());
  85. ZipFileAssert.assertEquals(new File(originalFile), targetFile);
  86. assertTrue(targetFile.delete());
  87. } finally {
  88. // use revert to not re-write the input file
  89. p.revert();
  90. }
  91. }
  92. /**
  93. * Test that when we create a new Package, we give it
  94. * the correct default content types
  95. * @throws IllegalAccessException
  96. * @throws NoSuchFieldException
  97. * @throws IllegalArgumentException
  98. * @throws SecurityException
  99. */
  100. @Test
  101. public void createGetsContentTypes()
  102. throws IOException, InvalidFormatException, SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException {
  103. File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestCreatePackageTMP.docx");
  104. // Zap the target file, in case of an earlier run
  105. if(targetFile.exists()) targetFile.delete();
  106. @SuppressWarnings("resource")
  107. OPCPackage pkg = OPCPackage.create(targetFile);
  108. // Check it has content types for rels and xml
  109. ContentTypeManager ctm = getContentTypeManager(pkg);
  110. assertEquals(
  111. "application/xml",
  112. ctm.getContentType(
  113. PackagingURIHelper.createPartName("/foo.xml")
  114. )
  115. );
  116. assertEquals(
  117. ContentTypes.RELATIONSHIPS_PART,
  118. ctm.getContentType(
  119. PackagingURIHelper.createPartName("/foo.rels")
  120. )
  121. );
  122. assertNull(
  123. ctm.getContentType(
  124. PackagingURIHelper.createPartName("/foo.txt")
  125. )
  126. );
  127. pkg.revert();
  128. }
  129. /**
  130. * Test package creation.
  131. */
  132. @Test
  133. public void createPackageAddPart() throws IOException, InvalidFormatException {
  134. File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestCreatePackageTMP.docx");
  135. File expectedFile = OpenXML4JTestDataSamples.getSampleFile("TestCreatePackageOUTPUT.docx");
  136. // Zap the target file, in case of an earlier run
  137. if(targetFile.exists()) targetFile.delete();
  138. // Create a package
  139. OPCPackage pkg = OPCPackage.create(targetFile);
  140. PackagePartName corePartName = PackagingURIHelper
  141. .createPartName("/word/document.xml");
  142. pkg.addRelationship(corePartName, TargetMode.INTERNAL,
  143. PackageRelationshipTypes.CORE_DOCUMENT, "rId1");
  144. PackagePart corePart = pkg
  145. .createPart(
  146. corePartName,
  147. "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
  148. Document doc = DocumentHelper.createDocument();
  149. Element elDocument = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:document");
  150. doc.appendChild(elDocument);
  151. Element elBody = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:body");
  152. elDocument.appendChild(elBody);
  153. Element elParagraph = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:p");
  154. elBody.appendChild(elParagraph);
  155. Element elRun = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:r");
  156. elParagraph.appendChild(elRun);
  157. Element elText = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:t");
  158. elRun.appendChild(elText);
  159. elText.setTextContent("Hello Open XML !");
  160. StreamHelper.saveXmlInStream(doc, corePart.getOutputStream());
  161. pkg.close();
  162. ZipFileAssert.assertEquals(expectedFile, targetFile);
  163. assertTrue(targetFile.delete());
  164. }
  165. /**
  166. * Tests that we can create a new package, add a core
  167. * document and another part, save and re-load and
  168. * have everything setup as expected
  169. * @throws SAXException
  170. */
  171. @Test
  172. public void createPackageWithCoreDocument() throws IOException, InvalidFormatException, URISyntaxException, SAXException {
  173. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  174. OPCPackage pkg = OPCPackage.create(baos);
  175. // Add a core document
  176. PackagePartName corePartName = PackagingURIHelper.createPartName("/xl/workbook.xml");
  177. // Create main part relationship
  178. pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT, "rId1");
  179. // Create main document part
  180. PackagePart corePart = pkg.createPart(corePartName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");
  181. // Put in some dummy content
  182. OutputStream coreOut = corePart.getOutputStream();
  183. coreOut.write("<dummy-xml />".getBytes("UTF-8"));
  184. coreOut.close();
  185. // And another bit
  186. PackagePartName sheetPartName = PackagingURIHelper.createPartName("/xl/worksheets/sheet1.xml");
  187. PackageRelationship rel =
  188. corePart.addRelationship(sheetPartName, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet", "rSheet1");
  189. PackagePart part = pkg.createPart(sheetPartName, "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml");
  190. assertNotNull(part);
  191. // Dummy content again
  192. coreOut = corePart.getOutputStream();
  193. coreOut.write("<dummy-xml2 />".getBytes("UTF-8"));
  194. coreOut.close();
  195. //add a relationship with internal target: "#Sheet1!A1"
  196. corePart.addRelationship(new URI("#Sheet1!A1"), TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", "rId2");
  197. // Check things are as expected
  198. PackageRelationshipCollection coreRels =
  199. pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);
  200. assertEquals(1, coreRels.size());
  201. PackageRelationship coreRel = coreRels.getRelationship(0);
  202. assertEquals("/", coreRel.getSourceURI().toString());
  203. assertEquals("/xl/workbook.xml", coreRel.getTargetURI().toString());
  204. assertNotNull(pkg.getPart(coreRel));
  205. // Save and re-load
  206. pkg.close();
  207. File tmp = TempFile.createTempFile("testCreatePackageWithCoreDocument", ".zip");
  208. OutputStream fout = new FileOutputStream(tmp);
  209. try {
  210. fout.write(baos.toByteArray());
  211. } finally {
  212. fout.close();
  213. }
  214. pkg = OPCPackage.open(tmp.getPath());
  215. //tmp.delete();
  216. try {
  217. // Check still right
  218. coreRels = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);
  219. assertEquals(1, coreRels.size());
  220. coreRel = coreRels.getRelationship(0);
  221. assertEquals("/", coreRel.getSourceURI().toString());
  222. assertEquals("/xl/workbook.xml", coreRel.getTargetURI().toString());
  223. corePart = pkg.getPart(coreRel);
  224. assertNotNull(corePart);
  225. PackageRelationshipCollection rels = corePart.getRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink");
  226. assertEquals(1, rels.size());
  227. rel = rels.getRelationship(0);
  228. assertEquals("Sheet1!A1", rel.getTargetURI().getRawFragment());
  229. assertMSCompatibility(pkg);
  230. } finally {
  231. pkg.close();
  232. }
  233. }
  234. private void assertMSCompatibility(OPCPackage pkg) throws IOException, InvalidFormatException, SAXException {
  235. PackagePartName relName = PackagingURIHelper.createPartName(PackageRelationship.getContainerPartRelationship());
  236. PackagePart relPart = pkg.getPart(relName);
  237. Document xmlRelationshipsDoc = DocumentHelper.readDocument(relPart.getInputStream());
  238. Element root = xmlRelationshipsDoc.getDocumentElement();
  239. NodeList nodeList = root.getElementsByTagName(PackageRelationship.RELATIONSHIP_TAG_NAME);
  240. int nodeCount = nodeList.getLength();
  241. for (int i = 0; i < nodeCount; i++) {
  242. Element element = (Element) nodeList.item(i);
  243. String value = element.getAttribute(PackageRelationship.TARGET_ATTRIBUTE_NAME);
  244. assertTrue("Root target must not start with a leading slash ('/'): " + value, value.charAt(0) != '/');
  245. }
  246. }
  247. /**
  248. * Test package opening.
  249. */
  250. @Test
  251. public void openPackage() throws IOException, InvalidFormatException {
  252. File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestOpenPackageTMP.docx");
  253. File inputFile = OpenXML4JTestDataSamples.getSampleFile("TestOpenPackageINPUT.docx");
  254. File expectedFile = OpenXML4JTestDataSamples.getSampleFile("TestOpenPackageOUTPUT.docx");
  255. // Copy the input file in the output directory
  256. FileHelper.copyFile(inputFile, targetFile);
  257. // Create a package
  258. OPCPackage pkg = OPCPackage.open(targetFile.getAbsolutePath());
  259. // Modify core part
  260. PackagePartName corePartName = PackagingURIHelper
  261. .createPartName("/word/document.xml");
  262. PackagePart corePart = pkg.getPart(corePartName);
  263. // Delete some part to have a valid document
  264. for (PackageRelationship rel : corePart.getRelationships()) {
  265. corePart.removeRelationship(rel.getId());
  266. pkg.removePart(PackagingURIHelper.createPartName(PackagingURIHelper
  267. .resolvePartUri(corePart.getPartName().getURI(), rel
  268. .getTargetURI())));
  269. }
  270. // Create a content
  271. Document doc = DocumentHelper.createDocument();
  272. Element elDocument = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:document");
  273. doc.appendChild(elDocument);
  274. Element elBody = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:body");
  275. elDocument.appendChild(elBody);
  276. Element elParagraph = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:p");
  277. elBody.appendChild(elParagraph);
  278. Element elRun = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:r");
  279. elParagraph.appendChild(elRun);
  280. Element elText = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:t");
  281. elRun.appendChild(elText);
  282. elText.setTextContent("Hello Open XML !");
  283. StreamHelper.saveXmlInStream(doc, corePart.getOutputStream());
  284. // Save and close
  285. try {
  286. pkg.close();
  287. } catch (IOException e) {
  288. fail();
  289. }
  290. ZipFileAssert.assertEquals(expectedFile, targetFile);
  291. assertTrue(targetFile.delete());
  292. }
  293. /**
  294. * Checks that we can write a package to a simple
  295. * OutputStream, in addition to the normal writing
  296. * to a file
  297. */
  298. @Test
  299. public void saveToOutputStream() throws IOException, InvalidFormatException {
  300. String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx");
  301. File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageOpenSaveTMP.docx");
  302. @SuppressWarnings("resource")
  303. OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE);
  304. try {
  305. FileOutputStream fout = new FileOutputStream(targetFile);
  306. try {
  307. p.save(fout);
  308. } finally {
  309. fout.close();
  310. }
  311. // Compare the original and newly saved document
  312. assertTrue(targetFile.exists());
  313. ZipFileAssert.assertEquals(new File(originalFile), targetFile);
  314. assertTrue(targetFile.delete());
  315. } finally {
  316. // use revert to not re-write the input file
  317. p.revert();
  318. }
  319. }
  320. /**
  321. * Checks that we can open+read a package from a
  322. * simple InputStream, in addition to the normal
  323. * reading from a file
  324. */
  325. @Test
  326. public void openFromInputStream() throws IOException, InvalidFormatException {
  327. String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx");
  328. FileInputStream finp = new FileInputStream(originalFile);
  329. @SuppressWarnings("resource")
  330. OPCPackage p = OPCPackage.open(finp);
  331. assertNotNull(p);
  332. assertNotNull(p.getRelationships());
  333. assertEquals(12, p.getParts().size());
  334. // Check it has the usual bits
  335. assertTrue(p.hasRelationships());
  336. assertTrue(p.containPart(PackagingURIHelper.createPartName("/_rels/.rels")));
  337. p.revert();
  338. finp.close();
  339. }
  340. /**
  341. * TODO: fix and enable
  342. * @throws URISyntaxException
  343. */
  344. @Test
  345. @Ignore
  346. public void removePartRecursive() throws IOException, InvalidFormatException, URISyntaxException {
  347. String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx");
  348. File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageRemovePartRecursiveOUTPUT.docx");
  349. File tempFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageRemovePartRecursiveTMP.docx");
  350. @SuppressWarnings("resource")
  351. OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE);
  352. p.removePartRecursive(PackagingURIHelper.createPartName(new URI(
  353. "/word/document.xml")));
  354. p.save(tempFile.getAbsoluteFile());
  355. // Compare the original and newly saved document
  356. assertTrue(targetFile.exists());
  357. ZipFileAssert.assertEquals(targetFile, tempFile);
  358. assertTrue(targetFile.delete());
  359. p.revert();
  360. }
  361. @Test
  362. public void deletePart() throws InvalidFormatException {
  363. TreeMap<PackagePartName, String> expectedValues;
  364. TreeMap<PackagePartName, String> values;
  365. values = new TreeMap<PackagePartName, String>();
  366. // Expected values
  367. expectedValues = new TreeMap<PackagePartName, String>();
  368. expectedValues.put(PackagingURIHelper.createPartName("/_rels/.rels"),
  369. "application/vnd.openxmlformats-package.relationships+xml");
  370. expectedValues
  371. .put(PackagingURIHelper.createPartName("/docProps/app.xml"),
  372. "application/vnd.openxmlformats-officedocument.extended-properties+xml");
  373. expectedValues.put(PackagingURIHelper
  374. .createPartName("/docProps/core.xml"),
  375. "application/vnd.openxmlformats-package.core-properties+xml");
  376. expectedValues
  377. .put(PackagingURIHelper.createPartName("/word/fontTable.xml"),
  378. "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml");
  379. expectedValues.put(PackagingURIHelper
  380. .createPartName("/word/media/image1.gif"), "image/gif");
  381. expectedValues
  382. .put(PackagingURIHelper.createPartName("/word/settings.xml"),
  383. "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml");
  384. expectedValues
  385. .put(PackagingURIHelper.createPartName("/word/styles.xml"),
  386. "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml");
  387. expectedValues.put(PackagingURIHelper
  388. .createPartName("/word/theme/theme1.xml"),
  389. "application/vnd.openxmlformats-officedocument.theme+xml");
  390. expectedValues
  391. .put(
  392. PackagingURIHelper
  393. .createPartName("/word/webSettings.xml"),
  394. "application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml");
  395. String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
  396. @SuppressWarnings("resource")
  397. OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ_WRITE);
  398. // Remove the core part
  399. p.deletePart(PackagingURIHelper.createPartName("/word/document.xml"));
  400. for (PackagePart part : p.getParts()) {
  401. values.put(part.getPartName(), part.getContentType());
  402. logger.log(POILogger.DEBUG, part.getPartName());
  403. }
  404. // Compare expected values with values return by the package
  405. for (PackagePartName partName : expectedValues.keySet()) {
  406. assertNotNull(values.get(partName));
  407. assertEquals(expectedValues.get(partName), values.get(partName));
  408. }
  409. // Don't save modifications
  410. p.revert();
  411. }
  412. @Test
  413. public void deletePartRecursive() throws InvalidFormatException {
  414. TreeMap<PackagePartName, String> expectedValues;
  415. TreeMap<PackagePartName, String> values;
  416. values = new TreeMap<PackagePartName, String>();
  417. // Expected values
  418. expectedValues = new TreeMap<PackagePartName, String>();
  419. expectedValues.put(PackagingURIHelper.createPartName("/_rels/.rels"),
  420. "application/vnd.openxmlformats-package.relationships+xml");
  421. expectedValues
  422. .put(PackagingURIHelper.createPartName("/docProps/app.xml"),
  423. "application/vnd.openxmlformats-officedocument.extended-properties+xml");
  424. expectedValues.put(PackagingURIHelper
  425. .createPartName("/docProps/core.xml"),
  426. "application/vnd.openxmlformats-package.core-properties+xml");
  427. String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
  428. @SuppressWarnings("resource")
  429. OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ_WRITE);
  430. // Remove the core part
  431. p.deletePartRecursive(PackagingURIHelper.createPartName("/word/document.xml"));
  432. for (PackagePart part : p.getParts()) {
  433. values.put(part.getPartName(), part.getContentType());
  434. logger.log(POILogger.DEBUG, part.getPartName());
  435. }
  436. // Compare expected values with values return by the package
  437. for (PackagePartName partName : expectedValues.keySet()) {
  438. assertNotNull(values.get(partName));
  439. assertEquals(expectedValues.get(partName), values.get(partName));
  440. }
  441. // Don't save modifications
  442. p.revert();
  443. }
  444. /**
  445. * Test that we can open a file by path, and then
  446. * write changes to it.
  447. */
  448. @Test
  449. public void openFileThenOverwrite() throws IOException, InvalidFormatException {
  450. File tempFile = TempFile.createTempFile("poiTesting","tmp");
  451. File origFile = OpenXML4JTestDataSamples.getSampleFile("TestPackageCommon.docx");
  452. FileHelper.copyFile(origFile, tempFile);
  453. // Open the temp file
  454. OPCPackage p = OPCPackage.open(tempFile.toString(), PackageAccess.READ_WRITE);
  455. // Close it
  456. p.close();
  457. // Delete it
  458. assertTrue(tempFile.delete());
  459. // Reset
  460. FileHelper.copyFile(origFile, tempFile);
  461. p = OPCPackage.open(tempFile.toString(), PackageAccess.READ_WRITE);
  462. // Save it to the same file - not allowed
  463. try {
  464. p.save(tempFile);
  465. fail("You shouldn't be able to call save(File) to overwrite the current file");
  466. } catch(InvalidOperationException e) {}
  467. p.close();
  468. // Delete it
  469. assertTrue(tempFile.delete());
  470. // Open it read only, then close and delete - allowed
  471. FileHelper.copyFile(origFile, tempFile);
  472. p = OPCPackage.open(tempFile.toString(), PackageAccess.READ);
  473. p.close();
  474. assertTrue(tempFile.delete());
  475. }
  476. /**
  477. * Test that we can open a file by path, save it
  478. * to another file, then delete both
  479. */
  480. @Test
  481. public void openFileThenSaveDelete() throws IOException, InvalidFormatException {
  482. File tempFile = TempFile.createTempFile("poiTesting","tmp");
  483. File tempFile2 = TempFile.createTempFile("poiTesting","tmp");
  484. File origFile = OpenXML4JTestDataSamples.getSampleFile("TestPackageCommon.docx");
  485. FileHelper.copyFile(origFile, tempFile);
  486. // Open the temp file
  487. OPCPackage p = OPCPackage.open(tempFile.toString(), PackageAccess.READ_WRITE);
  488. // Save it to a different file
  489. p.save(tempFile2);
  490. p.close();
  491. // Delete both the files
  492. assertTrue(tempFile.delete());
  493. assertTrue(tempFile2.delete());
  494. }
  495. private static ContentTypeManager getContentTypeManager(OPCPackage pkg) {
  496. return POITestCase.getFieldValue(OPCPackage.class, pkg, ContentTypeManager.class, "contentTypeManager");
  497. }
  498. @Test
  499. public void getPartsByName() throws IOException, InvalidFormatException {
  500. String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
  501. @SuppressWarnings("resource")
  502. OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ_WRITE);
  503. try {
  504. List<PackagePart> rs = pkg.getPartsByName(Pattern.compile("/word/.*?\\.xml"));
  505. HashMap<String, PackagePart> selected = new HashMap<String, PackagePart>();
  506. for(PackagePart p : rs)
  507. selected.put(p.getPartName().getName(), p);
  508. assertEquals(6, selected.size());
  509. assertTrue(selected.containsKey("/word/document.xml"));
  510. assertTrue(selected.containsKey("/word/fontTable.xml"));
  511. assertTrue(selected.containsKey("/word/settings.xml"));
  512. assertTrue(selected.containsKey("/word/styles.xml"));
  513. assertTrue(selected.containsKey("/word/theme/theme1.xml"));
  514. assertTrue(selected.containsKey("/word/webSettings.xml"));
  515. } finally {
  516. // use revert to not re-write the input file
  517. pkg.revert();
  518. }
  519. }
  520. @Test
  521. public void getPartSize() throws IOException, InvalidFormatException {
  522. String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
  523. OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ);
  524. try {
  525. int checked = 0;
  526. for (PackagePart part : pkg.getParts()) {
  527. // Can get the size of zip parts
  528. if (part.getPartName().getName().equals("/word/document.xml")) {
  529. checked++;
  530. assertEquals(ZipPackagePart.class, part.getClass());
  531. assertEquals(6031l, part.getSize());
  532. }
  533. if (part.getPartName().getName().equals("/word/fontTable.xml")) {
  534. checked++;
  535. assertEquals(ZipPackagePart.class, part.getClass());
  536. assertEquals(1312l, part.getSize());
  537. }
  538. // But not from the others
  539. if (part.getPartName().getName().equals("/docProps/core.xml")) {
  540. checked++;
  541. assertEquals(PackagePropertiesPart.class, part.getClass());
  542. assertEquals(-1, part.getSize());
  543. }
  544. }
  545. // Ensure we actually found the parts we want to check
  546. assertEquals(3, checked);
  547. } finally {
  548. pkg.close();
  549. }
  550. }
  551. @Test
  552. public void replaceContentType()
  553. throws IOException, InvalidFormatException, SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException {
  554. InputStream is = OpenXML4JTestDataSamples.openSampleStream("sample.xlsx");
  555. @SuppressWarnings("resource")
  556. OPCPackage p = OPCPackage.open(is);
  557. ContentTypeManager mgr = getContentTypeManager(p);
  558. assertTrue(mgr.isContentTypeRegister("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"));
  559. assertFalse(mgr.isContentTypeRegister("application/vnd.ms-excel.sheet.macroEnabled.main+xml"));
  560. assertTrue(
  561. p.replaceContentType(
  562. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
  563. "application/vnd.ms-excel.sheet.macroEnabled.main+xml")
  564. );
  565. assertFalse(mgr.isContentTypeRegister("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"));
  566. assertTrue(mgr.isContentTypeRegister("application/vnd.ms-excel.sheet.macroEnabled.main+xml"));
  567. p.revert();
  568. is.close();
  569. }
  570. /**
  571. * Verify we give helpful exceptions (or as best we can) when
  572. * supplied with non-OOXML file types (eg OLE2, ODF)
  573. */
  574. @Test
  575. public void NonOOXMLFileTypes() throws Exception {
  576. // Spreadsheet has a good mix of alternate file types
  577. POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
  578. // OLE2 - Stream
  579. try {
  580. OPCPackage.open(files.openResourceAsStream("SampleSS.xls"));
  581. fail("Shouldn't be able to open OLE2");
  582. } catch (OLE2NotOfficeXmlFileException e) {
  583. assertTrue(e.getMessage().indexOf("The supplied data appears to be in the OLE2 Format") > -1);
  584. assertTrue(e.getMessage().indexOf("You are calling the part of POI that deals with OOXML") > -1);
  585. }
  586. // OLE2 - File
  587. try {
  588. OPCPackage.open(files.getFile("SampleSS.xls"));
  589. fail("Shouldn't be able to open OLE2");
  590. } catch (OLE2NotOfficeXmlFileException e) {
  591. assertTrue(e.getMessage().indexOf("The supplied data appears to be in the OLE2 Format") > -1);
  592. assertTrue(e.getMessage().indexOf("You are calling the part of POI that deals with OOXML") > -1);
  593. }
  594. // Raw XML - Stream
  595. try {
  596. OPCPackage.open(files.openResourceAsStream("SampleSS.xml"));
  597. fail("Shouldn't be able to open XML");
  598. } catch (NotOfficeXmlFileException e) {
  599. assertTrue(e.getMessage().indexOf("The supplied data appears to be a raw XML file") > -1);
  600. assertTrue(e.getMessage().indexOf("Formats such as Office 2003 XML") > -1);
  601. }
  602. // Raw XML - File
  603. try {
  604. OPCPackage.open(files.getFile("SampleSS.xml"));
  605. fail("Shouldn't be able to open XML");
  606. } catch (NotOfficeXmlFileException e) {
  607. assertTrue(e.getMessage().indexOf("The supplied data appears to be a raw XML file") > -1);
  608. assertTrue(e.getMessage().indexOf("Formats such as Office 2003 XML") > -1);
  609. }
  610. // ODF / ODS - Stream
  611. try {
  612. OPCPackage.open(files.openResourceAsStream("SampleSS.ods"));
  613. fail("Shouldn't be able to open ODS");
  614. } catch (ODFNotOfficeXmlFileException e) {
  615. assertTrue(e.toString().contains("The supplied data appears to be in ODF"));
  616. assertTrue(e.toString().contains("Formats like these (eg ODS"));
  617. }
  618. // ODF / ODS - File
  619. try {
  620. OPCPackage.open(files.getFile("SampleSS.ods"));
  621. fail("Shouldn't be able to open ODS");
  622. } catch (ODFNotOfficeXmlFileException e) {
  623. assertTrue(e.toString().contains("The supplied data appears to be in ODF"));
  624. assertTrue(e.toString().contains("Formats like these (eg ODS"));
  625. }
  626. // Plain Text - Stream
  627. try {
  628. OPCPackage.open(files.openResourceAsStream("SampleSS.txt"));
  629. fail("Shouldn't be able to open Plain Text");
  630. } catch (NotOfficeXmlFileException e) {
  631. assertTrue(e.getMessage().indexOf("No valid entries or contents found") > -1);
  632. assertTrue(e.getMessage().indexOf("not a valid OOXML") > -1);
  633. }
  634. // Plain Text - File
  635. try {
  636. OPCPackage.open(files.getFile("SampleSS.txt"));
  637. fail("Shouldn't be able to open Plain Text");
  638. } catch (UnsupportedFileFormatException e) {
  639. // Unhelpful low-level error, sorry
  640. }
  641. }
  642. @Test(expected=IOException.class)
  643. public void zipBombCreateAndHandle()
  644. throws IOException, EncryptedDocumentException, InvalidFormatException {
  645. // #50090 / #56865
  646. ZipFile zipFile = ZipHelper.openZipFile(OpenXML4JTestDataSamples.getSampleFile("sample.xlsx"));
  647. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  648. ZipOutputStream append = new ZipOutputStream(bos);
  649. // first, copy contents from existing war
  650. Enumeration<? extends ZipEntry> entries = zipFile.entries();
  651. while (entries.hasMoreElements()) {
  652. ZipEntry e2 = entries.nextElement();
  653. ZipEntry e = new ZipEntry(e2.getName());
  654. e.setTime(e2.getTime());
  655. e.setComment(e2.getComment());
  656. e.setSize(e2.getSize());
  657. append.putNextEntry(e);
  658. if (!e.isDirectory()) {
  659. InputStream is = zipFile.getInputStream(e);
  660. if (e.getName().equals("[Content_Types].xml")) {
  661. ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
  662. IOUtils.copy(is, bos2);
  663. long size = bos2.size()-"</Types>".length();
  664. append.write(bos2.toByteArray(), 0, (int)size);
  665. byte spam[] = new byte[0x7FFF];
  666. for (int i=0; i<spam.length; i++) spam[i] = ' ';
  667. while (size < 0x7FFF0000) {
  668. append.write(spam);
  669. size += spam.length;
  670. }
  671. append.write("</Types>".getBytes("UTF-8"));
  672. size += 8;
  673. e.setSize(size);
  674. } else {
  675. IOUtils.copy(is, append);
  676. }
  677. is.close();
  678. }
  679. append.closeEntry();
  680. }
  681. append.close();
  682. zipFile.close();
  683. byte buf[] = bos.toByteArray();
  684. bos = null;
  685. Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(buf));
  686. wb.getSheetAt(0);
  687. wb.close();
  688. zipFile.close();
  689. }
  690. @Test
  691. public void zipBombCheckSizes()
  692. throws IOException, EncryptedDocumentException, InvalidFormatException {
  693. File file = OpenXML4JTestDataSamples.getSampleFile("sample.xlsx");
  694. try {
  695. double min_ratio = Double.MAX_VALUE;
  696. long max_size = 0;
  697. ZipFile zf = ZipHelper.openZipFile(file);
  698. Enumeration<? extends ZipEntry> entries = zf.entries();
  699. while (entries.hasMoreElements()) {
  700. ZipEntry ze = entries.nextElement();
  701. double ratio = (double)ze.getCompressedSize() / (double)ze.getSize();
  702. min_ratio = Math.min(min_ratio, ratio);
  703. max_size = Math.max(max_size, ze.getSize());
  704. }
  705. zf.close();
  706. // use values close to, but within the limits
  707. ZipSecureFile.setMinInflateRatio(min_ratio-0.002);
  708. ZipSecureFile.setMaxEntrySize(max_size+1);
  709. WorkbookFactory.create(file, null, true).close();
  710. // check ratio out of bounds
  711. ZipSecureFile.setMinInflateRatio(min_ratio+0.002);
  712. try {
  713. WorkbookFactory.create(file, null, true).close();
  714. // this is a bit strange, as there will be different exceptions thrown
  715. // depending if this executed via "ant test" or within eclipse
  716. // maybe a difference in JDK ...
  717. } catch (InvalidFormatException e) {
  718. checkForZipBombException(e);
  719. } catch (POIXMLException e) {
  720. checkForZipBombException(e);
  721. }
  722. // check max entry size ouf of bounds
  723. ZipSecureFile.setMinInflateRatio(min_ratio-0.002);
  724. ZipSecureFile.setMaxEntrySize(max_size-1);
  725. try {
  726. WorkbookFactory.create(file, null, true).close();
  727. } catch (InvalidFormatException e) {
  728. checkForZipBombException(e);
  729. } catch (POIXMLException e) {
  730. checkForZipBombException(e);
  731. }
  732. } finally {
  733. // reset otherwise a lot of ooxml tests will fail
  734. ZipSecureFile.setMinInflateRatio(0.01d);
  735. ZipSecureFile.setMaxEntrySize(0xFFFFFFFFl);
  736. }
  737. }
  738. private void checkForZipBombException(Throwable e) {
  739. if(e instanceof InvocationTargetException) {
  740. InvocationTargetException t = (InvocationTargetException)e;
  741. IOException t2 = (IOException)t.getTargetException();
  742. if(t2.getMessage().startsWith("Zip bomb detected!")) {
  743. return;
  744. }
  745. }
  746. String msg = e.getMessage();
  747. if(msg != null && msg.startsWith("Zip bomb detected!")) {
  748. return;
  749. }
  750. // recursively check the causes for the message as it can be nested further down in the exception-tree
  751. if(e.getCause() != null && e.getCause() != e) {
  752. checkForZipBombException(e.getCause());
  753. return;
  754. }
  755. 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);
  756. }
  757. }