ProtectedMemoryCas.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. using NUnit.Framework;
  30. using System;
  31. using System.Reflection;
  32. using System.Security;
  33. using System.Security.Cryptography;
  34. using System.Security.Permissions;
  35. using MonoTests.System.Security.Cryptography;
  36. namespace MonoCasTests.System.Security.Cryptography {
  37. [TestFixture]
  38. [Category ("CAS")]
  39. // problem with CSC when an assembly use permissions defined within itself
  40. [Category ("NotWorking")]
  41. public class ProtectedMemoryCas {
  42. [SetUp]
  43. public virtual void SetUp ()
  44. {
  45. if (!SecurityManager.SecurityEnabled)
  46. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  47. }
  48. private bool IsEmpty (byte[] array)
  49. {
  50. int total = 0;
  51. for (int i = 0; i < array.Length; i++)
  52. total += array[i];
  53. return (total == 0);
  54. }
  55. [Test]
  56. [DataProtectionPermission (SecurityAction.PermitOnly, ProtectMemory = true, UnprotectMemory = true)]
  57. public void UnitTestReuse ()
  58. {
  59. ProtectedMemoryTest unit = new ProtectedMemoryTest ();
  60. unit.ProtectSameProcess ();
  61. unit.ProtectSameLogon ();
  62. unit.ProtectCrossProcess ();
  63. unit.MemoryProtectionScope_All ();
  64. }
  65. [Test]
  66. [DataProtectionPermission (SecurityAction.PermitOnly, ProtectMemory = true)]
  67. // note: this implies that UnmanagedCode isn't allowed
  68. public void Protect_PermitOnly_Protect ()
  69. {
  70. try {
  71. byte[] data = new byte[16];
  72. ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
  73. Assert.IsFalse (IsEmpty (data), "SameProcess");
  74. data = new byte[16];
  75. ProtectedMemory.Protect (data, MemoryProtectionScope.SameLogon);
  76. Assert.IsFalse (IsEmpty (data), "SameLogon");
  77. data = new byte[16];
  78. ProtectedMemory.Protect (data, MemoryProtectionScope.CrossProcess);
  79. Assert.IsFalse (IsEmpty (data), "CrossProcess");
  80. }
  81. catch (PlatformNotSupportedException) {
  82. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  83. }
  84. }
  85. [Test]
  86. [DataProtectionPermission (SecurityAction.Deny, ProtectMemory = true)]
  87. [ExpectedException (typeof (SecurityException))]
  88. public void Protect_Deny_Protect ()
  89. {
  90. try {
  91. ProtectedMemory.Protect (new byte[16], MemoryProtectionScope.SameProcess);
  92. }
  93. catch (PlatformNotSupportedException) {
  94. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  95. }
  96. }
  97. [Test]
  98. [DataProtectionPermission (SecurityAction.PermitOnly, UnprotectMemory = true)]
  99. // note: this implies that UnmanagedCode isn't allowed
  100. public void Unprotect_PermitOnly_Unprotect ()
  101. {
  102. try {
  103. byte[] data = new byte[16];
  104. ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
  105. Assert.IsFalse (IsEmpty (data), "Unprotect unprotected");
  106. }
  107. catch (PlatformNotSupportedException) {
  108. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  109. }
  110. }
  111. [Test]
  112. [DataProtectionPermission (SecurityAction.Deny, UnprotectMemory = true)]
  113. [ExpectedException (typeof (SecurityException))]
  114. public void Unprotect_Deny_Unprotect ()
  115. {
  116. try {
  117. ProtectedMemory.Unprotect (new byte[16], MemoryProtectionScope.SameProcess);
  118. }
  119. catch (PlatformNotSupportedException) {
  120. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  121. }
  122. }
  123. [Test]
  124. [DataProtectionPermission (SecurityAction.PermitOnly, ProtectMemory = true, UnprotectMemory = true)]
  125. public void LinkDemand_PermitOnly_DataProtection ()
  126. {
  127. Type pm = typeof (ProtectedMemory);
  128. byte[] data = new byte[16];
  129. object[] parameters = new object[2] { data, MemoryProtectionScope.SameProcess };
  130. try {
  131. MethodInfo mi = pm.GetMethod ("Protect");
  132. Assert.IsNotNull (mi, "Protect");
  133. mi.Invoke (null, parameters);
  134. Assert.IsFalse (IsEmpty (data), "Encrypted");
  135. mi = pm.GetMethod ("Unprotect");
  136. Assert.IsNotNull (mi, "Unprotect");
  137. mi.Invoke (null, parameters);
  138. Assert.IsTrue (IsEmpty (data), "Decrypted");
  139. // so no LinkDemand are required (Demand are enough) and
  140. // no check for UnmanagedCode are required
  141. }
  142. catch (TargetInvocationException tie) {
  143. if (tie.InnerException is PlatformNotSupportedException)
  144. Assert.Ignore ("Only supported under Windows 2000 SP 3 and later");
  145. }
  146. }
  147. }
  148. }