ProtectedMemoryCas.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // ProtectedMemoryCas.cs
  3. // - CAS unit tests for System.Security.Cryptography.ProtectedMemory
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using NUnit.Framework;
  31. using System;
  32. using System.Reflection;
  33. using System.Security;
  34. using System.Security.Cryptography;
  35. using System.Security.Permissions;
  36. using MonoTests.System.Security.Cryptography;
  37. namespace MonoCasTests.System.Security.Cryptography {
  38. [TestFixture]
  39. [Category ("CAS")]
  40. // problem with CSC when an assembly use permissions defined within itself
  41. [Category ("NotWorking")]
  42. public class ProtectedMemoryCas {
  43. [SetUp]
  44. public virtual void SetUp ()
  45. {
  46. if (!SecurityManager.SecurityEnabled)
  47. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  48. }
  49. private bool IsEmpty (byte[] array)
  50. {
  51. int total = 0;
  52. for (int i = 0; i < array.Length; i++)
  53. total += array[i];
  54. return (total == 0);
  55. }
  56. [Test]
  57. [DataProtectionPermission (SecurityAction.PermitOnly, ProtectMemory = true, UnprotectMemory = true)]
  58. public void UnitTestReuse ()
  59. {
  60. ProtectedMemoryTest unit = new ProtectedMemoryTest ();
  61. unit.ProtectSameProcess ();
  62. unit.ProtectSameLogon ();
  63. unit.ProtectCrossProcess ();
  64. unit.MemoryProtectionScope_All ();
  65. }
  66. [Test]
  67. [DataProtectionPermission (SecurityAction.PermitOnly, ProtectMemory = true)]
  68. // note: this implies that UnmanagedCode isn't allowed
  69. public void Protect_PermitOnly_Protect ()
  70. {
  71. try {
  72. byte[] data = new byte[16];
  73. ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
  74. Assert.IsFalse (IsEmpty (data), "SameProcess");
  75. data = new byte[16];
  76. ProtectedMemory.Protect (data, MemoryProtectionScope.SameLogon);
  77. Assert.IsFalse (IsEmpty (data), "SameLogon");
  78. data = new byte[16];
  79. ProtectedMemory.Protect (data, MemoryProtectionScope.CrossProcess);
  80. Assert.IsFalse (IsEmpty (data), "CrossProcess");
  81. }
  82. catch (PlatformNotSupportedException) {
  83. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  84. }
  85. }
  86. [Test]
  87. [DataProtectionPermission (SecurityAction.Deny, ProtectMemory = true)]
  88. [ExpectedException (typeof (SecurityException))]
  89. public void Protect_Deny_Protect ()
  90. {
  91. try {
  92. ProtectedMemory.Protect (new byte[16], MemoryProtectionScope.SameProcess);
  93. }
  94. catch (PlatformNotSupportedException) {
  95. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  96. }
  97. }
  98. [Test]
  99. [DataProtectionPermission (SecurityAction.PermitOnly, UnprotectMemory = true)]
  100. // note: this implies that UnmanagedCode isn't allowed
  101. public void Unprotect_PermitOnly_Unprotect ()
  102. {
  103. try {
  104. byte[] data = new byte[16];
  105. ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
  106. Assert.IsFalse (IsEmpty (data), "Unprotect unprotected");
  107. }
  108. catch (PlatformNotSupportedException) {
  109. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  110. }
  111. }
  112. [Test]
  113. [DataProtectionPermission (SecurityAction.Deny, UnprotectMemory = true)]
  114. [ExpectedException (typeof (SecurityException))]
  115. public void Unprotect_Deny_Unprotect ()
  116. {
  117. try {
  118. ProtectedMemory.Unprotect (new byte[16], MemoryProtectionScope.SameProcess);
  119. }
  120. catch (PlatformNotSupportedException) {
  121. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  122. }
  123. }
  124. [Test]
  125. [DataProtectionPermission (SecurityAction.PermitOnly, ProtectMemory = true, UnprotectMemory = true)]
  126. public void LinkDemand_PermitOnly_DataProtection ()
  127. {
  128. Type pm = typeof (ProtectedMemory);
  129. byte[] data = new byte[16];
  130. object[] parameters = new object[2] { data, MemoryProtectionScope.SameProcess };
  131. try {
  132. MethodInfo mi = pm.GetMethod ("Protect");
  133. Assert.IsNotNull (mi, "Protect");
  134. mi.Invoke (null, parameters);
  135. Assert.IsFalse (IsEmpty (data), "Encrypted");
  136. mi = pm.GetMethod ("Unprotect");
  137. Assert.IsNotNull (mi, "Unprotect");
  138. mi.Invoke (null, parameters);
  139. Assert.IsTrue (IsEmpty (data), "Decrypted");
  140. // so no LinkDemand are required (Demand are enough) and
  141. // no check for UnmanagedCode are required
  142. }
  143. catch (TargetInvocationException tie) {
  144. if (tie.InnerException is PlatformNotSupportedException)
  145. Assert.Ignore ("Only supported under Windows 2000 SP 3 and later");
  146. }
  147. }
  148. }
  149. }
  150. #endif