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.

TestAllFiles.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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;
  16. import static org.junit.Assert.assertFalse;
  17. import static org.junit.Assert.assertNotNull;
  18. import java.io.BufferedInputStream;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.InputStream;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import org.apache.poi.hwpf.OldWordFileFormatException;
  29. import org.apache.poi.stress.*;
  30. import org.apache.tools.ant.DirectoryScanner;
  31. import org.junit.Test;
  32. import org.junit.runner.RunWith;
  33. import org.junit.runners.Parameterized;
  34. import org.junit.runners.Parameterized.Parameter;
  35. import org.junit.runners.Parameterized.Parameters;
  36. /**
  37. * This is an integration test which performs various actions on all stored test-files and tries
  38. * to reveal problems which are introduced, but not covered (yet) by unit tests.
  39. *
  40. * This test looks for any file under the test-data directory and tries to do some useful
  41. * processing with it based on it's type.
  42. *
  43. * The test is implemented as a junit {@link Parameterized} test, which leads
  44. * to one test-method call for each file (currently around 950 files are handled).
  45. *
  46. * There is a a mapping of extension to implementations of the interface
  47. * {@link FileHandler} which defines how the file is loaded and which actions are
  48. * tried with the file.
  49. *
  50. * The test can be expanded by adding more actions to the FileHandlers, this automatically
  51. * applies the action to any such file in our test-data repository.
  52. *
  53. * There is also a list of files that should actually fail.
  54. *
  55. * Note: It is also a test-failure if a file that is expected to fail now actually works,
  56. * i.e. if a bug was fixed in POI itself, the file should be removed from the expected-failures
  57. * here as well! This is to ensure that files that should not work really do not work, e.g.
  58. * that we do not remove expected sanity checks.
  59. */
  60. @RunWith(Parameterized.class)
  61. public class TestAllFiles {
  62. private static final File ROOT_DIR = new File("test-data");
  63. // map file extensions to the actual mappers
  64. private static final Map<String, FileHandler> HANDLERS = new HashMap<String, FileHandler>();
  65. static {
  66. // Excel
  67. HANDLERS.put(".xls", new HSSFFileHandler());
  68. HANDLERS.put(".xlsx", new XSSFFileHandler());
  69. HANDLERS.put(".xlsm", new XSSFFileHandler());
  70. HANDLERS.put(".xltx", new XSSFFileHandler());
  71. HANDLERS.put(".xlsb", new XSSFFileHandler());
  72. // Word
  73. HANDLERS.put(".doc", new HWPFFileHandler());
  74. HANDLERS.put(".docx", new XWPFFileHandler());
  75. HANDLERS.put(".dotx", new XWPFFileHandler());
  76. HANDLERS.put(".docm", new XWPFFileHandler());
  77. // OpenXML4J files
  78. HANDLERS.put(".ooxml", new OPCFileHandler()); // OPCPackage
  79. HANDLERS.put(".zip", new OPCFileHandler()); // OPCPackage
  80. // Powerpoint
  81. HANDLERS.put(".ppt", new HSLFFileHandler());
  82. HANDLERS.put(".pptx", new XSLFFileHandler());
  83. HANDLERS.put(".pptm", new XSLFFileHandler());
  84. HANDLERS.put(".ppsm", new XSLFFileHandler());
  85. HANDLERS.put(".ppsx", new XSLFFileHandler());
  86. HANDLERS.put(".thmx", new XSLFFileHandler());
  87. // Outlook
  88. HANDLERS.put(".msg", new HSMFFileHandler());
  89. // Publisher
  90. HANDLERS.put(".pub", new HPBFFileHandler());
  91. // Visio - binary
  92. HANDLERS.put(".vsd", new HDGFFileHandler());
  93. // Visio - ooxml
  94. HANDLERS.put(".vsdm", new XDGFFileHandler());
  95. HANDLERS.put(".vsdx", new XDGFFileHandler());
  96. HANDLERS.put(".vssm", new XDGFFileHandler());
  97. HANDLERS.put(".vssx", new XDGFFileHandler());
  98. HANDLERS.put(".vstm", new XDGFFileHandler());
  99. HANDLERS.put(".vstx", new XDGFFileHandler());
  100. // POIFS
  101. HANDLERS.put(".ole2", new POIFSFileHandler());
  102. // Microsoft Admin Template?
  103. HANDLERS.put(".adm", new HPSFFileHandler());
  104. // Microsoft TNEF
  105. HANDLERS.put(".dat", new HMEFFileHandler());
  106. // TODO: are these readable by some of the formats?
  107. HANDLERS.put(".shw", new NullFileHandler());
  108. HANDLERS.put(".zvi", new NullFileHandler());
  109. HANDLERS.put(".mpp", new NullFileHandler());
  110. HANDLERS.put(".qwp", new NullFileHandler());
  111. HANDLERS.put(".wps", new NullFileHandler());
  112. HANDLERS.put(".bin", new NullFileHandler());
  113. HANDLERS.put(".xps", new NullFileHandler());
  114. HANDLERS.put(".sldprt", new NullFileHandler());
  115. HANDLERS.put(".mdb", new NullFileHandler());
  116. HANDLERS.put(".vml", new NullFileHandler());
  117. // ignore some file types, images, other formats, ...
  118. HANDLERS.put(".txt", new NullFileHandler());
  119. HANDLERS.put(".pdf", new NullFileHandler());
  120. HANDLERS.put(".rtf", new NullFileHandler());
  121. HANDLERS.put(".gif", new NullFileHandler());
  122. HANDLERS.put(".html", new NullFileHandler());
  123. HANDLERS.put(".png", new NullFileHandler());
  124. HANDLERS.put(".wmf", new NullFileHandler());
  125. HANDLERS.put(".emf", new NullFileHandler());
  126. HANDLERS.put(".dib", new NullFileHandler());
  127. HANDLERS.put(".svg", new NullFileHandler());
  128. HANDLERS.put(".pict", new NullFileHandler());
  129. HANDLERS.put(".jpg", new NullFileHandler());
  130. HANDLERS.put(".wav", new NullFileHandler());
  131. HANDLERS.put(".pfx", new NullFileHandler());
  132. HANDLERS.put(".xml", new NullFileHandler());
  133. HANDLERS.put(".csv", new NullFileHandler());
  134. // map some files without extension
  135. HANDLERS.put("spreadsheet/BigSSTRecord", new NullFileHandler());
  136. HANDLERS.put("spreadsheet/BigSSTRecord2", new NullFileHandler());
  137. HANDLERS.put("spreadsheet/BigSSTRecord2CR1", new NullFileHandler());
  138. HANDLERS.put("spreadsheet/BigSSTRecord2CR2", new NullFileHandler());
  139. HANDLERS.put("spreadsheet/BigSSTRecord2CR3", new NullFileHandler());
  140. HANDLERS.put("spreadsheet/BigSSTRecord2CR4", new NullFileHandler());
  141. HANDLERS.put("spreadsheet/BigSSTRecord2CR5", new NullFileHandler());
  142. HANDLERS.put("spreadsheet/BigSSTRecord2CR6", new NullFileHandler());
  143. HANDLERS.put("spreadsheet/BigSSTRecord2CR7", new NullFileHandler());
  144. HANDLERS.put("spreadsheet/BigSSTRecordCR", new NullFileHandler());
  145. HANDLERS.put("spreadsheet/test_properties1", new NullFileHandler());
  146. }
  147. // Old Word Documents where we can at least extract some text
  148. private static final Set<String> OLD_FILES = new HashSet<String>();
  149. static {
  150. OLD_FILES.add("document/Bug49933.doc");
  151. OLD_FILES.add("document/Bug51944.doc");
  152. OLD_FILES.add("document/Word6.doc");
  153. OLD_FILES.add("document/Word6_sections.doc");
  154. OLD_FILES.add("document/Word6_sections2.doc");
  155. OLD_FILES.add("document/Word95.doc");
  156. OLD_FILES.add("document/word95err.doc");
  157. OLD_FILES.add("hpsf/TestMickey.doc");
  158. OLD_FILES.add("document/52117.doc");
  159. }
  160. private static final Set<String> EXPECTED_FAILURES = new HashSet<String>();
  161. static {
  162. // password protected files
  163. EXPECTED_FAILURES.add("spreadsheet/password.xls");
  164. EXPECTED_FAILURES.add("spreadsheet/protected_passtika.xlsx");
  165. EXPECTED_FAILURES.add("spreadsheet/51832.xls");
  166. EXPECTED_FAILURES.add("document/PasswordProtected.doc");
  167. EXPECTED_FAILURES.add("slideshow/Password_Protected-hello.ppt");
  168. EXPECTED_FAILURES.add("slideshow/Password_Protected-56-hello.ppt");
  169. EXPECTED_FAILURES.add("slideshow/Password_Protected-np-hello.ppt");
  170. EXPECTED_FAILURES.add("slideshow/cryptoapi-proc2356.ppt");
  171. //EXPECTED_FAILURES.add("document/bug53475-password-is-pass.docx");
  172. //EXPECTED_FAILURES.add("document/bug53475-password-is-solrcell.docx");
  173. EXPECTED_FAILURES.add("spreadsheet/xor-encryption-abc.xls");
  174. EXPECTED_FAILURES.add("spreadsheet/35897-type4.xls");
  175. //EXPECTED_FAILURES.add("poifs/protect.xlsx");
  176. //EXPECTED_FAILURES.add("poifs/protected_sha512.xlsx");
  177. //EXPECTED_FAILURES.add("poifs/extenxls_pwd123.xlsx");
  178. //EXPECTED_FAILURES.add("poifs/protected_agile.docx");
  179. EXPECTED_FAILURES.add("spreadsheet/58616.xlsx");
  180. // TODO: fails XMLExportTest, is this ok?
  181. EXPECTED_FAILURES.add("spreadsheet/CustomXMLMapping-singleattributenamespace.xlsx");
  182. EXPECTED_FAILURES.add("spreadsheet/55864.xlsx");
  183. EXPECTED_FAILURES.add("spreadsheet/57890.xlsx");
  184. // TODO: these fail now with some NPE/file read error because we now try to compute every value via Cell.toString()!
  185. EXPECTED_FAILURES.add("spreadsheet/44958.xls");
  186. EXPECTED_FAILURES.add("spreadsheet/44958_1.xls");
  187. EXPECTED_FAILURES.add("spreadsheet/testArraysAndTables.xls");
  188. // TODO: good to ignore?
  189. EXPECTED_FAILURES.add("spreadsheet/sample-beta.xlsx");
  190. // This is actually a spreadsheet!
  191. EXPECTED_FAILURES.add("hpsf/TestRobert_Flaherty.doc");
  192. // some files that are broken, eg Word 95, ...
  193. EXPECTED_FAILURES.add("spreadsheet/43493.xls");
  194. EXPECTED_FAILURES.add("spreadsheet/46904.xls");
  195. EXPECTED_FAILURES.add("document/56880.doc");
  196. EXPECTED_FAILURES.add("document/Bug50955.doc");
  197. EXPECTED_FAILURES.add("slideshow/PPT95.ppt");
  198. EXPECTED_FAILURES.add("openxml4j/OPCCompliance_CoreProperties_DCTermsNamespaceLimitedUseFAIL.docx");
  199. EXPECTED_FAILURES.add("openxml4j/OPCCompliance_CoreProperties_DoNotUseCompatibilityMarkupFAIL.docx");
  200. EXPECTED_FAILURES.add("openxml4j/OPCCompliance_CoreProperties_LimitedXSITypeAttribute_NotPresentFAIL.docx");
  201. EXPECTED_FAILURES.add("openxml4j/OPCCompliance_CoreProperties_LimitedXSITypeAttribute_PresentWithUnauthorizedValueFAIL.docx");
  202. EXPECTED_FAILURES.add("openxml4j/OPCCompliance_CoreProperties_OnlyOneCorePropertiesPartFAIL.docx");
  203. EXPECTED_FAILURES.add("openxml4j/OPCCompliance_CoreProperties_UnauthorizedXMLLangAttributeFAIL.docx");
  204. EXPECTED_FAILURES.add("openxml4j/OPCCompliance_DerivedPartNameFAIL.docx");
  205. EXPECTED_FAILURES.add("spreadsheet/54764-2.xlsx"); // see TestXSSFBugs.bug54764()
  206. EXPECTED_FAILURES.add("spreadsheet/54764.xlsx"); // see TestXSSFBugs.bug54764()
  207. EXPECTED_FAILURES.add("spreadsheet/Simple.xlsb");
  208. EXPECTED_FAILURES.add("poifs/unknown_properties.msg"); // POIFS properties corrupted
  209. EXPECTED_FAILURES.add("poifs/only-zero-byte-streams.ole2"); // No actual contents
  210. EXPECTED_FAILURES.add("spreadsheet/poc-xmlbomb.xlsx"); // contains xml-entity-expansion
  211. EXPECTED_FAILURES.add("spreadsheet/poc-shared-strings.xlsx"); // contains shared-string-entity-expansion
  212. // old Excel files, which we only support simple text extraction of
  213. EXPECTED_FAILURES.add("spreadsheet/testEXCEL_2.xls");
  214. EXPECTED_FAILURES.add("spreadsheet/testEXCEL_3.xls");
  215. EXPECTED_FAILURES.add("spreadsheet/testEXCEL_4.xls");
  216. EXPECTED_FAILURES.add("spreadsheet/testEXCEL_5.xls");
  217. EXPECTED_FAILURES.add("spreadsheet/testEXCEL_95.xls");
  218. // OOXML Strict is not yet supported, see bug #57699
  219. EXPECTED_FAILURES.add("spreadsheet/SampleSS.strict.xlsx");
  220. EXPECTED_FAILURES.add("spreadsheet/SimpleStrict.xlsx");
  221. EXPECTED_FAILURES.add("spreadsheet/sample.strict.xlsx");
  222. // non-TNEF files
  223. EXPECTED_FAILURES.add("ddf/Container.dat");
  224. EXPECTED_FAILURES.add("ddf/47143.dat");
  225. // will be fixed as part of bug 58760
  226. EXPECTED_FAILURES.add("spreadsheet/58760.xlsx");
  227. }
  228. private static final Set<String> IGNORED = new HashSet<String>();
  229. static {
  230. // need JDK8+ - https://bugs.openjdk.java.net/browse/JDK-8038081
  231. IGNORED.add("slideshow/42474-2.ppt");
  232. }
  233. @Parameters(name="{index}: {0} using {1}")
  234. public static Iterable<Object[]> files() {
  235. DirectoryScanner scanner = new DirectoryScanner();
  236. scanner.setBasedir(ROOT_DIR);
  237. scanner.setExcludes(new String[] { "**/.svn/**" });
  238. scanner.scan();
  239. System.out.println("Handling " + scanner.getIncludedFiles().length + " files");
  240. List<Object[]> files = new ArrayList<Object[]>();
  241. for(String file : scanner.getIncludedFiles()) {
  242. file = file.replace('\\', '/'); // ... failures/handlers lookup doesn't work on windows otherwise
  243. if (IGNORED.contains(file)) continue;
  244. FileHandler handler = HANDLERS.get(getExtension(file));
  245. files.add(new Object[] { file, handler });
  246. // for some file-types also run OPCFileHandler
  247. if(handler instanceof XSSFFileHandler ||
  248. handler instanceof XWPFFileHandler ||
  249. handler instanceof XSLFFileHandler ||
  250. handler instanceof XDGFFileHandler) {
  251. files.add(new Object[] { file, HANDLERS.get(".ooxml") });
  252. }
  253. }
  254. return files;
  255. }
  256. @Parameter(value=0)
  257. public String file;
  258. @Parameter(value=1)
  259. public FileHandler handler;
  260. @Test
  261. public void testAllFiles() throws Exception {
  262. assertNotNull("Unknown file extension for file: " + file + ": " + getExtension(file), handler);
  263. File inputFile = new File(ROOT_DIR, file);
  264. try {
  265. InputStream stream = new BufferedInputStream(new FileInputStream(inputFile), 64*1024);
  266. try {
  267. handler.handleFile(stream);
  268. assertFalse("Expected to fail for file " + file + " and handler " + handler + ", but did not fail!",
  269. OLD_FILES.contains(file));
  270. } finally {
  271. stream.close();
  272. }
  273. handler.handleExtracting(inputFile);
  274. // special cases where docx-handling breaks, but OPCPackage handling works
  275. boolean ignoredOPC = (file.endsWith(".docx") || file.endsWith(".xlsx") || file.endsWith(".xlsb")) &&
  276. handler instanceof OPCFileHandler;
  277. assertFalse("Expected to fail for file " + file + " and handler " + handler + ", but did not fail!",
  278. EXPECTED_FAILURES.contains(file) && !ignoredOPC);
  279. } catch (OldWordFileFormatException e) {
  280. // for old word files we should still support extracting text
  281. if(OLD_FILES.contains(file)) {
  282. handler.handleExtracting(inputFile);
  283. } else {
  284. // check if we expect failure for this file
  285. if(!EXPECTED_FAILURES.contains(file) && !AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.contains(file)) {
  286. System.out.println("Failed: " + file);
  287. throw new Exception("While handling " + file, e);
  288. }
  289. }
  290. } catch (Exception e) {
  291. // check if we expect failure for this file
  292. if(!EXPECTED_FAILURES.contains(file) && !AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.contains(file)) {
  293. System.out.println("Failed: " + file);
  294. throw new Exception("While handling " + file, e);
  295. }
  296. }
  297. }
  298. private static String getExtension(String file) {
  299. int pos = file.lastIndexOf('.');
  300. if(pos == -1 || pos == file.length()-1) {
  301. return file;
  302. }
  303. return file.substring(pos);
  304. }
  305. private static class NullFileHandler implements FileHandler {
  306. @Override
  307. public void handleFile(InputStream stream) throws Exception {
  308. }
  309. @Override
  310. public void handleExtracting(File file) throws Exception {
  311. }
  312. }
  313. }