Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastien Pouliot <sebastien@ximian.com>2005-10-20 22:40:44 +0400
committerSebastien Pouliot <sebastien@ximian.com>2005-10-20 22:40:44 +0400
commite6e724d23493c0a25973b285cd725df9624f1ab6 (patch)
treebc1f07c6cbc116355217a6b3bf9ee523bec0fbe4 /mcs/class/System.Security/Test
parenta3a3c06b24da6fa2dea69eff7f70e6bc7503947d (diff)
2005-10-20 Sebastien Pouliot <sebastien@ximian.com>
* CryptographicAttributeObjectCas.cs: New. CAS unit tests. * CryptographicAttributeObjectEnumeratorCas.cs: New. CAS unit tests. * CryptographicAttributeObjectEnumeratorTest.cs: New. Unit tests. * CryptographicAttributeObjectCollectionCas.cs: New. CAS unit tests. * CryptographicAttributeObjectCollectionTest.cs: New. Unit tests. * ProtectedDataCas.cs: New. CAS unit tests. * ProtectedDataTest.cs: Reworked/complete unit tests. * ProtectedMemoryCas.cs: New. CAS unit tests. * ProtectedMemoryTest.cs: Reworked/complete unit tests. svn path=/trunk/mcs/; revision=51996
Diffstat (limited to 'mcs/class/System.Security/Test')
-rw-r--r--mcs/class/System.Security/Test/System.Security.Cryptography/ChangeLog12
-rw-r--r--mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCas.cs77
-rw-r--r--mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCollectionCas.cs79
-rw-r--r--mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCollectionTest.cs227
-rw-r--r--mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectEnumeratorCas.cs84
-rw-r--r--mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectEnumeratorTest.cs116
-rw-r--r--mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedDataCas.cs174
-rw-r--r--mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedDataTest.cs120
-rw-r--r--mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedMemoryCas.cs170
-rw-r--r--mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedMemoryTest.cs113
10 files changed, 1097 insertions, 75 deletions
diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography/ChangeLog b/mcs/class/System.Security/Test/System.Security.Cryptography/ChangeLog
index 1a3ca8f748b..5b1a19af264 100644
--- a/mcs/class/System.Security/Test/System.Security.Cryptography/ChangeLog
+++ b/mcs/class/System.Security/Test/System.Security.Cryptography/ChangeLog
@@ -1,3 +1,15 @@
+2005-10-20 Sebastien Pouliot <sebastien@ximian.com>
+
+ * CryptographicAttributeObjectCas.cs: New. CAS unit tests.
+ * CryptographicAttributeObjectEnumeratorCas.cs: New. CAS unit tests.
+ * CryptographicAttributeObjectEnumeratorTest.cs: New. Unit tests.
+ * CryptographicAttributeObjectCollectionCas.cs: New. CAS unit tests.
+ * CryptographicAttributeObjectCollectionTest.cs: New. Unit tests.
+ * ProtectedDataCas.cs: New. CAS unit tests.
+ * ProtectedDataTest.cs: Reworked/complete unit tests.
+ * ProtectedMemoryCas.cs: New. CAS unit tests.
+ * ProtectedMemoryTest.cs: Reworked/complete unit tests.
+
2005-09-26 Sebastien Pouliot <sebastien@ximian.com>
* Asn*.cs, Oid*.cs: Moved to System.dll
diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCas.cs b/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCas.cs
new file mode 100644
index 00000000000..804dc18a2d2
--- /dev/null
+++ b/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCas.cs
@@ -0,0 +1,77 @@
+//
+// CryptographicAttributeObjectCas.cs - CAS unit tests for
+// System.Security.Cryptography.CryptographicAttributeObject
+//
+// Author:
+// Sebastien Pouliot <sebastien@ximian.com>
+//
+// Copyright (C) 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
+// "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.
+//
+
+#if NET_2_0
+
+using NUnit.Framework;
+
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Cryptography;
+using System.Security.Permissions;
+
+using MonoTests.System.Security.Cryptography;
+
+namespace MonoCasTests.System.Security.Cryptography {
+
+ [TestFixture]
+ [Category ("CAS")]
+ public class CryptographicAttributeObjectCas {
+
+ [SetUp]
+ public virtual void SetUp ()
+ {
+ if (!SecurityManager.SecurityEnabled)
+ Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
+ }
+
+ [Test]
+ [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
+ public void UnitTestReuse ()
+ {
+ CryptographicAttributeTest unit = new CryptographicAttributeTest ();
+ unit.ConstructorOid ();
+ unit.ConstructorOidCollection ();
+ unit.ConstructorOidAsnEncodedDataCollectionNull ();
+ }
+
+ [Test]
+ [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
+ public void LinkDemand_Deny_Unrestricted ()
+ {
+ Type[] types = new Type[1] { typeof (Oid) };
+ ConstructorInfo ci = typeof (CryptographicAttributeObject).GetConstructor (types);
+ Assert.IsNotNull (ci, ".ctor(Oid)");
+ Assert.IsNotNull (ci.Invoke (new object[1] { new Oid () }), "invoke");
+ }
+ }
+}
+
+#endif
diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCollectionCas.cs b/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCollectionCas.cs
new file mode 100644
index 00000000000..3a70665d4e8
--- /dev/null
+++ b/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCollectionCas.cs
@@ -0,0 +1,79 @@
+//
+// CryptographicAttributeObjectCollectionCas.cs - CAS unit tests for
+// System.Security.Cryptography.CryptographicAttributeObjectCollection
+//
+// Author:
+// Sebastien Pouliot <sebastien@ximian.com>
+//
+// Copyright (C) 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
+// "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.
+//
+
+#if NET_2_0
+
+using NUnit.Framework;
+
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Cryptography;
+using System.Security.Permissions;
+
+using MonoTests.System.Security.Cryptography;
+
+namespace MonoCasTests.System.Security.Cryptography {
+
+ [TestFixture]
+ [Category ("CAS")]
+ public class CryptographicAttributeObjectCollectionCas {
+
+ [SetUp]
+ public virtual void SetUp ()
+ {
+ if (!SecurityManager.SecurityEnabled)
+ Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
+ }
+
+ [Test]
+ [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
+ public void UnitTestReuse ()
+ {
+ CryptographicAttributeObjectCollectionTest unit = new CryptographicAttributeObjectCollectionTest ();
+ unit.Constructor_Empty ();
+ unit.Constructor_CryptographicAttributeObject ();
+ unit.Add_MultipleSameOid ();
+ unit.Remove_MultipleSameOid_First ();
+ unit.Remove_MultipleSameOid_Last ();
+ unit.Remove_WithDifferentInstance ();
+ }
+
+ [Test]
+ [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
+ public void LinkDemand_Deny_Unrestricted ()
+ {
+ ConstructorInfo ci = typeof (CryptographicAttributeObjectCollection).GetConstructor (new Type [0]);
+ Assert.IsNotNull (ci, "default .ctor()");
+ Assert.IsNotNull (ci.Invoke (null), "invoke");
+ }
+ }
+}
+
+#endif
diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCollectionTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCollectionTest.cs
new file mode 100644
index 00000000000..5b58cb0c610
--- /dev/null
+++ b/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectCollectionTest.cs
@@ -0,0 +1,227 @@
+//
+// CryptographicAttributeObjectCollectionTest.cs - NUnit tests for
+// System.Security.Cryptography.CryptographicAttributeObjectCollection
+//
+// Author:
+// Sebastien Pouliot <sebastien@ximian.com>
+//
+// Copyright (C) 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
+// "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.
+//
+
+#if NET_2_0
+
+using NUnit.Framework;
+
+using System;
+using System.Collections;
+using System.Security.Cryptography;
+
+namespace MonoTests.System.Security.Cryptography {
+
+ [TestFixture]
+ public class CryptographicAttributeObjectCollectionTest {
+
+ static string defaultOid = "1.2.840.113549.1.7.1";
+
+ private void CommonStuff (CryptographicAttributeObjectCollection coll)
+ {
+ Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
+ Assert.AreSame (coll, coll.SyncRoot, "SyncRoot");
+ Assert.IsNotNull (coll.GetEnumerator (), "GetEnumerator");
+
+ int i = coll.Count;
+ Oid o1 = new Oid ("1.2.840.113549.1.7.3");
+ AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x05, 0x00 });
+ Assert.AreEqual (i, coll.Add (aed), "Add(AsnEncodedData)");
+ Assert.IsTrue ((coll[i++] is CryptographicAttributeObject), "converted");
+
+ Oid o2 = new Oid ("1.2.840.113549.1.7.2");
+ CryptographicAttributeObject cao = new CryptographicAttributeObject (o2);
+ Assert.AreEqual (i, coll.Add (cao), "Add(CryptographicAttributeObject)");
+
+ CryptographicAttributeObject[] array = new CryptographicAttributeObject [coll.Count];
+ coll.CopyTo (array, 0);
+
+ Array a = (Array) new object [coll.Count];
+ ICollection c = (ICollection) coll;
+ c.CopyTo (a, 0);
+
+ IEnumerable e = (IEnumerable) coll;
+ Assert.IsNotNull (e.GetEnumerator (), "GetEnumerator");
+
+ coll.Remove (cao);
+ Assert.AreEqual (i, coll.Count, "Remove(CryptographicAttributeObject)");
+ }
+
+ [Test]
+ public void Constructor_Empty ()
+ {
+ CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection ();
+ Assert.AreEqual (0, coll.Count, "Count");
+ CommonStuff (coll);
+ }
+
+ [Test]
+ public void Constructor_CryptographicAttributeObject ()
+ {
+ Oid o = new Oid (defaultOid);
+ CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
+ CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
+ Assert.AreEqual (1, coll.Count, "Count");
+ Assert.AreSame (cao, coll[0], "this[int]");
+ CommonStuff (coll);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void Add_AsnEncodedData_Null ()
+ {
+ AsnEncodedData aed = null;
+ new CryptographicAttributeObjectCollection ().Add (aed);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void Add_CryptographicAttributeObject_Null ()
+ {
+ CryptographicAttributeObject cao = null;
+ new CryptographicAttributeObjectCollection ().Add (cao);
+ }
+
+ [Test]
+ public void Add_MultipleSameOid ()
+ {
+ Oid o = new Oid (defaultOid);
+ CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
+ CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
+
+ int i = 0;
+ while (i < 10) {
+ Assert.AreEqual (1, coll.Count, String.Format ("Count-{0}", i));
+ Assert.AreEqual (i * 2, coll[0].Values.Count, String.Format ("Values.Count-{0}", i++));
+
+ Oid o1 = new Oid (defaultOid);
+ AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte)i });
+ coll.Add (aed);
+
+ aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) i });
+ coll.Add (aed);
+
+ Oid o2 = new Oid (defaultOid);
+ coll.Add (new CryptographicAttributeObject (o2));
+ }
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void CopyTo_Null ()
+ {
+ new CryptographicAttributeObjectCollection ().CopyTo (null, 0);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void ICollection_CopyTo_Null ()
+ {
+ CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection ();
+ ICollection c = (coll as ICollection);
+ c.CopyTo (null, 0);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void Remove_Null ()
+ {
+ new CryptographicAttributeObjectCollection ().Remove (null);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void Remove_Null_WithNullItem ()
+ {
+ CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (null);
+ Assert.AreEqual (1, coll.Count, "Count");
+ Assert.IsNull (coll[0], "this[int]");
+ coll.Remove (null);
+ }
+
+ [Test]
+ public void Remove_MultipleSameOid_First ()
+ {
+ Oid o = new Oid (defaultOid);
+ CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
+ CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
+
+ Oid o1 = new Oid (defaultOid);
+ AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
+ coll.Add (aed);
+
+ aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
+ coll.Add (aed);
+
+ Oid o2 = new Oid (defaultOid);
+ coll.Add (new CryptographicAttributeObject (o2));
+
+ Assert.AreEqual (1, coll.Count, "before Remove");
+ coll.Remove (cao);
+ Assert.AreEqual (0, coll.Count, "after Remove");
+ }
+
+ [Test]
+ public void Remove_MultipleSameOid_Last ()
+ {
+ Oid o = new Oid (defaultOid);
+ CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
+ CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
+
+ Oid o1 = new Oid (defaultOid);
+ AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
+ coll.Add (aed);
+
+ aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
+ coll.Add (aed);
+
+ Oid o2 = new Oid (defaultOid);
+ CryptographicAttributeObject last = new CryptographicAttributeObject (o2);
+ coll.Add (last);
+
+ Assert.AreEqual (1, coll.Count, "before Remove");
+ coll.Remove (last);
+ Assert.AreEqual (1, coll.Count, "after Remove");
+ }
+
+ [Test]
+ public void Remove_WithDifferentInstance ()
+ {
+ Oid o = new Oid (defaultOid);
+ CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
+ CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
+
+ Assert.AreEqual (1, coll.Count, "before Remove");
+ cao = new CryptographicAttributeObject (o);
+ coll.Remove (cao);
+ Assert.AreEqual (1, coll.Count, "after Remove");
+ }
+ }
+}
+
+#endif
diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectEnumeratorCas.cs b/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectEnumeratorCas.cs
new file mode 100644
index 00000000000..4da810c6e1f
--- /dev/null
+++ b/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectEnumeratorCas.cs
@@ -0,0 +1,84 @@
+//
+// CryptographicAttributeObjectEnumeratorCas.cs - CAS unit tests for
+// System.Security.Cryptography.CryptographicAttributeObjectEnumerator
+//
+// Author:
+// Sebastien Pouliot <sebastien@ximian.com>
+//
+// Copyright (C) 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
+// "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.
+//
+
+#if NET_2_0
+
+using NUnit.Framework;
+
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Cryptography;
+using System.Security.Permissions;
+
+using MonoTests.System.Security.Cryptography;
+
+namespace MonoCasTests.System.Security.Cryptography {
+
+ [TestFixture]
+ [Category ("CAS")]
+ public class CryptographicAttributeObjectEnumeratorCas {
+
+ static string defaultOid = "1.2.840.113549.1.7.1";
+
+ [SetUp]
+ public virtual void SetUp ()
+ {
+ if (!SecurityManager.SecurityEnabled)
+ Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
+ }
+
+ [Test]
+ [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
+ public void UnitTestReuse ()
+ {
+ CryptographicAttributeObjectEnumeratorTest unit = new CryptographicAttributeObjectEnumeratorTest ();
+ unit.Empty ();
+ unit.One_CryptographicAttributeObject ();
+ unit.One_AsnEncodedData ();
+ unit.Two_Both ();
+ }
+
+ [Test]
+ [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
+ public void LinkDemand_Deny_Unrestricted ()
+ {
+ Oid o = new Oid (defaultOid);
+ CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
+ CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
+ CryptographicAttributeObjectEnumerator e = coll.GetEnumerator ();
+
+ MethodInfo mi = typeof (CryptographicAttributeObjectEnumerator).GetMethod ("MoveNext");
+ Assert.IsNotNull (mi, "default .ctor()");
+ Assert.IsTrue ((bool)mi.Invoke (e, null), "invoke");
+ }
+ }
+}
+
+#endif
diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectEnumeratorTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectEnumeratorTest.cs
new file mode 100644
index 00000000000..edd4f8684a9
--- /dev/null
+++ b/mcs/class/System.Security/Test/System.Security.Cryptography/CryptographicAttributeObjectEnumeratorTest.cs
@@ -0,0 +1,116 @@
+//
+// CryptographicAttributeObjectEnumeratorTest.cs - NUnit tests for
+// System.Security.Cryptography.CryptographicAttributeObjectEnumerator
+//
+// Author:
+// Sebastien Pouliot <sebastien@ximian.com>
+//
+// Copyright (C) 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
+// "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.
+//
+
+#if NET_2_0
+
+using NUnit.Framework;
+
+using System;
+using System.Collections;
+using System.Security.Cryptography;
+
+namespace MonoTests.System.Security.Cryptography {
+
+ [TestFixture]
+ public class CryptographicAttributeObjectEnumeratorTest {
+
+ static string defaultOid = "1.2.840.113549.1.7.1";
+
+ private CryptographicAttributeObjectCollection coll;
+
+ private void Count (int count)
+ {
+ Assert.AreEqual (count, coll.Count, "Count");
+ int i = 0;
+ foreach (CryptographicAttributeObject cao in coll) {
+ i++;
+ }
+ Assert.AreEqual (count, i, "foreach");
+
+ i = 0;
+ CryptographicAttributeObjectEnumerator e = coll.GetEnumerator ();
+ while (e.MoveNext ()) {
+ if (e.Current is CryptographicAttributeObject)
+ i++;
+ }
+ Assert.AreEqual (count, i, "GetEnumerator");
+
+ i = 0;
+ e.Reset ();
+ while (e.MoveNext ()) {
+ if (e.Current is CryptographicAttributeObject)
+ i++;
+ }
+ Assert.AreEqual (count, i, "Reset");
+ }
+
+ [Test]
+ public void Empty ()
+ {
+ coll = new CryptographicAttributeObjectCollection ();
+ Count (0);
+ }
+
+ [Test]
+ public void One_CryptographicAttributeObject ()
+ {
+ Oid o = new Oid (defaultOid);
+ CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
+ coll = new CryptographicAttributeObjectCollection (cao);
+ Count (1);
+ }
+
+ [Test]
+ public void One_AsnEncodedData ()
+ {
+ Oid o = new Oid (defaultOid);
+ AsnEncodedData aed = new AsnEncodedData (o, new byte[] { 0x05, 0x00 });
+ coll = new CryptographicAttributeObjectCollection ();
+ coll.Add (aed);
+ Count (1);
+ }
+
+ [Test]
+ public void Two_Both ()
+ {
+ coll = new CryptographicAttributeObjectCollection ();
+
+ Oid o1 = new Oid (defaultOid + ".1");
+ AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x05, 0x00 });
+ coll.Add (aed);
+
+ Oid o2 = new Oid (defaultOid + ".2");
+ coll.Add (new CryptographicAttributeObject (o2));
+
+ Count (2);
+ }
+ }
+}
+
+#endif
diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedDataCas.cs b/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedDataCas.cs
new file mode 100644
index 00000000000..d552b927cf8
--- /dev/null
+++ b/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedDataCas.cs
@@ -0,0 +1,174 @@
+//
+// ProtectedDataCas.cs
+// - CAS unit tests for System.Security.Cryptography.ProtectedData
+//
+// Author:
+// Sebastien Pouliot <sebastien@ximian.com>
+//
+// Copyright (C) 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
+// "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.
+//
+
+#if NET_2_0
+
+using NUnit.Framework;
+
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Cryptography;
+using System.Security.Permissions;
+
+using MonoTests.System.Security.Cryptography;
+
+namespace MonoCasTests.System.Security.Cryptography {
+
+ [TestFixture]
+ [Category ("CAS")]
+ // problem with CSC when an assembly use permissions defined within itself
+ [Category ("NotWorking")]
+ public class ProtectedDataCas {
+
+ [SetUp]
+ public virtual void SetUp ()
+ {
+ if (!SecurityManager.SecurityEnabled)
+ Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
+ }
+
+ private bool IsEmpty (byte[] array)
+ {
+ int total = 0;
+ for (int i = 0; i < array.Length; i++)
+ total += array[i];
+ return (total == 0);
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.PermitOnly, ProtectData = true, UnprotectData = true)]
+ public void UnitTestReuse ()
+ {
+ ProtectedDataTest unit = new ProtectedDataTest ();
+ unit.ProtectCurrentUser ();
+ unit.ProtectLocalMachine ();
+ unit.DataProtectionScope_All ();
+ unit.ProtectNullEntropy ();
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.PermitOnly, ProtectData = true)]
+ // note: this implies that UnmanagedCode isn't allowed
+ public void Protect_PermitOnly_Protect ()
+ {
+ byte[] data = new byte[8];
+ byte[] entropy = new byte[1];
+
+ try {
+ byte[] encdata = ProtectedData.Protect (data, null, DataProtectionScope.CurrentUser);
+ Assert.IsFalse (IsEmpty (encdata), "null-CurrentUser");
+
+ encdata = ProtectedData.Protect (data, entropy, DataProtectionScope.CurrentUser);
+ Assert.IsFalse (IsEmpty (encdata), "entropy-CurrentUser");
+
+ encdata = ProtectedData.Protect (data, null, DataProtectionScope.LocalMachine);
+ Assert.IsFalse (IsEmpty (encdata), "null-LocalMachine");
+
+ encdata = ProtectedData.Protect (data, entropy, DataProtectionScope.LocalMachine);
+ Assert.IsFalse (IsEmpty (encdata), "entropy-LocalMachine");
+ }
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 and later");
+ }
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.Deny, ProtectData = true)]
+ [ExpectedException (typeof (SecurityException))]
+ public void Protect_Deny_Protect ()
+ {
+ try {
+ ProtectedData.Protect (new byte[8], null, DataProtectionScope.CurrentUser);
+ }
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 and later");
+ }
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.PermitOnly, UnprotectData = true)]
+ // note: this implies that UnmanagedCode isn't allowed
+ [ExpectedException (typeof (CryptographicException))]
+ public void Unprotect_PermitOnly_Unprotect ()
+ {
+ try {
+ ProtectedData.Unprotect (new byte[8], null, DataProtectionScope.CurrentUser);
+ }
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 and later");
+ }
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.Deny, UnprotectData = true)]
+ [ExpectedException (typeof (SecurityException))]
+ public void Unprotect_Deny_Unprotect ()
+ {
+ try {
+ ProtectedData.Unprotect (new byte[8], null, DataProtectionScope.CurrentUser);
+ }
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 and later");
+ }
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.PermitOnly, ProtectData = true, UnprotectData = true)]
+ public void LinkDemand_PermitOnly_DataProtection ()
+ {
+ Type pd = typeof (ProtectedData);
+ object[] parameters = new object[3] { new byte[8], null, DataProtectionScope.CurrentUser };
+
+ try {
+ MethodInfo mi = pd.GetMethod ("Protect");
+ Assert.IsNotNull (mi, "Protect");
+ byte[] encdata = (byte[]) mi.Invoke (null, parameters);
+ Assert.IsNotNull (encdata, "Invoke Protect");
+ Assert.IsFalse (IsEmpty (encdata), "Encrypted");
+
+ mi = pd.GetMethod ("Unprotect");
+ Assert.IsNotNull (mi, "Unprotect");
+ parameters[0] = encdata;
+ byte[] decdata = (byte[]) mi.Invoke (null, parameters);
+ Assert.IsNotNull (decdata, "Invoke Unprotect");
+ Assert.IsTrue (IsEmpty (decdata), "Decrypted");
+
+ // so no LinkDemand are required (Demand are enough) and
+ // no check for UnmanagedCode are required
+ }
+ catch (TargetInvocationException tie) {
+ if (tie.InnerException is PlatformNotSupportedException)
+ Assert.Ignore ("Only supported under Windows 2000 and later");
+ }
+ }
+ }
+}
+
+#endif
diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedDataTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedDataTest.cs
index 02ddda5f074..41c39862fbd 100644
--- a/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedDataTest.cs
+++ b/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedDataTest.cs
@@ -1,12 +1,12 @@
//
// ProtectedDataTest.cs - NUnit Test Cases for ProtectedData
//
-// Author:
-// Sebastien Pouliot <sebastien@ximian.com>
+// Author:
+// Sebastien Pouliot <sebastien@ximian.com>
+//
+// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
-// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
#if NET_2_0
@@ -20,30 +20,67 @@ namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class ProtectedDataTest {
- private void ProtectUnprotect (byte[] entropy, DataProtectionScope scope)
+ private byte[] notMuchEntropy = new byte[16];
+
+ private bool IsEmpty (byte[] array)
{
- byte[] data = new byte [16];
- byte[] encdata = ProtectedData.Protect (data, entropy, scope);
int total = 0;
- for (int i=0; i < 16; i++)
- total += encdata [i];
- Assert.IsFalse ((total == 0), "Protect");
-
- byte[] decdata = ProtectedData.Unprotect (encdata, entropy, scope);
- total = 0;
- for (int i=0; i < 16; i++)
- total += decdata [i];
- Assert.IsTrue ((total == 0), "Unprotect");
+ for (int i = 0; i < array.Length; i++)
+ total += array[i];
+ return (total == 0);
+ }
+
+ private void ProtectUnprotect (byte[] entropy, DataProtectionScope scope)
+ {
+ try {
+ byte[] data = new byte [16];
+ byte[] encdata = ProtectedData.Protect (data, entropy, scope);
+ Assert.IsFalse (IsEmpty (encdata), "Protect");
+
+ byte[] decdata = ProtectedData.Unprotect (encdata, entropy, scope);
+ Assert.IsTrue (IsEmpty (decdata), "Unprotect");
+ }
+ catch (CryptographicException ce) {
+ if (ce.InnerException is UnauthorizedAccessException)
+ Assert.Ignore ("The machine key store hasn't yet been created (as root).");
+ }
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 and later");
+ }
}
[Test]
public void ProtectCurrentUser ()
{
+ // we're testing the DataProtectionScope definition but
+ // not if it's really limited to the scope specified
+ ProtectUnprotect (notMuchEntropy, DataProtectionScope.CurrentUser);
+ }
+
+ [Test]
+ public void ProtectLocalMachine ()
+ {
+ // we're testing the DataProtectionScope definition but
+ // not if it's really limited to the scope specified
+ ProtectUnprotect (notMuchEntropy, DataProtectionScope.LocalMachine);
+ }
+
+ [Test]
+ public void DataProtectionScope_All ()
+ {
+ byte[] data = new byte[16];
try {
- byte[] notMuchEntropy = new byte [16];
- // we're testing the DataProtectionScope definition but
- // not if it's really limited to the scope specified
- ProtectUnprotect (notMuchEntropy, DataProtectionScope.CurrentUser);
+ foreach (DataProtectionScope dps in Enum.GetValues (typeof (DataProtectionScope))) {
+ byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, dps);
+ Assert.IsFalse (IsEmpty (encdata), "Protect");
+ Assert.IsTrue (IsEmpty (data), "Protect(original unmodified)");
+ byte[] decdata = ProtectedData.Unprotect (encdata, notMuchEntropy, dps);
+ Assert.IsTrue (IsEmpty (decdata), "Unprotect");
+ }
+ }
+ catch (CryptographicException ce) {
+ if (ce.InnerException is UnauthorizedAccessException)
+ Assert.Ignore ("The machine key store hasn't yet been created (as root).");
}
catch (PlatformNotSupportedException) {
Assert.Ignore ("Only supported under Windows 2000 and later");
@@ -51,13 +88,15 @@ namespace MonoTests.System.Security.Cryptography {
}
[Test]
- public void ProtectLocalMachine ()
+ [ExpectedException (typeof (ArgumentException))]
+ [Category ("NotDotNet")]
+ public void Protect_InvalidDataProtectionScope ()
{
try {
- byte[] notMuchEntropy = new byte [16];
- // we're testing the DataProtectionScope definition but
- // not if it's really limited to the scope specified
- ProtectUnprotect (notMuchEntropy, DataProtectionScope.LocalMachine);
+ byte[] data = new byte[16];
+ ProtectedData.Protect (data, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
+ // MS doesn't throw an ArgumentException but returning from
+ // this method will throw an UnhandledException in NUnit
}
catch (PlatformNotSupportedException) {
Assert.Ignore ("Only supported under Windows 2000 and later");
@@ -68,17 +107,24 @@ namespace MonoTests.System.Security.Cryptography {
[ExpectedException (typeof (ArgumentNullException))]
public void ProtectNull ()
{
- byte[] notMuchEntropy = new byte [16];
ProtectedData.Protect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
}
[Test]
public void ProtectNullEntropy ()
{
+ // we're testing the DataProtectionScope definition but
+ // not if it's really limited to the scope specified
+ ProtectUnprotect (null, DataProtectionScope.CurrentUser);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void UnprotectNotProtectedData ()
+ {
try {
- // we're testing the DataProtectionScope definition but
- // not if it's really limited to the scope specified
- ProtectUnprotect (null, DataProtectionScope.LocalMachine);
+ byte[] baddata = new byte [16];
+ ProtectedData.Unprotect (baddata, notMuchEntropy, DataProtectionScope.CurrentUser);
}
catch (PlatformNotSupportedException) {
Assert.Ignore ("Only supported under Windows 2000 and later");
@@ -86,13 +132,16 @@ namespace MonoTests.System.Security.Cryptography {
}
[Test]
- [ExpectedException (typeof (CryptographicException))]
- public void UnprotectNotProtectedData ()
+ [ExpectedException (typeof (ArgumentException))]
+ [Category ("NotDotNet")]
+ public void Unprotect_InvalidDataProtectionScope ()
{
try {
- byte[] baddata = new byte [16];
- byte[] notMuchEntropy = new byte [16];
- ProtectedData.Unprotect (baddata, notMuchEntropy, DataProtectionScope.CurrentUser);
+ byte[] data = new byte[16];
+ byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, DataProtectionScope.CurrentUser);
+ ProtectedData.Unprotect (encdata, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
+ // MS doesn't throw an ArgumentException but returning from
+ // this method will throw an UnhandledException in NUnit
}
catch (PlatformNotSupportedException) {
Assert.Ignore ("Only supported under Windows 2000 and later");
@@ -103,7 +152,6 @@ namespace MonoTests.System.Security.Cryptography {
[ExpectedException (typeof (ArgumentNullException))]
public void UnprotectNull ()
{
- byte[] notMuchEntropy = new byte [16];
ProtectedData.Unprotect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
}
}
diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedMemoryCas.cs b/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedMemoryCas.cs
new file mode 100644
index 00000000000..c9d01d46e35
--- /dev/null
+++ b/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedMemoryCas.cs
@@ -0,0 +1,170 @@
+//
+// ProtectedMemoryCas.cs
+// - CAS unit tests for System.Security.Cryptography.ProtectedMemory
+//
+// Author:
+// Sebastien Pouliot <sebastien@ximian.com>
+//
+// Copyright (C) 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
+// "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.
+//
+
+#if NET_2_0
+
+using NUnit.Framework;
+
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Cryptography;
+using System.Security.Permissions;
+
+using MonoTests.System.Security.Cryptography;
+
+namespace MonoCasTests.System.Security.Cryptography {
+
+ [TestFixture]
+ [Category ("CAS")]
+ // problem with CSC when an assembly use permissions defined within itself
+ [Category ("NotWorking")]
+ public class ProtectedMemoryCas {
+
+ [SetUp]
+ public virtual void SetUp ()
+ {
+ if (!SecurityManager.SecurityEnabled)
+ Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
+ }
+
+ private bool IsEmpty (byte[] array)
+ {
+ int total = 0;
+ for (int i = 0; i < array.Length; i++)
+ total += array[i];
+ return (total == 0);
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.PermitOnly, ProtectMemory = true, UnprotectMemory = true)]
+ public void UnitTestReuse ()
+ {
+ ProtectedMemoryTest unit = new ProtectedMemoryTest ();
+ unit.ProtectSameProcess ();
+ unit.ProtectSameLogon ();
+ unit.ProtectCrossProcess ();
+ unit.MemoryProtectionScope_All ();
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.PermitOnly, ProtectMemory = true)]
+ // note: this implies that UnmanagedCode isn't allowed
+ public void Protect_PermitOnly_Protect ()
+ {
+ try {
+ byte[] data = new byte[16];
+ ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
+ Assert.IsFalse (IsEmpty (data), "SameProcess");
+
+ data = new byte[16];
+ ProtectedMemory.Protect (data, MemoryProtectionScope.SameLogon);
+ Assert.IsFalse (IsEmpty (data), "SameLogon");
+
+ data = new byte[16];
+ ProtectedMemory.Protect (data, MemoryProtectionScope.CrossProcess);
+ Assert.IsFalse (IsEmpty (data), "CrossProcess");
+ }
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
+ }
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.Deny, ProtectMemory = true)]
+ [ExpectedException (typeof (SecurityException))]
+ public void Protect_Deny_Protect ()
+ {
+ try {
+ ProtectedMemory.Protect (new byte[16], MemoryProtectionScope.SameProcess);
+ }
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
+ }
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.PermitOnly, UnprotectMemory = true)]
+ // note: this implies that UnmanagedCode isn't allowed
+ public void Unprotect_PermitOnly_Unprotect ()
+ {
+ try {
+ byte[] data = new byte[16];
+ ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
+ Assert.IsFalse (IsEmpty (data), "Unprotect unprotected");
+ }
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
+ }
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.Deny, UnprotectMemory = true)]
+ [ExpectedException (typeof (SecurityException))]
+ public void Unprotect_Deny_Unprotect ()
+ {
+ try {
+ ProtectedMemory.Unprotect (new byte[16], MemoryProtectionScope.SameProcess);
+ }
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
+ }
+ }
+
+ [Test]
+ [DataProtectionPermission (SecurityAction.PermitOnly, ProtectMemory = true, UnprotectMemory = true)]
+ public void LinkDemand_PermitOnly_DataProtection ()
+ {
+ Type pm = typeof (ProtectedMemory);
+ byte[] data = new byte[16];
+ object[] parameters = new object[2] { data, MemoryProtectionScope.SameProcess };
+
+ try {
+ MethodInfo mi = pm.GetMethod ("Protect");
+ Assert.IsNotNull (mi, "Protect");
+ mi.Invoke (null, parameters);
+ Assert.IsFalse (IsEmpty (data), "Encrypted");
+
+ mi = pm.GetMethod ("Unprotect");
+ Assert.IsNotNull (mi, "Unprotect");
+ mi.Invoke (null, parameters);
+ Assert.IsTrue (IsEmpty (data), "Decrypted");
+
+ // so no LinkDemand are required (Demand are enough) and
+ // no check for UnmanagedCode are required
+ }
+ catch (TargetInvocationException tie) {
+ if (tie.InnerException is PlatformNotSupportedException)
+ Assert.Ignore ("Only supported under Windows 2000 SP 3 and later");
+ }
+ }
+ }
+}
+
+#endif
diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedMemoryTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedMemoryTest.cs
index bf586252928..3e751691cbf 100644
--- a/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedMemoryTest.cs
+++ b/mcs/class/System.Security/Test/System.Security.Cryptography/ProtectedMemoryTest.cs
@@ -1,11 +1,11 @@
//
// ProtectedMemoryTest.cs - NUnit Test Cases for ProtectedMemory
//
-// Author:
-// Sebastien Pouliot <sebastien@ximian.com>
-//
-// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+// Author:
+// Sebastien Pouliot <sebastien@ximian.com>
+//
+// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
#if NET_2_0
@@ -20,67 +20,89 @@ namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class ProtectedMemoryTest {
- private void ProtectUnprotect (MemoryProtectionScope scope)
+ private bool IsEmpty (byte[] array)
{
- byte[] data = new byte [16];
- ProtectedMemory.Protect (data, scope);
int total = 0;
- for (int i=0; i < 16; i++)
- total += data [i];
- Assert.IsFalse ((total == 0), "Protect");
-
- ProtectedMemory.Unprotect (data, scope);
- total = 0;
- for (int i=0; i < 16; i++)
- total += data [i];
- Assert.IsTrue ((total == 0), "Unprotect");
+ for (int i = 0; i < array.Length; i++)
+ total += array [i];
+ return (total == 0);
}
- [Test]
- public void ProtectSameProcess ()
+ private void ProtectUnprotect (MemoryProtectionScope scope)
{
try {
- // we're testing the MemoryProtectionScope definition but
- // not if it's really limited to the scope specified
- ProtectUnprotect (MemoryProtectionScope.SameProcess);
+ byte[] data = new byte [16];
+ ProtectedMemory.Protect (data, scope);
+ Assert.IsFalse (IsEmpty (data), "Protect");
+
+ ProtectedMemory.Unprotect (data, scope);
+ Assert.IsTrue (IsEmpty (data), "Unprotect");
}
- catch (PlatformNotSupportedException) {
- Assert.Ignore ("Only supported under Windows 2000 and later");
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
}
}
[Test]
+ public void ProtectSameProcess ()
+ {
+ // we're testing the MemoryProtectionScope definition but
+ // not if it's really limited to the scope specified
+ ProtectUnprotect (MemoryProtectionScope.SameProcess);
+ }
+
+ [Test]
public void ProtectSameLogon ()
{
- try {
- // we're testing the MemoryProtectionScope definition but
- // not if it's really limited to the scope specified
- ProtectUnprotect (MemoryProtectionScope.SameLogon);
- }
- catch (PlatformNotSupportedException) {
- Assert.Ignore ("Only supported under Windows 2000 and later");
- }
+ // we're testing the MemoryProtectionScope definition but
+ // not if it's really limited to the scope specified
+ ProtectUnprotect (MemoryProtectionScope.SameLogon);
}
[Test]
public void ProtectCrossProcess ()
{
+ // we're testing the MemoryProtectionScope definition but
+ // not if it's really limited to the scope specified
+ ProtectUnprotect (MemoryProtectionScope.CrossProcess);
+ }
+
+ [Test]
+ public void MemoryProtectionScope_All ()
+ {
+ byte[] data = new byte[16];
try {
- // we're testing the MemoryProtectionScope definition but
- // not if it's really limited to the scope specified
- ProtectUnprotect (MemoryProtectionScope.CrossProcess);
+ foreach (MemoryProtectionScope mps in Enum.GetValues (typeof (MemoryProtectionScope))) {
+ ProtectedMemory.Protect (data, mps);
+ Assert.IsFalse (IsEmpty (data), "Protect");
+ ProtectedMemory.Unprotect (data, mps);
+ Assert.IsTrue (IsEmpty (data), "Unprotect");
+ }
}
- catch (PlatformNotSupportedException) {
- Assert.Ignore ("Only supported under Windows 2000 and later");
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
}
}
[Test]
+ [ExpectedException (typeof (ArgumentException))]
+ public void Protect_InvalidMemoryProtectionScope ()
+ {
+ byte[] data = new byte[16];
+ ProtectedMemory.Protect (data, (MemoryProtectionScope) Int32.MinValue);
+ }
+
+ [Test]
[ExpectedException (typeof (CryptographicException))]
public void ProtectBadDataLength ()
{
byte[] data = new byte [15];
- ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
+ try {
+ ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
+ }
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
+ }
}
[Test]
@@ -91,11 +113,24 @@ namespace MonoTests.System.Security.Cryptography {
}
[Test]
+ [ExpectedException (typeof (ArgumentException))]
+ public void Unprotect_InvalidMemoryProtectionScope ()
+ {
+ byte[] data = new byte[16];
+ ProtectedMemory.Unprotect (data, (MemoryProtectionScope) Int32.MinValue);
+ }
+
+ [Test]
[ExpectedException (typeof (CryptographicException))]
public void UnprotectBadDataLength ()
{
byte[] data = new byte [15];
- ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
+ try {
+ ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
+ }
+ catch (PlatformNotSupportedException) {
+ Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
+ }
}
[Test]