View Javadoc
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    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.imaging.formats.jpeg;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.awt.image.BufferedImage;
25  import java.io.File;
26  import java.util.stream.Stream;
27  
28  import org.apache.commons.imaging.ImageInfo;
29  import org.apache.commons.imaging.Imaging;
30  import org.apache.commons.imaging.ImagingException;
31  import org.apache.commons.imaging.bytesource.ByteSource;
32  import org.apache.commons.imaging.common.ImageMetadata;
33  import org.apache.commons.imaging.formats.tiff.TiffImagingParameters;
34  import org.apache.commons.imaging.internal.Debug;
35  import org.apache.commons.imaging.test.TestResources;
36  import org.junit.jupiter.api.Test;
37  import org.junit.jupiter.params.ParameterizedTest;
38  import org.junit.jupiter.params.provider.MethodSource;
39  
40  public class JpegReadTest extends JpegBaseTest {
41  
42      public static Stream<File> data() throws Exception {
43          return getJpegImages().stream();
44      }
45  
46      @ParameterizedTest
47      @MethodSource("data")
48      public void test(final File imageFile) throws Exception {
49          final JpegImageParser jpegImageParser = new JpegImageParser();
50          final ImageMetadata metadata = jpegImageParser.getExifMetadata(ByteSource.file(imageFile), new TiffImagingParameters());
51          // TODO only run this tests with images that have metadata...
52          // assertNotNull(metadata);
53          Debug.debug("metadata", metadata);
54  
55          Debug.debug("ICC profile", Imaging.getIccProfile(imageFile));
56  
57          final ImageInfo imageInfo = Imaging.getImageInfo(imageFile);
58          assertNotNull(imageInfo);
59  
60          try {
61              final BufferedImage image = Imaging.getBufferedImage(imageFile);
62              assertNotNull(image);
63          } catch (final ImagingException imageReadException) {
64              assertEquals("Only sequential, baseline JPEGs are supported at the moment", imageReadException.getMessage());
65          }
66      }
67  
68      /**
69       * The JPEG image data may contain a negative number of segments, in which case the parser could throw a NegativeArraySizeException.
70       *
71       * <p>
72       * This test case verifies that we are handling that scenario, and throwing an ImageReadException instead.
73       * </p>
74       *
75       * <p>
76       * See Google OSS Fuzz issue 33458
77       * </p>
78       */
79      @Test
80      public void testUncaughtExceptionOssFuzz33458() {
81          final File file = TestResources.resourceToFile("/images/jpeg/oss-fuzz-33458/clusterfuzz-testcase-minimized-ImagingJpegFuzzer-4548690447564800");
82          final JpegImageParser parser = new JpegImageParser();
83          assertThrows(ImagingException.class, () -> parser.getBufferedImage(ByteSource.file(file), new JpegImagingParameters()));
84      }
85  
86  }