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.gif;
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  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.awt.image.BufferedImage;
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.List;
29  import java.util.stream.Stream;
30  
31  import org.apache.commons.imaging.ImageInfo;
32  import org.apache.commons.imaging.Imaging;
33  import org.apache.commons.imaging.ImagingException;
34  import org.apache.commons.imaging.bytesource.ByteSource;
35  import org.apache.commons.imaging.common.ImageMetadata;
36  import org.apache.commons.imaging.test.TestResources;
37  import org.junit.jupiter.api.Test;
38  import org.junit.jupiter.params.ParameterizedTest;
39  import org.junit.jupiter.params.provider.MethodSource;
40  
41  public class GifReadTest extends AbstractGifTest {
42  
43      public static Stream<File> animatedImageData() throws Exception {
44          return getAnimatedGifImages().stream();
45      }
46  
47      public static Stream<File> data() throws Exception {
48          return getGifImages().stream();
49      }
50  
51      public static Stream<File> singleImageData() throws Exception {
52          return getGifImagesWithSingleImage().stream();
53      }
54  
55      @ParameterizedTest
56      @MethodSource("data")
57      public void testBufferedImage(final File imageFile) throws Exception {
58          final BufferedImage image = Imaging.getBufferedImage(imageFile);
59          assertNotNull(image);
60          // TODO assert more
61      }
62  
63      @ParameterizedTest
64      @MethodSource("animatedImageData")
65      public void testBufferedImagesForAnimatedImageGif(final File imageFile) throws Exception {
66          final List<BufferedImage> images = Imaging.getAllBufferedImages(imageFile);
67          assertTrue(images.size() > 1);
68      }
69  
70      @ParameterizedTest
71      @MethodSource("singleImageData")
72      public void testBufferedImagesForSingleImageGif(final File imageFile) throws Exception {
73          final List<BufferedImage> images = Imaging.getAllBufferedImages(imageFile);
74          assertEquals(1, images.size());
75      }
76  
77      @Test
78      public void testConvertInvalidDisposalMethodValues() {
79          assertThrows(ImagingException.class, () -> GifImageParser.createDisposalMethodFromIntValue(8));
80      }
81  
82      @Test
83      public void testConvertValidDisposalMethodValues() throws ImagingException {
84          final DisposalMethod unspecified = GifImageParser.createDisposalMethodFromIntValue(0);
85          final DisposalMethod doNotDispose = GifImageParser.createDisposalMethodFromIntValue(1);
86          final DisposalMethod restoreToBackground = GifImageParser.createDisposalMethodFromIntValue(2);
87          final DisposalMethod restoreToPrevious = GifImageParser.createDisposalMethodFromIntValue(3);
88          final DisposalMethod toBeDefined1 = GifImageParser.createDisposalMethodFromIntValue(4);
89          final DisposalMethod toBeDefined2 = GifImageParser.createDisposalMethodFromIntValue(5);
90          final DisposalMethod toBeDefined3 = GifImageParser.createDisposalMethodFromIntValue(6);
91          final DisposalMethod toBeDefined4 = GifImageParser.createDisposalMethodFromIntValue(7);
92          assertEquals(unspecified, DisposalMethod.UNSPECIFIED);
93          assertEquals(doNotDispose, DisposalMethod.DO_NOT_DISPOSE);
94          assertEquals(restoreToBackground, DisposalMethod.RESTORE_TO_BACKGROUND);
95          assertEquals(restoreToPrevious, DisposalMethod.RESTORE_TO_PREVIOUS);
96          assertEquals(toBeDefined1, DisposalMethod.TO_BE_DEFINED_1);
97          assertEquals(toBeDefined2, DisposalMethod.TO_BE_DEFINED_2);
98          assertEquals(toBeDefined3, DisposalMethod.TO_BE_DEFINED_3);
99          assertEquals(toBeDefined4, DisposalMethod.TO_BE_DEFINED_4);
100     }
101 
102     @Test
103     public void testCreateMetadataWithDisposalMethods() {
104         for (final DisposalMethod disposalMethod : DisposalMethod.values()) {
105             final GifImageMetadataItem metadataItem = new GifImageMetadataItem(0, 0, 0, disposalMethod);
106             assertEquals(disposalMethod, metadataItem.getDisposalMethod());
107         }
108     }
109 
110     @ParameterizedTest
111     @MethodSource("data")
112     public void testImageDimensions(final File imageFile) throws Exception {
113         final ImageInfo imageInfo = Imaging.getImageInfo(imageFile);
114         final GifImageMetadata metadata = (GifImageMetadata) Imaging.getMetadata(imageFile);
115         final List<BufferedImage> images = Imaging.getAllBufferedImages(imageFile);
116 
117         int width = 0;
118         int height = 0;
119         for (int i = 0; i < images.size(); i++) {
120             final BufferedImage image = images.get(i);
121             final GifImageMetadataItem metadataItem = metadata.getItems().get(i);
122             final int xOffset = metadataItem.getLeftPosition();
123             final int yOffset = metadataItem.getTopPosition();
124             width = Math.max(width, image.getWidth() + xOffset);
125             height = Math.max(height, image.getHeight() + yOffset);
126         }
127 
128         assertEquals(width, metadata.getWidth());
129         assertEquals(height, metadata.getHeight());
130         assertEquals(width, imageInfo.getWidth());
131         assertEquals(height, imageInfo.getHeight());
132     }
133 
134     @ParameterizedTest
135     @MethodSource("data")
136     public void testImageInfo(final File imageFile) throws Exception {
137         final ImageInfo imageInfo = Imaging.getImageInfo(imageFile);
138         assertNotNull(imageInfo);
139         // TODO assert more
140     }
141 
142     @ParameterizedTest
143     @MethodSource("data")
144     public void testMetadata(final File imageFile) throws IOException {
145         final ImageMetadata metadata = Imaging.getMetadata(imageFile);
146         assertNotNull(metadata);
147         assertTrue(metadata instanceof GifImageMetadata);
148         assertTrue(((GifImageMetadata) metadata).getWidth() > 0);
149         assertTrue(((GifImageMetadata) metadata).getHeight() > 0);
150         assertNotNull(metadata.getItems());
151     }
152 
153     /**
154      * The GIF image Lzw compression may contain a table with length inferior to the length of entries in the image data. Which results in an
155      * ArrayOutOfBoundsException. This verifies that instead of throwing an AOOBE, we are handling the case and informing the user why the parser failed to read
156      * it, by throwin an ImageReadException with a more descriptive message.
157      *
158      * <p>
159      * See Google OSS Fuzz issue 33464
160      * </p>
161      *
162      * @throws IOException if it fails to read the test image
163      */
164     @Test
165     public void testUncaughtExceptionOssFuzz33464() throws IOException {
166         final File file = TestResources.resourceToFile("/images/gif/oss-fuzz-33464/clusterfuzz-testcase-minimized-ImagingGifFuzzer-5174009164595200");
167         final GifImageParser parser = new GifImageParser();
168         assertThrows(ImagingException.class, () -> parser.getBufferedImage(ByteSource.file(file), new GifImagingParameters()));
169     }
170 
171     /**
172      * The GIF image data may lead to out of bound array access. This test verifies that we handle that case and raise an appropriate exception.
173      *
174      * <p>
175      * See Google OSS Fuzz issue 33501
176      * </p>
177      *
178      * @throws IOException if it fails to read the test image
179      */
180     @Test
181     public void testUncaughtExceptionOssFuzz33501() throws IOException {
182         final File file = TestResources.resourceToFile("/images/gif/oss-fuzz-33501/clusterfuzz-testcase-minimized-ImagingGifFuzzer-5914278319226880");
183         final GifImageParser parser = new GifImageParser();
184         assertThrows(ImagingException.class, () -> parser.getBufferedImage(ByteSource.file(file), new GifImagingParameters()));
185     }
186 
187     /**
188      * Test that invalid indexes are validated when accessing GIF color table array.
189      *
190      * <p>
191      * See Google OSS Fuzz issue 34185
192      * </p>
193      *
194      * @throws IOException if it fails to read the test image
195      */
196     @Test
197     public void testUncaughtExceptionOssFuzz34185() throws IOException {
198         final File file = TestResources.resourceToFile("/images/gif/IMAGING-318/clusterfuzz-testcase-minimized-ImagingGifFuzzer-5005192379629568");
199         final GifImageParser parser = new GifImageParser();
200         assertThrows(ImagingException.class, () -> parser.getBufferedImage(ByteSource.file(file), new GifImagingParameters()));
201     }
202 }