1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
155
156
157
158
159
160
161
162
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
173
174
175
176
177
178
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
189
190
191
192
193
194
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 }