// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 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
}
[TestFixture]
- public class CryptoStreamTest : Assertion {
+ public class CryptoStreamTest {
Stream readStream;
Stream writeStream;
ICryptoTransform encryptor;
- ICryptoTransform decryptor;
CryptoStream cs;
SymmetricAlgorithm aes;
public void SetUp ()
{
if (readStream == null) {
- readStream = new FileStream ("read", FileMode.OpenOrCreate, FileAccess.Read);
- writeStream = new FileStream ("write", FileMode.OpenOrCreate, FileAccess.Write);
+ readStream = new MemoryStream (new byte [0], false);
+ writeStream = new MemoryStream (new byte [0], true);
aes = SymmetricAlgorithm.Create ();
encryptor = aes.CreateEncryptor ();
- decryptor = aes.CreateEncryptor ();
}
}
- [TearDown]
- public void TearDown ()
- {
- try {
- if (File.Exists ("read"))
- File.Delete ("read");
- if (File.Exists ("write"))
- File.Delete ("write");
- }
- catch {}
- }
-
public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
- AllTests.AssertEquals (msg, array1, array2);
+ Assert.AreEqual (array1, array2, msg);
}
[Test]
public void StreamReadModeRead ()
{
cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
- Assert ("Read.CanRead", cs.CanRead);
- Assert ("Read.CanWrite", !cs.CanWrite);
- Assert ("Read.CanSeek", !cs.CanSeek);
+ Assert.IsTrue (cs.CanRead, "Read.CanRead");
+ Assert.IsFalse (cs.CanWrite, "Read.CanWrite");
+ Assert.IsFalse (cs.CanSeek, "Read.CanSeek");
}
[Test]
{
cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Write);
}
-
+
[Test]
[ExpectedException (typeof (ArgumentException))]
public void StreamWriteModeRead ()
{
- cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Read);
+ // This needs a stream which can't be read from; memory stream won't do that.
+ string f = Path.GetTempFileName ();
+ FileStream fs = new FileStream (f, FileMode.OpenOrCreate, FileAccess.Write);
+ try {
+ cs = new CryptoStream (fs, encryptor, CryptoStreamMode.Read);
+ } finally {
+ fs.Close ();
+ File.Delete (f);
+ }
}
[Test]
public void StreamWriteModeWrite ()
{
cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
- Assert ("Read.CanRead", !cs.CanRead);
- Assert ("Read.CanWrite", cs.CanWrite);
- Assert ("Read.CanSeek", !cs.CanSeek);
+ Assert.IsFalse (cs.CanRead, "Read.CanRead");
+ Assert.IsTrue (cs.CanWrite, "Read.CanWrite");
+ Assert.IsFalse (cs.CanSeek, "Read.CanSeek");
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void GetLength ()
{
- cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
+ DebugStream debug = new DebugStream ();
+ cs = new CryptoStream (debug, encryptor, CryptoStreamMode.Read);
long x = cs.Length;
}
[ExpectedException (typeof (NotSupportedException))]
public void GetPosition ()
{
- cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
+ DebugStream debug = new DebugStream ();
+ cs = new CryptoStream (debug, encryptor, CryptoStreamMode.Read);
long x = cs.Position;
}
[ExpectedException (typeof (NotSupportedException))]
public void SetPosition ()
{
- cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
+ DebugStream debug = new DebugStream ();
+ cs = new CryptoStream (debug, encryptor, CryptoStreamMode.Read);
cs.Position = 1;
}
[ExpectedException (typeof (NotSupportedException))]
public void Seek ()
{
- cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
+ DebugStream debug = new DebugStream ();
+ cs = new CryptoStream (debug, encryptor, CryptoStreamMode.Read);
cs.Seek (0, SeekOrigin.Begin);
}
[ExpectedException (typeof (NotSupportedException))]
public void SetLength ()
{
- cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
+ DebugStream debug = new DebugStream ();
+ cs = new CryptoStream (debug, encryptor, CryptoStreamMode.Read);
cs.SetLength (0);
}
}
[Test]
+#if ONLY_1_1
[ExpectedException (typeof (NotSupportedException))]
+#endif
public void FlushFinalBlockReadStream ()
{
cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
[ExpectedException (typeof (NotSupportedException))]
public void FlushFinalBlock_Dual ()
{
- byte[] data = {0, 1, 2, 3, 4, 5, 6, 7};
- cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
- cs.Write (data, 0, data.Length);
- cs.FlushFinalBlock ();
- cs.FlushFinalBlock ();
+ // do no corrupt writeStream in further tests
+ using (Stream s = new MemoryStream ()) {
+ byte[] data = {0, 1, 2, 3, 4, 5, 6, 7};
+ cs = new CryptoStream (s, encryptor, CryptoStreamMode.Write);
+ cs.Write (data, 0, data.Length);
+ cs.FlushFinalBlock ();
+ cs.FlushFinalBlock ();
+ }
}
[Test]
// LAMESPEC or MS BUG [ExpectedException (typeof (ObjectDisposedException))]
+#if NET_2_0
+ [ExpectedException (typeof (NotSupportedException))]
+#else
[ExpectedException (typeof (ArgumentNullException))]
+#endif
public void FlushFinalBlock_Disposed ()
{
- cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
- cs.Clear ();
- cs.FlushFinalBlock ();
+ // do no corrupt writeStream in further tests
+ using (Stream s = new MemoryStream ()) {
+ cs = new CryptoStream (s, encryptor, CryptoStreamMode.Write);
+ cs.Clear ();
+ cs.FlushFinalBlock ();
+ }
}
[Test]
// LAMESPEC or MS BUG [ExpectedException (typeof (ObjectDisposedException))]
+#if ONLY_1_1
[ExpectedException (typeof (ArgumentNullException))]
+#endif
public void Read_Disposed ()
{
- byte[] buffer = new byte [8];
- cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
- cs.Clear ();
- cs.Read (buffer, 0, 8);
+ // do no corrupt readStream in further tests
+ using (Stream s = new MemoryStream ()) {
+ byte[] buffer = new byte [8];
+ cs = new CryptoStream (s, encryptor, CryptoStreamMode.Read);
+ cs.Clear ();
+ Assert.AreEqual (0, cs.Read (buffer, 0, 8), "Read from disposed");
+ }
}
-
+
+#if !NET_2_1
[Test]
// MS BUG [ExpectedException (typeof (ObjectDisposedException))]
#if NET_2_0
+ [Category ("NotWorking")]
[ExpectedException (typeof (IndexOutOfRangeException))]
#else
- [Ignore ("Test cause System.ExecutionEngineException on MS runtime")]
+ [Category ("NotDotNet")] // Test cause System.ExecutionEngineException on MS runtime
+ [ExpectedException (typeof (ArgumentNullException))] // should fail like previous test case
#endif
public void Read_Disposed_Break ()
{
- byte[] buffer = new byte [8];
- cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
- int len = cs.Read (buffer, 0, 4);
- AssertEquals ("Read 4", 4, len);
- cs.Clear ();
- len = cs.Read (buffer, 3, 4);
+ // do no corrupt readStream in further tests
+ using (Stream s = new MemoryStream ()) {
+ byte[] buffer = new byte [8];
+ cs = new CryptoStream (s, encryptor, CryptoStreamMode.Read);
+ int len = cs.Read (buffer, 0, 4);
+ Assert.AreEqual (4, len, "Read 4");
+ cs.Clear ();
+ len = cs.Read (buffer, 3, 4);
+ }
}
+#endif
[Test]
[ExpectedException (typeof (NotSupportedException))]
}
[Test]
- // [ExpectedException (typeof (ArgumentNullException))]
[ExpectedException (typeof (NullReferenceException))]
public void Read_NullBuffer ()
{
byte[] buffer = new byte [0];
cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
int len = cs.Read (buffer, 0, 0);
- AssertEquals ("Read 0", 0, len);
+ Assert.AreEqual (0, len, "Read 0");
}
[Test]
byte[] buffer = new byte [8];
cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
int len = cs.Read (buffer, 0, 0);
- AssertEquals ("Read 0", 0, len);
+ Assert.AreEqual (0, len, "Read 0");
}
[Test]
cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
cs.Read (buffer, Int32.MaxValue, 4);
}
-
+
+#if !NET_2_1
[Test]
// MS BUG [ExpectedException (typeof (ObjectDisposedException))]
#if NET_2_0
+ [Category ("NotWorking")]
[ExpectedException (typeof (IndexOutOfRangeException))]
#else
- [Ignore ("Test cause System.ExecutionEngineException on MS runtime")]
+ [Category ("NotDotNet")] // Test cause System.ExecutionEngineException on MS runtime
+ [ExpectedException (typeof (ArgumentNullException))] // to match exception throw by Read in a similar case
#endif
public void Write_Disposed ()
{
- byte[] buffer = new byte [8];
- cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
- cs.Clear ();
- cs.Write (buffer, 0, 8);
+ // do no corrupt writeStream in further tests
+ using (Stream s = new MemoryStream ()) {
+ byte[] buffer = new byte [8];
+ cs = new CryptoStream (s, encryptor, CryptoStreamMode.Write);
+ cs.Clear ();
+ cs.Write (buffer, 0, 8);
+ }
}
-
+#endif
+
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void Write_ReadStream ()
}
[Test]
- // [ExpectedException (typeof (ArgumentNullException))]
[ExpectedException (typeof (NullReferenceException))]
public void Write_NullBuffer ()
{
[ExpectedException (typeof (ArgumentException))]
public void Write_InvalidOffset ()
{
+ DebugStream debug = new DebugStream ();
byte[] buffer = new byte [8];
- cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
+ cs = new CryptoStream (debug, encryptor, CryptoStreamMode.Write);
cs.Write (buffer, 5, 4);
}
[ExpectedException (typeof (ArgumentException))]
public void Write_OverflowCount ()
{
+ DebugStream debug = new DebugStream ();
byte[] buffer = new byte [8];
- cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
+ cs = new CryptoStream (debug, encryptor, CryptoStreamMode.Write);
cs.Write (buffer, 0, Int32.MaxValue);
}
CryptoStream cr = new CryptoStream (mem2, aes.CreateDecryptor (), CryptoStreamMode.Read);
int len = cr.Read (buffer, 0, buffer.Length);
cr.Close ();
- AssertEquals ("Full Length Read", 34, len);
- AssertEquals ("Full Block Read", "Please encode me!", Encoding.Unicode.GetString (buffer, 0, len));
+ Assert.AreEqual (34, len, "Full Length Read");
+ Assert.AreEqual ("Please encode me!", Encoding.Unicode.GetString (buffer, 0, len), "Full Block Read");
}
}
int len = cr.Read (buffer, 0, 20);
cr.Clear ();
cr.Close ();
- AssertEquals ("Partial Length Read", 20, len);
- AssertEquals ("Partial Block Read", "Please enc", Encoding.Unicode.GetString (buffer, 0, len));
+ Assert.AreEqual (20, len, "Partial Length Read");
+ Assert.AreEqual ("Please enc", Encoding.Unicode.GetString (buffer, 0, len), "Partial Block Read");
}
}
enc.Write (data, 0, 2200);
enc.FlushFinalBlock ();
msin.Position = 0;
- AssertEquals ("Encryped Write Length", 2208, msin.Length); // 2200 + padding
+ Assert.AreEqual (2208, msin.Length, "Encryped Write Length"); // 2200 + padding
MemoryStream msout = new MemoryStream ();
msout.SetLength (0);
readlen += len;
len = msin.Read (tmp, 0, 1024);
}
- AssertEquals ("Decryped Write Length", 2200, msout.Length);
+ Assert.AreEqual (2200, msout.Length, "Decryped Write Length");
dec.Close ();
dec.Clear ();
msout.Close ();
msin.Close ();
- AssertEquals ("Read Length", 2208, readlen); // 2200 + padding
+ Assert.AreEqual (2208, readlen, "Read Length"); // 2200 + padding
}
[Test]
string result = BitConverter.ToString (msin.ToArray ());
switch (i) {
case 0:
- AssertEquals (msg, "92-C9-DB-45-30-0B-93-2F", result);
+ Assert.AreEqual ("92-C9-DB-45-30-0B-93-2F", result, msg);
break;
case 1:
- AssertEquals (msg, "08-CF-A1-37-BD-56-D0-65", result);
+ Assert.AreEqual ("08-CF-A1-37-BD-56-D0-65", result, msg);
break;
case 2:
- AssertEquals (msg, "58-87-D4-9B-2C-27-97-0C", result);
+ Assert.AreEqual ("58-87-D4-9B-2C-27-97-0C", result, msg);
break;
case 3:
- AssertEquals (msg, "07-35-90-94-68-7D-51-FB", result);
+ Assert.AreEqual ("07-35-90-94-68-7D-51-FB", result, msg);
break;
case 4:
- AssertEquals (msg, "BF-00-98-C5-20-71-D0-DB", result);
+ Assert.AreEqual ("BF-00-98-C5-20-71-D0-DB", result, msg);
break;
case 5:
- AssertEquals (msg, "1A-55-C8-6E-C1-9B-31-82", result);
+ Assert.AreEqual ("1A-55-C8-6E-C1-9B-31-82", result, msg);
break;
case 6:
- AssertEquals (msg, "2D-2B-76-41-61-0E-00-0C", result);
+ Assert.AreEqual ("2D-2B-76-41-61-0E-00-0C", result, msg);
break;
case 7:
- AssertEquals (msg, "DC-FF-73-D2-7F-D7-48-5D", result);
+ Assert.AreEqual ("DC-FF-73-D2-7F-D7-48-5D", result, msg);
break;
case 8:
- AssertEquals (msg, "E1-B2-46-E5-A7-C7-4C-BC-0E-40-4A-FC-08-92-B1-EB", result);
+ Assert.AreEqual ("E1-B2-46-E5-A7-C7-4C-BC-0E-40-4A-FC-08-92-B1-EB", result, msg);
break;
}
}
public void EmptyStreamWithPaddingNone ()
{
byte[] result = EmptyStream (PaddingMode.None);
- AssertEquals ("Result Length", 0, result.Length);
+ Assert.AreEqual (0, result.Length, "Result Length");
}
[Test]
{
byte[] expected = { 0x07, 0xFE, 0xEF, 0x74, 0xE1, 0xD5, 0x03, 0x6E, 0x90, 0x0E, 0xEE, 0x11, 0x8E, 0x94, 0x92, 0x93 };
byte[] result = EmptyStream (PaddingMode.PKCS7);
- AssertEquals ("Result Length", 16, result.Length);
- AssertEquals ("Result", expected, result);
+ Assert.AreEqual (16, result.Length, "Result Length");
+ Assert.AreEqual (expected, result, "Result");
}
[Test]
public void EmptyStreamWithPaddingZeros ()
{
byte[] result = EmptyStream (PaddingMode.Zeros);
- AssertEquals ("Result Length", 0, result.Length);
+ Assert.AreEqual (0, result.Length, "Result Length");
}
- // bugzilla: 49323 (adapted from test case by Carlos Guzmán Álvarez)
+ // bugzilla: 49323 (adapted from test case by Carlos Guzmán Álvarez)
[Test]
public void MultiblocksWithPartial ()
{
cs.Close ();
byte[] encrypted = ms.ToArray ();
byte[] expected = new byte[] { 0x9c, 0x99, 0x56, 0x8e, 0x75, 0x3e, 0x02, 0x95, 0x5b, 0x5c, 0x46, 0x8b, 0xcf, 0xf8, 0x27, 0x21, 0x53, 0x5f, 0x3d, 0xd8, 0x16, 0x95, 0x82, 0x3d, 0x88, 0x9b, 0x9a, 0x47, 0xda, 0x97, 0x90, 0x86, 0x50, 0x0e, 0x48, 0xee, 0xe7, 0x9b, 0x25, 0x41 };
- AssertEquals ("MultiblocksWithPartial", expected, encrypted);
+ Assert.AreEqual (expected, encrypted, "MultiblocksWithPartial");
}
// Adapted from Subba Rao Thirumoorthy email on mono-devel-list (december 2003)
}
CryptStream.FlushFinalBlock ();
// we must ensure that the result is correct
- AssertEquals ("Length(final)", 64, len);
+ Assert.AreEqual (64, len, "Length(final)");
byte[] result = stream.ToArray ();
string end = BitConverter.ToString (result, 65520, 16);
- AssertEquals ("End part", "04-70-19-1D-28-C5-BD-9A-23-C6-60-E2-28-96-38-65", end);
+ Assert.AreEqual ("04-70-19-1D-28-C5-BD-9A-23-C6-60-E2-28-96-38-65", end, "End part");
CryptStream.Close();
stream.Close();
RijndaelManaged aes = new RijndaelManaged ();
ICryptoTransform encryptor = aes.CreateEncryptor (key, iv);
byte[] encdata = NonMultipleOfBlockSize_Encrypt (encryptor, data);
- AssertEquals ("Encrypted Data Length", (data.Length + (aes.BlockSize >> 3)), encdata.Length);
+ Assert.AreEqual ((data.Length + (aes.BlockSize >> 3)), encdata.Length, "Encrypted Data Length");
ICryptoTransform decryptor = aes.CreateDecryptor (key, iv);
byte[] decdata = NonMultipleOfBlockSize_Decrypt (decryptor, encdata);
- AssertEquals ("Decrypted Data Length", data.Length, decdata.Length);
+ Assert.AreEqual (data.Length, decdata.Length, "Decrypted Data Length");
int i = 0;
bool b = true;
b = (data [i] == decdata [i]);
i++;
}
- Assert ("NonMultipleOfBlockSize", b);
+ Assert.IsTrue (b, "NonMultipleOfBlockSize");
}
// bugzilla: 51322 - indirectly related but it explains why my first (unapplied) patch didn't work
cs = new CryptoStream (decrypted, des.CreateDecryptor (key, iv), CryptoStreamMode.Read);
int len = cs.Read (data, 0, data.Length);
cs.Close ();
- AssertEquals ("Length", 12, len);
- AssertEquals ("Unicode DES Roundtrip", "ximian", Encoding.Unicode.GetString (data, 0, len));
+ Assert.AreEqual (12, len, "Length");
+ Assert.AreEqual ("ximian", Encoding.Unicode.GetString (data, 0, len), "Unicode DES Roundtrip");
}
+#if NET_2_0
+ [Category ("NotWorking")]
+#endif
[Test]
public void DecryptPartial_TransformFinalBlock_2Pass ()
{
DebugStream decrypted = new DebugStream (data);
cs = new CryptoStream (decrypted, des.CreateDecryptor (key, iv), CryptoStreamMode.Read);
int len = cs.Read (data, 0, 6);
- AssertEquals ("Length (1st pass)", 6, len);
- AssertEquals ("Partial DES Roundtrip", "xim", Encoding.Unicode.GetString (data, 0, len));
+ Assert.AreEqual (6, len, "Length (1st pass)");
+ Assert.AreEqual ("xim", Encoding.Unicode.GetString (data, 0, len), "Partial DES Roundtrip");
len += cs.Read (data, 6, 8);
- AssertEquals ("Length (1st+2nd)", 12, len);
- AssertEquals ("Full DES Roundtrip", "ximian", Encoding.Unicode.GetString (data, 0, len));
+ Assert.AreEqual (12, len, "Length (1st+2nd)");
+ Assert.AreEqual ("ximian", Encoding.Unicode.GetString (data, 0, len), "Full DES Roundtrip");
cs.Close ();
}
// based on http://www.c-sharpcorner.com/Code/2002/May/FileEncryption.asp
+#if NET_2_0
+ [Category ("NotWorking")]
+#endif
[Test]
public void WriteByteReadByte ()
{
cs.Close ();
byte[] result = encrypted.ToArray ();
- AssertEquals ("Encrypted", "18-EA-93-3F-20-86-D2-AA-78-02-D7-6F-E4-47-17-9C", BitConverter.ToString (result));
+ Assert.AreEqual ("18-EA-93-3F-20-86-D2-AA-78-02-D7-6F-E4-47-17-9C", BitConverter.ToString (result), "Encrypted");
encrypted = new DebugStream (result);
DebugStream decrypted = new DebugStream ();
cs.Close ();
decrypted.Close ();
- AssertEquals ("W/R Byte Roundtrip", "ximian", Encoding.Unicode.GetString (decrypted.ToArray ()));
+ Assert.AreEqual ("ximian", Encoding.Unicode.GetString (decrypted.ToArray ()), "W/R Byte Roundtrip");
}
// based http://www.4guysfromrolla.com/webtech/090501-1.shtml
string data = sb.ToString ();
string encdata = EncryptData (des.CreateEncryptor (key, iv), data);
string decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "9YVfvrh5yj0=", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("9YVfvrh5yj0=", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "qNe4d0UlkU8=", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("qNe4d0UlkU8=", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "OcernYAQ1NAME/Gny+ZuaA==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("OcernYAQ1NAME/Gny+ZuaA==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "H5UveR2lds1T+IWN4pks2Q==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("H5UveR2lds1T+IWN4pks2Q==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "dDQ3HAVtTbiRwwUqWANaeA==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("dDQ3HAVtTbiRwwUqWANaeA==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "At1r7dVDjJlQidf4QzCNkw==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("At1r7dVDjJlQidf4QzCNkw==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "DFDJWJGaNrFVBDXovsq1ew==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("DFDJWJGaNrFVBDXovsq1ew==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "gM040QGMPOBj3u1lEK4XHQ==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("gM040QGMPOBj3u1lEK4XHQ==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "P5hRUhrxOWFX0ER/IjJL/Q==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("P5hRUhrxOWFX0ER/IjJL/Q==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "uDIaQ1uXtWUIboGFLt306Q==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("uDIaQ1uXtWUIboGFLt306Q==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "giJKTXfad5Z8hebhXtYZ4hmKX/EC8w6x", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("giJKTXfad5Z8hebhXtYZ4hmKX/EC8w6x", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "lBehBplIrjjrlIrMjYcNz1DOoXLHjZdn", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("lBehBplIrjjrlIrMjYcNz1DOoXLHjZdn", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "2elWrUnjmsAOpo2s4voJyZXEJ/rtKB7P", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("2elWrUnjmsAOpo2s4voJyZXEJ/rtKB7P", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "GB3BaIZGf9K+T82j7T8Fri2rQ2/YUdSe", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("GB3BaIZGf9K+T82j7T8Fri2rQ2/YUdSe", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "Gc+wkJL+CVjdJchgcIoi8dkH2BVpHJgB", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("Gc+wkJL+CVjdJchgcIoi8dkH2BVpHJgB", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "loeuyII/PvWb91M4pFVkyaPxQoQVYpNb", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("loeuyII/PvWb91M4pFVkyaPxQoQVYpNb", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "PHXmi/sxNIgApXAfdm+Bf54/nCM//N8o", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("PHXmi/sxNIgApXAfdm+Bf54/nCM//N8o", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "xpb+wj/8LmH2ScTg3OU4JOsE5Owj6flF", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("xpb+wj/8LmH2ScTg3OU4JOsE5Owj6flF", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "WJz4VfsZ2emzhYWoSf+PNBDpHooxEregqMWnzm4gcqU=", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("WJz4VfsZ2emzhYWoSf+PNBDpHooxEregqMWnzm4gcqU=", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "PaouZu1iOKbCMRJSu04y/kB+TcOk4yp8K2BOGDs1PPE=", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("PaouZu1iOKbCMRJSu04y/kB+TcOk4yp8K2BOGDs1PPE=", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "qbTDs4dFy7eERdn5vV7JRPk2/m9smtwvZjA6+TmGlkI=", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("qbTDs4dFy7eERdn5vV7JRPk2/m9smtwvZjA6+TmGlkI=", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "f2FsphcpM7Fu90S5V17ptly44lL4GvFCCaFdnnU4twk=", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("f2FsphcpM7Fu90S5V17ptly44lL4GvFCCaFdnnU4twk=", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "imD+ntHsUmp9ALJedzC1JmAJY0r2O4KkP8271+XuG4g=", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("imD+ntHsUmp9ALJedzC1JmAJY0r2O4KkP8271+XuG4g=", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "80QLLUmHwx1fcEYGeFz1WXlS13kUy994sQLI6GhcjuM=", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("80QLLUmHwx1fcEYGeFz1WXlS13kUy994sQLI6GhcjuM=", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "DtIIlj8BCOppmIgQ9AEdUj7pBB49S/9Q38kbWLjwiVs=", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("DtIIlj8BCOppmIgQ9AEdUj7pBB49S/9Q38kbWLjwiVs=", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "LNkprYaaUFtyan204OzX+a2pzOb/Pg5WXzXJ6WWB1rQ=", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("LNkprYaaUFtyan204OzX+a2pzOb/Pg5WXzXJ6WWB1rQ=", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "FRgx9m2lT2PxtYSIdRwc+SznJetNiRk1MEIZDl3D13pvo2yOtJ1MSQ==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("FRgx9m2lT2PxtYSIdRwc+SznJetNiRk1MEIZDl3D13pvo2yOtJ1MSQ==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "V7JlnpJscrdIpX4z5S+/Q5WDjKzK4aB5TiqI3JZOYJ+KE1CWQNNeow==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("V7JlnpJscrdIpX4z5S+/Q5WDjKzK4aB5TiqI3JZOYJ+KE1CWQNNeow==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "wVwPv1c2KQynbwiOBCAhmQlReOQT52qFR34AX4dtjEeQ1oCQ1N1tHg==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("wVwPv1c2KQynbwiOBCAhmQlReOQT52qFR34AX4dtjEeQ1oCQ1N1tHg==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "Zi+G0yfmuFjSjP455pjVeKBDDWB4qvTb0K0h20UtflrYG6wcWqUzDw==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("Zi+G0yfmuFjSjP455pjVeKBDDWB4qvTb0K0h20UtflrYG6wcWqUzDw==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
sb.Append ("a");
data = sb.ToString ();
encdata = EncryptData (des.CreateEncryptor (key, iv), data);
decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
- AssertEquals ("Encrypt-" + data, "0hGoonZ8jrLhMNDKBuWrlvFnq15ZLvnyq+Ilq8r4aYUEDxttQMwi5w==", encdata);
- AssertEquals ("Decrypt-" + data, data, decdata);
+ Assert.AreEqual ("0hGoonZ8jrLhMNDKBuWrlvFnq15ZLvnyq+Ilq8r4aYUEDxttQMwi5w==", encdata, "Encrypt-" + data);
+ Assert.AreEqual (data, decdata, "Decrypt-" + data);
}
// based on System.Security assembly XmlDsigBase64TransformTest
byte[] encoded = debug.ToArray ();
string result = Encoding.UTF8.GetString (encoded);
- AssertEquals ("FromBase64_Write", expected, result);
+ Assert.AreEqual (expected, result, "FromBase64_Write");
}
[Test]
cs.Close ();
string result = Encoding.UTF8.GetString (data, 0, length);
- AssertEquals ("ToBase64_Read", "http://www.go-mono.com/", result);
+ Assert.AreEqual ("http://www.go-mono.com/", result, "ToBase64_Read");
}
[Test]
byte[] encoded = debug.ToArray ();
string result = Encoding.UTF8.GetString (encoded);
- AssertEquals ("ToBase64_Write", "aHR0cDovL3d3dy5nby1tb25vLmNvbS8=", result);
+ Assert.AreEqual ("aHR0cDovL3d3dy5nby1tb25vLmNvbS8=", result, "ToBase64_Write");
}
[Test]
cs.Close ();
string result = Encoding.UTF8.GetString (data, 0, length);
- AssertEquals ("ToBase64_Read", "aHR0cDovL3d3dy5nby1tb25vLmNvbS8=", result);
+ Assert.AreEqual ("aHR0cDovL3d3dy5nby1tb25vLmNvbS8=", result, "ToBase64_Read");
}
// Cascaded CryptoStream - like sample in book .NET Framework Security, chapter 30
csh.FlushFinalBlock ();
byte[] result = debug.ToArray ();
- AssertEquals ("Encrypted", "8C-24-76-74-09-79-2B-D3-47-C3-32-F5-F3-1A-5E-57-04-33-2E-B8-50-77-B2-A1", BitConverter.ToString (result));
+ Assert.AreEqual ("8C-24-76-74-09-79-2B-D3-47-C3-32-F5-F3-1A-5E-57-04-33-2E-B8-50-77-B2-A1", BitConverter.ToString (result), "Encrypted");
byte[] digest = hash.Hash;
- AssertEquals ("Hash", "71-04-12-D1-95-01-CF-F9-8D-8F-F8-0D-F9-AA-11-7D", BitConverter.ToString (digest));
+ Assert.AreEqual ("71-04-12-D1-95-01-CF-F9-8D-8F-F8-0D-F9-AA-11-7D", BitConverter.ToString (digest), "Hash");
}
-
+#if NET_2_0
+ [Category ("NotWorking")]
+#endif
[Test]
public void CascadedCryptoStream_Read ()
{
csh.Close ();
string result = Encoding.UTF8.GetString (data, 0, length);
- AssertEquals ("Decrypted", "http://www.go-mono.com/", result);
+ Assert.AreEqual ("http://www.go-mono.com/", result, "Decrypted");
byte[] digest = hash.Hash;
- AssertEquals ("Hash Validation", "71-04-12-D1-95-01-CF-F9-8D-8F-F8-0D-F9-AA-11-7D", BitConverter.ToString (digest));
+ Assert.AreEqual ("71-04-12-D1-95-01-CF-F9-8D-8F-F8-0D-F9-AA-11-7D", BitConverter.ToString (digest), "Hash Validation");
}
// bugzilla: 60573 - the number of block is not reduced for encryptors
byte[] data = new byte [64];
cse.Write (data, 0, 64);
- AssertEquals ("Length", 64, debug.Length);
+ Assert.AreEqual (64, debug.Length, "Length");
cse.Close ();
}
byte[] data = new byte [64] { 0xE1, 0xB2, 0x46, 0xE5, 0xA7, 0xC7, 0x4C, 0xBC, 0xD5, 0xF0, 0x8E, 0x25, 0x3B, 0xFA, 0x23, 0x80, 0x03, 0x16, 0x18, 0x17, 0xA3, 0x59, 0xBA, 0xAC, 0xFC, 0x47, 0x57, 0x2A, 0xF9, 0x44, 0x07, 0x84, 0x20, 0x74, 0x06, 0x38, 0xC2, 0xF3, 0xA1, 0xCE, 0x8C, 0x73, 0xB1, 0xE3, 0x75, 0x03, 0x66, 0x89, 0xF0, 0x4E, 0x98, 0x68, 0xB1, 0xBD, 0x85, 0x25, 0xFF, 0x4B, 0x11, 0x74, 0xEF, 0x14, 0xC8, 0xE9 };
csd.Write (data, 0, 64);
- AssertEquals ("Length", 56, debug.Length);
+ Assert.AreEqual (56, debug.Length, "Length");
// last block is kept for later processing
}
+
+ [Test]
+ public void PaddingModeNone ()
+ {
+ byte[] Key = new byte [16];
+ byte[] IV = new byte [16];
+ byte[] Buffer = new byte [64];
+
+ Rijndael alg = Rijndael.Create ();
+ alg.Mode = CipherMode.CBC;
+ alg.Padding = PaddingMode.None;
+
+ MemoryStream cms = new MemoryStream ();
+ ICryptoTransform ct = alg.CreateDecryptor (Key, IV);
+ CryptoStream cs = new CryptoStream (cms, ct, CryptoStreamMode.Write);
+ cs.Write (Buffer, 0, 64);
+ cs.Close ();
+
+ Assert.AreEqual (64, cms.ToArray ().Length, "Length");
+ }
+
+ private void WriteByte (PaddingMode mode, bool padded)
+ {
+ byte[] Key = new byte[16];
+ byte[] IV = new byte[16];
+ byte[] Buffer = new byte[64];
+
+ Rijndael alg = Rijndael.Create ();
+ alg.Mode = CipherMode.CBC;
+ alg.Padding = mode;
+
+ MemoryStream cms = new MemoryStream ();
+ ICryptoTransform ct = alg.CreateEncryptor (Key, IV);
+ CryptoStream cs = new CryptoStream (cms, ct, CryptoStreamMode.Write);
+ for (int i = 0; i < Buffer.Length; i++)
+ cs.WriteByte (Buffer[i]);
+ cs.Close ();
+
+ byte[] result = cms.ToArray ();
+ // if padded then add one block, if not then it's the original length
+ int len = padded ? 80: 64;
+ Assert.AreEqual (len, result.Length, mode.ToString () + ".Encrypted.Length");
+
+ cms = new MemoryStream ();
+ ct = alg.CreateDecryptor (Key, IV);
+ cs = new CryptoStream (cms, ct, CryptoStreamMode.Write);
+ for (int i = 0; i < result.Length; i++)
+ cs.WriteByte (result[i]);
+ cs.Close ();
+
+ byte[] plaintext = cms.ToArray ();
+ Assert.AreEqual (64, plaintext.Length, mode.ToString () + ".Decrypted.Length");
+
+ Assert.AreEqual (Buffer, plaintext, mode.ToString () + ".Date");
+ }
+
+ [Test]
+ public void WriteByte ()
+ {
+ WriteByte (PaddingMode.None, false);
+ WriteByte (PaddingMode.Zeros, false);
+ WriteByte (PaddingMode.PKCS7, true); // related to bug #81597
+#if NET_2_0
+ WriteByte (PaddingMode.ANSIX923, true);
+ WriteByte (PaddingMode.ISO10126, true);
+#endif
+ }
+
+ [Test]
+ public void ReadModeDispose_FinalBlock ()
+ {
+ using (SHA1 sha1 = SHA1.Create()) {
+ using (MemoryStream mem = new MemoryStream(new byte[] { 1, 2, 3 }, false))
+ using (CryptoStream cs = new CryptoStream(mem, sha1, CryptoStreamMode.Read))
+ {
+ }
+ byte b = sha1.Hash [0]; // This will throw if TransformFinalBlock not called in sha1
+ GC.KeepAlive (b); // just the warning...
+ }
+ }
+
+ [Test]
+ public void CustomDisposeCalled ()
+ {
+ using (MemoryStream mem = new MemoryStream(new byte[] { 1, 2, 3 }, false)) {
+ MyCryptoStream cs;
+ using (cs = new MyCryptoStream (mem, SHA1.Create()))
+ {
+ }
+ Assert.IsTrue (cs.DisposeCalled, "#1");
+ }
+ }
+
+ [Test]
+ public void ExplicitFlush ()
+ {
+ // Tests that explicitly calling Flush does not call Flush in the underlying stream
+ MyStream ms = new MyStream ();
+ using (CryptoStream cs = new CryptoStream (ms, SHA1.Create (), CryptoStreamMode.Read)) {
+ ms.FlushCounterEnabled = true;
+ cs.Flush ();
+ ms.FlushCounterEnabled = false;
+ }
+ Assert.IsTrue (ms.FlushCounter == 0);
+ }
+
+ [Test]
+ public void ImplicitFlush ()
+ {
+ // Tests that Dispose() calls Flush on the underlying stream
+ MyStream ms = new MyStream ();
+ ms.FlushCounterEnabled = true;
+ using (CryptoStream cs = new CryptoStream (ms, SHA1.Create (), CryptoStreamMode.Read)) {
+ }
+ Assert.IsTrue (ms.FlushCounter == 1);
+ }
+
+ [Test]
+ public void ImplicitFlushCascade ()
+ {
+ // Tests that Dispose() calls FlushFinalBlock() on the underlying stream
+ MyStream ms = new MyStream ();
+ ms.FlushCounterEnabled = true;
+ CryptoStream cs1 = new CryptoStream (ms, SHA1.Create (), CryptoStreamMode.Read);
+ using (CryptoStream cs = new CryptoStream (cs1, SHA1.Create (), CryptoStreamMode.Read)) {
+ }
+ Assert.IsTrue (ms.FlushCounter == 1);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentException))]
+ public void Ctor_InvalidEnumValue ()
+ {
+ CryptoStream cs = new CryptoStream (Stream.Null, SHA1.Create (), (CryptoStreamMode) 0xff);
+ }
+
+ [Test]
+ public void OutputBlock_Smaller ()
+ {
+ // The OutputBlockSize is smaller than the InputBlockSize
+ using (CryptoStream cs = new CryptoStream(Stream.Null, new MyCryptAlgorithm(), CryptoStreamMode.Write)) {
+ byte[] buffer = new byte[512 * 1024];
+ cs.Write(buffer, 0, buffer.Length);
+ }
+ }
+
+ class MyCryptoStream : CryptoStream {
+ public bool DisposeCalled { get; private set;}
+
+ public MyCryptoStream(Stream stream, ICryptoTransform transform)
+ : base(stream, transform, CryptoStreamMode.Read)
+ {
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ base.Dispose(disposing);
+ DisposeCalled = true;
+ }
+ }
+
+ class ExpandTransform : ICryptoTransform {
+
+ public bool CanReuseTransform {
+ get { return true; }
+ }
+
+ public bool CanTransformMultipleBlocks {
+ get; private set;
+ }
+
+ public int InputBlockSize {
+ get { return 1; }
+ }
+
+ public int OutputBlockSize {
+ get; private set;
+ }
+
+ public ExpandTransform (bool canTranformMultipleBlocks, int outputBlockSize)
+ {
+ this.CanTransformMultipleBlocks = canTranformMultipleBlocks;
+ this.OutputBlockSize = outputBlockSize;
+ }
+
+ public void Dispose ()
+ {
+ }
+
+ public int TransformBlock (byte [] inputBuffer, int inputOffset, int inputCount, byte [] outputBuffer, int outputOffset)
+ {
+ var ret = 0;
+ for (var i = 0; i < inputCount; i++, inputOffset++) {
+ for (var j = 0; j < OutputBlockSize; j++, outputOffset++, ret++) {
+ outputBuffer [outputOffset] = inputBuffer [inputOffset];
+ }
+ }
+ return ret;
+ }
+
+ public byte [] TransformFinalBlock (byte [] inputBuffer, int inputOffset, int inputCount)
+ {
+ var outputBuffer = new byte [inputCount * OutputBlockSize];
+ TransformBlock (inputBuffer, inputOffset, inputCount, outputBuffer, 0);
+ return outputBuffer;
+ }
+ }
+
+ static string[] expand_values = {
+ "00-01-02-03-04-05-06-07",
+ "00-00-01-01-02-02-03-03-04-04-05-05-06-06-07-07",
+ "00-00-00-01-01-01-02-02-02-03-03-03-04-04-04-05-05-05-06-06-06-07-07-07",
+ "00-00-00-00-01-01-01-01-02-02-02-02-03-03-03-03-04-04-04-04-05-05-05-05-06-06-06-06-07-07-07-07",
+ "00-01-02-03-04-05-06-07",
+ "00-00-01-01-02-02-03-03-04-04-05-05-06-06-07-07",
+ "00-00-00-01-01-01-02-02-02-03-03-03-04-04-04-05-05-05-06-06-06-07-07-07",
+ "00-00-00-00-01-01-01-01-02-02-02-02-03-03-03-03-04-04-04-04-05-05-05-05-06-06-06-06-07-07-07-07"
+ };
+
+ [Test]
+ public void Expand ()
+ {
+ int n = 0;
+ foreach (var transformMultiple in new [] { false, true }) {
+ foreach (var outputBlockSize in new [] { 1, 2, 3, 4 }) {
+ var expantedStream = new MemoryStream ();
+ var inputData = new byte [] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
+
+ using (var stream = new CryptoStream (expantedStream, new ExpandTransform (transformMultiple, outputBlockSize), CryptoStreamMode.Write)) {
+ stream.Write (inputData, 0, inputData.Length);
+ }
+ expantedStream.Close ();
+
+ string value = BitConverter.ToString (expantedStream.ToArray ());
+ Assert.AreEqual (expand_values [n++], value);
+ }
+ }
+ }
+
+ class CompressTransform : ICryptoTransform {
+ public bool CanReuseTransform {
+ get { return true; }
+ }
+
+ public bool CanTransformMultipleBlocks {
+ get { return true; }
+ }
+
+ public int InputBlockSize {
+ get; private set;
+ }
+
+ public int OutputBlockSize {
+ get { return 1; }
+ }
+
+ public CompressTransform (int inputBlockSize)
+ {
+ this.InputBlockSize = inputBlockSize;
+ }
+
+ public void Dispose ()
+ {
+ }
+
+ private int bufferedCount = 0;
+
+ public int TransformBlock (byte [] inputBuffer, int inputOffset, int inputCount, byte [] outputBuffer, int outputOffset)
+ {
+ var ret = 0;
+ for (var i = 0; i < inputCount; i++, inputOffset++) {
+ if (++bufferedCount == InputBlockSize) {
+ outputBuffer [outputOffset++] = inputBuffer [inputOffset];
+ ret++;
+ bufferedCount = 0;
+ }
+ }
+ return ret;
+ }
+
+ public byte [] TransformFinalBlock (byte [] inputBuffer, int inputOffset, int inputCount)
+ {
+ var outputBuffer = new byte [inputCount * OutputBlockSize];
+ var ret = TransformBlock (inputBuffer, inputOffset, inputCount, outputBuffer, 0);
+ byte[] result = new byte [ret];
+ Array.Copy (outputBuffer, result, ret);
+ return result;
+ }
+ }
+
+ static string[] compress_values = {
+ "00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F-10-11-12-13-14-15-16-17-18-19-1A-1B-1C-1D-1E-1F",
+ "01-03-05-07-09-0B-0D-0F-11-13-15-17-19-1B-1D-1F",
+ "02-05-08-0B-0E-11-14-17-1A-1D",
+ "03-07-0B-0F-13-17-1B-1F",
+ };
+
+ [Test]
+ public void Compress ()
+ {
+ int n = 0;
+ foreach (var inputBlockSize in new [] { 1, 2, 3, 4 }) {
+ var inputData = new byte [] {
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ };
+
+ using (var stream = new CryptoStream (new MemoryStream (inputData), new CompressTransform (inputBlockSize), CryptoStreamMode.Read)) {
+ var buffer = new byte [inputData.Length];
+ var ret = stream.Read (buffer, 0, buffer.Length);
+ string value = BitConverter.ToString (buffer, 0, ret);
+ Assert.AreEqual (compress_values [n++], value);
+ }
+ }
+ }
+
+ class MyCryptAlgorithm : ICryptoTransform {
+ public bool CanReuseTransform { get { return true; } }
+ public bool CanTransformMultipleBlocks { get { return false; } }
+ public int InputBlockSize { get { return 128 * 1024; } }
+ public int OutputBlockSize { get { return 64 * 1024; } }
+
+ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
+ {
+ return this.OutputBlockSize;
+ }
+
+ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
+ {
+ return new byte[this.OutputBlockSize];
+ }
+
+ public void Dispose() {}
+ }
+
+ class MyStream : Stream {
+ public bool FlushCounterEnabled;
+ public int FlushCounter;
+
+ public override bool CanRead
+ {
+ get {
+ return true;
+ }
+ }
+
+ public override bool CanSeek
+ {
+ get {
+ return true;
+ }
+ }
+
+ public override bool CanWrite
+ {
+ get {
+ return true;
+ }
+ }
+
+ public override long Length
+ {
+ get {
+ return 0;
+ }
+ }
+
+ public override long Position
+ {
+ get {
+ return 0;
+ }
+ set {
+ }
+ }
+
+ public override void Flush ()
+ {
+ if (FlushCounterEnabled)
+ FlushCounter++;
+ }
+
+ public override int Read (byte[] buffer, int offset, int count)
+ {
+ return 0;
+ }
+
+ public override int ReadByte ()
+ {
+ return -1;
+ }
+
+ public override long Seek (long offset, SeekOrigin origin)
+ {
+ return 0;
+ }
+
+ public override void SetLength (long value)
+ {
+ }
+
+ public override void Write (byte[] buffer, int offset, int count)
+ {
+ }
+
+ public override void WriteByte (byte value)
+ {
+ }
+ }
}
}