2 // TestSuite.System.Security.Cryptography.FromBase64Transform.cs
5 // Martin Baulig (martin@gnome.org)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2002 Ximian, Inc. http://www.ximian.com
9 // (C) 2004 Novell http://www.novell.com
13 using System.Security.Cryptography;
14 using NUnit.Framework;
16 namespace MonoTests.System.Security.Cryptography {
19 public class FromBase64TransformTest {
21 private FromBase64Transform _algo;
26 _algo = new FromBase64Transform ();
29 protected void TransformFinalBlock (string name, byte[] input, byte[] expected,
30 int inputOffset, int inputCount)
32 byte[] output = _algo.TransformFinalBlock (input, inputOffset, inputCount);
34 Assert.AreEqual (expected.Length, output.Length, name);
35 for (int i = 0; i < expected.Length; i++)
36 Assert.AreEqual (expected [i], output [i], name + "(" + i + ")");
39 protected void TransformFinalBlock (string name, byte[] input, byte[] expected)
41 TransformFinalBlock (name, input, expected, 0, input.Length);
45 public void Properties ()
47 Assert.IsTrue (_algo.CanReuseTransform, "CanReuseTransform");
48 Assert.IsTrue (!_algo.CanTransformMultipleBlocks, "CanTransformMultipleBlocks");
49 Assert.AreEqual (1, _algo.InputBlockSize, "InputBlockSize");
50 Assert.AreEqual (3, _algo.OutputBlockSize, "OutputBlockSize");
56 byte[] input = { 114, 108, 112, 55, 81, 115, 110, 69 };
57 byte[] expected = { 174, 90, 123, 66, 201, 196 };
59 _algo = new FromBase64Transform (FromBase64TransformMode.DoNotIgnoreWhiteSpaces);
60 TransformFinalBlock ("#A0", input, expected);
66 byte[] input = { 114, 108, 112, 55, 81, 115, 110, 69 };
67 byte[] expected = { 174, 90, 123, 66, 201, 196 };
69 TransformFinalBlock ("#A1", input, expected);
75 byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
76 byte[] expected = { 174, 90, 123, 66 };
78 TransformFinalBlock ("#A2", input, expected);
84 byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
85 byte[] expected = { 150, 158, 208 };
87 TransformFinalBlock ("#A3", input, expected, 1, 5);
91 public void IgnoreTAB ()
93 byte[] input = { 9, 114, 108, 112, 55, 9, 81, 115, 61, 61, 9 };
94 byte[] expected = { 174, 90, 123, 66 };
96 TransformFinalBlock ("IgnoreTAB", input, expected);
100 public void IgnoreLF ()
102 byte[] input = { 10, 114, 108, 112, 55, 10, 81, 115, 61, 61, 10 };
103 byte[] expected = { 174, 90, 123, 66 };
105 TransformFinalBlock ("IgnoreLF", input, expected);
109 public void IgnoreCR ()
111 byte[] input = { 13, 114, 108, 112, 55, 13, 81, 115, 61, 61, 13 };
112 byte[] expected = { 174, 90, 123, 66 };
114 TransformFinalBlock ("IgnoreCR", input, expected);
118 public void IgnoreSPACE ()
120 byte[] input = { 32, 114, 108, 112, 55, 32, 81, 115, 61, 61, 32 };
121 byte[] expected = { 174, 90, 123, 66 };
123 TransformFinalBlock ("IgnoreSPACE", input, expected);
127 [ExpectedException (typeof (FormatException))]
128 public void DontIgnore ()
130 byte[] input = { 7, 114, 108, 112, 55, 81, 115, 61, 61 };
131 byte[] expected = { 174, 90, 123, 66 };
133 TransformFinalBlock ("DontIgnore", input, expected);
137 public void ReuseTransform ()
139 byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
140 byte[] expected = { 174, 90, 123, 66 };
142 TransformFinalBlock ("UseTransform", input, expected);
143 TransformFinalBlock ("ReuseTransform", input, expected);
147 [ExpectedException (typeof (ObjectDisposedException))]
148 public void ReuseDisposedTransform ()
150 byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
151 byte[] output = new byte [16];
154 _algo.TransformBlock (input, 0, input.Length, output, 0);
158 [ExpectedException (typeof (ObjectDisposedException))]
159 public void ReuseDisposedTransformFinal ()
161 byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
164 _algo.TransformFinalBlock (input, 0, input.Length);
168 public void InvalidLength ()
170 byte[] input = { 114, 108, 112 };
171 byte[] result = _algo.TransformFinalBlock (input, 0, input.Length);
172 Assert.AreEqual (0, result.Length);
176 public void InvalidData ()
178 byte[] input = { 114, 108, 112, 32 };
179 byte[] result = _algo.TransformFinalBlock (input, 0, input.Length);
180 Assert.AreEqual (0, result.Length);
184 public void Dispose ()
186 byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
187 byte[] expected = { 174, 90, 123, 66 };
188 byte[] output = null;
190 using (ICryptoTransform t = new FromBase64Transform ()) {
191 output = t.TransformFinalBlock (input, 0, input.Length);
194 Assert.AreEqual (expected.Length, output.Length, "IDisposable");
195 for (int i = 0; i < expected.Length; i++)
196 Assert.AreEqual (expected[i], output[i], "IDisposable(" + i + ")");
200 [ExpectedException (typeof (ArgumentNullException))]
201 public void TransformBlock_Input_Null ()
203 byte[] output = new byte [16];
204 using (ICryptoTransform t = new FromBase64Transform ()) {
205 t.TransformBlock (null, 0, output.Length, output, 0);
210 [ExpectedException (typeof (ArgumentOutOfRangeException))]
211 public void TransformBlock_InputOffset_Negative ()
213 byte[] input = new byte [16];
214 byte[] output = new byte [16];
215 using (ICryptoTransform t = new FromBase64Transform ()) {
216 t.TransformBlock (input, -1, input.Length, output, 0);
221 [ExpectedException (typeof (ArgumentException))]
222 public void TransformBlock_InputOffset_Overflow ()
224 byte[] input = new byte [16];
225 byte[] output = new byte [16];
226 using (ICryptoTransform t = new FromBase64Transform ()) {
227 t.TransformBlock (input, Int32.MaxValue, input.Length, output, 0);
232 [ExpectedException (typeof (OverflowException))]
233 public void TransformBlock_InputCount_Negative ()
235 byte[] input = new byte [16];
236 byte[] output = new byte [16];
237 using (ICryptoTransform t = new FromBase64Transform ()) {
238 t.TransformBlock (input, 0, -1, output, 0);
243 [ExpectedException (typeof (OutOfMemoryException))]
244 public void TransformBlock_InputCount_Overflow ()
246 byte[] input = new byte [16];
247 byte[] output = new byte [16];
248 using (ICryptoTransform t = new FromBase64Transform ()) {
249 t.TransformBlock (input, 0, Int32.MaxValue, output, 0);
254 [ExpectedException (typeof (FormatException))]
255 public void TransformBlock_Output_Null ()
257 byte[] input = new byte [16];
258 using (ICryptoTransform t = new FromBase64Transform ()) {
259 t.TransformBlock (input, 0, input.Length, null, 0);
264 [ExpectedException (typeof (FormatException))]
265 public void TransformBlock_OutputOffset_Negative ()
267 byte[] input = new byte [16];
268 byte[] output = new byte [16];
269 using (ICryptoTransform t = new FromBase64Transform ()) {
270 t.TransformBlock (input, 0, input.Length, output, -1);
275 [ExpectedException (typeof (FormatException))]
276 public void TransformBlock_OutputOffset_Overflow ()
278 byte[] input = new byte [16];
279 byte[] output = new byte [16];
280 using (ICryptoTransform t = new FromBase64Transform ()) {
281 t.TransformBlock (input, 0, input.Length, output, Int32.MaxValue);
286 [ExpectedException (typeof (ArgumentNullException))]
287 public void TransformFinalBlock_Input_Null ()
289 using (ICryptoTransform t = new FromBase64Transform ()) {
290 t.TransformFinalBlock (null, 0, 16);
295 [ExpectedException (typeof (ArgumentOutOfRangeException))]
296 public void TransformFinalBlock_InputOffset_Negative ()
298 byte[] input = new byte [16];
299 using (ICryptoTransform t = new FromBase64Transform ()) {
300 t.TransformFinalBlock (input, -1, input.Length);
305 [ExpectedException (typeof (ArgumentException))]
306 public void TransformFinalBlock_InputOffset_Overflow ()
308 byte[] input = new byte [16];
309 using (ICryptoTransform t = new FromBase64Transform ()) {
310 t.TransformFinalBlock (input, Int32.MaxValue, input.Length);
315 [ExpectedException (typeof (OverflowException))]
316 public void TransformFinalBlock_InputCount_Negative ()
318 byte[] input = new byte [16];
319 using (ICryptoTransform t = new FromBase64Transform ()) {
320 t.TransformFinalBlock (input, 0, -1);
325 [ExpectedException (typeof (OutOfMemoryException))]
326 public void TransformFinalBlock_InputCount_Overflow ()
328 byte[] input = new byte [16];
329 using (ICryptoTransform t = new FromBase64Transform ()) {
330 t.TransformFinalBlock (input, 0, Int32.MaxValue);