ProtectedDataTest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #if NET_2_0
  11. using NUnit.Framework;
  12. using System;
  13. using System.Security.Cryptography;
  14. namespace MonoTests.System.Security.Cryptography {
  15. [TestFixture]
  16. public class ProtectedDataTest {
  17. private byte[] notMuchEntropy = new byte[16];
  18. private bool IsEmpty (byte[] array)
  19. {
  20. int total = 0;
  21. for (int i = 0; i < array.Length; i++)
  22. total += array[i];
  23. return (total == 0);
  24. }
  25. private void ProtectUnprotect (byte[] entropy, DataProtectionScope scope)
  26. {
  27. try {
  28. byte[] data = new byte [16];
  29. byte[] encdata = ProtectedData.Protect (data, entropy, scope);
  30. Assert.IsFalse (IsEmpty (encdata), "Protect");
  31. byte[] decdata = ProtectedData.Unprotect (encdata, entropy, scope);
  32. Assert.IsTrue (IsEmpty (decdata), "Unprotect");
  33. }
  34. catch (CryptographicException ce) {
  35. if (ce.InnerException is UnauthorizedAccessException)
  36. Assert.Ignore ("The machine key store hasn't yet been created (as root).");
  37. }
  38. catch (PlatformNotSupportedException) {
  39. Assert.Ignore ("Only supported under Windows 2000 and later");
  40. }
  41. }
  42. [Test]
  43. public void ProtectCurrentUser ()
  44. {
  45. // we're testing the DataProtectionScope definition but
  46. // not if it's really limited to the scope specified
  47. ProtectUnprotect (notMuchEntropy, DataProtectionScope.CurrentUser);
  48. }
  49. [Test]
  50. public void ProtectLocalMachine ()
  51. {
  52. // we're testing the DataProtectionScope definition but
  53. // not if it's really limited to the scope specified
  54. ProtectUnprotect (notMuchEntropy, DataProtectionScope.LocalMachine);
  55. }
  56. [Test]
  57. public void DataProtectionScope_All ()
  58. {
  59. byte[] data = new byte[16];
  60. try {
  61. foreach (DataProtectionScope dps in Enum.GetValues (typeof (DataProtectionScope))) {
  62. byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, dps);
  63. Assert.IsFalse (IsEmpty (encdata), "Protect");
  64. Assert.IsTrue (IsEmpty (data), "Protect(original unmodified)");
  65. byte[] decdata = ProtectedData.Unprotect (encdata, notMuchEntropy, dps);
  66. Assert.IsTrue (IsEmpty (decdata), "Unprotect");
  67. }
  68. }
  69. catch (CryptographicException ce) {
  70. if (ce.InnerException is UnauthorizedAccessException)
  71. Assert.Ignore ("The machine key store hasn't yet been created (as root).");
  72. }
  73. catch (PlatformNotSupportedException) {
  74. Assert.Ignore ("Only supported under Windows 2000 and later");
  75. }
  76. }
  77. [Test]
  78. [ExpectedException (typeof (ArgumentException))]
  79. [Category ("NotDotNet")]
  80. public void Protect_InvalidDataProtectionScope ()
  81. {
  82. try {
  83. byte[] data = new byte[16];
  84. ProtectedData.Protect (data, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
  85. // MS doesn't throw an ArgumentException but returning from
  86. // this method will throw an UnhandledException in NUnit
  87. }
  88. catch (PlatformNotSupportedException) {
  89. Assert.Ignore ("Only supported under Windows 2000 and later");
  90. }
  91. }
  92. [Test]
  93. [ExpectedException (typeof (ArgumentNullException))]
  94. public void ProtectNull ()
  95. {
  96. ProtectedData.Protect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
  97. }
  98. [Test]
  99. public void ProtectNullEntropy ()
  100. {
  101. // we're testing the DataProtectionScope definition but
  102. // not if it's really limited to the scope specified
  103. ProtectUnprotect (null, DataProtectionScope.CurrentUser);
  104. }
  105. [Test]
  106. [ExpectedException (typeof (CryptographicException))]
  107. public void UnprotectNotProtectedData ()
  108. {
  109. try {
  110. byte[] baddata = new byte [16];
  111. ProtectedData.Unprotect (baddata, notMuchEntropy, DataProtectionScope.CurrentUser);
  112. }
  113. catch (PlatformNotSupportedException) {
  114. Assert.Ignore ("Only supported under Windows 2000 and later");
  115. }
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentException))]
  119. [Category ("NotDotNet")]
  120. public void Unprotect_InvalidDataProtectionScope ()
  121. {
  122. try {
  123. byte[] data = new byte[16];
  124. byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, DataProtectionScope.CurrentUser);
  125. ProtectedData.Unprotect (encdata, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
  126. // MS doesn't throw an ArgumentException but returning from
  127. // this method will throw an UnhandledException in NUnit
  128. }
  129. catch (PlatformNotSupportedException) {
  130. Assert.Ignore ("Only supported under Windows 2000 and later");
  131. }
  132. }
  133. [Test]
  134. [ExpectedException (typeof (ArgumentNullException))]
  135. public void UnprotectNull ()
  136. {
  137. ProtectedData.Unprotect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
  138. }
  139. }
  140. }
  141. #endif