1 //
2 // TestSuite.System.Security.Cryptography.FromBase64Transform.cs
3 //
4 // Author:
5 //      Martin Baulig (martin@gnome.org)
6 //	Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002 Ximian, Inc.  http://www.ximian.com
9 // (C) 2004 Novell  http://www.novell.com
10 //
11 
12 using System;
13 using System.Security.Cryptography;
14 using NUnit.Framework;
15 
16 namespace MonoTests.System.Security.Cryptography {
17 
18 	[TestFixture]
19 	public class FromBase64TransformTest {
20 
21 		private FromBase64Transform _algo;
22 
23 		[SetUp]
SetUp()24 		public void SetUp ()
25 		{
26 			_algo = new FromBase64Transform ();
27 		}
28 
TransformFinalBlock(string name, byte[] input, byte[] expected, int inputOffset, int inputCount)29 		protected void TransformFinalBlock (string name, byte[] input, byte[] expected,
30 						    int inputOffset, int inputCount)
31 		{
32 			byte[] output = _algo.TransformFinalBlock (input, inputOffset, inputCount);
33 
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 + ")");
37 		}
38 
TransformFinalBlock(string name, byte[] input, byte[] expected)39 		protected void TransformFinalBlock (string name, byte[] input, byte[] expected)
40 		{
41 			TransformFinalBlock (name, input, expected, 0, input.Length);
42 		}
43 
44 		[Test]
Properties()45 		public void Properties ()
46 		{
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");
51 		}
52 
53 		[Test]
A0()54 		public void A0 ()
55 		{
56 			byte[] input = { 114, 108, 112, 55, 81, 115, 110, 69 };
57 			byte[] expected = { 174, 90, 123, 66, 201, 196 };
58 
59 			_algo = new FromBase64Transform (FromBase64TransformMode.DoNotIgnoreWhiteSpaces);
60 			TransformFinalBlock ("#A0", input, expected);
61 		}
62 
63 		[Test]
A1()64 		public void A1 ()
65 		{
66 			byte[] input = { 114, 108, 112, 55, 81, 115, 110, 69 };
67 			byte[] expected = { 174, 90, 123, 66, 201, 196 };
68 
69 			TransformFinalBlock ("#A1", input, expected);
70 		}
71 
72 		[Test]
A2()73 		public void A2 ()
74 		{
75 			byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
76 			byte[] expected = { 174, 90, 123, 66 };
77 
78 			TransformFinalBlock ("#A2", input, expected);
79 		}
80 
81 		[Test]
A3()82 		public void A3 ()
83 		{
84 			byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
85 			byte[] expected = { 150, 158, 208 };
86 
87 			TransformFinalBlock ("#A3", input, expected, 1, 5);
88 		}
89 
90 		[Test]
IgnoreTAB()91 		public void IgnoreTAB ()
92 		{
93 			byte[] input = { 9, 114, 108, 112, 55, 9, 81, 115, 61, 61, 9 };
94 			byte[] expected = { 174, 90, 123, 66 };
95 
96 			TransformFinalBlock ("IgnoreTAB", input, expected);
97 		}
98 
99 		[Test]
IgnoreLF()100 		public void IgnoreLF ()
101 		{
102 			byte[] input = { 10, 114, 108, 112, 55, 10, 81, 115, 61, 61, 10 };
103 			byte[] expected = { 174, 90, 123, 66 };
104 
105 			TransformFinalBlock ("IgnoreLF", input, expected);
106 		}
107 
108 		[Test]
IgnoreCR()109 		public void IgnoreCR ()
110 		{
111 			byte[] input = { 13, 114, 108, 112, 55, 13, 81, 115, 61, 61, 13 };
112 			byte[] expected = { 174, 90, 123, 66 };
113 
114 			TransformFinalBlock ("IgnoreCR", input, expected);
115 		}
116 
117 		[Test]
IgnoreSPACE()118 		public void IgnoreSPACE ()
119 		{
120 			byte[] input = { 32, 114, 108, 112, 55, 32, 81, 115, 61, 61, 32 };
121 			byte[] expected = { 174, 90, 123, 66 };
122 
123 			TransformFinalBlock ("IgnoreSPACE", input, expected);
124 		}
125 
126 		[Test]
127 		[ExpectedException (typeof (FormatException))]
DontIgnore()128 		public void DontIgnore ()
129 		{
130 			byte[] input = { 7, 114, 108, 112, 55, 81, 115, 61, 61 };
131 			byte[] expected = { 174, 90, 123, 66 };
132 
133 			TransformFinalBlock ("DontIgnore", input, expected);
134 		}
135 
136 		[Test]
ReuseTransform()137 		public void ReuseTransform ()
138 		{
139 			byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
140 			byte[] expected = { 174, 90, 123, 66 };
141 
142 			TransformFinalBlock ("UseTransform", input, expected);
143 			TransformFinalBlock ("ReuseTransform", input, expected);
144 		}
145 
146 		[Test]
147 		[ExpectedException (typeof (ObjectDisposedException))]
ReuseDisposedTransform()148 		public void ReuseDisposedTransform ()
149 		{
150 			byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
151 			byte[] output = new byte [16];
152 
153 			_algo.Clear ();
154 			_algo.TransformBlock (input, 0, input.Length, output, 0);
155 		}
156 
157 		[Test]
158 		[ExpectedException (typeof (ObjectDisposedException))]
ReuseDisposedTransformFinal()159 		public void ReuseDisposedTransformFinal ()
160 		{
161 			byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
162 
163 			_algo.Clear ();
164 			_algo.TransformFinalBlock (input, 0, input.Length);
165 		}
166 
167 		[Test]
InvalidLength()168 		public void InvalidLength ()
169 		{
170 			byte[] input = { 114, 108, 112 };
171 			byte[] result = _algo.TransformFinalBlock (input, 0, input.Length);
172 			Assert.AreEqual (0, result.Length);
173 		}
174 
175 		[Test]
InvalidData()176 		public void InvalidData ()
177 		{
178 			byte[] input = { 114, 108, 112, 32 };
179 			byte[] result = _algo.TransformFinalBlock (input, 0, input.Length);
180 			Assert.AreEqual (0, result.Length);
181 		}
182 
183 		[Test]
Dispose()184 		public void Dispose ()
185 		{
186 			byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
187 			byte[] expected = { 174, 90, 123, 66 };
188 			byte[] output = null;
189 
190 			using (ICryptoTransform t = new FromBase64Transform ()) {
191 				output = t.TransformFinalBlock (input, 0, input.Length);
192 			}
193 
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 + ")");
197 		}
198 
199 		[Test]
200 		[ExpectedException (typeof (ArgumentNullException))]
TransformBlock_Input_Null()201 		public void TransformBlock_Input_Null ()
202 		{
203 			byte[] output = new byte [16];
204 			using (ICryptoTransform t = new FromBase64Transform ()) {
205 				t.TransformBlock (null, 0, output.Length, output, 0);
206 			}
207 		}
208 
209 		[Test]
210 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
TransformBlock_InputOffset_Negative()211 		public void TransformBlock_InputOffset_Negative ()
212 		{
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);
217 			}
218 		}
219 
220 		[Test]
221 		[ExpectedException (typeof (ArgumentException))]
TransformBlock_InputOffset_Overflow()222 		public void TransformBlock_InputOffset_Overflow ()
223 		{
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);
228 			}
229 		}
230 
231 		[Test]
232 		[ExpectedException (typeof (ArgumentException))]
TransformBlock_InputCount_Negative()233 		public void TransformBlock_InputCount_Negative ()
234 		{
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);
239 			}
240 		}
241 
242 		[Test]
243 		[ExpectedException (typeof (ArgumentException))]
TransformBlock_InputCount_Overflow()244 		public void TransformBlock_InputCount_Overflow ()
245 		{
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);
250 			}
251 		}
252 
253 		[Test]
254 		[ExpectedException (typeof (FormatException))]
TransformBlock_Output_Null()255 		public void TransformBlock_Output_Null ()
256 		{
257 			byte[] input = new byte [16];
258 			using (ICryptoTransform t = new FromBase64Transform ()) {
259 				t.TransformBlock (input, 0, input.Length, null, 0);
260 			}
261 		}
262 
263 		[Test]
264 		[ExpectedException (typeof (FormatException))]
TransformBlock_OutputOffset_Negative()265 		public void TransformBlock_OutputOffset_Negative ()
266 		{
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);
271 			}
272 		}
273 
274 		[Test]
275 		[ExpectedException (typeof (FormatException))]
TransformBlock_OutputOffset_Overflow()276 		public void TransformBlock_OutputOffset_Overflow ()
277 		{
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);
282 			}
283 		}
284 
285 		[Test]
286 		[ExpectedException (typeof (ArgumentNullException))]
TransformFinalBlock_Input_Null()287 		public void TransformFinalBlock_Input_Null ()
288 		{
289 			using (ICryptoTransform t = new FromBase64Transform ()) {
290 				t.TransformFinalBlock (null, 0, 16);
291 			}
292 		}
293 
294 		[Test]
295 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
TransformFinalBlock_InputOffset_Negative()296 		public void TransformFinalBlock_InputOffset_Negative ()
297 		{
298 			byte[] input = new byte [16];
299 			using (ICryptoTransform t = new FromBase64Transform ()) {
300 				t.TransformFinalBlock (input, -1, input.Length);
301 			}
302 		}
303 
304 		[Test]
305 		[ExpectedException (typeof (ArgumentException))]
TransformFinalBlock_InputOffset_Overflow()306 		public void TransformFinalBlock_InputOffset_Overflow ()
307 		{
308 			byte[] input = new byte [16];
309 			using (ICryptoTransform t = new FromBase64Transform ()) {
310 				t.TransformFinalBlock (input, Int32.MaxValue, input.Length);
311 			}
312 		}
313 
314 		[Test]
315 		[ExpectedException (typeof (ArgumentException))]
TransformFinalBlock_InputCount_Negative()316 		public void TransformFinalBlock_InputCount_Negative ()
317 		{
318 			byte[] input = new byte [16];
319 			using (ICryptoTransform t = new FromBase64Transform ()) {
320 				t.TransformFinalBlock (input, 0, -1);
321 			}
322 		}
323 
324 		[Test]
325 		[ExpectedException (typeof (ArgumentException))]
TransformFinalBlock_InputCount_Overflow()326 		public void TransformFinalBlock_InputCount_Overflow ()
327 		{
328 			byte[] input = new byte [16];
329 			using (ICryptoTransform t = new FromBase64Transform ()) {
330 				t.TransformFinalBlock (input, 0, Int32.MaxValue);
331 			}
332 		}
333 	}
334 }
335