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.

TestEmbedOLEPackage.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.ss.usermodel;
  16. import static org.apache.poi.sl.SLCommonUtils.xslfOnly;
  17. import static org.junit.Assert.assertArrayEquals;
  18. import static org.junit.Assert.assertEquals;
  19. import static org.junit.Assert.assertTrue;
  20. import static org.junit.Assume.assumeFalse;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.security.MessageDigest;
  26. import java.util.ArrayList;
  27. import java.util.Arrays;
  28. import java.util.Iterator;
  29. import java.util.List;
  30. import java.util.Locale;
  31. import org.apache.commons.codec.binary.Base64;
  32. import org.apache.poi.POIDataSamples;
  33. import org.apache.poi.hpsf.ClassIDPredefined;
  34. import org.apache.poi.hssf.HSSFTestDataSamples;
  35. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  36. import org.apache.poi.poifs.crypt.CryptoFunctions;
  37. import org.apache.poi.poifs.crypt.HashAlgorithm;
  38. import org.apache.poi.poifs.filesystem.DirectoryNode;
  39. import org.apache.poi.poifs.filesystem.EntryUtils;
  40. import org.apache.poi.poifs.filesystem.Ole10Native;
  41. import org.apache.poi.poifs.filesystem.Ole10NativeException;
  42. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  43. import org.apache.poi.sl.usermodel.AutoShape;
  44. import org.apache.poi.sl.usermodel.ShapeType;
  45. import org.apache.poi.sl.usermodel.Slide;
  46. import org.apache.poi.sl.usermodel.SlideShow;
  47. import org.apache.poi.ss.extractor.EmbeddedData;
  48. import org.apache.poi.ss.extractor.EmbeddedExtractor;
  49. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  50. import org.apache.poi.xssf.XSSFTestDataSamples;
  51. import org.apache.poi.xssf.usermodel.XSSFObjectData;
  52. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  53. import org.junit.BeforeClass;
  54. import org.junit.Test;
  55. public class TestEmbedOLEPackage {
  56. private static byte[] samplePPT, samplePPTX, samplePNG;
  57. private static final POIDataSamples ssamples = POIDataSamples.getSpreadSheetInstance();
  58. @BeforeClass
  59. public static void init() throws IOException, ReflectiveOperationException {
  60. samplePPT = getSamplePPT(false);
  61. samplePPTX = getSamplePPT(true);
  62. samplePNG = ssamples.readFile("logoKarmokar4.png");
  63. }
  64. @Test
  65. public void embedPDF() throws IOException {
  66. try (InputStream is = ssamples.openResourceAsStream("bug64512_embed.xlsx");
  67. XSSFWorkbook wb = new XSSFWorkbook(is)) {
  68. List<XSSFObjectData> oleShapes = new ArrayList<>();
  69. List<Ole10Native> ole10s = new ArrayList<>();
  70. List<String> digests = new ArrayList<>();
  71. final boolean digestMatch =
  72. wb.getSheetAt(0).getDrawingPatriarch().getShapes().stream()
  73. .map(s -> (XSSFObjectData)s)
  74. .filter(oleShapes::add)
  75. .map(TestEmbedOLEPackage::extractOle10Native)
  76. .filter(ole10s::add)
  77. .map(TestEmbedOLEPackage::digest)
  78. .allMatch("FUJBVHTAZ0ly/TNDNmEj1gQ4a2TbZwDMVF4WUkDQLaM="::equals);
  79. assertEquals(2, oleShapes.size());
  80. assertEquals("Package", oleShapes.get(0).getOLE2ClassName());
  81. assertEquals("Package2", oleShapes.get(1).getOLE2ClassName());
  82. assertTrue(digestMatch);
  83. final String expLabel = "Apache_POI_project_logo_(2018).pdf";
  84. final String expFilenName = "C:\\Dell\\Apache_POI_project_logo_(2018).pdf";
  85. final String expCmd1 = "C:\\Users\\KIWIWI~1\\AppData\\Local\\Temp\\{84287F34-B79C-4F3A-9A92-6BB664586F48}\\Apache_POI_project_logo_(2018).pdf";
  86. final String expCmd2 = "C:\\Users\\KIWIWI~1\\AppData\\Local\\Temp\\{84287F34-B79C-4F3A-9A92-6BB664586F48}\\Apache_POI_project_logo_(2).pdf";
  87. assertTrue(ole10s.stream().map(Ole10Native::getLabel).allMatch(expLabel::equals));
  88. assertTrue(ole10s.stream().map(Ole10Native::getFileName).allMatch(expFilenName::equals));
  89. assertEquals(expCmd1, ole10s.get(0).getCommand());
  90. assertEquals(expCmd2, ole10s.get(1).getCommand());
  91. for (Ole10Native o : ole10s) {
  92. assertEquals(o.getLabel(), o.getLabel2());
  93. assertEquals(o.getCommand(), o.getCommand2());
  94. assertEquals(o.getFileName(), o.getFileName2());
  95. }
  96. Ole10Native scratch = new Ole10Native(expLabel, expFilenName, expCmd1, ole10s.get(0).getDataBuffer());
  97. scratch.setLabel2(expLabel);
  98. scratch.setFileName2(expFilenName);
  99. scratch.setCommand2(expCmd1);
  100. try (POIFSFileSystem scratchFS = new POIFSFileSystem();
  101. POIFSFileSystem ole1FS = new POIFSFileSystem(new ByteArrayInputStream(oleShapes.get(0).getObjectData()))) {
  102. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  103. scratch.writeOut(bos);
  104. scratchFS.createDocument(new ByteArrayInputStream(bos.toByteArray()), Ole10Native.OLE10_NATIVE);
  105. scratchFS.getRoot().setStorageClsid(ClassIDPredefined.OLE_V1_PACKAGE.getClassID());
  106. assertTrue(EntryUtils.areDirectoriesIdentical(ole1FS.getRoot(), scratchFS.getRoot()));
  107. }
  108. }
  109. }
  110. private static Ole10Native extractOle10Native(XSSFObjectData objectData) {
  111. try (InputStream is = objectData.getObjectPart().getInputStream();
  112. POIFSFileSystem poifs = new POIFSFileSystem(is)) {
  113. return Ole10Native.createFromEmbeddedOleObject(poifs);
  114. } catch (IOException | Ole10NativeException e) {
  115. throw new AssertionError(e.getMessage(), e);
  116. }
  117. }
  118. private static String digest(Ole10Native ole10) {
  119. MessageDigest sha = CryptoFunctions.getMessageDigest(HashAlgorithm.sha256);
  120. byte[] digest = sha.digest(ole10.getDataBuffer());
  121. return Base64.encodeBase64String(digest);
  122. }
  123. @Test
  124. public void embedXSSF() throws IOException {
  125. Workbook wb1 = new XSSFWorkbook();
  126. addEmbeddedObjects(wb1);
  127. Workbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
  128. validateEmbeddedObjects(wb2);
  129. wb2.close();
  130. wb1.close();
  131. }
  132. @Test
  133. public void embedHSSF() throws IOException {
  134. assumeFalse(xslfOnly());
  135. HSSFWorkbook wb1 = new HSSFWorkbook();
  136. addEmbeddedObjects(wb1);
  137. Workbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
  138. validateEmbeddedObjects(wb2);
  139. wb2.close();
  140. wb1.close();
  141. }
  142. static void validateEmbeddedObjects(Workbook wb) throws IOException {
  143. boolean ooxml = wb.getClass().getName().toLowerCase(Locale.ROOT).contains("xssf");
  144. byte[] data = (ooxml) ? samplePPTX : samplePPT;
  145. Iterator<Integer> shapeIds = Arrays.asList(1025,1026,2049).iterator();
  146. EmbeddedExtractor ee = new EmbeddedExtractor();
  147. for (Sheet sheet : wb) {
  148. Drawing<? extends Shape> pat = sheet.getDrawingPatriarch();
  149. for (Shape shape : pat) {
  150. assertTrue(shape instanceof ObjectData);
  151. ObjectData od = (ObjectData)shape;
  152. EmbeddedData ed = ee.extractOne((DirectoryNode)od.getDirectory());
  153. assertArrayEquals(data, ed.getEmbeddedData());
  154. assertArrayEquals(samplePNG, od.getPictureData().getData());
  155. assertEquals((int)shapeIds.next(), od.getShapeId());
  156. }
  157. }
  158. }
  159. static void addEmbeddedObjects(Workbook wb) throws IOException {
  160. boolean ooxml = wb.getClass().getName().toLowerCase(Locale.ROOT).contains("xssf");
  161. int picIdx = wb.addPicture(samplePNG, Workbook.PICTURE_TYPE_PNG);
  162. byte[] data = (ooxml) ? samplePPTX : samplePPT;
  163. String ext = (ooxml) ? ".pptx" : ".ppt";
  164. int oleIdx1a = wb.addOlePackage(data, "dummy1a"+ext, "dummy1a"+ext, "dummy1a"+ext);
  165. int oleIdx1b = wb.addOlePackage(data, "dummy1b"+ext, "dummy1b"+ext, "dummy1b"+ext);
  166. int oleIdx2 = wb.addOlePackage(data, "dummy2"+ext, "dummy2"+ext, "dummy2"+ext);
  167. Sheet sh1 = wb.createSheet();
  168. Drawing<?> pat1 = sh1.createDrawingPatriarch();
  169. ClientAnchor anchor1a = pat1.createAnchor(0, 0, 0, 0, 1, 1, 3, 6);
  170. pat1.createObjectData(anchor1a, oleIdx1a, picIdx);
  171. ClientAnchor anchor1b = pat1.createAnchor(0, 0, 0, 0, 1, 1+7, 3, 6+7);
  172. pat1.createObjectData(anchor1b, oleIdx1b, picIdx);
  173. Sheet sh2 = wb.createSheet();
  174. Drawing<?> pat2 = sh2.createDrawingPatriarch();
  175. ClientAnchor anchor2 = pat2.createAnchor(0, 0, 0, 0, 1, 1, 3, 6);
  176. pat2.createObjectData(anchor2, oleIdx2, picIdx);
  177. }
  178. static byte[] getSamplePPT(boolean ooxml) throws IOException, ReflectiveOperationException {
  179. SlideShow<?,?> ppt = (ooxml) ? new XMLSlideShow()
  180. : (SlideShow<?,?>)Class.forName("org.apache.poi.hslf.usermodel.HSLFSlideShow").newInstance();
  181. Slide<?,?> slide = ppt.createSlide();
  182. AutoShape<?,?> sh1 = slide.createAutoShape();
  183. sh1.setShapeType(ShapeType.STAR_32);
  184. sh1.setAnchor(new java.awt.Rectangle(50, 50, 100, 200));
  185. sh1.setFillColor(java.awt.Color.red);
  186. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  187. ppt.write(bos);
  188. ppt.close();
  189. return bos.toByteArray();
  190. }
  191. }