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.

TestVBAMacroReader.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.macros;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.apache.poi.POITestCase.skipTest;
  18. import static org.apache.poi.POITestCase.testPassesNow;
  19. import static org.junit.Assert.assertFalse;
  20. import static org.junit.Assert.assertNotNull;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import org.apache.poi.POIDataSamples;
  29. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  30. import org.apache.poi.util.IOUtils;
  31. import org.apache.poi.util.StringUtil;
  32. import org.junit.Ignore;
  33. import org.junit.Test;
  34. public class TestVBAMacroReader {
  35. private static final Map<POIDataSamples, String> expectedMacroContents;
  36. protected static String readVBA(POIDataSamples poiDataSamples) {
  37. File macro = poiDataSamples.getFile("SimpleMacro.vba");
  38. final byte[] bytes;
  39. try {
  40. FileInputStream stream = new FileInputStream(macro);
  41. try {
  42. bytes = IOUtils.toByteArray(stream);
  43. } finally {
  44. stream.close();
  45. }
  46. } catch (IOException e) {
  47. throw new RuntimeException(e);
  48. }
  49. String testMacroContents = new String(bytes, StringUtil.UTF8);
  50. if (! testMacroContents.startsWith("Sub ")) {
  51. throw new IllegalArgumentException("Not a macro");
  52. }
  53. return testMacroContents.substring(testMacroContents.indexOf("()")+3);
  54. }
  55. static {
  56. final Map<POIDataSamples, String> _expectedMacroContents = new HashMap<POIDataSamples, String>();
  57. final POIDataSamples[] dataSamples = {
  58. POIDataSamples.getSpreadSheetInstance(),
  59. POIDataSamples.getSlideShowInstance(),
  60. POIDataSamples.getDocumentInstance(),
  61. POIDataSamples.getDiagramInstance()
  62. };
  63. for (POIDataSamples sample : dataSamples) {
  64. _expectedMacroContents.put(sample, readVBA(sample));
  65. }
  66. expectedMacroContents = Collections.unmodifiableMap(_expectedMacroContents);
  67. }
  68. //////////////////////////////// From Stream /////////////////////////////
  69. @Test
  70. public void HSSFfromStream() throws Exception {
  71. fromStream(POIDataSamples.getSpreadSheetInstance(), "SimpleMacro.xls");
  72. }
  73. @Test
  74. public void XSSFfromStream() throws Exception {
  75. fromStream(POIDataSamples.getSpreadSheetInstance(), "SimpleMacro.xlsm");
  76. }
  77. @Ignore("bug 59302: Found 0 macros; See org.apache.poi.hslf.usermodel.TestBugs.getMacrosFromHSLF()" +
  78. "for an example of how to get macros out of ppt. TODO: make integration across file formats more elegant")
  79. @Test
  80. public void HSLFfromStream() throws Exception {
  81. fromStream(POIDataSamples.getSlideShowInstance(), "SimpleMacro.ppt");
  82. }
  83. @Test
  84. public void XSLFfromStream() throws Exception {
  85. fromStream(POIDataSamples.getSlideShowInstance(), "SimpleMacro.pptm");
  86. }
  87. @Test
  88. public void HWPFfromStream() throws Exception {
  89. fromStream(POIDataSamples.getDocumentInstance(), "SimpleMacro.doc");
  90. }
  91. @Test
  92. public void XWPFfromStream() throws Exception {
  93. fromStream(POIDataSamples.getDocumentInstance(), "SimpleMacro.docm");
  94. }
  95. @Ignore("Found 0 macros")
  96. @Test
  97. public void HDGFfromStream() throws Exception {
  98. fromStream(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsd");
  99. }
  100. @Test
  101. public void XDGFfromStream() throws Exception {
  102. fromStream(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsdm");
  103. }
  104. //////////////////////////////// From File /////////////////////////////
  105. @Test
  106. public void HSSFfromFile() throws Exception {
  107. fromFile(POIDataSamples.getSpreadSheetInstance(), "SimpleMacro.xls");
  108. }
  109. @Test
  110. public void XSSFfromFile() throws Exception {
  111. fromFile(POIDataSamples.getSpreadSheetInstance(), "SimpleMacro.xlsm");
  112. }
  113. @Ignore("bug 59302: Found 0 macros; See org.apache.poi.hslf.usermodel.TestBugs.getMacrosFromHSLF()" +
  114. "for an example of how to get macros out of ppt. TODO: make integration across file formats more elegant")
  115. @Test
  116. public void HSLFfromFile() throws Exception {
  117. fromFile(POIDataSamples.getSlideShowInstance(), "SimpleMacro.ppt");
  118. }
  119. @Test
  120. public void XSLFfromFile() throws Exception {
  121. fromFile(POIDataSamples.getSlideShowInstance(), "SimpleMacro.pptm");
  122. }
  123. @Test
  124. public void HWPFfromFile() throws Exception {
  125. fromFile(POIDataSamples.getDocumentInstance(), "SimpleMacro.doc");
  126. }
  127. @Test
  128. public void XWPFfromFile() throws Exception {
  129. fromFile(POIDataSamples.getDocumentInstance(), "SimpleMacro.docm");
  130. }
  131. @Ignore("Found 0 macros")
  132. @Test
  133. public void HDGFfromFile() throws Exception {
  134. fromFile(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsd");
  135. }
  136. @Test
  137. public void XDGFfromFile() throws Exception {
  138. fromFile(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsdm");
  139. }
  140. //////////////////////////////// From NPOIFS /////////////////////////////
  141. @Test
  142. public void HSSFfromNPOIFS() throws Exception {
  143. fromNPOIFS(POIDataSamples.getSpreadSheetInstance(), "SimpleMacro.xls");
  144. }
  145. @Ignore("bug 59302: Found 0 macros")
  146. @Test
  147. public void HSLFfromNPOIFS() throws Exception {
  148. fromNPOIFS(POIDataSamples.getSlideShowInstance(), "SimpleMacro.ppt");
  149. }
  150. @Test
  151. public void HWPFfromNPOIFS() throws Exception {
  152. fromNPOIFS(POIDataSamples.getDocumentInstance(), "SimpleMacro.doc");
  153. }
  154. @Ignore("Found 0 macros")
  155. @Test
  156. public void HDGFfromNPOIFS() throws Exception {
  157. fromNPOIFS(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsd");
  158. }
  159. protected void fromFile(POIDataSamples dataSamples, String filename) throws IOException {
  160. File f = dataSamples.getFile(filename);
  161. VBAMacroReader r = new VBAMacroReader(f);
  162. try {
  163. assertMacroContents(dataSamples, r);
  164. } finally {
  165. r.close();
  166. }
  167. }
  168. protected void fromStream(POIDataSamples dataSamples, String filename) throws IOException {
  169. InputStream fis = dataSamples.openResourceAsStream(filename);
  170. try {
  171. VBAMacroReader r = new VBAMacroReader(fis);
  172. try {
  173. assertMacroContents(dataSamples, r);
  174. } finally {
  175. r.close();
  176. }
  177. } finally {
  178. fis.close();
  179. }
  180. }
  181. protected void fromNPOIFS(POIDataSamples dataSamples, String filename) throws IOException {
  182. File f = dataSamples.getFile(filename);
  183. NPOIFSFileSystem fs = new NPOIFSFileSystem(f);
  184. try {
  185. VBAMacroReader r = new VBAMacroReader(fs);
  186. try {
  187. assertMacroContents(dataSamples, r);
  188. } finally {
  189. r.close();
  190. }
  191. } finally {
  192. fs.close();
  193. }
  194. }
  195. protected void assertMacroContents(POIDataSamples samples, VBAMacroReader r) throws IOException {
  196. assertNotNull(r);
  197. Map<String,String> contents = r.readMacros();
  198. assertNotNull(contents);
  199. assertFalse("Found 0 macros", contents.isEmpty());
  200. /*
  201. assertEquals(5, contents.size());
  202. // Check the ones without scripts
  203. String[] noScripts = new String[] { "ThisWorkbook",
  204. "Sheet1", "Sheet2", "Sheet3" };
  205. for (String entry : noScripts) {
  206. assertTrue(entry, contents.containsKey(entry));
  207. String content = contents.get(entry);
  208. assertContains(content, "Attribute VB_Exposed = True");
  209. assertContains(content, "Attribute VB_Customizable = True");
  210. assertContains(content, "Attribute VB_TemplateDerived = False");
  211. assertContains(content, "Attribute VB_GlobalNameSpace = False");
  212. assertContains(content, "Attribute VB_Exposed = True");
  213. }
  214. */
  215. // Check the script one
  216. assertContains(contents, "Module1");
  217. String content = contents.get("Module1");
  218. assertNotNull(content);
  219. assertContains(content, "Attribute VB_Name = \"Module1\"");
  220. //assertContains(content, "Attribute TestMacro.VB_Description = \"This is a test macro\"");
  221. // And the macro itself
  222. String testMacroNoSub = expectedMacroContents.get(samples);
  223. assertContains(content, testMacroNoSub);
  224. }
  225. @Test
  226. public void bug59830() throws IOException {
  227. //test file is "609751.xls" in govdocs1
  228. File f = POIDataSamples.getSpreadSheetInstance().getFile("59830.xls");
  229. VBAMacroReader r = new VBAMacroReader(f);
  230. Map<String, String> macros = r.readMacros();
  231. assertNotNull(macros.get("Module20"));
  232. assertContains(macros.get("Module20"), "here start of superscripting");
  233. }
  234. @Test
  235. public void bug59858() throws IOException {
  236. File f = POIDataSamples.getSpreadSheetInstance().getFile("59858.xls");
  237. VBAMacroReader r = new VBAMacroReader(f);
  238. Map<String, String> macros = r.readMacros();
  239. assertNotNull(macros.get("Sheet4"));
  240. assertContains(macros.get("Sheet4"), "intentional constituent");
  241. }
  242. @Test
  243. public void bug60158() throws IOException {
  244. File f = POIDataSamples.getDocumentInstance().getFile("60158.docm");
  245. VBAMacroReader r = new VBAMacroReader(f);
  246. Map<String, String> macros = r.readMacros();
  247. assertNotNull(macros.get("NewMacros"));
  248. assertContains(macros.get("NewMacros"), "' dirty");
  249. }
  250. @Test
  251. public void bug60273() throws IOException {
  252. //test file derives from govdocs1 147240.xls
  253. File f = POIDataSamples.getSpreadSheetInstance().getFile("60273.xls");
  254. VBAMacroReader r = new VBAMacroReader(f);
  255. Map<String, String> macros = r.readMacros();
  256. assertNotNull(macros.get("Module1"));
  257. assertContains(macros.get("Module1"), "9/8/2004");
  258. }
  259. }