Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TestAllFiles.java 20KB

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