ProtectedMemoryTest.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. //
  9. #if NET_2_0
  10. using NUnit.Framework;
  11. using System;
  12. using System.Security.Cryptography;
  13. namespace MonoTests.System.Security.Cryptography {
  14. // References:
  15. // a.
  16. [TestFixture]
  17. public class ProtectedMemoryTest : Assertion {
  18. private void ProtectUnprotect (MemoryProtectionScope scope)
  19. {
  20. byte[] data = new byte [16];
  21. ProtectedMemory.Protect (data, scope);
  22. int total = 0;
  23. for (int i=0; i < 16; i++)
  24. total += data [i];
  25. Assert ("Protect", (total != 0));
  26. ProtectedMemory.Unprotect (data, scope);
  27. total = 0;
  28. for (int i=0; i < 16; i++)
  29. total += data [i];
  30. Assert ("Unprotect", (total == 0));
  31. }
  32. [Test]
  33. public void ProtectSameProcess ()
  34. {
  35. try {
  36. // we're testing the MemoryProtectionScope definition but
  37. // not if it's really limited to the scope specified
  38. ProtectUnprotect (MemoryProtectionScope.SameProcess);
  39. }
  40. catch (PlatformNotSupportedException) {
  41. Console.WriteLine ("Only supported under Windows XP and later");
  42. }
  43. }
  44. [Test]
  45. public void ProtectSameLogon ()
  46. {
  47. try {
  48. // we're testing the MemoryProtectionScope definition but
  49. // not if it's really limited to the scope specified
  50. ProtectUnprotect (MemoryProtectionScope.SameLogon);
  51. }
  52. catch (PlatformNotSupportedException) {
  53. Console.WriteLine ("Only supported under Windows XP and later");
  54. }
  55. }
  56. [Test]
  57. public void ProtectCrossProcess ()
  58. {
  59. try {
  60. // we're testing the MemoryProtectionScope definition but
  61. // not if it's really limited to the scope specified
  62. ProtectUnprotect (MemoryProtectionScope.CrossProcess);
  63. }
  64. catch (PlatformNotSupportedException) {
  65. Console.WriteLine ("Only supported under Windows XP and later");
  66. }
  67. }
  68. [Test]
  69. [ExpectedException (typeof (CryptographicException))]
  70. public void ProtectBadDataLength ()
  71. {
  72. byte[] data = new byte [15];
  73. ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
  74. }
  75. [Test]
  76. [ExpectedException (typeof (ArgumentNullException))]
  77. public void ProtectNull ()
  78. {
  79. ProtectedMemory.Protect (null, MemoryProtectionScope.SameProcess);
  80. }
  81. [Test]
  82. [ExpectedException (typeof (CryptographicException))]
  83. public void UnprotectBadDataLength ()
  84. {
  85. byte[] data = new byte [15];
  86. ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
  87. }
  88. [Test]
  89. [ExpectedException (typeof (ArgumentNullException))]
  90. public void UnprotectNull ()
  91. {
  92. ProtectedMemory.Unprotect (null, MemoryProtectionScope.SameProcess);
  93. }
  94. }
  95. }
  96. #endif