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

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