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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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")
  78. @Test
  79. public void HSLFfromStream() throws Exception {
  80. fromStream(POIDataSamples.getSlideShowInstance(), "SimpleMacro.ppt");
  81. }
  82. @Test
  83. public void XSLFfromStream() throws Exception {
  84. fromStream(POIDataSamples.getSlideShowInstance(), "SimpleMacro.pptm");
  85. }
  86. @Test
  87. public void HWPFfromStream() throws Exception {
  88. fromStream(POIDataSamples.getDocumentInstance(), "SimpleMacro.doc");
  89. }
  90. @Test
  91. public void XWPFfromStream() throws Exception {
  92. fromStream(POIDataSamples.getDocumentInstance(), "SimpleMacro.docm");
  93. }
  94. @Ignore("Found 0 macros")
  95. @Test
  96. public void HDGFfromStream() throws Exception {
  97. fromStream(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsd");
  98. }
  99. @Test
  100. public void XDGFfromStream() throws Exception {
  101. fromStream(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsdm");
  102. }
  103. //////////////////////////////// From File /////////////////////////////
  104. @Test
  105. public void HSSFfromFile() throws Exception {
  106. fromFile(POIDataSamples.getSpreadSheetInstance(), "SimpleMacro.xls");
  107. }
  108. @Test
  109. public void XSSFfromFile() throws Exception {
  110. fromFile(POIDataSamples.getSpreadSheetInstance(), "SimpleMacro.xlsm");
  111. }
  112. @Ignore("bug 59302: Found 0 macros")
  113. @Test
  114. public void HSLFfromFile() throws Exception {
  115. fromFile(POIDataSamples.getSlideShowInstance(), "SimpleMacro.ppt");
  116. }
  117. @Test
  118. public void XSLFfromFile() throws Exception {
  119. fromFile(POIDataSamples.getSlideShowInstance(), "SimpleMacro.pptm");
  120. }
  121. @Test
  122. public void HWPFfromFile() throws Exception {
  123. fromFile(POIDataSamples.getDocumentInstance(), "SimpleMacro.doc");
  124. }
  125. @Test
  126. public void XWPFfromFile() throws Exception {
  127. fromFile(POIDataSamples.getDocumentInstance(), "SimpleMacro.docm");
  128. }
  129. @Ignore("Found 0 macros")
  130. @Test
  131. public void HDGFfromFile() throws Exception {
  132. fromFile(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsd");
  133. }
  134. @Test
  135. public void XDGFfromFile() throws Exception {
  136. fromFile(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsdm");
  137. }
  138. //////////////////////////////// From NPOIFS /////////////////////////////
  139. @Test
  140. public void HSSFfromNPOIFS() throws Exception {
  141. fromNPOIFS(POIDataSamples.getSpreadSheetInstance(), "SimpleMacro.xls");
  142. }
  143. @Ignore("bug 59302: Found 0 macros")
  144. @Test
  145. public void HSLFfromNPOIFS() throws Exception {
  146. fromNPOIFS(POIDataSamples.getSlideShowInstance(), "SimpleMacro.ppt");
  147. }
  148. @Test
  149. public void HWPFfromNPOIFS() throws Exception {
  150. fromNPOIFS(POIDataSamples.getDocumentInstance(), "SimpleMacro.doc");
  151. }
  152. @Ignore("Found 0 macros")
  153. @Test
  154. public void HDGFfromNPOIFS() throws Exception {
  155. fromNPOIFS(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsd");
  156. }
  157. protected void fromFile(POIDataSamples dataSamples, String filename) throws IOException {
  158. File f = dataSamples.getFile(filename);
  159. VBAMacroReader r = new VBAMacroReader(f);
  160. try {
  161. assertMacroContents(dataSamples, r);
  162. } finally {
  163. r.close();
  164. }
  165. }
  166. protected void fromStream(POIDataSamples dataSamples, String filename) throws IOException {
  167. InputStream fis = dataSamples.openResourceAsStream(filename);
  168. try {
  169. VBAMacroReader r = new VBAMacroReader(fis);
  170. try {
  171. assertMacroContents(dataSamples, r);
  172. } finally {
  173. r.close();
  174. }
  175. } finally {
  176. fis.close();
  177. }
  178. }
  179. protected void fromNPOIFS(POIDataSamples dataSamples, String filename) throws IOException {
  180. File f = dataSamples.getFile(filename);
  181. NPOIFSFileSystem fs = new NPOIFSFileSystem(f);
  182. try {
  183. VBAMacroReader r = new VBAMacroReader(fs);
  184. try {
  185. assertMacroContents(dataSamples, r);
  186. } finally {
  187. r.close();
  188. }
  189. } finally {
  190. fs.close();
  191. }
  192. }
  193. protected void assertMacroContents(POIDataSamples samples, VBAMacroReader r) throws IOException {
  194. assertNotNull(r);
  195. Map<String,String> contents = r.readMacros();
  196. assertNotNull(contents);
  197. assertFalse("Found 0 macros", contents.isEmpty());
  198. /*
  199. assertEquals(5, contents.size());
  200. // Check the ones without scripts
  201. String[] noScripts = new String[] { "ThisWorkbook",
  202. "Sheet1", "Sheet2", "Sheet3" };
  203. for (String entry : noScripts) {
  204. assertTrue(entry, contents.containsKey(entry));
  205. String content = contents.get(entry);
  206. assertContains(content, "Attribute VB_Exposed = True");
  207. assertContains(content, "Attribute VB_Customizable = True");
  208. assertContains(content, "Attribute VB_TemplateDerived = False");
  209. assertContains(content, "Attribute VB_GlobalNameSpace = False");
  210. assertContains(content, "Attribute VB_Exposed = True");
  211. }
  212. */
  213. // Check the script one
  214. assertContains(contents, "Module1");
  215. String content = contents.get("Module1");
  216. assertNotNull(content);
  217. assertContains(content, "Attribute VB_Name = \"Module1\"");
  218. //assertContains(content, "Attribute TestMacro.VB_Description = \"This is a test macro\"");
  219. // And the macro itself
  220. String testMacroNoSub = expectedMacroContents.get(samples);
  221. assertContains(content, testMacroNoSub);
  222. }
  223. @Test
  224. public void bug59830() throws IOException {
  225. //test file is "609751.xls" in govdocs1
  226. File f = POIDataSamples.getSpreadSheetInstance().getFile("59830.xls");
  227. VBAMacroReader r = new VBAMacroReader(f);
  228. Map<String, String> macros = r.readMacros();
  229. assertNotNull(macros.get("Module20"));
  230. assertContains(macros.get("Module20"), "here start of superscripting");
  231. }
  232. // This test is written as expected-to-fail and should be rewritten
  233. // as expected-to-pass when the bug is fixed.
  234. @Test
  235. public void bug59858() throws IOException {
  236. try {
  237. fromFile(POIDataSamples.getSpreadSheetInstance(), "59858.xls");
  238. testPassesNow(59858);
  239. } catch (IOException e) {
  240. if (e.getMessage().matches("Module offset for '.+' was never read.")) {
  241. //e.printStackTrace();
  242. // NPE when reading module.offset in VBAMacroReader.readMacros (approx line 258)
  243. skipTest(e);
  244. } else {
  245. // something unexpected failed
  246. throw e;
  247. }
  248. }
  249. }
  250. @Test
  251. public void bug60158() throws IOException {
  252. File f = POIDataSamples.getDocumentInstance().getFile("60158.docm");
  253. VBAMacroReader r = new VBAMacroReader(f);
  254. Map<String, String> macros = r.readMacros();
  255. assertNotNull(macros.get("NewMacros"));
  256. assertContains(macros.get("NewMacros"), "' dirty");
  257. }
  258. }