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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.stress;
  16. import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
  17. import static org.junit.jupiter.api.Assertions.assertEquals;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assertions.assertThrows;
  20. import static org.junit.jupiter.api.Assertions.assertTrue;
  21. import static org.junit.jupiter.api.Assertions.fail;
  22. import java.io.BufferedInputStream;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Set;
  30. import java.util.stream.Stream;
  31. import org.apache.poi.POIDataSamples;
  32. import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
  33. import org.apache.tools.ant.DirectoryScanner;
  34. import org.junit.jupiter.api.function.Executable;
  35. import org.junit.jupiter.api.parallel.Execution;
  36. import org.junit.jupiter.api.parallel.ExecutionMode;
  37. import org.junit.jupiter.params.ParameterizedTest;
  38. import org.junit.jupiter.params.provider.Arguments;
  39. import org.junit.jupiter.params.provider.MethodSource;
  40. import org.opentest4j.AssertionFailedError;
  41. /**
  42. * This is an integration test which performs various actions on all stored test-files and tries
  43. * to reveal problems which are introduced, but not covered (yet) by unit tests.
  44. *
  45. * This test looks for any file under the test-data directory and tries to do some useful
  46. * processing with it based on its type.
  47. *
  48. * The test is implemented as a junit {@link ParameterizedTest} test, which leads
  49. * to one test-method call for each file (currently around 950 files are handled).
  50. *
  51. * There is a mapping of extension to implementations of the interface
  52. * {@link FileHandler} which defines how the file is loaded and which actions are
  53. * tried with the file.
  54. *
  55. * The test can be expanded by adding more actions to the FileHandlers, this automatically
  56. * applies the action to any such file in our test-data repository.
  57. *
  58. * There is also a list of files that should actually fail.
  59. *
  60. * Note: It is also a test-failure if a file that is expected to fail now actually works,
  61. * i.e. if a bug was fixed in POI itself, the file should be removed from the expected-failures
  62. * here as well! This is to ensure that files that should not work really do not work, e.g.
  63. * that we do not remove expected sanity checks.
  64. */
  65. // also need to set JVM parameter: -Djunit.jupiter.execution.parallel.enabled=true
  66. @Execution(ExecutionMode.CONCURRENT)
  67. public class TestAllFiles {
  68. private static final String DEFAULT_TEST_DATA_PATH = "test-data";
  69. public static final File ROOT_DIR;
  70. static {
  71. File dir = new File(System.getProperty(POIDataSamples.TEST_PROPERTY, DEFAULT_TEST_DATA_PATH));
  72. if (!dir.exists()) {
  73. dir = new File(System.getProperty(POIDataSamples.TEST_PROPERTY, "../" + DEFAULT_TEST_DATA_PATH));
  74. }
  75. ROOT_DIR = dir;
  76. }
  77. public static final String[] SCAN_EXCLUDES = {
  78. "**/.svn/**",
  79. "lost+found",
  80. "**/.git/**",
  81. //the DocType (DTD) declaration causes this to fail
  82. "**/ExternalEntityInText.docx",
  83. // exclude files failing on windows nodes, because of limited JCE policies
  84. "document/bug53475-password-is-pass.docx",
  85. "poifs/60320-protected.xlsx",
  86. "poifs/protected_sha512.xlsx",
  87. "poifs/60320-protected.xlsx",
  88. "poifs/protected_sha512.xlsx",
  89. };
  90. // cheap workaround of skipping the few problematic files
  91. public static final String[] SCAN_EXCLUDES_NOSCRATCHPAD = {
  92. "**/.svn/**",
  93. "lost+found",
  94. "**/.git/**",
  95. "**/ExternalEntityInText.docx", //the DocType (DTD) declaration causes this to fail
  96. "**/right-to-left.xlsx", //the threaded comments in this file cause XSSF clone to fail
  97. "document/word2.doc",
  98. "document/cpansearch.perl.org_src_tobyink_acme-rundoc-0.001_word-lib_hello_world.docm",
  99. "document/Fuzzed.doc",
  100. "hpsf/Test0313rur.adm",
  101. "spreadsheet/43493.xls",
  102. "spreadsheet/44958.xls",
  103. "spreadsheet/44958_1.xls",
  104. "spreadsheet/46904.xls",
  105. "spreadsheet/51832.xls",
  106. "spreadsheet/60284.xls",
  107. "spreadsheet/testArraysAndTables.xls",
  108. "spreadsheet/testEXCEL_3.xls",
  109. "spreadsheet/testEXCEL_4.xls",
  110. "poifs/unknown_properties.msg",
  111. // exclude files failing on windows nodes, because of limited JCE policies
  112. "document/bug53475-password-is-pass.docx",
  113. "poifs/60320-protected.xlsx",
  114. "poifs/protected_sha512.xlsx",
  115. "poifs/60320-protected.xlsx",
  116. "poifs/protected_sha512.xlsx",
  117. };
  118. private static final Set<String> EXPECTED_FAILURES = StressTestUtils.unmodifiableHashSet(
  119. "document/truncated62886.docx"
  120. );
  121. public static Stream<Arguments> allfiles(String testName) throws IOException {
  122. StressMap sm = new StressMap();
  123. sm.load(new File(ROOT_DIR, "spreadsheet/stress.xls"));
  124. boolean noScratch = Boolean.getBoolean("scratchpad.ignore");
  125. DirectoryScanner scanner = new DirectoryScanner();
  126. scanner.setBasedir(ROOT_DIR);
  127. scanner.setExcludes(noScratch ? SCAN_EXCLUDES_NOSCRATCHPAD : SCAN_EXCLUDES);
  128. scanner.scan();
  129. final List<Arguments> result = new ArrayList<>(100);
  130. for (String file : scanner.getIncludedFiles()) {
  131. // avoid running on files leftover from previous failed runs
  132. // or being created by tests run in parallel
  133. if(file.endsWith("-saved.xls") || file.endsWith("TestHPSFWritingFunctionality.doc")) {
  134. continue;
  135. }
  136. for (FileHandlerKnown handler : sm.getHandler(file)) {
  137. ExcInfo info1 = sm.getExcInfo(file, testName, handler);
  138. if (info1 == null || info1.isValid(testName, handler.name())) {
  139. result.add(Arguments.of(
  140. file,
  141. handler,
  142. (info1 != null) ? info1.getPassword() : null,
  143. (info1 != null) ? info1.getExClazz() : null,
  144. (info1 != null) ? info1.getExMessage() : null
  145. ));
  146. }
  147. }
  148. }
  149. return result.stream();
  150. }
  151. public static Stream<Arguments> extractFiles() throws IOException {
  152. return allfiles("extract");
  153. }
  154. @ParameterizedTest(name = "Extracting - #{index} {0} {1}")
  155. @MethodSource("extractFiles")
  156. void handleExtracting(String file, FileHandlerKnown handler, String password, Class<? extends Throwable> exClass, String exMessage) throws IOException {
  157. String threadName = Thread.currentThread().getName();
  158. try {
  159. Thread.currentThread().setName("Extracting - " + file + " - " + handler);
  160. if (StressTestUtils.excludeFile(file, EXPECTED_FAILURES))
  161. return;
  162. System.out.println("Running extractFiles on " + file);
  163. FileHandler fileHandler = handler.getHandler();
  164. assertNotNull(fileHandler, "Did not find a handler for file " + file);
  165. Executable exec = () -> fileHandler.handleExtracting(new File(ROOT_DIR, file));
  166. verify(file, exec, exClass, exMessage, password, fileHandler);
  167. } finally {
  168. Thread.currentThread().setName(threadName);
  169. }
  170. }
  171. public static Stream<Arguments> handleFiles() throws IOException {
  172. return allfiles("handle");
  173. }
  174. @ParameterizedTest(name = "#{index} {0} {1}")
  175. @MethodSource("handleFiles")
  176. void handleFile(String file, FileHandlerKnown handler, String password, Class<? extends Throwable> exClass, String exMessage) throws IOException {
  177. String threadName = Thread.currentThread().getName();
  178. try {
  179. Thread.currentThread().setName("Handle - " + file + " - " + handler);
  180. System.out.println("Running handleFiles on "+file);
  181. FileHandler fileHandler = handler.getHandler();
  182. assertNotNull(fileHandler, "Did not find a handler for file " + file);
  183. try (InputStream stream = new BufferedInputStream(new FileInputStream(new File(ROOT_DIR, file)), 64 * 1024)) {
  184. Executable exec = () -> fileHandler.handleFile(stream, file);
  185. verify(file, exec, exClass, exMessage, password, fileHandler);
  186. }
  187. } finally {
  188. Thread.currentThread().setName(threadName);
  189. }
  190. }
  191. public static Stream<Arguments> handleAdditionals() throws IOException {
  192. return allfiles("additional");
  193. }
  194. @ParameterizedTest(name = "Additional - #{index} {0} {1}")
  195. @MethodSource("handleAdditionals")
  196. void handleAdditional(String file, FileHandlerKnown handler, String password, Class<? extends Throwable> exClass, String exMessage) {
  197. String threadName = Thread.currentThread().getName();
  198. try {
  199. Thread.currentThread().setName("Additional - " + file + " - " + handler);
  200. System.out.println("Running additionals on "+file);
  201. FileHandler fileHandler = handler.getHandler();
  202. assertNotNull(fileHandler, "Did not find a handler for file " + file);
  203. Executable exec = () -> fileHandler.handleAdditional(new File(ROOT_DIR, file));
  204. verify(file, exec, exClass, exMessage, password, fileHandler);
  205. } finally {
  206. Thread.currentThread().setName(threadName);
  207. }
  208. }
  209. @SuppressWarnings("unchecked")
  210. private static void verify(String file, Executable exec, Class<? extends Throwable> exClass, String exMessage, String password,
  211. FileHandler fileHandler) {
  212. final String errPrefix = file.replace("\\", "/") + " - failed for handler " + fileHandler.getClass().getSimpleName() + ": ";
  213. // this also removes the password for non encrypted files
  214. Biff8EncryptionKey.setCurrentUserPassword(password);
  215. if (exClass != null && AssertionFailedError.class.isAssignableFrom(exClass)) {
  216. try {
  217. exec.execute();
  218. fail(errPrefix + "Expected failed assertion " + exClass + " and message " + exMessage);
  219. } catch (AssertionFailedError e) {
  220. String actMsg = pathReplace(e.getMessage());
  221. assertEquals(exMessage, actMsg, errPrefix);
  222. } catch (Throwable e) {
  223. fail(errPrefix + "Unexpected exception, expected " + exClass + " and message " + exMessage, e);
  224. }
  225. } else if (exClass != null) {
  226. Exception e = assertThrows((Class<? extends Exception>)exClass, exec, errPrefix + " expected " + exClass);
  227. String actMsg = pathReplace(e.getMessage());
  228. // perform special handling of NullPointerException as
  229. // JDK started to add more information in some newer JDK, so
  230. // it sometimes has a message and sometimes not!
  231. if (NullPointerException.class.isAssignableFrom(exClass)) {
  232. if (actMsg != null) {
  233. assertTrue(actMsg.contains(exMessage), errPrefix + "Message: "+actMsg+" - didn't contain: "+exMessage);
  234. }
  235. } else {
  236. // verify that message is either null for both or set for both
  237. assertTrue(actMsg != null || isBlank(exMessage),
  238. errPrefix + " for " + exClass + " expected message '" + exMessage + "' but had '" + actMsg + "'");
  239. if (actMsg != null) {
  240. assertTrue(actMsg.contains(exMessage),
  241. errPrefix + "Message: " + actMsg + " - didn't contain: " + exMessage);
  242. }
  243. }
  244. } else {
  245. assertDoesNotThrow(exec, errPrefix);
  246. }
  247. }
  248. private static boolean isBlank(final String str) {
  249. if (str != null) {
  250. final int strLen = str.length();
  251. for (int i = 0; i < strLen; i++) {
  252. if (!Character.isWhitespace(str.charAt(i))) {
  253. return false;
  254. }
  255. }
  256. }
  257. return true;
  258. }
  259. private static String pathReplace(String msg) {
  260. if (msg == null) return null;
  261. // Windows path replacement
  262. msg = msg.replace('\\', '/');
  263. // Adjust file paths to remove unwanted file path info.
  264. int filePathIndex = msg.indexOf(ROOT_DIR.toString());
  265. if (filePathIndex > 0) {
  266. int testDataDirectoryIndex = msg.indexOf(DEFAULT_TEST_DATA_PATH);
  267. msg = msg.substring(0, filePathIndex) + msg.substring(testDataDirectoryIndex);
  268. }
  269. return msg;
  270. }
  271. }