ProtectedDataCas.cs 5.6 KB

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