ProtectedMemoryTest.cs 3.6 KB

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