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.

BaseTestPPTIterating.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.hslf.dev;
  16. import static org.junit.jupiter.api.Assertions.assertNotNull;
  17. import static org.junit.jupiter.api.Assertions.assertThrows;
  18. import static org.junit.jupiter.api.Assertions.assertTrue;
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.PrintStream;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.HashMap;
  25. import java.util.HashSet;
  26. import java.util.List;
  27. import java.util.Locale;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import java.util.stream.Stream;
  31. import org.apache.poi.POIDataSamples;
  32. import org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException;
  33. import org.apache.poi.hslf.exceptions.OldPowerPointFormatException;
  34. import org.apache.poi.util.IOUtils;
  35. import org.apache.commons.io.output.NullPrintStream;
  36. import org.junit.jupiter.api.AfterEach;
  37. import org.junit.jupiter.api.BeforeEach;
  38. import org.junit.jupiter.api.parallel.Isolated;
  39. import org.junit.jupiter.params.ParameterizedTest;
  40. import org.junit.jupiter.params.provider.Arguments;
  41. import org.junit.jupiter.params.provider.MethodSource;
  42. @Isolated // this test changes global static BYTE_ARRAY_MAX_OVERRIDE
  43. public abstract class BaseTestPPTIterating {
  44. static final Set<String> OLD_FILES = new HashSet<>(Arrays.asList(
  45. "PPT95.ppt", "pp40only.ppt"
  46. ));
  47. static final Set<String> ENCRYPTED_FILES = new HashSet<>(Arrays.asList(
  48. "cryptoapi-proc2356.ppt",
  49. "Password_Protected-np-hello.ppt",
  50. "Password_Protected-56-hello.ppt",
  51. "Password_Protected-hello.ppt",
  52. "ppt_with_png_encrypted.ppt"
  53. ));
  54. static final Map<String,Class<? extends Throwable>> EXCLUDED = new HashMap<>();
  55. static {
  56. EXCLUDED.put("clusterfuzz-testcase-minimized-POIHSLFFuzzer-6416153805979648.ppt", Exception.class);
  57. EXCLUDED.put("clusterfuzz-testcase-minimized-POIHSLFFuzzer-6710128412590080.ppt", RuntimeException.class);
  58. EXCLUDED.put("clusterfuzz-testcase-minimized-POIFuzzer-5429732352851968.ppt", FileNotFoundException.class);
  59. EXCLUDED.put("clusterfuzz-testcase-minimized-POIFuzzer-5681320547975168.ppt", FileNotFoundException.class);
  60. EXCLUDED.put("clusterfuzz-testcase-minimized-POIHSLFFuzzer-5962760801091584.ppt", RuntimeException.class);
  61. EXCLUDED.put("clusterfuzz-testcase-minimized-POIHSLFFuzzer-5231088823566336.ppt", FileNotFoundException.class);
  62. EXCLUDED.put("clusterfuzz-testcase-minimized-POIFuzzer-6411649193738240.ppt", FileNotFoundException.class);
  63. EXCLUDED.put("clusterfuzz-testcase-minimized-POIHSLFFuzzer-4838893004128256.ppt", FileNotFoundException.class);
  64. }
  65. public static Stream<Arguments> files() {
  66. String dataDirName = System.getProperty(POIDataSamples.TEST_PROPERTY);
  67. if(dataDirName == null) {
  68. dataDirName = "test-data";
  69. }
  70. List<Arguments> files = new ArrayList<>();
  71. findFile(files, dataDirName + "/slideshow");
  72. return files.stream();
  73. }
  74. private final PrintStream save = System.out;
  75. @BeforeEach
  76. void setUpBase() {
  77. // set a higher max allocation limit as some test-files require more
  78. IOUtils.setByteArrayMaxOverride(5*1024*1024);
  79. // redirect standard out during the test to avoid spamming the console with output
  80. System.setOut(NullPrintStream.INSTANCE);
  81. }
  82. @AfterEach
  83. void tearDownBase() {
  84. System.setOut(save);
  85. // reset
  86. IOUtils.setByteArrayMaxOverride(-1);
  87. }
  88. private static void findFile(List<Arguments> list, String dir) {
  89. File dirFile = new File(dir);
  90. assertTrue(dirFile.exists(), "Directory does not exist: " + dirFile.getAbsolutePath());
  91. assertTrue(dirFile.isDirectory(), "Not a directory: " + dirFile.getAbsolutePath());
  92. String[] files = dirFile.list((arg0, arg1) -> arg1.toLowerCase(Locale.ROOT).endsWith(".ppt"));
  93. assertNotNull(files, "Did not find any ppt files in directory " + dir);
  94. for(String file : files) {
  95. list.add(Arguments.of(new File(dir, file)));
  96. }
  97. }
  98. @ParameterizedTest
  99. @MethodSource("files")
  100. void testAllFiles(File file) throws Exception {
  101. String fileName = file.getName();
  102. Class<? extends Throwable> t = null;
  103. if (EXCLUDED.containsKey(fileName)) {
  104. t = EXCLUDED.get(fileName);
  105. } else if (getFailedOldFiles().contains(fileName)) {
  106. t = OldPowerPointFormatException.class;
  107. } else if (getFailedEncryptedFiles().contains(fileName)) {
  108. t = EncryptedPowerPointFileException.class;
  109. }
  110. if (t == null) {
  111. runOneFile(file);
  112. } else {
  113. assertThrows(t, () -> runOneFile(file));
  114. }
  115. }
  116. abstract void runOneFile(File pFile) throws Exception;
  117. protected Set<String> getFailedEncryptedFiles() {
  118. return ENCRYPTED_FILES;
  119. }
  120. protected Set<String> getFailedOldFiles() {
  121. return OLD_FILES;
  122. }
  123. }