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  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   * Tests for {@link PngChunkIccp}.
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); // null
56          bytes.add((byte) 0); // 0=deflate compression method
57  
58          // generate some 100 bytes of dummy data
59          final byte[] uncompressedData = new byte[100];
60          IntStream.range(0, 100).forEach(i -> {
61              uncompressedData[i] = (byte) (i + 1); // dummy data
62          });
63          try (ByteArrayOutputStream baos = new ByteArrayOutputStream(100)) {
64              // compress the dummy data with deflate
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              // gather everything, except for the compressed data
73              for (int i = 0; i < bytes.size(); ++i) {
74                  data[i] = bytes.get(i).byteValue();
75              }
76              // gather the compressed data
77              IntStream.range(0, compressedData.length).forEach(i -> data[bytes.size() + i] = compressedData[i]);
78              // create the chunk
79              final PngChunkIccp chunk = new PngChunkIccp(data.length, chunkType, 0, data);
80              assertArrayEquals(uncompressedData, chunk.getUncompressedProfile());
81          }
82      }
83  }