ProtectedDataTest.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // ProtectedDataTest.cs - NUnit Test Cases for ProtectedData
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. using NUnit.Framework;
  11. using System;
  12. using System.Text;
  13. using System.Collections.Generic;
  14. using System.Threading.Tasks;
  15. using System.Security.Cryptography;
  16. namespace MonoTests.System.Security.Cryptography {
  17. [TestFixture]
  18. public class ProtectedDataTest {
  19. private byte[] notMuchEntropy = new byte[16];
  20. private bool IsEmpty (byte[] array)
  21. {
  22. int total = 0;
  23. for (int i = 0; i < array.Length; i++)
  24. total += array[i];
  25. return (total == 0);
  26. }
  27. private void ProtectUnprotect (byte[] entropy, DataProtectionScope scope)
  28. {
  29. try {
  30. byte[] data = new byte [16];
  31. byte[] encdata = ProtectedData.Protect (data, entropy, scope);
  32. Assert.IsFalse (IsEmpty (encdata), "Protect");
  33. byte[] decdata = ProtectedData.Unprotect (encdata, entropy, scope);
  34. Assert.IsTrue (IsEmpty (decdata), "Unprotect");
  35. }
  36. catch (CryptographicException ce) {
  37. if (ce.InnerException is UnauthorizedAccessException)
  38. Assert.Ignore ("The machine key store hasn't yet been created (as root).");
  39. }
  40. catch (PlatformNotSupportedException) {
  41. Assert.Ignore ("Only supported under Windows 2000 and later");
  42. }
  43. }
  44. [Test]
  45. public void ProtectCurrentUser ()
  46. {
  47. // we're testing the DataProtectionScope definition but
  48. // not if it's really limited to the scope specified
  49. ProtectUnprotect (notMuchEntropy, DataProtectionScope.CurrentUser);
  50. }
  51. [Test]
  52. public void ProtectLocalMachine ()
  53. {
  54. // we're testing the DataProtectionScope definition but
  55. // not if it's really limited to the scope specified
  56. ProtectUnprotect (notMuchEntropy, DataProtectionScope.LocalMachine);
  57. }
  58. #if !MOBILE // System.PlatformNotSupportedException: Operation is not supported on this platform.
  59. [Test] // https://bugzilla.xamarin.com/show_bug.cgi?id=38933
  60. public void ProtectCurrentUserMultiThread ()
  61. {
  62. string data = "Hello World";
  63. string entropy = "This is a long string with no meaningful content.";
  64. var entropyBytes = Encoding.UTF8.GetBytes (entropy);
  65. var dataBytes = Encoding.UTF8.GetBytes (data);
  66. var tasks = new List<Task> ();
  67. for (int i = 0; i < 20; i++)
  68. {
  69. tasks.Add (new Task (() => {
  70. byte[] encryptedBytes = ProtectedData.Protect (dataBytes, entropyBytes, DataProtectionScope.CurrentUser);
  71. Assert.IsFalse (IsEmpty (encryptedBytes), "#1");
  72. byte[] decryptedBytes = ProtectedData.Unprotect (encryptedBytes, entropyBytes, DataProtectionScope.CurrentUser);
  73. string decryptedString = Encoding.UTF8.GetString(decryptedBytes);
  74. Assert.AreEqual (data, decryptedString, "#2");
  75. }, TaskCreationOptions.LongRunning));
  76. }
  77. foreach (var t in tasks) t.Start ();
  78. Task.WaitAll (tasks.ToArray ());
  79. }
  80. #endif
  81. [Test]
  82. public void DataProtectionScope_All ()
  83. {
  84. byte[] data = new byte[16];
  85. try {
  86. foreach (DataProtectionScope dps in Enum.GetValues (typeof (DataProtectionScope))) {
  87. byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, dps);
  88. Assert.IsFalse (IsEmpty (encdata), "Protect");
  89. Assert.IsTrue (IsEmpty (data), "Protect(original unmodified)");
  90. byte[] decdata = ProtectedData.Unprotect (encdata, notMuchEntropy, dps);
  91. Assert.IsTrue (IsEmpty (decdata), "Unprotect");
  92. }
  93. }
  94. catch (CryptographicException ce) {
  95. if (ce.InnerException is UnauthorizedAccessException)
  96. Assert.Ignore ("The machine key store hasn't yet been created (as root).");
  97. }
  98. catch (PlatformNotSupportedException) {
  99. Assert.Ignore ("Only supported under Windows 2000 and later");
  100. }
  101. }
  102. [Test]
  103. [ExpectedException (typeof (ArgumentException))]
  104. [Category ("NotDotNet")]
  105. public void Protect_InvalidDataProtectionScope ()
  106. {
  107. try {
  108. byte[] data = new byte[16];
  109. ProtectedData.Protect (data, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
  110. // MS doesn't throw an ArgumentException but returning from
  111. // this method will throw an UnhandledException in NUnit
  112. }
  113. catch (PlatformNotSupportedException) {
  114. Assert.Ignore ("Only supported under Windows 2000 and later");
  115. }
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentNullException))]
  119. public void ProtectNull ()
  120. {
  121. ProtectedData.Protect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
  122. }
  123. [Test]
  124. public void ProtectNullEntropy ()
  125. {
  126. // we're testing the DataProtectionScope definition but
  127. // not if it's really limited to the scope specified
  128. ProtectUnprotect (null, DataProtectionScope.CurrentUser);
  129. }
  130. [Test]
  131. [ExpectedException (typeof (CryptographicException))]
  132. public void UnprotectNotProtectedData ()
  133. {
  134. try {
  135. byte[] baddata = new byte [16];
  136. ProtectedData.Unprotect (baddata, notMuchEntropy, DataProtectionScope.CurrentUser);
  137. }
  138. catch (PlatformNotSupportedException) {
  139. Assert.Ignore ("Only supported under Windows 2000 and later");
  140. }
  141. }
  142. [Test]
  143. [ExpectedException (typeof (ArgumentException))]
  144. [Category ("NotDotNet")]
  145. public void Unprotect_InvalidDataProtectionScope ()
  146. {
  147. try {
  148. byte[] data = new byte[16];
  149. byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, DataProtectionScope.CurrentUser);
  150. ProtectedData.Unprotect (encdata, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
  151. // MS doesn't throw an ArgumentException but returning from
  152. // this method will throw an UnhandledException in NUnit
  153. }
  154. catch (PlatformNotSupportedException) {
  155. Assert.Ignore ("Only supported under Windows 2000 and later");
  156. }
  157. }
  158. [Test]
  159. [ExpectedException (typeof (ArgumentNullException))]
  160. public void UnprotectNull ()
  161. {
  162. ProtectedData.Unprotect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
  163. }
  164. }
  165. }