ProtectedDataCas.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // ProtectedDataCas.cs
  3. // - CAS unit tests for System.Security.Cryptography.ProtectedData
  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 ProtectedDataCas {
  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, ProtectData = true, UnprotectData = true)]
  58. public void UnitTestReuse ()
  59. {
  60. ProtectedDataTest unit = new ProtectedDataTest ();
  61. unit.ProtectCurrentUser ();
  62. unit.ProtectLocalMachine ();
  63. unit.DataProtectionScope_All ();
  64. unit.ProtectNullEntropy ();
  65. }
  66. [Test]
  67. [DataProtectionPermission (SecurityAction.PermitOnly, ProtectData = true)]
  68. // note: this implies that UnmanagedCode isn't allowed
  69. public void Protect_PermitOnly_Protect ()
  70. {
  71. byte[] data = new byte[8];
  72. byte[] entropy = new byte[1];
  73. try {
  74. byte[] encdata = ProtectedData.Protect (data, null, DataProtectionScope.CurrentUser);
  75. Assert.IsFalse (IsEmpty (encdata), "null-CurrentUser");
  76. encdata = ProtectedData.Protect (data, entropy, DataProtectionScope.CurrentUser);
  77. Assert.IsFalse (IsEmpty (encdata), "entropy-CurrentUser");
  78. encdata = ProtectedData.Protect (data, null, DataProtectionScope.LocalMachine);
  79. Assert.IsFalse (IsEmpty (encdata), "null-LocalMachine");
  80. encdata = ProtectedData.Protect (data, entropy, DataProtectionScope.LocalMachine);
  81. Assert.IsFalse (IsEmpty (encdata), "entropy-LocalMachine");
  82. }
  83. catch (PlatformNotSupportedException) {
  84. Assert.Ignore ("Only supported under Windows 2000 and later");
  85. }
  86. }
  87. [Test]
  88. [DataProtectionPermission (SecurityAction.Deny, ProtectData = true)]
  89. [ExpectedException (typeof (SecurityException))]
  90. public void Protect_Deny_Protect ()
  91. {
  92. try {
  93. ProtectedData.Protect (new byte[8], null, DataProtectionScope.CurrentUser);
  94. }
  95. catch (PlatformNotSupportedException) {
  96. Assert.Ignore ("Only supported under Windows 2000 and later");
  97. }
  98. }
  99. [Test]
  100. [DataProtectionPermission (SecurityAction.PermitOnly, UnprotectData = true)]
  101. // note: this implies that UnmanagedCode isn't allowed
  102. [ExpectedException (typeof (CryptographicException))]
  103. public void Unprotect_PermitOnly_Unprotect ()
  104. {
  105. try {
  106. ProtectedData.Unprotect (new byte[8], null, DataProtectionScope.CurrentUser);
  107. }
  108. catch (PlatformNotSupportedException) {
  109. Assert.Ignore ("Only supported under Windows 2000 and later");
  110. }
  111. }
  112. [Test]
  113. [DataProtectionPermission (SecurityAction.Deny, UnprotectData = true)]
  114. [ExpectedException (typeof (SecurityException))]
  115. public void Unprotect_Deny_Unprotect ()
  116. {
  117. try {
  118. ProtectedData.Unprotect (new byte[8], null, DataProtectionScope.CurrentUser);
  119. }
  120. catch (PlatformNotSupportedException) {
  121. Assert.Ignore ("Only supported under Windows 2000 and later");
  122. }
  123. }
  124. [Test]
  125. [DataProtectionPermission (SecurityAction.PermitOnly, ProtectData = true, UnprotectData = true)]
  126. public void LinkDemand_PermitOnly_DataProtection ()
  127. {
  128. Type pd = typeof (ProtectedData);
  129. object[] parameters = new object[3] { new byte[8], null, DataProtectionScope.CurrentUser };
  130. try {
  131. MethodInfo mi = pd.GetMethod ("Protect");
  132. Assert.IsNotNull (mi, "Protect");
  133. byte[] encdata = (byte[]) mi.Invoke (null, parameters);
  134. Assert.IsNotNull (encdata, "Invoke Protect");
  135. Assert.IsFalse (IsEmpty (encdata), "Encrypted");
  136. mi = pd.GetMethod ("Unprotect");
  137. Assert.IsNotNull (mi, "Unprotect");
  138. parameters[0] = encdata;
  139. byte[] decdata = (byte[]) mi.Invoke (null, parameters);
  140. Assert.IsNotNull (decdata, "Invoke Unprotect");
  141. Assert.IsTrue (IsEmpty (decdata), "Decrypted");
  142. // so no LinkDemand are required (Demand are enough) and
  143. // no check for UnmanagedCode are required
  144. }
  145. catch (TargetInvocationException tie) {
  146. if (tie.InnerException is PlatformNotSupportedException)
  147. Assert.Ignore ("Only supported under Windows 2000 and later");
  148. }
  149. }
  150. }
  151. }
  152. #endif