ProtectedMemoryTest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // ProtectedMemoryTest.cs - NUnit Test Cases for ProtectedMemory
  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 ProtectedMemoryTest {
  17. private bool IsEmpty (byte[] array)
  18. {
  19. int total = 0;
  20. for (int i = 0; i < array.Length; i++)
  21. total += array [i];
  22. return (total == 0);
  23. }
  24. private void ProtectUnprotect (MemoryProtectionScope scope)
  25. {
  26. try {
  27. byte[] data = new byte [16];
  28. ProtectedMemory.Protect (data, scope);
  29. Assert.IsFalse (IsEmpty (data), "Protect");
  30. ProtectedMemory.Unprotect (data, scope);
  31. Assert.IsTrue (IsEmpty (data), "Unprotect");
  32. }
  33. catch (PlatformNotSupportedException) {
  34. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  35. }
  36. }
  37. [Test]
  38. public void ProtectSameProcess ()
  39. {
  40. // we're testing the MemoryProtectionScope definition but
  41. // not if it's really limited to the scope specified
  42. ProtectUnprotect (MemoryProtectionScope.SameProcess);
  43. }
  44. [Test]
  45. public void ProtectSameLogon ()
  46. {
  47. // we're testing the MemoryProtectionScope definition but
  48. // not if it's really limited to the scope specified
  49. ProtectUnprotect (MemoryProtectionScope.SameLogon);
  50. }
  51. [Test]
  52. public void ProtectCrossProcess ()
  53. {
  54. // we're testing the MemoryProtectionScope definition but
  55. // not if it's really limited to the scope specified
  56. ProtectUnprotect (MemoryProtectionScope.CrossProcess);
  57. }
  58. [Test]
  59. public void MemoryProtectionScope_All ()
  60. {
  61. byte[] data = new byte[16];
  62. try {
  63. foreach (MemoryProtectionScope mps in Enum.GetValues (typeof (MemoryProtectionScope))) {
  64. ProtectedMemory.Protect (data, mps);
  65. Assert.IsFalse (IsEmpty (data), "Protect");
  66. ProtectedMemory.Unprotect (data, mps);
  67. Assert.IsTrue (IsEmpty (data), "Unprotect");
  68. }
  69. }
  70. catch (PlatformNotSupportedException) {
  71. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  72. }
  73. }
  74. [Test]
  75. [ExpectedException (typeof (ArgumentException))]
  76. public void Protect_InvalidMemoryProtectionScope ()
  77. {
  78. byte[] data = new byte[16];
  79. ProtectedMemory.Protect (data, (MemoryProtectionScope) Int32.MinValue);
  80. }
  81. [Test]
  82. [ExpectedException (typeof (CryptographicException))]
  83. public void ProtectBadDataLength ()
  84. {
  85. byte[] data = new byte [15];
  86. try {
  87. ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
  88. }
  89. catch (PlatformNotSupportedException) {
  90. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  91. }
  92. }
  93. [Test]
  94. [ExpectedException (typeof (ArgumentNullException))]
  95. public void ProtectNull ()
  96. {
  97. ProtectedMemory.Protect (null, MemoryProtectionScope.SameProcess);
  98. }
  99. [Test]
  100. [ExpectedException (typeof (ArgumentException))]
  101. public void Unprotect_InvalidMemoryProtectionScope ()
  102. {
  103. byte[] data = new byte[16];
  104. ProtectedMemory.Unprotect (data, (MemoryProtectionScope) Int32.MinValue);
  105. }
  106. [Test]
  107. [ExpectedException (typeof (CryptographicException))]
  108. public void UnprotectBadDataLength ()
  109. {
  110. byte[] data = new byte [15];
  111. try {
  112. ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
  113. }
  114. catch (PlatformNotSupportedException) {
  115. Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
  116. }
  117. }
  118. [Test]
  119. [ExpectedException (typeof (ArgumentNullException))]
  120. public void UnprotectNull ()
  121. {
  122. ProtectedMemory.Unprotect (null, MemoryProtectionScope.SameProcess);
  123. }
  124. }
  125. }
  126. #endif