|
| 1 | +/* |
| 2 | +This file is part of the iText (R) project. |
| 3 | +Copyright (c) 1998-2025 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 | +using System; |
| 24 | +using System.IO; |
| 25 | +using iText.IO.Exceptions; |
| 26 | +using iText.Test; |
| 27 | + |
| 28 | +namespace iText.IO.Resolver.Resource { |
| 29 | + [NUnit.Framework.Category("UnitTest")] |
| 30 | + public class LimitedInputStreamTest : ExtendedITextTest { |
| 31 | + [NUnit.Framework.Test] |
| 32 | + public virtual void ReadingByteAfterFileReadingTest() { |
| 33 | + using (Stream stream = new LimitedInputStream(new LimitedInputStreamTest.TestStreamGenerator().OpenStream( |
| 34 | + ), 100)) { |
| 35 | + // The user can call the reading methods as many times as he want, and if the |
| 36 | + // stream has been read, then should not throw an ReadingByteLimitException exception |
| 37 | + for (int i = 0; i < 101; i++) { |
| 38 | + NUnit.Framework.Assert.DoesNotThrow(() => stream.Read()); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + [NUnit.Framework.Test] |
| 44 | + public virtual void ReadingByteArrayAfterFileReadingTest() { |
| 45 | + using (Stream stream = new LimitedInputStream(new LimitedInputStreamTest.TestStreamGenerator().OpenStream( |
| 46 | + ), 100)) { |
| 47 | + // The user can call the reading methods as many times as he want, and if the |
| 48 | + // stream has been read, then should not throw an ReadingByteLimitException exception |
| 49 | + NUnit.Framework.Assert.DoesNotThrow(() => stream.Read(new byte[100])); |
| 50 | + NUnit.Framework.Assert.DoesNotThrow(() => stream.Read(new byte[1])); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + [NUnit.Framework.Test] |
| 55 | + public virtual void ReadingByteArrayWithOffsetAfterFileReadingTest() { |
| 56 | + using (Stream stream = new LimitedInputStream(new LimitedInputStreamTest.TestStreamGenerator().OpenStream( |
| 57 | + ), 100)) { |
| 58 | + // The user can call the reading methods as many times as he want, and if the |
| 59 | + // stream has been read, then should not throw an ReadingByteLimitException exception |
| 60 | + NUnit.Framework.Assert.DoesNotThrow(() => { |
| 61 | + stream.JRead(new byte[100], 0, 100); |
| 62 | + } |
| 63 | + ); |
| 64 | + NUnit.Framework.Assert.DoesNotThrow(() => { |
| 65 | + stream.JRead(new byte[1], 0, 1); |
| 66 | + } |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + [NUnit.Framework.Test] |
| 72 | + public virtual void ReadingByteWithLimitOfOneLessThenFileSizeTest() { |
| 73 | + using (Stream stream = new LimitedInputStream(new LimitedInputStreamTest.TestStreamGenerator().OpenStream( |
| 74 | + ), 88)) { |
| 75 | + for (int i = 0; i < 88; i++) { |
| 76 | + NUnit.Framework.Assert.AreNotEqual(-1, stream.Read()); |
| 77 | + } |
| 78 | + NUnit.Framework.Assert.Catch(typeof(ReadingByteLimitException), () => stream.Read()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + [NUnit.Framework.Test] |
| 83 | + public virtual void ReadingByteArrayWithLimitOfOneLessThenFileSizeTest() { |
| 84 | + using (Stream stream = new LimitedInputStream(new LimitedInputStreamTest.TestStreamGenerator().OpenStream( |
| 85 | + ), 88)) { |
| 86 | + byte[] bytes = new byte[100]; |
| 87 | + int numOfReadBytes = stream.Read(bytes); |
| 88 | + NUnit.Framework.Assert.AreEqual(88, numOfReadBytes); |
| 89 | + NUnit.Framework.Assert.AreEqual(10, bytes[87]); |
| 90 | + NUnit.Framework.Assert.AreEqual(0, bytes[88]); |
| 91 | + NUnit.Framework.Assert.Catch(typeof(ReadingByteLimitException), () => stream.Read(new byte[1])); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + [NUnit.Framework.Test] |
| 96 | + public virtual void ReadingByteArrayWithOffsetAndLimitOfOneLessThenFileSizeTest() { |
| 97 | + using (Stream stream = new LimitedInputStream(new LimitedInputStreamTest.TestStreamGenerator().OpenStream( |
| 98 | + ), 88)) { |
| 99 | + byte[] bytes = new byte[100]; |
| 100 | + int numOfReadBytes = stream.JRead(bytes, 0, 88); |
| 101 | + NUnit.Framework.Assert.AreEqual(88, numOfReadBytes); |
| 102 | + NUnit.Framework.Assert.AreEqual(10, bytes[87]); |
| 103 | + NUnit.Framework.Assert.AreEqual(0, bytes[88]); |
| 104 | + NUnit.Framework.Assert.Catch(typeof(ReadingByteLimitException), () => stream.JRead(bytes, 88, 1)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + [NUnit.Framework.Test] |
| 109 | + public virtual void ReadingByteArrayWithSmallBufferTest() { |
| 110 | + using (Stream stream = new LimitedInputStream(new LimitedInputStreamTest.TestStreamGenerator().OpenStream( |
| 111 | + ), 89)) { |
| 112 | + byte[] bytes = new byte[20]; |
| 113 | + MemoryStream output = new MemoryStream(); |
| 114 | + while (true) { |
| 115 | + int read = stream.Read(bytes); |
| 116 | + if (read < 1) { |
| 117 | + break; |
| 118 | + } |
| 119 | + output.Write(bytes, 0, read); |
| 120 | + } |
| 121 | + NUnit.Framework.Assert.AreEqual(89, output.Length); |
| 122 | + output.Dispose(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + [NUnit.Framework.Test] |
| 127 | + public virtual void ReadingByteArrayWithBigBufferTest() { |
| 128 | + // retrieveStyleSheetTest.css.dat size is 89 bytes |
| 129 | + using (Stream stream = new LimitedInputStream(new LimitedInputStreamTest.TestStreamGenerator().OpenStream( |
| 130 | + ), 89)) { |
| 131 | + byte[] bytes = new byte[100]; |
| 132 | + NUnit.Framework.Assert.AreEqual(89, stream.Read(bytes)); |
| 133 | + byte[] tempBytes = (byte[])bytes.Clone(); |
| 134 | + NUnit.Framework.Assert.AreEqual(-1, stream.Read(bytes)); |
| 135 | + // Check that the array has not changed when we have read the entire LimitedInputStream |
| 136 | + NUnit.Framework.Assert.AreEqual(tempBytes, bytes); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + [NUnit.Framework.Test] |
| 141 | + public virtual void ReadingByteArrayWithOffsetAndBigBufferTest() { |
| 142 | + using (Stream stream = new LimitedInputStream(new LimitedInputStreamTest.TestStreamGenerator().OpenStream( |
| 143 | + ), 89)) { |
| 144 | + byte[] bytes = new byte[100]; |
| 145 | + NUnit.Framework.Assert.AreEqual(89, stream.JRead(bytes, 0, 100)); |
| 146 | + byte[] tempBytes = (byte[])bytes.Clone(); |
| 147 | + NUnit.Framework.Assert.AreEqual(-1, stream.JRead(bytes, 0, 100)); |
| 148 | + // Check that the array has not changed when we have read the entire LimitedInputStream |
| 149 | + NUnit.Framework.Assert.AreEqual(tempBytes, bytes); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + [NUnit.Framework.Test] |
| 154 | + public virtual void ByteArrayOverwritingTest() { |
| 155 | + using (Stream stream = new LimitedInputStream(new LimitedInputStreamTest.TestStreamGenerator().OpenStream( |
| 156 | + ), 90)) { |
| 157 | + byte[] bytes = new byte[100]; |
| 158 | + bytes[89] = 13; |
| 159 | + NUnit.Framework.Assert.AreEqual(89, stream.Read(bytes)); |
| 160 | + // Check that when calling the read(byte[]) method, as many bytes were copied into |
| 161 | + // the original array as were read, and not all bytes from the auxiliary array. |
| 162 | + NUnit.Framework.Assert.AreEqual(13, bytes[89]); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + [NUnit.Framework.Test] |
| 167 | + public virtual void ReadingByteWithZeroLimitTest() { |
| 168 | + using (LimitedInputStream stream = new LimitedInputStream(new MemoryStream(new byte[1]), 0)) { |
| 169 | + NUnit.Framework.Assert.Catch(typeof(ReadingByteLimitException), () => stream.Read()); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + [NUnit.Framework.Test] |
| 174 | + public virtual void ReadingByteArrayWithZeroLimitTest() { |
| 175 | + using (LimitedInputStream stream = new LimitedInputStream(new MemoryStream(new byte[1]), 0)) { |
| 176 | + byte[] bytes = new byte[100]; |
| 177 | + NUnit.Framework.Assert.Catch(typeof(ReadingByteLimitException), () => stream.Read(bytes)); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + [NUnit.Framework.Test] |
| 182 | + public virtual void ReadingByteArrayWithOffsetAndZeroLimitTest() { |
| 183 | + using (LimitedInputStream stream = new LimitedInputStream(new MemoryStream(new byte[1]), 0)) { |
| 184 | + byte[] bytes = new byte[100]; |
| 185 | + NUnit.Framework.Assert.Catch(typeof(ReadingByteLimitException), () => stream.JRead(bytes, 0, 100)); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + [NUnit.Framework.Test] |
| 190 | + public virtual void ReadingEmptyByteWithZeroLimitTest() { |
| 191 | + using (LimitedInputStream stream = new LimitedInputStream(new MemoryStream(new byte[0]), 0)) { |
| 192 | + NUnit.Framework.Assert.AreEqual(-1, stream.Read()); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + [NUnit.Framework.Test] |
| 197 | + public virtual void ReadingEmptyByteArrayWithZeroLimitTest() { |
| 198 | + using (LimitedInputStream stream = new LimitedInputStream(new MemoryStream(new byte[0]), 0)) { |
| 199 | + byte[] bytes = new byte[100]; |
| 200 | + NUnit.Framework.Assert.AreEqual(-1, stream.Read(bytes)); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + [NUnit.Framework.Test] |
| 205 | + public virtual void ReadingEmptyByteArrayWithOffsetAndZeroLimitTest() { |
| 206 | + using (LimitedInputStream stream = new LimitedInputStream(new MemoryStream(new byte[0]), 0)) { |
| 207 | + byte[] bytes = new byte[100]; |
| 208 | + NUnit.Framework.Assert.AreEqual(-1, stream.JRead(bytes, 0, 100)); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + [NUnit.Framework.Test] |
| 213 | + public virtual void IllegalReadingByteLimitValueTest() { |
| 214 | + Exception e = NUnit.Framework.Assert.Catch(typeof(ArgumentException), () => new LimitedInputStream(new MemoryStream |
| 215 | + (new byte[0]), -1)); |
| 216 | + NUnit.Framework.Assert.AreEqual(IoExceptionMessageConstant.READING_BYTE_LIMIT_MUST_NOT_BE_LESS_ZERO, e.Message |
| 217 | + ); |
| 218 | + } |
| 219 | + |
| 220 | +//\cond DO_NOT_DOCUMENT |
| 221 | + internal class TestStreamGenerator { |
| 222 | +//\cond DO_NOT_DOCUMENT |
| 223 | + internal String data = "body {\n" + " background-color: lightblue;\n" + "}\n" + "\n" + "h1 {\n" + " color: navy;\n" |
| 224 | + + " margin-left: 20px;\n" + "}"; |
| 225 | +//\endcond |
| 226 | + |
| 227 | +//\cond DO_NOT_DOCUMENT |
| 228 | + internal virtual Stream OpenStream() { |
| 229 | + return new MemoryStream(data.GetBytes(System.Text.Encoding.UTF8)); |
| 230 | + } |
| 231 | +//\endcond |
| 232 | + } |
| 233 | +//\endcond |
| 234 | + } |
| 235 | +} |
0 commit comments