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