1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.png.chunks;
18
19 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.stream.IntStream;
28 import java.util.zip.Deflater;
29 import java.util.zip.DeflaterOutputStream;
30
31 import org.apache.commons.imaging.ImagingConstants;
32 import org.apache.commons.imaging.ImagingException;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38 public class PngChunkIccpTest {
39
40 private static final int chunkType = 1766015824;
41
42 @Test
43 public void testErrorOnNoProfileName() {
44 final byte[] data = ImagingConstants.EMPTY_BYTE_ARRAY;
45 assertThrows(ImagingException.class, () -> new PngChunkIccp(0, chunkType, 0, data));
46 }
47
48 @Test
49 public void testParsingIccpChunk() throws ImagingException, IOException {
50 final List<Byte> bytes = new ArrayList<>();
51 final String profileName = "my-profile-01";
52 for (final byte b : profileName.getBytes(StandardCharsets.ISO_8859_1)) {
53 bytes.add(b);
54 }
55 bytes.add((byte) 0);
56 bytes.add((byte) 0);
57
58
59 final byte[] uncompressedData = new byte[100];
60 IntStream.range(0, 100).forEach(i -> {
61 uncompressedData[i] = (byte) (i + 1);
62 });
63 try (ByteArrayOutputStream baos = new ByteArrayOutputStream(100)) {
64
65 final Deflater def = new Deflater();
66 try (DeflaterOutputStream ios = new DeflaterOutputStream(baos, def)) {
67 ios.write(uncompressedData);
68 }
69 baos.flush();
70 final byte[] compressedData = baos.toByteArray();
71 final byte[] data = new byte[bytes.size() + compressedData.length];
72
73 for (int i = 0; i < bytes.size(); ++i) {
74 data[i] = bytes.get(i).byteValue();
75 }
76
77 IntStream.range(0, compressedData.length).forEach(i -> data[bytes.size() + i] = compressedData[i]);
78
79 final PngChunkIccp chunk = new PngChunkIccp(data.length, chunkType, 0, data);
80 assertArrayEquals(uncompressedData, chunk.getUncompressedProfile());
81 }
82 }
83 }