|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2024 Apryse Group NV |
| 4 | + Authors: Apryse Software. |
| 5 | +
|
| 6 | + This program is offered under a commercial and under the AGPL license. |
| 7 | + For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. |
| 8 | +
|
| 9 | + AGPL licensing: |
| 10 | + This program is free software: you can redistribute it and/or modify |
| 11 | + it under the terms of the GNU Affero General Public License as published by |
| 12 | + the Free Software Foundation, either version 3 of the License, or |
| 13 | + (at your option) any later version. |
| 14 | +
|
| 15 | + This program is distributed in the hope that it will be useful, |
| 16 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + GNU Affero General Public License for more details. |
| 19 | +
|
| 20 | + You should have received a copy of the GNU Affero General Public License |
| 21 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | +package com.itextpdf.io.util; |
| 24 | + |
| 25 | +import com.itextpdf.commons.utils.FileUtil; |
| 26 | +import com.itextpdf.commons.utils.ZipFileReader; |
| 27 | +import com.itextpdf.io.source.DeflaterOutputStream; |
| 28 | +import com.itextpdf.test.ExtendedITextTest; |
| 29 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.experimental.categories.Category; |
| 34 | + |
| 35 | +import java.io.InputStream; |
| 36 | +import java.io.OutputStream; |
| 37 | + |
| 38 | +@Category(IntegrationTest.class) |
| 39 | +public class ZlibUtilTest extends ExtendedITextTest { |
| 40 | + |
| 41 | + private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/io/util/"; |
| 42 | + private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/io/util/"; |
| 43 | + |
| 44 | + @Before |
| 45 | + public void setUp() { |
| 46 | + createOrClearDestinationFolder(DESTINATION_FOLDER); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void arrayIndexOutOfBoundsDeflateTest() throws Exception { |
| 51 | + // Test file is taken from https://issues.jenkins.io/browse/JENKINS-19473 |
| 52 | + // Unzip test file first |
| 53 | + try (ZipFileReader reader = new ZipFileReader(SOURCE_FOLDER + "jzlib.zip"); |
| 54 | + InputStream is = reader.readFromZip("jzlib.fail"); |
| 55 | + OutputStream os = FileUtil.getFileOutputStream(DESTINATION_FOLDER + "jzlib.fail")) { |
| 56 | + byte[] buf = new byte[8192]; |
| 57 | + int length; |
| 58 | + while ((length = is.read(buf)) != -1) { |
| 59 | + os.write(buf, 0, length); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Deflate it |
| 64 | + try (InputStream is = FileUtil.getInputStreamForFile(DESTINATION_FOLDER + "jzlib.fail"); |
| 65 | + OutputStream os = FileUtil.getFileOutputStream(DESTINATION_FOLDER + "jzlib.fail.zz"); |
| 66 | + // -1 stands for default compression |
| 67 | + DeflaterOutputStream zip = new DeflaterOutputStream(os, -1)) { |
| 68 | + byte[] buf = new byte[8192]; |
| 69 | + int length; |
| 70 | + while ((length = is.read(buf)) != -1) { |
| 71 | + zip.write(buf, 0, length); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + Assert.assertTrue(FileUtil.fileExists(DESTINATION_FOLDER + "jzlib.fail.zz")); |
| 76 | + Assert.assertTrue(FileUtil.isFileNotEmpty(DESTINATION_FOLDER + "jzlib.fail.zz")); |
| 77 | + } |
| 78 | +} |
0 commit comments