ProtectedDataTest.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. [Test] // https://bugzilla.xamarin.com/show_bug.cgi?id=38933
  59. public void ProtectCurrentUserMultiThread ()
  60. {
  61. string data = "Hello World";
  62. string entropy = "This is a long string with no meaningful content.";
  63. var entropyBytes = Encoding.UTF8.GetBytes (entropy);
  64. var dataBytes = Encoding.UTF8.GetBytes (data);
  65. var tasks = new List<Task> ();
  66. for (int i = 0; i < 20; i++)
  67. {
  68. tasks.Add (new Task (() => {
  69. byte[] encryptedBytes = ProtectedData.Protect (dataBytes, entropyBytes, DataProtectionScope.CurrentUser);
  70. Assert.IsFalse (IsEmpty (encryptedBytes), "#1");
  71. byte[] decryptedBytes = ProtectedData.Unprotect (encryptedBytes, entropyBytes, DataProtectionScope.CurrentUser);
  72. string decryptedString = Encoding.UTF8.GetString(decryptedBytes);
  73. Assert.AreEqual (data, decryptedString, "#2");
  74. }, TaskCreationOptions.LongRunning));
  75. }
  76. foreach (var t in tasks) t.Start ();
  77. Task.WaitAll (tasks.ToArray ());
  78. }
  79. [Test]
  80. public void DataProtectionScope_All ()
  81. {
  82. byte[] data = new byte[16];
  83. try {
  84. foreach (DataProtectionScope dps in Enum.GetValues (typeof (DataProtectionScope))) {
  85. byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, dps);
  86. Assert.IsFalse (IsEmpty (encdata), "Protect");
  87. Assert.IsTrue (IsEmpty (data), "Protect(original unmodified)");
  88. byte[] decdata = ProtectedData.Unprotect (encdata, notMuchEntropy, dps);
  89. Assert.IsTrue (IsEmpty (decdata), "Unprotect");
  90. }
  91. }
  92. catch (CryptographicException ce) {
  93. if (ce.InnerException is UnauthorizedAccessException)
  94. Assert.Ignore ("The machine key store hasn't yet been created (as root).");
  95. }
  96. catch (PlatformNotSupportedException) {
  97. Assert.Ignore ("Only supported under Windows 2000 and later");
  98. }
  99. }
  100. [Test]
  101. [ExpectedException (typeof (ArgumentException))]
  102. [Category ("NotDotNet")]
  103. public void Protect_InvalidDataProtectionScope ()
  104. {
  105. try {
  106. byte[] data = new byte[16];
  107. ProtectedData.Protect (data, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
  108. // MS doesn't throw an ArgumentException but returning from
  109. // this method will throw an UnhandledException in NUnit
  110. }
  111. catch (PlatformNotSupportedException) {
  112. Assert.Ignore ("Only supported under Windows 2000 and later");
  113. }
  114. }
  115. [Test]
  116. [ExpectedException (typeof (ArgumentNullException))]
  117. public void ProtectNull ()
  118. {
  119. ProtectedData.Protect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
  120. }
  121. [Test]
  122. public void ProtectNullEntropy ()
  123. {
  124. // we're testing the DataProtectionScope definition but
  125. // not if it's really limited to the scope specified
  126. ProtectUnprotect (null, DataProtectionScope.CurrentUser);
  127. }
  128. [Test]
  129. [ExpectedException (typeof (CryptographicException))]
  130. public void UnprotectNotProtectedData ()
  131. {
  132. try {
  133. byte[] baddata = new byte [16];
  134. ProtectedData.Unprotect (baddata, notMuchEntropy, DataProtectionScope.CurrentUser);
  135. }
  136. catch (PlatformNotSupportedException) {
  137. Assert.Ignore ("Only supported under Windows 2000 and later");
  138. }
  139. }
  140. [Test]
  141. [ExpectedException (typeof (ArgumentException))]
  142. [Category ("NotDotNet")]
  143. public void Unprotect_InvalidDataProtectionScope ()
  144. {
  145. try {
  146. byte[] data = new byte[16];
  147. byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, DataProtectionScope.CurrentUser);
  148. ProtectedData.Unprotect (encdata, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
  149. // MS doesn't throw an ArgumentException but returning from
  150. // this method will throw an UnhandledException in NUnit
  151. }
  152. catch (PlatformNotSupportedException) {
  153. Assert.Ignore ("Only supported under Windows 2000 and later");
  154. }
  155. }
  156. [Test]
  157. [ExpectedException (typeof (ArgumentNullException))]
  158. public void UnprotectNull ()
  159. {
  160. ProtectedData.Unprotect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
  161. }
  162. }
  163. }