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.

TestOle10Native.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.poifs.filesystem;
  16. import static org.hamcrest.core.IsEqual.equalTo;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertThat;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.ArrayList;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import org.apache.poi.POIDataSamples;
  27. import org.apache.poi.util.IOUtils;
  28. import org.apache.poi.util.RecordFormatException;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.rules.ExpectedException;
  32. public class TestOle10Native {
  33. private static final POIDataSamples dataSamples = POIDataSamples.getPOIFSInstance();
  34. @Rule
  35. public ExpectedException thrown = ExpectedException.none();
  36. @Test
  37. public void testOleNative() throws IOException, Ole10NativeException {
  38. POIFSFileSystem fs = new POIFSFileSystem(dataSamples.openResourceAsStream("oleObject1.bin"));
  39. Ole10Native ole = Ole10Native.createFromEmbeddedOleObject(fs);
  40. assertEquals("File1.svg", ole.getLabel());
  41. assertEquals("D:\\Documents and Settings\\rsc\\My Documents\\file1.svg", ole.getCommand());
  42. }
  43. @Test
  44. public void testFiles() throws IOException, Ole10NativeException {
  45. File[] files = {
  46. // bug 51891
  47. POIDataSamples.getPOIFSInstance().getFile("multimedia.doc"),
  48. // tika bug 1072
  49. POIDataSamples.getPOIFSInstance().getFile("20-Force-on-a-current-S00.doc"),
  50. // other files containing ole10native records ...
  51. POIDataSamples.getDocumentInstance().getFile("Bug53380_3.doc"),
  52. POIDataSamples.getDocumentInstance().getFile("Bug47731.doc")
  53. };
  54. for (File f : files) {
  55. POIFSFileSystem fs = new POIFSFileSystem(f, true);
  56. List<Entry> entries = new ArrayList<>();
  57. findOle10(entries, fs.getRoot(), "/");
  58. for (Entry e : entries) {
  59. ByteArrayOutputStream bosExp = new ByteArrayOutputStream();
  60. InputStream is = ((DirectoryNode)e.getParent()).createDocumentInputStream(e);
  61. IOUtils.copy(is,bosExp);
  62. is.close();
  63. Ole10Native ole = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)e.getParent());
  64. ByteArrayOutputStream bosAct = new ByteArrayOutputStream();
  65. ole.writeOut(bosAct);
  66. assertThat(bosExp.toByteArray(), equalTo(bosAct.toByteArray()));
  67. }
  68. fs.close();
  69. }
  70. }
  71. private void findOle10(List<Entry> entries, DirectoryNode dn, String path) {
  72. Iterator<Entry> iter = dn.getEntries();
  73. while (iter.hasNext()) {
  74. Entry e = iter.next();
  75. if (Ole10Native.OLE10_NATIVE.equals(e.getName())) {
  76. if (entries != null) entries.add(e);
  77. // System.out.println(filename+" : "+path);
  78. } else if (e.isDirectoryEntry()) {
  79. findOle10(entries, (DirectoryNode)e, path+e.getName()+"/");
  80. }
  81. }
  82. }
  83. @Test
  84. public void testOleNativeOOM() throws IOException, Ole10NativeException {
  85. POIFSFileSystem fs = new POIFSFileSystem(dataSamples.openResourceAsStream("60256.bin"));
  86. thrown.expect(RecordFormatException.class);
  87. thrown.expectMessage("Tried to allocate");
  88. Ole10Native.createFromEmbeddedOleObject(fs);
  89. }
  90. }