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.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
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
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
151
152
153
154
155
156
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
168
169
170
171
172
173
174
175
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
187
188
189
190
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 }