1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
|
//
// HashAlgorithmTestBase.cs - NUnit Test Cases for HashAlgorithm
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004, 2006, 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace MonoTests.System.Security.Cryptography {
// HashAlgorithm is a abstract class - so most of it's functionality wont
// be tested here (but will be in its descendants).
// we can't make this a [TestFixture] since the class is shared with System.Core
// and causes issues in XA where these are in the same process.
public abstract class HashAlgorithmTestBase {
protected HashAlgorithm hash;
[SetUp]
public virtual void SetUp ()
{
hash = HashAlgorithm.Create ();
}
// Note: These tests will only be valid without a "machine.config" file
// or a "machine.config" file that do not modify the default algorithm
// configuration.
private const string defaultSHA1 = "System.Security.Cryptography.SHA1CryptoServiceProvider";
private const string defaultMD5 = "System.Security.Cryptography.MD5CryptoServiceProvider";
private const string defaultSHA256 = "System.Security.Cryptography.SHA256Managed";
private const string defaultSHA384 = "System.Security.Cryptography.SHA384Managed";
private const string defaultSHA512 = "System.Security.Cryptography.SHA512Managed";
private const string defaultHash = defaultSHA1;
[Test]
public virtual void Create ()
{
// try the default hash algorithm (created in SetUp)
Assert.AreEqual (defaultHash, hash.ToString (), "HashAlgorithm.Create()");
// try to build all hash algorithms
hash = HashAlgorithm.Create ("SHA");
Assert.AreEqual (defaultSHA1, hash.ToString (), "HashAlgorithm.Create('SHA')");
hash = HashAlgorithm.Create ("SHA1");
Assert.AreEqual (defaultSHA1, hash.ToString (), "HashAlgorithm.Create('SHA1')");
hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA1");
Assert.AreEqual (defaultSHA1, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.SHA1')");
hash = HashAlgorithm.Create ("System.Security.Cryptography.HashAlgorithm" );
Assert.AreEqual (defaultHash, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.HashAlgorithm')");
hash = HashAlgorithm.Create ("MD5");
Assert.AreEqual (defaultMD5, hash.ToString (), "HashAlgorithm.Create('MD5')");
hash = HashAlgorithm.Create ("System.Security.Cryptography.MD5");
Assert.AreEqual (defaultMD5, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.MD5')");
hash = HashAlgorithm.Create ("SHA256");
Assert.AreEqual (defaultSHA256, hash.ToString (), "HashAlgorithm.Create('SHA256')");
hash = HashAlgorithm.Create ("SHA-256");
Assert.AreEqual (defaultSHA256, hash.ToString (), "HashAlgorithm.Create('SHA-256')");
hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA256");
Assert.AreEqual (defaultSHA256, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.SHA256')");
hash = HashAlgorithm.Create ("SHA384");
Assert.AreEqual (defaultSHA384, hash.ToString (), "HashAlgorithm.Create('SHA384')");
hash = HashAlgorithm.Create ("SHA-384");
Assert.AreEqual (defaultSHA384, hash.ToString (), "HashAlgorithm.Create('SHA-384')");
hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA384");
Assert.AreEqual (defaultSHA384, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.SHA384')");
hash = HashAlgorithm.Create ("SHA512");
Assert.AreEqual (defaultSHA512, hash.ToString (), "HashAlgorithm.Create('SHA512')");
hash = HashAlgorithm.Create ("SHA-512");
Assert.AreEqual (defaultSHA512, hash.ToString (), "HashAlgorithm.Create('SHA-512')");
hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA512");
Assert.AreEqual (defaultSHA512, hash.ToString (), "HashAlgorithm.Create('System.Security.Cryptography.SHA512')");
// try to build invalid implementation
hash = HashAlgorithm.Create ("InvalidHash");
Assert.IsNull (hash, "HashAlgorithm.Create('InvalidHash')");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public virtual void CreateNull ()
{
// try to build null implementation
hash = HashAlgorithm.Create (null);
}
[Test]
[ExpectedException (typeof (ObjectDisposedException))]
public void Clear ()
{
byte[] inputABC = Encoding.Default.GetBytes ("abc");
hash.ComputeHash (inputABC);
hash.Clear ();
// cannot use a disposed object
hash.ComputeHash (inputABC);
}
[Test]
[ExpectedException (typeof (ObjectDisposedException))]
public void Clear2 ()
{
byte[] inputABC = Encoding.Default.GetBytes ("abc");
MemoryStream ms = new MemoryStream (inputABC);
hash.ComputeHash (ms);
hash.Clear ();
// cannot use a disposed object
hash.ComputeHash (ms);
}
[Test]
[ExpectedException (typeof (NullReferenceException))]
public void NullStream ()
{
Stream s = null;
hash.ComputeHash (s);
}
[Test]
public void Disposable ()
{
using (HashAlgorithm hash = HashAlgorithm.Create ()) {
hash.ComputeHash (new byte [0]);
}
}
[Test]
[ExpectedException (typeof (ObjectDisposedException))]
public void InitializeDisposed ()
{
hash.ComputeHash (new byte [0]);
hash.Clear (); // disposed
hash.Initialize ();
hash.ComputeHash (new byte [0]);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ComputeHash_ArrayNull ()
{
byte[] array = null;
hash.ComputeHash (array);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ComputeHash_ArrayNullIntInt ()
{
byte[] array = null;
hash.ComputeHash (array, 0, 0);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void ComputeHash_OffsetNegative ()
{
byte[] array = new byte [0];
hash.ComputeHash (array, -1, 0);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ComputeHash_OffsetOverflow ()
{
byte[] array = new byte [1];
hash.ComputeHash (array, Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ComputeHash_CountNegative ()
{
byte[] array = new byte [0];
hash.ComputeHash (array, 0, -1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ComputeHash_CountOverflow ()
{
byte[] array = new byte [1];
hash.ComputeHash (array, 1, Int32.MaxValue);
}
[Test]
// not checked in Fx 1.1
// [ExpectedException (typeof (ObjectDisposedException))]
public void TransformBlock_Disposed ()
{
hash.ComputeHash (new byte [0]);
hash.Initialize ();
byte[] input = new byte [8];
byte[] output = new byte [8];
hash.TransformBlock (input, 0, input.Length, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TransformBlock_InputBuffer_Null ()
{
byte[] output = new byte [8];
hash.TransformBlock (null, 0, output.Length, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformBlock_InputOffset_Negative ()
{
byte[] input = new byte [8];
byte[] output = new byte [8];
hash.TransformBlock (input, -1, input.Length, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformBlock_InputOffset_Overflow ()
{
byte[] input = new byte [8];
byte[] output = new byte [8];
hash.TransformBlock (input, Int32.MaxValue, input.Length, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformBlock_InputCount_Negative ()
{
byte[] input = new byte [8];
byte[] output = new byte [8];
hash.TransformBlock (input, 0, -1, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformBlock_InputCount_Overflow ()
{
byte[] input = new byte [8];
byte[] output = new byte [8];
hash.TransformBlock (input, 0, Int32.MaxValue, output, 0);
}
[Test]
public void TransformBlock_OutputBuffer_Null ()
{
byte[] input = new byte [8];
hash.TransformBlock (input, 0, input.Length, null, 0);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformBlock_OutputOffset_Negative ()
{
byte[] input = new byte [8];
byte[] output = new byte [8];
hash.TransformBlock (input, 0, input.Length, output, -1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformBlock_OutputOffset_Overflow ()
{
byte[] input = new byte [8];
byte[] output = new byte [8];
hash.TransformBlock (input, 0, input.Length, output, Int32.MaxValue);
}
[Test]
// not checked in Fx 1.1
// [ExpectedException (typeof (ObjectDisposedException))]
public void TransformFinalBlock_Disposed ()
{
hash.ComputeHash (new byte [0]);
hash.Initialize ();
byte[] input = new byte [8];
hash.TransformFinalBlock (input, 0, input.Length);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TransformFinalBlock_InputBuffer_Null ()
{
hash.TransformFinalBlock (null, 0, 8);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformFinalBlock_InputOffset_Negative ()
{
byte[] input = new byte [8];
hash.TransformFinalBlock (input, -1, input.Length);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformFinalBlock_InputOffset_Overflow ()
{
byte[] input = new byte [8];
hash.TransformFinalBlock (input, Int32.MaxValue, input.Length);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformFinalBlock_InputCount_Negative ()
{
byte[] input = new byte [8];
hash.TransformFinalBlock (input, 0, -1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformFinalBlock_InputCount_Overflow ()
{
byte[] input = new byte [8];
hash.TransformFinalBlock (input, 0, Int32.MaxValue);
}
public virtual bool ManagedHashImplementation {
get { return false; }
}
#if !MOBILE
[Test]
[Category ("NotWorking")] // Mono nevers throws an exception (and we're all managed ;-)
public void TransformFinalBlock_Twice ()
{
bool exception = false;
byte[] input = new byte [8];
hash.TransformFinalBlock (input, 0, input.Length);
try {
hash.TransformFinalBlock (input, 0, input.Length);
}
catch (CryptographicException) {
exception = true;
if (ManagedHashImplementation)
Assert.Fail ("*Managed don't throw CryptographicException");
}
if (!ManagedHashImplementation && !exception)
Assert.Fail ("Expected CryptographicException from non *Managed classes");
}
[Test]
[Category ("NotWorking")] // Mono nevers throws an exception (and we're all managed ;-)
public void TransformFinalBlock_TransformBlock ()
{
bool exception = false;
byte[] input = new byte[8];
hash.TransformFinalBlock (input, 0, input.Length);
try {
hash.TransformBlock (input, 0, input.Length, input, 0);
}
catch (CryptographicException) {
exception = true;
if (ManagedHashImplementation)
Assert.Fail ("*Managed don't throw CryptographicException");
}
if (!ManagedHashImplementation && !exception)
Assert.Fail ("Expected CryptographicException from non *Managed classes");
}
#endif
[Test]
public void TransformFinalBlock_Twice_Initialize ()
{
byte[] input = new byte[8];
hash.TransformFinalBlock (input, 0, input.Length);
hash.Initialize ();
hash.TransformFinalBlock (input, 0, input.Length);
}
[Test]
public void TransformFinalBlock_ReturnedBuffer ()
{
byte[] input = new byte[8];
byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
Assert.AreEqual (input, output, "buffer");
output[0] = 1;
Assert.AreEqual (0, input[0], "0"); // output is a copy (not a reference)
}
private byte[] HashBuffer (bool intersect)
{
byte[] buffer = new byte [256];
for (int i = 0; i < buffer.Length; i++)
buffer [i] = (byte) i;
hash.Initialize ();
// ok
hash.TransformBlock (buffer, 0, 64, buffer, 0);
// bad - we rewrite the beginning of the buffer
hash.TransformBlock (buffer, 64, 128, buffer, intersect ? 0 : 64);
// ok
hash.TransformFinalBlock (buffer, 192, 64);
return hash.Hash;
}
[Test]
public void InputOutputIntersection ()
{
Assert.AreEqual (HashBuffer (false), HashBuffer (true), "Intersect");
}
#if !MOBILE
[Test]
[ExpectedException (typeof (NullReferenceException))]
[Category ("NotWorking")] // initialization problem ? fx2.0 only ?
public void Hash_AfterInitialize_FirstTime ()
{
hash.Initialize ();
// getting the property throws
Assert.IsNull (hash.Hash);
}
#endif
[Test]
[ExpectedException (typeof (CryptographicUnexpectedOperationException))]
public void Hash_AfterInitialize_SecondTime ()
{
byte[] input = new byte[8];
hash.Initialize ();
hash.TransformBlock (input, 0, input.Length, input, 0);
hash.Initialize ();
// getting the property throws
Assert.IsNull (hash.Hash);
}
[Test]
[ExpectedException (typeof (CryptographicUnexpectedOperationException))]
public void Hash_AfterTransformBlock ()
{
byte[] input = new byte[8];
hash.Initialize ();
hash.TransformBlock (input, 0, input.Length, input, 0);
// getting the property throws
Assert.IsNull (hash.Hash);
}
[Test]
public void Hash_AfterTransformFinalBlock ()
{
byte[] input = new byte[8];
hash.Initialize ();
hash.TransformFinalBlock (input, 0, input.Length);
Assert.IsNotNull (hash.Hash);
}
[Test]
public void Hash_NoExternalChanges ()
{
byte[] input = new byte [512];
for (int i = 0; i < input.Length; i++)
input[i] = (byte)(i % 0xFF);
hash.TransformFinalBlock (input, 0, input.Length);
byte[] brokenHash = hash.Hash;
Array.Clear (brokenHash, 0, brokenHash.Length);
// if the byte array is returned as a ref, both instance will be cleared.
Assert.AreNotEqual (hash.Hash, brokenHash, "ExternalChanges");
}
[Test]
[ExpectedException (typeof (ObjectDisposedException))]
public void Hash_DisposedException ()
{
hash.Dispose ();
// should fail since the hash object is disposed.
Assert.IsNull (hash.Hash);
}
[Test]
[ExpectedException (typeof (CryptographicUnexpectedOperationException))]
public void Hash_StateExeception ()
{
hash.Initialize ();
byte[] input = new byte [8];
byte[] output = new byte [8];
hash.TransformBlock (input, 0, input.Length, output, 0);
// should fail with CryptographicUnexpectedOperationException as TransformFinalBlock was not called.
Assert.IsNull (hash.Hash);
}
}
}
|