|
| 1 | +/* |
| 2 | +This file is part of the iText (R) project. |
| 3 | +Copyright (c) 1998-2022 iText Group NV |
| 4 | +Authors: iText 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 | +using System; |
| 24 | +using System.Collections.Generic; |
| 25 | +using System.IO; |
| 26 | +using iText.Commons.Exceptions; |
| 27 | +using iText.Test; |
| 28 | + |
| 29 | +namespace iText.Commons.Utils { |
| 30 | + public class ZipFileReaderTest : ExtendedITextTest { |
| 31 | + private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 32 | + .CurrentContext.TestDirectory) + "/resources/itext/commons/utils/ZipFileReaderTest/"; |
| 33 | + |
| 34 | + [NUnit.Framework.Test] |
| 35 | + public virtual void ConstructorWithNullPathTest() { |
| 36 | + Exception ex = NUnit.Framework.Assert.Catch(typeof(System.IO.IOException), () => new ZipFileReader(null)); |
| 37 | + NUnit.Framework.Assert.AreEqual(CommonsExceptionMessageConstant.FILE_NAME_CAN_NOT_BE_NULL, ex.Message); |
| 38 | + } |
| 39 | + |
| 40 | + [NUnit.Framework.Test] |
| 41 | + public virtual void ConstructorWithInvalidPathTest() { |
| 42 | + NUnit.Framework.Assert.Catch(typeof(Exception), () => new ZipFileReader("invalidPath")); |
| 43 | + } |
| 44 | + |
| 45 | + [NUnit.Framework.Test] |
| 46 | + public virtual void ConstructorWithNonZipPathTest() { |
| 47 | + NUnit.Framework.Assert.Catch(typeof(Exception), () => new ZipFileReader(SOURCE_FOLDER + "firstFile.txt")); |
| 48 | + } |
| 49 | + |
| 50 | + [NUnit.Framework.Test] |
| 51 | + public virtual void GetFileNamesFromEmptyZipTest() { |
| 52 | + using (ZipFileReader fileReader = new ZipFileReader(SOURCE_FOLDER + "emptyZip.zip")) { |
| 53 | + ICollection<String> nameSet = fileReader.GetFileNames(); |
| 54 | + NUnit.Framework.Assert.IsTrue(nameSet.IsEmpty()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + [NUnit.Framework.Test] |
| 59 | + public virtual void GetFileNamesFromZipTest() { |
| 60 | + using (ZipFileReader fileReader = new ZipFileReader(SOURCE_FOLDER + "archive.zip")) { |
| 61 | + ICollection<String> nameSet = fileReader.GetFileNames(); |
| 62 | + NUnit.Framework.Assert.IsNotNull(nameSet); |
| 63 | + NUnit.Framework.Assert.AreEqual(6, nameSet.Count); |
| 64 | + NUnit.Framework.Assert.IsTrue(nameSet.Contains("firstFile.txt")); |
| 65 | + NUnit.Framework.Assert.IsTrue(nameSet.Contains("secondFile.txt")); |
| 66 | + NUnit.Framework.Assert.IsTrue(nameSet.Contains("subfolder/thirdFile.txt")); |
| 67 | + NUnit.Framework.Assert.IsTrue(nameSet.Contains("subfolder/fourthFile.txt")); |
| 68 | + NUnit.Framework.Assert.IsTrue(nameSet.Contains("subfolder/subsubfolder/fifthFile.txt")); |
| 69 | + NUnit.Framework.Assert.IsTrue(nameSet.Contains("subfolder/subsubfolder/sixthFile.txt")); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + [NUnit.Framework.Test] |
| 74 | + public virtual void ReadFromZipWithNullPathTest() { |
| 75 | + using (ZipFileReader reader = new ZipFileReader(SOURCE_FOLDER + "archive.zip")) { |
| 76 | + Exception ex = NUnit.Framework.Assert.Catch(typeof(System.IO.IOException), () => reader.ReadFromZip(null)); |
| 77 | + NUnit.Framework.Assert.AreEqual(CommonsExceptionMessageConstant.FILE_NAME_CAN_NOT_BE_NULL, ex.Message); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + [NUnit.Framework.Test] |
| 82 | + public virtual void ReadFromZipWithNotExistingPathTest() { |
| 83 | + String fileName = "name"; |
| 84 | + using (ZipFileReader reader = new ZipFileReader(SOURCE_FOLDER + "archive.zip")) { |
| 85 | + Exception ex = NUnit.Framework.Assert.Catch(typeof(System.IO.IOException), () => reader.ReadFromZip(fileName |
| 86 | + )); |
| 87 | + NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(CommonsExceptionMessageConstant.ZIP_ENTRY_NOT_FOUND |
| 88 | + , fileName), ex.Message); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + [NUnit.Framework.Test] |
| 93 | + public virtual void ReadFromZipWithInvalidPathTest() { |
| 94 | + String fileName = "thirdFile.txt"; |
| 95 | + using (ZipFileReader reader = new ZipFileReader(SOURCE_FOLDER + "archive.zip")) { |
| 96 | + Exception ex = NUnit.Framework.Assert.Catch(typeof(System.IO.IOException), () => reader.ReadFromZip(fileName |
| 97 | + )); |
| 98 | + NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(CommonsExceptionMessageConstant.ZIP_ENTRY_NOT_FOUND |
| 99 | + , fileName), ex.Message); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [NUnit.Framework.Test] |
| 104 | + public virtual void ReadFromZipWithPathAtRootTest() { |
| 105 | + using (ZipFileReader reader = new ZipFileReader(SOURCE_FOLDER + "archive.zip")) { |
| 106 | + using (Stream inputStream = reader.ReadFromZip("firstFile.txt")) { |
| 107 | + NUnit.Framework.Assert.IsNotNull(inputStream); |
| 108 | + NUnit.Framework.Assert.AreEqual("1", ConvertInputStreamToString(inputStream)); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + [NUnit.Framework.Test] |
| 114 | + public virtual void ReadFromZipWithFileInSubFolderTest() { |
| 115 | + using (ZipFileReader reader = new ZipFileReader(SOURCE_FOLDER + "archive.zip")) { |
| 116 | + using (Stream inputStream = reader.ReadFromZip("subfolder/thirdFile.txt")) { |
| 117 | + NUnit.Framework.Assert.IsNotNull(inputStream); |
| 118 | + NUnit.Framework.Assert.AreEqual("3", ConvertInputStreamToString(inputStream)); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + [NUnit.Framework.Test] |
| 124 | + public virtual void ReadFromZipWithFileInSubSubFolderPathTest() { |
| 125 | + using (ZipFileReader reader = new ZipFileReader(SOURCE_FOLDER + "archive.zip")) { |
| 126 | + using (Stream inputStream = reader.ReadFromZip("subfolder/subsubfolder/fifthFile.txt")) { |
| 127 | + NUnit.Framework.Assert.IsNotNull(inputStream); |
| 128 | + NUnit.Framework.Assert.AreEqual("5", ConvertInputStreamToString(inputStream)); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + [NUnit.Framework.Test] |
| 134 | + public virtual void ReadFromZipWithClosedReaderTest() { |
| 135 | + ZipFileReader reader = new ZipFileReader(SOURCE_FOLDER + "archive.zip"); |
| 136 | + reader.Dispose(); |
| 137 | + NUnit.Framework.Assert.Catch(typeof(InvalidOperationException), () => reader.ReadFromZip("subfolder/subsubfolder/fifthFile.txt" |
| 138 | + )); |
| 139 | + } |
| 140 | + |
| 141 | + private static String ConvertInputStreamToString(Stream inputStream) { |
| 142 | + using (MemoryStream result = new MemoryStream()) { |
| 143 | + byte[] buffer = new byte[1024]; |
| 144 | + int length; |
| 145 | + while ((length = inputStream.Read(buffer)) != -1) { |
| 146 | + result.Write(buffer, 0, length); |
| 147 | + } |
| 148 | + result.Flush(); |
| 149 | + return EncodingUtil.ConvertToString(result.ToArray(), "UTF-8"); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments